Sunday 26 September 2010

Chapter 2 Test

    
    
    
    



  
/**
 * Write a description of class MovieRating here.
 *
 * @author (THE JONATHAN)
 * @version (a version number or a date)
 */
public class MovieRating
{
    // instance variables - replace the example below with your own
    private int RunningTime;
    private String MovieName; //declaration
    private int Rating;
    private int TotalRating=0;
    private int NumberOfRatings=0;// initialization
    private int AverageRating;
    private int NumberOfTens=0;

    /**
     * Constructor for objects of class MovieRating
     */
    public MovieRating(String name, int time)//constructor
    {
        // initialise instance variables
        MovieName = name;
        RunningTime = time; //assignment statement //local variable time
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
    /**
     * return movie runtime // comment
     */
    public int getRunningTime()//method signature//return type int
    {
      
        return RunningTime; // method body //accessor
    }
   /**
    * return movie name
    */
    public String getName()
    {
        return MovieName; //return statement
    }

    /**
     * changes movie name
     */
    public void changeName(String newName)
    {
        MovieName = newName;
    }
  
     /**
     * changes movie run time
     */
    public void changeRunTime(int newRunTime)//mutator //formal parameter RunTime
    {
        RunningTime = newRunTime;
    }
    /**
     * Rate movie
     */
    public void addRating(int Rating)
    {
        if (Rating>10 || Rating<1)
        System.out.println("Sorry, only ratings between 1 and 10 are accepted");
    
        else NumberOfRatings= NumberOfRatings+1;
      
        if (Rating>=1 && Rating <=10)
        TotalRating = TotalRating + Rating;//expression
        else TotalRating=TotalRating;
      
        if(Rating>=1 && Rating <=10)//conditional statement
        AverageRating = TotalRating/NumberOfRatings; //operator //"TotalRating+Rating" is an
        else AverageRating = AverageRating;
      
        if(Rating == 10)
        NumberOfTens = NumberOfTens+1;
        else NumberOfTens = NumberOfTens;
        /**
         * return number of rating movie recieved
         */
    }
    public int getNumberOfRatings()
    {
        return NumberOfRatings;
      
        /**
         * return the average of ratings
         */
    }
    public void getAverageRating()
    {
        System.out.println ("The Average Rating out of " + NumberOfRatings + " ratings for " + MovieName+ " is " + AverageRating + " and there were " +NumberOfTens + " 10's submitted.");
    } //anything between curly brackets is block statement
  
    /**
     * tells you how many perfect scores the movie recieved
     */
    public int getNumberOfTens()
    {
        return NumberOfTens;
    }
}

3)

Instance variables are fields, we use fields to define out methods in our source code
 Lifetime is the time that the method is called
Scope is the method the local variable is in

No comments:

Post a Comment