• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Can someone help me with a exercise on classes?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you help me understand my error bcz it's driving me crazy. Please send help.
class-definition-part-1.PNG
[Thumbnail for class-definition-part-1.PNG]
class-definition-part-2.PNG
[Thumbnail for class-definition-part-2.PNG]
Demo_of_the_class.PNG
[Thumbnail for Demo_of_the_class.PNG]
 
Rancher
Posts: 3795
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

understand my error


Please explain what the error is.

Can you post the code as text so that if needed parts can be copied from the text to include in a response?
It is not possible to copy text from an image.

Be sure to wrap all posted code in code tags: Select the code and press the Code button.
 
Ryan Sefrio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah of course here is the code:



error.PNG
Here is a screenshot of what happens when I run the program.
Here is a screenshot of what happens when I run the program.
 
Saloon Keeper
Posts: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you input for "price"? The code was looking for a double and says that's not what it found.
 
Carey Brown
Saloon Keeper
Posts: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code, or other preformatted text, please UseCodeTags (<--link).

I fixed it for you this time. Doesn't it look better, all indented and line numbered?
 
Carey Brown
Saloon Keeper
Posts: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your Movie.equals() method use equals() to compare Strings, not "==". "==" will compare the references only and not the content the references refer to. ".equals()" compares the content.
 
Carey Brown
Saloon Keeper
Posts: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Movie.tostring() method is poorly named. It "looks" like the overridden method "toString()" which returns a String but it is not. And the name "tostring()" implies that it is converting something to a String, which it is not. A better name would have been "print()". Alternatively you could have made it a proper toString() method and let the caller do the printing part.
 
Rancher
Posts: 4588
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:What did you input for "price"? The code was looking for a double and says that's not what it found.



I'm guessing he didn't get a chance.

It's the usual Scanner issue.

When getting data for movie2 it reads in using nextLine, nextLine then nextDouble.
Then reads in for the new movie1 the same again (nextLine, nextLine, nextDouble).

So the name of the movie will be blank (reading the new line left behind by the last nextDouble), the schedule will be the film name, and then it attempts to read the price from the schedule entered.

Isn't there a link explaining this somewhere?
I'm not very good at keeping hold of those...
 
Ryan Sefrio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote: I'm guessing he didn't get a chance.


Yeah, you're right each time it fails before letting me input the newPrice.
Also, the names of the methods are supposed to be that way because my teacher said so.(But I agree that the names aren't that great)
 
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ryan Sr wrote:Also, the names of the methods are supposed to be that way because my teacher said so.(But I agree that the names aren't that great)


Double check what your teacher actually instructed -- the difference between toString() and tostring() is one letter. Did you write that tostring() method yourself or was it provided by your teacher? As already mentioned, a properly overridden toString() implementation will not have any output statements and it will return a String representation of the object which the caller of toString() can do whatever it needs to do with.
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, a properly overridden equals() method will look something like this:

Your equals() method takes a Movie and is an overloaded, not overridden, version of the equals(Object) method.
 
Ryan Sefrio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the instructions that my teacher gave me. I might be misunderstanding what she really wants.
Question.png
The question
The question
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The requirement for toString() (note the exact case used -- you wrote tostring(), the instructions specified toString(), which is different) is either poorly worded or poorly thought out. It does reference an example though so it will help us if you shared that example as well.

The requirement for equals() is just plain bad. This is NOT a good requirement. Something like this would be better:

- override the equals() method so that one Movie equals another Movie if they both have the same name, schedule, and price.
 
Ryan Sefrio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The requirement for toString() It does reference an example though so it will help us if you shared that example as well.


yeah, here it is:
example.PNG
[Thumbnail for example.PNG]
 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ryan Sefrio wrote:Here's the instructions . . . .

I hope the idea is that you override toString() to fulfil what it does in the superclasses: returns a String representation of the object. Not,but,...but you will have to discuss that with your teacher. You display the result of the toString() call like this:-You cannot even get toString() to compile with a return type other than String.
Another minor complaint: the second constructor is not called a default constructor, but I like the idea of having a copy constructor.

[edit] Correct the capital S in “toStrig()”.
 
Ryan Sefrio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I changed the method tostring() like you said, but I still have the same problem when I'm changing the the values of movie1. It gives me the error message right after I input the new schedule.
 
Norm Radder
Rancher
Posts: 3795
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a description of the Scanner problem that Dave talked about: https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/
 
Ryan Sefrio
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Here's a description of the Scanner problem that Dave talked about: https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/



Thank you, that was really helpfull. With that site, I managed to solve my problem.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic