Home Segments Top Top Previous Next

766: Mainline

The following definition implements the interface and declares all the instance variables to be private, ensuring that external access to those variables must go through the setters and getters specified in the interface.

The class definition also extends the Observable class, which is found in the java.util package. Henceforward, Movie instances are also Observable instances, and the Movie class is a model class.

import java.util.*; 
public class Movie extends Observable implements MovieInterface {  
 // Define instance variables: 
 private String title, poster; 
 private int script = 5, acting = 5, direction = 5; 
 // Define three-parameter constructor:  
 public Movie (int s, int a, int d) {  
  script = s; acting = a; direction = d; 
  title = "Title Unknown"; 
 }  
 // Define four-parameter constructor:  
 public Movie (int s, int a, int d, String t) {  
  this(s, a, d); 
  title = t;  
 }  
 // Define five-parameter constructor:  
 public Movie (int s, int a, int d, String t, String p) {  
  this(s, a, d, t); 
  poster = p;  
 }  
 // Define setters and getters: 
 public void setScript(int s) {script = s;} 
 public void setActing(int a) {acting = a;} 
 public void setDirection(int d) {direction = d;} 
 public int getScript() {return script;} 
 public int getActing() {return acting;} 
 public int getDirection() {return direction;} 
 public String getTitle() {return title;} 
 public String getPoster() {return poster;} 
 // Define other methods: 
 public int rating () { 
  return script + acting + direction; 
 } 
 // changed method defined here 
}