We have now covered all the material for this introductory course.
That means there will be no more Ungraded quizzes.
It also means that this week's graded quiz will be the last.
From now until the end of the semester I will show you code that uses what you have learned.
Since class discussions from now on will make incremental changes in a couple of applications, we need a new source of programs for Class Exercises, so you don't have to keep typing in the same code over and over again.
From now on, Class Exercises will come from a various sources and will not be directly relevant to the Class Notes.
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)); } }
while
loop
after we have collected all the values needed for the constructor
1 Forbidden Planet 98 DVD Fred McLeod Wilcox MGM Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens
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);
while (input.hasNextLine()){
movie.add_actor(input.nextLine());
}
System.out.println(movie);
return movie;
}
$ java VideoLibrary6 M(ovie), L(ecture), S(ee entries), D(one): m Filename: forbidden_planet_2.txt #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens M(ovie), L(ecture), S(ee entries), D(one): d Exiting
2 180 DVD Origins of Great Ancient Civilizations 1 Kenneth W. Harl Cradles of Civilization First Cities of Sumer Mesopotamian Kings and Scribes Hammurabi’s Babylon Egypt in the Pyramid Age The Middle Kingdom
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);
while (input.hasNextLine()){
lectures.add_lecture(input.nextLine());
}
System.out.println(lectures);
return lectures;
}
$ java VideoLibrary6 M(ovie), L(ecture), S(ee entries), D(one): l Filename: ancient_civilizations_1_2.txt #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl Cradles of Civilization First Cities of Sumer Mesopotamian Kings and Scribes Hammurabi’s Babylon Egypt in the Pyramid Age The Middle Kingdom M(ovie), L(ecture), S(ee entries), D(one): d Exiting
private static int next_collection_no = 1;
public static void main (String[] args) throws FileNotFoundException{
private static int get_collection_no(){ int value = next_collection_no; next_collection_no += 1; return value; }
int collection_no = Integer.parseInt(input.nextLine());with
int collection_no = get_collection_no();
$ jv VideoLibrary7.java M(ovie), L(ecture), S(ee entries), D(one): m Filename: forbidden_planet_3.txt #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens M(ovie), L(ecture), S(ee entries), D(one): l Filename: ancient_civilizations_1_3.txt #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl Cradles of Civilization First Cities of Sumer Mesopotamian Kings and Scribes Hammurabi’s Babylon Egypt in the Pyramid Age The Middle Kingdom M(ovie), L(ecture), S(ee entries), D(one): d Exiting
private static final String archive_filename = "library_archive.txt";
private static final String separator = "----------------------------------------";
String class_name = entry.getClass().getSimpleName();
} else if (action.equals("d")) { store_library_state(); System.out.println("Exiting"); done = true; }
private static void store_library_state() throws FileNotFoundException, IOException { for (int i =0; i < library.size(); i++){ Video entry = library.get(i); String class_name = entry.getClass().getSimpleName(); if (class_name.equals("Movie")) { archive_text += archive_movie_entry(entry); archive_text += separator; } else { archive_text += archive_lectures_entry(entry); archive_text += separator; } } FileWriter output = new FileWriter(archive_filename); output.write(archive_text); output.close(); }
private static String archive_movie_entry(Video entry){ Movie m = (Movie) entry; String result = "\n" + "Movie"; result += "\n" + m.title(); result += "\n" + m.length(); result += "\n" + m.format(); result += "\n" + m.director(); result += "\n" + m.studio(); ArrayList<String> actors = m.actors(); for (int i = 0; i < actors.size(); i++){ result += "\n" + actors.get(i); } return result; }
private static String archive_lectures_entry(Video entry) { Lectures l = (Lectures) entry; String result = "\n" + "Lectures"; result += "\n" + l.length(); result += "\n" + l.format(); result += "\n" + l.disc_no(); result += "\n" + l.instructor(); ArrayList<String> lectures = l.lectures(); for (int i = 0; i < lectures.size(); i++){ result += "\n" + lectures.get(i); } return result; }
$ java VideoLibrary8 M(ovie), L(ecture), S(ee entries), D(one): m Filename: forbidden_planet_3.txt #1: Forbidden Planet, 98 minutes, DVD, Fred McLeod Wilcox, MGM Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens M(ovie), L(ecture), S(ee entries), D(one): l Filename: ancient_civilizations_1_3.txt #2: Origins of Great Ancient Civilizations - Disc 1, 180 minutes, DVD, Kenneth W. Harl Cradles of Civilization First Cities of Sumer Mesopotamian Kings and Scribes Hammurabi’s Babylon Egypt in the Pyramid Age The Middle Kingdom M(ovie), L(ecture), S(ee entries), D(one): d Exiting
$ cat library_archive.txt Movie Forbidden Planet 98 DVD Fred McLeod Wilcox MGM Walter Pidgeon Anne Francis Leslie Nielson Warren Stevens ---------------------------------------- Lectures 180 DVD Origins of Great Ancient Civilizations 1 Kenneth W. Harl Cradles of Civilization First Cities of Sumer Mesopotamian Kings and Scribes Hammurabi’s Babylon Egypt in the Pyramid Age The Middle Kingdom ----------------------------------------
Respect the work required to produce new ideas, inventions, creative works, and computing artifacts.
Respect privacy