* Adds movies, and rates them.
*
* @Michael Silber
* @1.0 (September 7, 2010)
*/
public class Movie // or MovieRating, except I thought "Movie" makes more sense, as you're adding a movie.
{
private String name; // field
private int time; // in minutes.
private int raters; //declaration
private double rating;
private int tens;
/**
* Constructors for class.
*/
public Movie() // constructor
{
name = ""; //initialization
time = 0;
raters = 0;
rating = 0.0;
tens = 0;
}
/**
* Constructors for class.
*/
public Movie(String theName, int theTime)
{
name = theName;
time = theTime;
raters = 0;
rating = 0.0;
tens = 0;
}
/**
* Get the movie name.
*/ // comment
public String getName() //accessor method
{
return name; //return statement
}
/**
*
*/
public void movieDetails()
{
System.out.println(""); //block statement
System.out.println("Movie name:"+" "+name);
System.out.println("Movie duration:"+" "+time);
System.out.println("");
}
/**
* Get the movie duration (in minutes).
*/
public int getLength() //method signature
{
return time; // method body
}
/**
* Change the movie name.
*/
public void changeName(String newName)
{
name //expression
= //assignment operator
newName;
}
/**
* Change the movie lenght.
*/
public void changeLength(int newTime //formal parameter
) //mutator method
{
time = newTime; //assignment statement
}
/**
* Rate the movie.
*/
public void //return type
rate(int newRating)
{
if(newRating<1 || newRating>10) //conditional statement
{
System.out.println("");
System.out.println("Please enter a rating value of 1 through 10");
System.out.println("");
}
else
{
double overallRating; //local variable
overallRating = ((rating*raters)+newRating)/(raters+1);
rating = overallRating;
raters = raters+1;
if(newRating == 10)
{
tens=tens+1;
}
}
}
/**
* Get rating.
*/
public void getRating()
{
System.out.println("");
System.out.println("*************************************************************************");
System.out.println(raters+" "+"person(s) have rated this movie.");
System.out.println("The average rating is" + rating+".");
System.out.println(tens +" "+"person(s) rated this movie a value of 10/10");
System.out.println("*************************************************************************");
System.out.println("");
}
}
Meaningful Sentences for Four Vocabulary Words:
1. Often fields in a class are left with formal parameters which have to be given actual parameters, or specific values, by the creator of an object upon its creation. An example of this is the "name" field in the MovieRating class, which either receives a unique actual name from its creator, or uses the default settings "", upon creation, both of which are actual parameters.
2. Another term for a field is an instance variable, both of which are defined at the beginning of the class source code, and present spaces in which values can be stored. The earlier example of "name" in the MovieRating class, or others, such as "time," "raters," and "rating," are all fields.
3. The scope of a method is defined at the signature and is either, from what we've learned so far, "public" or "private."Public means the scope of the method is accessible to objects created by that class, and private, on the contrary, means that the scope is limited to that method and/or declaration. The scope of my public String getName() method in the MovieRating class is defined at the beginning as "public."
4. The lifetime of a variable is often only from the invocation of the method, to its completion. Such variables are referred to as local variables. In the MovieRating class, one such example of a variable with a lifetime that exists until the end of the method is my double overallRating local variable, which exists in the public void rate() method.
No comments:
Post a Comment