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.
private String director; private String studio; private ArrayList actors;
import java.util.ArrayList; public class Lectures1 extends Video2 { private String course; private int disc_no; private String instructor; private ArrayList<String> lectures; public Lectures1 (int collection_no, String title, int length, String format, String course, int disc_no, String instructor){ super(collection_no, title, length, format); this.course = course; this.disc_no = disc_no; this.instructor = instructor; this.lectures = new ArrayList<>(); } }
public class Lectures1Driver { public static void main (String[] args){ Lectures1 l = new Lectures1(2, "Origins of Great Ancient Civilizations - Disc 1", 180, "DVD", "Origins of Great Ancient Civilizations", 1, "Kenneth W. Harl"); System.out.println(l); } } $ java Lectures1Driver #1: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD
super
public Lectures2 (int collection_no, int length, String format, String course, int disc_no, String instructor){ super(collection_no, course + " - Disc " + disc_no, length, format); this.course = course; this.disc_no = disc_no; this.instructor = instructor; this.lectures = new ArrayList<>(); } }
public class Lectures2Driver { public static void main (String[] args){ Lectures2 l = new Lectures2(2, 180, "DVD", "Origins of Great Ancient Civilizations", 1, "Kenneth W. Harl"); System.out.println(l); } } $ java Lectures2Driver #1: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD
public String course(){ return course; } public int disc_no() { return disc_no; } public String instructor(){ return instructor; }
public String toString() { return super.toString() + ", " + instructor; }
public void add_lecture(String lecture){ lectures.add(lecture); } public String lectures(){ String value = ""; for (int i = 0; i < lectures.size(); i++){ value += lectures.get(i) + "\n"; } return value; }
public class Lectures3Driver { public static void main (String[] args){ Lectures3 l = new Lectures3(2, 180, "DVD", "Origins of Great Ancient Civilizations", 1, "Kenneth W. Harl"); System.out.println(l); l.add_lecture("Cradles of Civilization"); l.add_lecture("First Cities of Sumer"); l.add_lecture("Mesopotamian Kings and Scribes"); System.out.println("Course: " + l.course()); System.out.println("Disc number: " + l.disc_no()); System.out.println("Instructor: " + l.instructor()); System.out.println("Lectures:\n" + l.lectures()); } } $ java Lectures3Driver #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl Course: Origins of Great Ancient Civilizations Disc number: 1 Instructor: Kenneth W. Harl Lectures: Cradles of Civilization First Cities of Sumer Mesopotamian Kings and Scribes
while
loop
asking the user to say what they want to do
import java.util.ArrayList; import java.util.Scanner; public class VideoLibrary1 { private static ArrayList<Video> library = new ArrayList<>(); private static final String [] action_values = {"m", "l", "s", "d"}; private static final String prompt = "M(ovie), L(ecture), S(ee entries), D(one): "; public static void main (String[] args){ Scanner console = new Scanner(System.in); boolean done = false; while (! done){ System.out.print(prompt); String action = console.next(); action = action.trim(); // remove any leading or trailing whitespace action = action.toLowerCase(); // only lowercase letters if (! action_valid(action)){ System.out.println(action + " is not a valid choice"); continue; } else if (action.equals("d")) { System.out.println("Exiting"); done = true; } } } private static boolean action_valid(String action){ action = action.trim(); boolean result = false; for (int i = 0; i < action_values.length; i++){ if (action.equals(action_values[i])) { return true; } } return result; } }
$ java VideoLibrary1.java M(ovie), L(ecture), S(ee entries), D(one): m M(ovie), L(ecture), S(ee entries), D(one): M M(ovie), L(ecture), S(ee entries), D(one): l M(ovie), L(ecture), S(ee entries), D(one): L M(ovie), L(ecture), S(ee entries), D(one): s M(ovie), L(ecture), S(ee entries), D(one): S M(ovie), L(ecture), S(ee entries), D(one): D Exiting
private static Movie add_movie(Scanner console)throws FileNotFoundException{ System.out.print("Filename: "); String filename = console.next(); File file = new File(filename); Scanner input = new Scanner(file); int collection_no = Integer.parseInt(input.nextLine()); String title = input.nextLine(); int length = Integer.parseInt(input.nextLine()); String format = input.nextLine(); String director = input.nextLine(); String studio = input.nextLine(); Movie movie = new Movie(collection_no, title, length, format, director, studio); System.out.println(movie); return movie }
else if
clause to our while loop
} else if (action.equals("m")) { Video2 movie = add_movie(console); library.add(movie); }
1 Forbidden Planet 98 DVD Fred McLeod Wilcox MGM
$ java VideoLibrary M(ovie), L(ecture), S(ee entries), D(one): m Filename: forbidden_planet.txt Forbidden Planet MGM #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM M(ovie), L(ecture), S(ee entries), D(one): d Exiting
private static Lectures add_lectures(Scanner console)throws FileNotFoundException{ System.out.print("Filename: "); String filename = console.next(); File file = new File(filename); Scanner input = new Scanner(file); int collection_no = Integer.parseInt(input.nextLine()); int length = Integer.parseInt(input.nextLine()); String format = input.nextLine(); String course = input.nextLine(); int disc_no = Integer.parseInt(input.nextLine()); String instructor = input.nextLine(); Lectures lectures = new Lectures(collection_no, length, format, course, disc_no, instructor); System.out.println(lectures); return lectures; }
else if
clause to the while
loop in the main method
} else if (action.equals("l")) { Video2 lectures = add_lectures(console); library.add(lectures); }
2 180 DVD Origins of Great Ancient Civilizations 1 Kenneth W. Harl
$ java VideoLibrary4 M(ovie), L(ecture), S(ee entries), D(one): l Filename: ancient_civilizations_1.txt #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl M(ovie), L(ecture), S(ee entries), D(one): d Exiting
private static void list_videos(){ for (int i = 0; i < library.size(); i++){ System.out.println(library.get(i)); } }
else if
clause to the while
loop in the
main method
} else if (action.equals("s")) { list_videos(); }
$ java VideoLibrary5 M(ovie), L(ecture), S(ee entries), D(one): m Filename: forbidden_planet.txt #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM M(ovie), L(ecture), S(ee entries), D(one): l Filename: ancient_civilizations_1.txt #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl M(ovie), L(ecture), S(ee entries), D(one): s #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl M(ovie), L(ecture), S(ee entries), D(one): d Exiting
private static ArrayList<Video2> library = new ArrayList<>(); ... } else if (action.equals("m")) { Video2 movie = add_movie(console); library.add(movie); } else if (action.equals("l")) { Video2 lectures = add_lectures(console); library.add(lectures); }
private static void list_videos(){ for (int i = 0; i < library.size(); i++){ System.out.println(library.get(i)); } }