We will continue working on the Section class at a future date.
Today we will start discussing inheritance.
You will need to know this for the homework assignment.
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.
FileWriter VARIABLE_NAME = new FileWriter(FILENAME;)
import java.io.FileWriter; import java.io.IOException;
import java.io.FileWriter; import java.io.IOException; public class WriteToFile { public static void main (String[] args){ String filename = args[0]; try { FileWriter output = new FileWriter(filename); output.write("Line 1 \n"); output.write("Line 2 \n"); output.write("Line 3 \n"); output.close(); } catch (IOException e) { System.out.println("Could not create a FileWriter object for " + filename); } } }
public class Video1 { private int collection_no; private String title; private int length; private String format; public Video1(int collection_no, String title, int length, String format){ this.collection_no = collection_no; this.title = title; this.length = length; this.format = format; } public String toString(){ return "#" + collection_no + ": " + title + ", " + length + " minutes, " + format; } public int collection_no(){ return collection_no; } public String title(){ return title; } public int length(){ return length; } public String format(){ return format; } }
public class Video1Driver { public static void main (String[] args){ Video1 v = new Video1(1, "Forbidden Planet", 98, "DVD"); System.out.println("Collection number: " + v.collection_no()); System.out.println("Title: " + v.title()); System.out.println("Length" + v.length()); System.out.println("Format:" + v.format()); System.out.println(v); } } $ java Video1Driver Collection number: 1 Title: Forbidden Planet Length98 Format:DVD #1: Forbidden Planet, 98 minutes, DVD
private
methods since only the constructor will be
using them
Night of the Day of the Dawn of the Son of the Bride of the Return of the Revenge of the Terror of the Attack of the Evil, Mutant, Hellbound, Flesh-Eating Subhumanoid Zombified Living Dead, Part 3
private boolean title_good(String title) throws IllegalArgumentException { if (title.length() < MIN_TITLE_LENGTH || title.length() > MAX_TITLE_LENGTH){ throw new IllegalArgumentException("Title must be between " + MIN_TITLE_LENGTH + " and " + MAX_TITLE_LENGTH + " characters long"); } else { return true; } }
true
if it doespublic class Video2 { final int MIN_TITLE_LENGTH = 2; final int MAX_TITLE_LENGTH = 250; ... }
public Video2(int collection_no, String title, int length, String format) throws IllegalArgumentException { if (title_good(title)){ this.collection_no = collection_no; this.title = title; this.length = length; this.format = format; } }
public class Video2Driver {
public static void main (String[] args){
try {
Video2 v1 = new Video2(1, "I", 98, "DVD");
System.out.println(v1);
} catch (Exception e) {
System.out.println(e);
}
Video2 v2 = new Video2(1, "Forbidden Planet", 98, "DVD");
System.out.println(v2);
}
}
$ java Video2Driver
java.lang.IllegalArgumentException: Name must be between 2 and 250 characters long
#1: Forbidden Planet, 98 minutes, DVD
final int MIN_LENGTH = 15; final int MAX_LENGTH = 1000;
private boolean length_good(int length) throws IllegalArgumentException { if (length < MIN_LENGTH || length > MAX_LENGTH){ throw new IllegalArgumentException("Length must be between " + MIN_LENGTH + " and " + MAX_LENGTH + " characters long"); } else { return true; } }
public Video3(int collection_no, String title, int length, String format)
throws IllegalArgumentException {
if (title_good(title) && length_good(length)){
this.collection_no = collection_no;
this.title = title;
this.length = length;
this.format = format;
}
}
public class Video3Driver {
public static void main (String[] args){
try {
Video3 v1 = new Video3(1, "Forbidden Planet", 2000, "DVD");
System.out.println(v1);
} catch (Exception e) {
System.out.println(e);
}
Video3 v2 = new Video3(1, "Forbidden Planet", 98, "DVD");
System.out.println(v2);
}
}
$ java Video3Driver
java.lang.IllegalArgumentException: Length must be between 15 and 1000 characters long
#1: Forbidden Planet, 98 minutes, DVD
final String [] FORMATS = {"DVD", "Blu-ray"};
for
looptrue
private boolean length_good(int length) throws IllegalArgumentException { if (length < MIN_LENGTH || length > MAX_LENGTH){ throw new IllegalArgumentException("Length must be between " + MIN_LENGTH + " and " + MAX_LENGTH + " characters long"); } else { return true; } } private boolean format_good(String format) throws IllegalArgumentException { if (format.length() == 0) { throw new IllegalArgumentException("You must specify a format"); } else { for (int i = 0; i < FORMATS.length; i++){ if (format.equals(FORMATS[i])) { return true; } } throw new IllegalArgumentException( format + " is not a valid format"); } }
public Video4(int collection_no, String title, int length, String format) throws IllegalArgumentException { if (title_good(title) && length_good(length) && format_good(format)){ this.collection_no = collection_no; this.title = title; this.length = length; this.format = format; } }
public class Video4Driver { public static void main (String[] args){ try { Video4 v1 = new Video4(1, "Forbidden Planet", 98, ""); System.out.println(v1); } catch (Exception e1) { System.out.println(e1); try { Video4 v2 = new Video4(1, "Forbidden Planet", 98, "XXX"); System.out.println(v2); } catch (Exception e2) { System.out.println(e2); } } Video4 v3 = new Video4(1, "Forbidden Planet", 98, "DVD"); System.out.println(v3); } } $ java Video4Driver java.lang.IllegalArgumentException: You must specify a format java.lang.IllegalArgumentException: XXX is not a valid format #1: Forbidden Planet, 98 minutes, DVD
Be honest and trustworthy
Be fair and take action not to discriminate