Homework 11 is due this Sunday at 11:59 PM.
You will find the assignment here.
This will be the last homework assignment.
If you have a problem or a question, make a post on the Class Discussion Area.
Be honest and trustworthy
Be fair and take action not to discriminate
private String director;
private String studio;
private ArrayList actors;
public class SUBCLASS_NAME extends SUPERCLASS_NAME
import java.util.ArrayList;
public class Movie1 extends Video {
private String director;
private String studio;
private ArrayList<String> actors;
public Movie1 (int collection_no, String title, int length,
String format, String director, String studio){
this.collection_no = collection_no;
this.title = title;
this.length = length;
this.format = format;
this.director = director;
this.studio = studio;
this.actors = new ArrayList<>();
}
}
$ javac Movie1.java
./Movie1.java:9: error: constructor Video in class Video cannot be applied to given types;
String format, String director, String studio){
^
required: int,String,int,String
found: no arguments
reason: actual and formal argument lists differ in length
./Movie1.java:10: error: collection_no has private access in Video
this.collection_no = collection_no;
^
./Movie1.java:11: error: title has private access in Video
this.title = title;
^
./Movie1.java:12: error: length has private access in Video
this.length = length;
^
./Movie1.java:13: error: format has private access in Video
this.format = format;
^
5 errors
...
super(EXPRESSION, ...);
public Movie2 (int collection_no, String title, int length,
String format, String director, String studio){
super(collection_no, title, length, format);
this.director = director;
this.studio = studio;
this.actors = new ArrayList<>();
}
public class Movie2Driver {
public static void main (String[] args){
Movie2 m = new Movie2(1, "Forbidden Planet", 98, "DVD", "Fred McLeod Wilcox", "MGM");
System.out.println(m);
}
}
$ java Movie2Driver
#1: Forbidden Planet, 98 minutes, DVD
super and dot notation
super refers to the parent class
public String toString(){
return super.toString() + ", " + director + ", " + studio;
}
public class Movie3Driver {
public static void main (String[] args){
Movie3 m = new Movie3(1, "Forbidden Planet", 98, "DVD", "Fred McLeod Wilcox", "MGM");
System.out.println(m);
}
}
$ java Movie3Driver
#1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM
public String director(){
return director;
}
public String studio(){
return studio;
}
public class Movie4Driver {
public static void main (String[] args){
Movie4 m = new Movie4(1, "Forbidden Planet", 98, "DVD", "Fred McLeod Wilcox", "MGM");
System.out.println("Collection #: " + m.collection_no());
System.out.println("Title: " + m.title());
System.out.println("Length: " + m.length());
System.out.println("Format: " + m.format());
System.out.println("Director: " + m.director());
System.out.println("Studio: " + m.studio());
}
}
$ java Movie4Driver
Collection #: 1
Title: Forbidden Planet
Length: 98
Format: DVD
Director: Fred McLeod Wilcox
Studio: MGM
public void add_actor(String actor){
actors.add(actor);
}
public String actors(){
String value = "";
for (int i = 0; i < actors.size(); i++){
value += actors.get(i) + " ";
}
return value;
}
public class Movie5Driver {
public static void main (String[] args){
Movie5 m = new Movie5(1, "Forbidden Planet", 98, "DVD", "Fred McLeod Wilcox", "MGM");
m.add_actor("Walter Pidgeon");
m.add_actor("Anne Francis");
m.add_actor("Leslie Nielson");
m.add_actor("Warren Stevens");
System.out.println(m.actors());
}
}
$ java Movie5Driver
Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens
public boolean name_good(String field_name, String value, int min, int max)
throws IllegalArgumentException {
if (value.length() < min || value.length() > max){
throw new IllegalArgumentException(field_name + " must be between " +
min + " and " + max + " characters long");
} else {
return true;
}
}
public Video5(int collection_no, String name, int length, String format)
throws IllegalArgumentException {
if (name_good("name", name, MIN_NAME_LENGTH, MAX_NAME_LENGTH) &&
length_good(length) && format_good(format)){
this.collection_no = collection_no;
this.name = name;
this.length = length;
this.format = format;
}
}
public class Video5Driver {
public static void main (String[] args){
try {
Video5 v1 = new Video5(1, "", 98, "DVD");
System.out.println(v1);
} catch (Exception e1) {
System.out.println(e1);
}
Video5 v2 = new Video5(1, "Forbidden Planet", 98, "DVD");
System.out.println(v2);
}
}
$ java Video5Driver
java.lang.IllegalArgumentException: name must be between 2 and 250 characters long
#1: Forbidden Planet, 98 minutes, DVD
final int MIN_NAME_LENGTH = 10;
final int MAX_NAME_LENGTH = 50;
final int STUDIO_MIN_LENGTH = 3;
final int STUDIO_MAX_LENGTH = 25;
public Movie6 (int collection_no, String title, int length, String format, String director, String studio) {
super(collection_no, title, length, format);
if (super.name_good("director", director, MIN_NAME_LENGTH, MAX_NAME_LENGTH ) &&
super.name_good("studio", studio,STUDIO_MIN_LENGTH, STUDIO_MAX_LENGTH)) {
this.director = director;
this.studio = studio;
this.actors = new ArrayList<>();
}
}
public void add_actor(String actor){
if (super.name_good("actor", actor, MIN_NAME_LENGTH, MAX_NAME_LENGTH )) {
actors.add(actor);
}
}
public class Movie6Driver {
public static void main (String[] args){
try {
Movie6 m1 = new Movie6(1, "Forbidden Planet", 98, "DVD", "", "MGM");
System.out.println(m1);
} catch (Exception e1){
System.out.println(e1);
try {
Movie6 m2 = new Movie6(1, "Forbidden Planet", 98, "DVD", "Fred McLeod Wilcox", "");
System.out.println(m2);
} catch (Exception e2) {
System.out.println(e2);
try {
Movie6 m3 = new Movie6(1, "Forbidden Planet", 98, "DVD", "Fred McLeod Wilcox", "MGM");
m3.add_actor("X");
System.out.println(m3);
System.out.println(m3.actors());
} catch (Exception e3) {
System.out.println(e3);
Movie6 m4 = new Movie6(1, "Forbidden Planet", 98, "DVD", "Fred McLeod Wilcox", "MGM");
m4.add_actor("Walter Pidgeon");
m4.add_actor("Anne Francis");
m4.add_actor("Leslie Nielson");
m4.add_actor("Warren Stevens");
System.out.println(m4.actors());
}
}
}
}
}
$ java Movie6Driver
java.lang.IllegalArgumentException: director must be between 10 and 50 characters long
java.lang.IllegalArgumentException: studio must be between 3 and 25 characters long
java.lang.IllegalArgumentException: actor must be between 10 and 50 characters long
Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens