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.
private
private DATA_TYPE FIELD_NAME;
private
can only be changed by instance methods
inside the class
throw new EXCEPTION_TYPE()
throws
clause
private RETURN_TYPE METHOD_NAME(PARAMETER)
public class Student1 { private String student_id; private String first_name; private String last_name; private String prefered; private String phonetic; private String email; private String username; public Student1(String first, String last, String id){ student_id = id; first_name = first; last_name = last; prefered = ""; phonetic = ""; email = ""; username = ""; } public String toString(){ return first_name + " " + last_name + ", " + student_id; } }
public class Student1Driver { public static void main (String[] args){ Student1 s = new Student1("John", "Smith", "09999999"); System.out.println(s); } } $ java Student1Driver John Smith, 09999999
public void set_prefered(String prefered){ this.prefered = prefered; } public void set_phonetic(String phonetic){ this.phonetic = phonetic; } public void set_email(String email){ this.email = email; } public void set_username(String username){ this.username = username; } public String student_id(){ return student_id; } public String prefered(){ return prefered; } public String phonetic(){ return phonetic; } public String email(){ return email; } public String username(){ return username; }
public class Student2Driver { public static void main (String[] args){ Student2 s = new Student2("William", "Dimetrio", "07777777"); System.out.println(s); System.out.println("Student ID: " + s.student_id()); s.set_prefered("Bill"); System.out.println("Prefered: " + s.prefered()); s.set_phonetic("di MEET tree oh"); System.out.println("Phonetic: " + s.phonetic()); s.set_email("bill.dimetrio001@umb.edu"); System.out.println("Email: " + s.email()); s.set_username("bdimetrio"); System.out.println("Username: " + s.username()); } } $ java Student2Driver William Dimetrio, 07777777 Student ID: 07777777 Prefered: Bill Phonetic: di MEET tree oh Email: bill.dimetrio001@umb.edu Username: bdimetrio
public String full_name() { if (prefered.length() > 0){ return prefered + " " + last_name; } else { return first_name + " " + last_name; } }
public String wiser_name(){ return last_name + ", " + first_name; }
public class Student3Driver { public static void main (String[] args){ Student3 s = new Student3("William", "Dimetrio", "07777777"); s.set_prefered("Bill"); System.out.println(s.full_name()); System.out.println(s.wiser_name()); } } $ java Student3Driver Bill Dimetrio Dimetrio, William
public String toString(){ return full_name() + ", " + student_id + ", " + email + ", " + username + " " + phonetic; }
public class Student4Driver { public static void main (String[] args){ Student4 s = new Student4("William", "Dimetrio", "07777777"); s.set_prefered("Bill"); s.set_phonetic("di MEET tree oh"); s.set_email("bill.dimetrio001@umb.edu"); s.set_username("bdimetrio"); System.out.println(s); } } $ java Student4Driver Bill Dimetrio, 07777777, bill.dimetrio001@umb.edu, bdimetrio di MEET tree oh
import java.util.ArrayList; public class Section1 { private String class_id; private String class_name; private String section_no; private ArrayList<Student> students; private ArrayList<String> grades; public Section1(String class_id, String class_name, String section_no){ this.class_id = class_id; this.class_name = class_name; this.section_no = section_no; students = new ArrayList<>(); grades = new ArrayList<>(); } public String toString(){ return class_id + "-" + section_no + ": " + class_name; } }
public class Section1Driver { public static void main (String[] args){ Section1 section = new Section1("IT 114", "Introduction to Java", "1"); System.out.println(section); } } $ java Section1Driver IT 114-1: Introduction to Java
07777777,Dimetrio,William 06666666,Smith,Andrew 05555555,Sanders,Joan
public void add_students(String filename){ try { File file = new File(filename); Scanner input = new Scanner(file); while (input.hasNext()){ add_student(input.next()); } } catch (Exception e) { System.out.println("Could not open " + filename); } }
private String toString(){ return class_id + "-" + section_no + ": " + class_name; }
public static void main (String[] args){ Section2 section = new Section2("IT 114", "Introduction to Java", "1"); System.out.println(section); section.add_students("it114-1_student.txt"); } } $ java Section2Driver IT 114-1: Introduction to Java 07777777,Dimetrio,William 06666666,Smith,Andrew 05555555,Sanders,Joan
public void add_student(String line){ String [] fields = line.split(","); Student s = new Student(fields[2], fields[1], fields[0]); students.add(s); }
import java.util.ArrayList; public class Section3Driver { public static void main (String[] args){ Section3 section = new Section3("IT 114", "Introduction to Java", "1"); System.out.println(section); section.add_students("it114-1_student.txt"); ArrayList students = section.students(); for (int i = 0; i < students.size(); i++){ System.out.println(students.get(i)); } } } $ java Section3Driver IT 114-1: Introduction to Java William Dimetrio, 07777777, , Andrew Smith, 06666666, , Joan Sanders, 05555555, ,