Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Not sure if this code is correct ?  RSS feed

 
Mihai Gherghina
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, so i have an assignment due for java and i would like to see if anyone in here could tell me if my code is correct. We just started learning about object orientation and we had the following assignment .....

Implement a class named Movie that contains information about a movie. The Movie class has the following instance variables (a.k.a. fields or data):

The movie name
The director's name
The MPAA rating (e.g. G, PG, PG-13, R)
Number of people who have rated movie as terrible (1)
Number of people who have rated movie as bad (2)
Number of people who have rated movie as ok (3)
Number of people who have rated movie as good (4)
Number of people who have rated movie as excellent (5)

The Movie class will have methods to:

Create a new Movie (given a movie name, director name and MPAA rating) [constructor]
Create a new Movie (from another Movie) [copy constructor]

*** Add accessors/mutators for: movie name, director name, MPAA rating***
*** Add only accessors for:
getTerribleNumber - returns the number of people who rated movie as terrible
getBadNumber
getOkNumber
getGoodNumber
getExcellentNumber

calculateAverageRating - calculates and returns the average rating by multiplying the number of terrible ratings * 1, bad ratings * 2, ok ratings * 3, good ratings * 4, excellent ratings * 5 and dividing by the total number of ratings. If there are no ratings for the movie yet, return 0.0
addRating(int rating) - adds a rating for the movie, returns true if rating is between 1-5, false otherwise.
removeRating(int rating) - removes a rating for a movie. Returns true if rating is between 1-5 and there is at least one rating to remove, false otherwise
equals - method to check if one Movie is the same as another by comparing all instance variables
toString - method to turn a Movie into a string for display, e.g. display as "Movie [Name=Orange County, Director=Jake Kasdan, MPAA Rating=PG-13, Average Rating=2.5]"




THE FOLLOWING IS MY CLASS CODE :




THE FOLLOWING IS MY DEMO CLASS, BUT I AM CONFUSED ON THIS PART OF THE ASSIGNMENT BECAUSE I AM NOT SURE AS TO HOW I MAY BE ABLE TO ADD THE RATING TO A MOVIE... AM I GOING ABOUT THIS THE WRONG WAY ?
// Names are just random people in case you are wondering. Im just testing out the methods to see if they go into the database

 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I basically ask most of the time for assignments:

Does it compile?
Can you enter the required information according to the assignment?
Can you get the proper data out according to the assignment?
Does you program give you the desire output according to the assignment?

If your answer is yes to all 4 of those questions, then I would have to say the program is correct.

If you answer no to any one of the questions, then I have to ask why?

To me, you program is OK if it does all 4 of those things, can it be better, most definitely, but that will come with time and experience.

So you ask yourself those 4 questions and if they are good, then answer your own question on the program's correctness, but in the case you have a problem, then ask that specific question and we can help you.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HOW I MAY BE ABLE TO ADD THE RATING

Well, you did create an addRating() method, so just use it.
 
Mihai Gherghina
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand, i apologize for not being more specific in my question...

So the assignment want me to output something like this in the console

Movie [Name=Orange County, Director=Jake Kasdan, MPAA Rating=PG-13, Average Rating=2.5]"

I am outputting this

Movie [name=Deadpool, director=vincent, mPAARating=R, AverageRating()=NaN]

How do i implement a rating to be added in order for my Movie class to increment it and take the average ?
 
Mihai Gherghina
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i guess I'm having trouble understanding how to use the addRating method.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use the addRating method, the same way you used the setDirector method to set "martin" as the director for the "Zootopia" movie.
 
Mihai Gherghina
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would i do it for the first part, line 4 in my MovieDemo
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at lines 4, 5, 31, and 39 in your first object. It looks like it is already done.

Mihai Gherghina wrote:I understand, i apologize for not being more specific in my question...

So the assignment want me to output something like this in the console

Movie [Name=Orange County, Director=Jake Kasdan, MPAA Rating=PG-13, Average Rating=2.5]"

I am outputting this

Movie [name=Deadpool, director=vincent, mPAARating=R, AverageRating()=NaN]

How do i implement a rating to be added in order for my Movie class to increment it and take the average ?
 
Mihai Gherghina
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. So may I ask any of you here in the industry. Just be looking at this code, what do you think my biggest flaws are ? What should i improve on ? and lastly, how efficient or inefficient is my structure ?
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and welcome to the Ranch

I trust that your approach to a long list of instructions like those above is to take them one step at a time. Get the first line working, then try the second instruction.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!