Homework 10 is due this Sunday at 11:59 PM.
You will find the assignment here.
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); } } } $ java WriteToFile test.txt $ cat test.txt Line 1 Line 2 Line 3
import java.time.LocalDateTime; public class CurrentDate { public static void main (String[] args){ LocalDateTime today = LocalDateTime.now(); System.out.println(today); } }
$ java CurrentDate 2020-04-10T12:40:58.990which has both the date and the time
MM-DD_attendance_CLASS_ID_SECTION_NUMBER.txt
05-04_attendance_it114_1.txt
public void attendance_file_write(){ String filename = attendance_filename(); try { FileWriter output = new FileWriter(filename); output.write(header()); for (int i = 0; i < students.size(); i++){ output.write(student_line(students.get(i))); } output.close(); } catch (IOException e) { System.out.println("Could not create FileWriter on " + filename); } }
private String attendance_filename(){ return "attendance.txt"; } private String header(){ return "-------------------------------"; } private String student_line(Student s){ return ""; }
import java.util.ArrayList; public class Section4Driver { public static void main (String[] args){ Section4 section = new Section4("IT 114", "Introduction to Java", "1"); section.add_students("it114-1_student.txt"); ArrayList students = section.get_students(); section.attendance_file_write(); } } $ java Section4Driver $ cat attendance.txt -------------------------------
2020-04-10T16:23:10.186
LocalDateTime today = LocalDateTime.now();does not return a string
// create a string of the form MM-DD_attendance_CLASS_ID_SECTION_NUMBER.txt private String attendance_filename(){ // get a string with date and time data LocalDateTime today = LocalDateTime.now(); String today_string = today.toString(); // get month and day strings String [] fields = today_string.split("T"); String yr_mnth_day = fields[0]; fields = yr_mnth_day.split("-"); String month_no = fields[1]; String day_no = fields[2]; // get class ID in the correct format String class_id = this.class_id; // make a local copy we can manipulate class_id = class_id.toLowerCase(); class_id = class_id.replace(" ", ""); String filename = month_no + "_" + day_no + "_attendance_" + class_id + "-" + this.section_no + ".txt"; return filename; }
import java.util.ArrayList; public class Section5Driver { public static void main (String[] args){ Section5 section = new Section5("IT 114", "Introduction to Java", "1"); section.add_students("it114-1_student.txt"); ArrayList students = section.get_students(); section.attendance_file_write(); } } $ java Section5Driver $ cat 04_10_attendance_it114-1.txt -------------------------------
private String header(){ return this.class_id + "-" + this.section_no + " Attendance\n" + "-------------------------------\n"; }
private String student_line(Student s){ String line = "__ " + s.full_name(); String phonetic = s.get_phonetic(); if ( phonetic.length() > 0) { line += " (" + phonetic + ")"; } System.out.println(line); return line + "\n"; }
import java.util.ArrayList; public class Section6Driver { public static void main (String[] args){ Section6 section = new Section6("IT 114", "Introduction to Java", "1"); section.add_students("it114-1_student.txt"); ArrayList<Student> students = section.get_students(); // add preferred and phonetic fields for Dimetrio for (int i = 0; i < students.size(); i++) { Student s = students.get(i); String name = s.full_name(); if (name.contains("Dimetrio")) { s.set_prefered("Bill"); s.set_phonetic("di MEET tree oh"); } } section.attendance_file_write(); } } $ java Section6Driver __ Bill Dimetrio (di MEET tree oh) __ Andrew Smith __ Joan Sanders
public class Grade1 { private String student_id; private String class_id; private String grade; public Grade1(String student_id, String class_id, String grade){ this.student_id = student_id; this.class_id = class_id; this.grade = grade; } public String toString(){ return student_id + ", " + class_id + ", " + grade; } }
public class Grade1Driver { public static void main (String[] args){ Grade1 g = new Grade1("09999999", "IT 114", "A" ); System.out.println(g); } } $ java Grade1Driver 09999999, IT 114, A
private boolean grade_good(String grade) throws IllegalArgumentException{ switch(grade) { case "A": case "A-": case "B+": case "B": case "B-": case "C+": case "C": case "C-": case "D+": case "D": case "D-": case "F": return true; default: throw new IllegalArgumentException(grade + " is not a legal grade"); } }
switch
statementpublic Grade2(String student_id, String class_id, String grade) throws IllegalArgumentException{ if (grade_good(grade)){ this.student_id = student_id; this.class_id = class_id; this.grade = grade;
public class Grade2Driver {
public static void main (String[] args){
try {
Grade2 g1 = new Grade2("08888888", "IT 114", "XX" );
System.out.println(g1);
} catch (Exception e) {
System.out.println(e);
}
Grade2 g2 = new Grade2("09999999", "IT 114", "A-" );
System.out.println(g2);
}
}
$ java Grade2Driver
java.lang.IllegalArgumentException: XX is not a legal grade
09999999, IT 114, A-
public String get_student_id(){ return student_id; } public String get_grade(){ return grade; }
public class Grade3Driver { public static void main (String[] args){ Grade3 g = new Grade3("09999999", "IT 114", "A-" ); System.out.println(g); System.out.println("Student ID: " + g.get_student_id()); System.out.println("Grade: " + g.get_grade()); } } $ java Grade3Driver 09999999, IT 114, A- Student ID: 09999999 Grade: A-
public void grade_all_students(){ for (int i =0; i < students.size(); i++){ Student s = students.get(i); System.out.println(s.full_name()); String student_id = s.get_student_id(); Grade grade = grade_create(student_id); grades.add(grade); } }
private Grade grade_create(String student_id){ Scanner console = new Scanner(System.in); System.out.print("Grade: "); String grade_letter = console.next(); Grade grade = new Grade(student_id, this.class_id, grade_letter); return grade; }
import java.util.ArrayList; public class Section7Driver { public static void main (String[] args){ Section7 section = new Section7("IT 114", "Introduction to Java", "1"); section.add_students("it114-1_student.txt"); ArrayList<Student> students = section.get_students(); section.grade_all_students(); ArrayList<Grade> grades = section.get_grades(); for (int i = 0; i < grades.size(); i++){ Grade grade = grades.get(i); System.out.println(grade); } } } $ java Section7Driver William Dimetrio Grade: A Andrew Smith Grade: B Joan Sanders Grade: C 07777777, IT 114, A 06666666, IT 114, B 05555555, IT 114, C