/**
* Create rating for movie
*/
public class MovieRating
{
private String MovieName; //field
private int RunningTime;
private int numberOfRatings = 0;
private int averageRating = 0;
private int totalRating = 0; //declaration
private int maxRating = 0;
/**
* Constructor for objects of class MovieRating
*/
public MovieRating(String name, int time) //constuctor
{
MovieName = name; // local variable initialized
RunningTime = time;
}
/**
* return MovieName
*/
public void changeName (String newName) //method signature
{
MovieName = newName; //method body
}
/**
* Set the running time in minutes.
*/
public void setRunningTime (int newRunningTime) //mutator
{
RunningTime = newRunningTime;
}
/**
* Get the running time in minutes. //comment
*/
public int getRunningTime()
{
//RunningTime = RunningTime;
return RunningTime; //return statement
}
/**
* Rate movie from 1 to 10.
*/
public void addRating(int movieRating) //formal parameter (movieRating)
{
if (movieRating <> 10){ //conditional statement
System.out.println("Sorry. Only ratings of between 1 and 10 are accepted.");
System.out.println("");
return;
}
if (movieRating == 10){
maxRating = maxRating +1; // assignment statement
totalRating = totalRating + movieRating; //expression
numberOfRatings = numberOfRatings +1;
}
else {numberOfRatings = numberOfRatings +1;
totalRating = totalRating + movieRating; //"=" is an assignment operator
}
}
/**
* Print the movie rating.
*/
public void getaverageRating() // void return type
{
System.out.println(MovieName+" has an average rating of "+totalRating/numberOfRatings+" rated by "+numberOfRatings+ " people.");
}
/**
* Print the maximum rating.
*/
public void printMax() //accessor
{
System.out.println(MovieName + " has an average rating of "+averageRating+" rated by "+numberOfRatings+ " people. "+ maxRating+" person(s) gave the movie the maximum rating of 10.");
} //things inside these {} brackets are block statement.
}
3. For each of the 4 vocabulary items marked with an "o", write one meaningful sentence using that item in reference to an object of class MovieRating.
§ The actual parameter of a movieName is the name of the movie the use puts in. For example – Inception.
§ Instance variable are fields. For example - numberOfRatings, totalRating, MovieName, RunningTime etc.
§ Scope of a field is the whole class. Therefore, for example – scope of MovieName is the whole class.
§ For a local variable, its lifetime will last only until the method exists. Once the method is removed, it gets destroyed. However, for fields the lifetime exists forever. For example – RunningTime is a field and therefore it lasts until the instance exists. MovieName is a local variable and therefore its lifetimes is only until that method exists.
No comments:
Post a Comment