1 and 2:
/**
* Creates a system for rating movies.
*/
public class MovieRating
{
private String name; //field
private String time; // in minutes
private int rating; //declaration
private int numRatings;
private int max;
private int tempRating;
/**
* Constructor for objects of class MovieRating //comment
*/
public MovieRating() //constructor
{
name = ""; // initialization
time = "";
rating = 0;
numRatings = 0;
max = 0;
tempRating = 0;
}
/**
* Second constructor.
*/
public MovieRating(String movieName)
{
name = movieName; //initialization
rating = 0;
numRatings = 0;
max = 0;
tempRating = 0;
}
/**
* Set the movie name.
*/
public void setName(String changeName) //method signature
{
name = changeName; //method body
}
/**
* Set the running time in minutes.
*/
public void setTime(String movieTime) //mutator
{
time = movieTime;
}
/**
* Print movie details.
*/
public void printDetails() //accessor
{
System.out.println("Movie Name: "+name); //everything inside these curly brackets is a block statement
System.out.println("Running Time: "+time);
System.out.println("");
}
/**
* Rate the movie from 1 to 10.
*/
public void setRating(int movieRating) //formal parameter (movieRating)
{
if (movieRating < 1 || movieRating > 10){ //conditional statement
System.out.println("Sorry. Only ratings of between 1 and 10 are accepted.");
System.out.println("");
}
if (movieRating == 10){
max = max +1; // assignment statement
}
else {
numRatings = numRatings + 1; // '=' is an assignment operator
tempRating = tempRating + movieRating;
rating = tempRating/numRatings; //expression
}
}
/**
* Print the movie rating.
*/
public void printRating() // void return type
{
System.out.println(name+" has an average rating of "+rating+" rated by "+numRatings+ " people.");
}
/**
* Print the maximum rating.
*/
public void printMax()
{
System.out.println(name + " has an average rating of "+rating+" rated by "+numRatings+ " people. "+ max+" person(s) gave the movie the maximum rating of 10.");
}
}
3:
1. Actual parameters (also known as arguments) are what are passed by the caller. This refers to the MovieRating test because I changed the formal parameter of "Rating" with the actual parameter of 6.
2. An instance variable is a variable which is related to a single instance of a class. Each time an instance of a class is created, the system creates one copy of the instance variables related to that class. I used this as a field to my method in MovieRatings project.
3. The scope of my public String getName() method in the MovieRating class is defined at the beginning as "public."
4. During the execution of a Java program, each variable has its own time within which it can be accessed. This is called the lifetime of the variable. I used this in the Chapter 2 test at the end of the method: overallRating.
No comments:
Post a Comment