The fully developed
Movie
class is as
follows. Note that the setters have been brought up to professional
standards through two flourishes. First, the setters do nothing unless the value
to be set is different from the existing value. This do-nothing behavior prevents the
mechanism that activates observers from working with useless information.
Second, the setters refuse to set a value outside a prescribed range
established by the static variables MIN
and MAX
.
import java.util.*; public class Movie extends Observable implements MovieInterface { // Define instance and static variables: private String title, poster; private int script = 5, acting = 5, direction = 5; private static int MIN = 0, MAX = 10; // 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: public void setScript(int s) { if (script != s) { script = Math.max(Math.min(s, MAX), MIN); setChanged(); notifyObservers(); } } public void setActing(int a) { if (acting != a) { acting = Math.max(Math.min(a, MAX), MIN); setChanged(); notifyObservers(); } } public void setDirection(int d) { if (direction != d) { direction = Math.max(Math.min(d, MAX), MIN);; setChanged(); notifyObservers(); } } // Define getters: 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 rating and changed methods: public int rating () { return script + acting + direction; } public void changed () { setChanged(); notifyObservers(); } }