- Adding Section objects to CourseWork
is more complicated then adding Course or
Semester objects
- For one thing we are going to have to make significant changes in the
Section class
- Here are the fields and constructor for the current version of Section
private String class_id;
private String class_name;
private String section_no;
private ArrayList<Student> students;
private ArrayList<Grade> grades;
public Section(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<>();
}
- We will keep the last three fields
- But the first two will be replace by
a Course object and
a Semester object
- The methods that deal with attendance files and grading
will need substantial revision
- So we will delete them for now
- Here is the new version of Section
import java.util.*;
import java.io.*;
public class Section {
private Course course;
private Semester semester;
private String section_no;
private ArrayList<Student> students;
private ArrayList<Grade> grades;
public Section(Course course, Semester semester, String section_no){
this.course = course;
this.semester = semester;
this.section_no = section_no;
students = new ArrayList<>();
grades = new ArrayList<>();
}
public String toString(){
return semester + ":" + course + ":" + section_no;
}
public ArrayList students(){
return students;
}
public ArrayList grades(){
return grades;
}
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 void add_student(String line){
String [] fields = line.split(",");
Student s = new Student(fields[2], fields[1], fields[0]);
students.add(s);
}
}
- And here is code to test it
public class SectionTest {
public static void main (String[] args){
Course course = new Course("IT", "114", "Introduction to Java");
Semester semester = new Semester("Fall", 2020);
Section section = new Section(course, semester, "1");
System.out.println("Course: " + course);
System.out.println("Semester: " + semester);
System.out.println("Section: " + section);
}
}
- Running this code we get
$ java SectionTest
Course: IT 114: Introduction to Java
Semester: Fall 2020
Section: Fall 2020:IT 114: Introduction to Java:1
- We also have to make some changes to assign grades
- If we add back Section the
two methods that deal with grades
- grade_all_students
- grade_create
- We get the following
$ javac Section.java
Section.java:111: error: cannot find symbol
Grade grade = new Grade(student_id, section, grade_letter);
^
symbol: variable section
location: class Section
1 error
- In the current version of Grade we
link it to a course with the field class_id
private String class_id;
- We need to change this field to a Section object
- Which will require changing the constructor and toString
public class Grade {
private String student_id;
private Section section;
private String grade;
public Grade(String student_id, Section section, String grade)
throws IllegalArgumentException{
if (! grade_good(grade)){
throw new IllegalArgumentException(grade + " is not a legal grade");
} else {
this.student_id = student_id;
this.section = section;
this.grade = grade;
}
}
public String toString(){
return student_id + ", " + section + ", " + grade;
}
- But when we try to compile it we get a new error, this time in
Section
$ javac Grade.java
./Section.java:111: error: cannot find symbol
Grade grade = new Grade(student_id, section, grade_letter);
^
symbol: variable section
location: class Section
1 error
- Which is easily fixed by replacing section
with
this
- We test it with another small program
public class GradeTest {
public static void main (String[] args){
Course course = new Course("IT", "114", "Introduction to Java");
Semester semester = new Semester("Fall", 2020);
Section section = new Section(course, semester, "1");
System.out.println("Course: " + course);
System.out.println("Semester: " + semester);
System.out.println("Section: " + section);
Grade g = new Grade("09999999", section, "A" );
System.out.println(g);
}
}
$ java GradeTest
Course: IT 114: Introduction to Java
Semester: Fall 2020
Section: Fall 2020:IT 114: Introduction to Java:1
09999999, Fall 2020:IT 114: Introduction to Java:1, A
- The work of adding Section objects to
CourseWork is a little more difficult
than adding Course and Semester
objects
- But we start off the same way by creating a list for sections
private static ArrayList<Section> sections = new ArrayList<>();
- Adding a new line to menu
private static void menu(){
String menu = "";
menu += "1 : Add Course\n";
menu += "2 : Add Semester\n";
menu += "3 : Add Section\n";
menu += "q : Quit\n";
menu += "? ";
System.out.print(menu);
}
- And a new else if clause to the
while
loop
} else if (choice.equals("3")){
add_section(console);
- So far, so good
- But add_section will be very different from
the other add methods
- Those required the use to enter text
- But add_section needs to have the user
chose from objects that are already contained in CourseWork
- So we need to construct the helper methods select_course
and select_semseter
- Here is the code for add_section
private static void add_section(Scanner console) {
Course course = select_course(console);
Semester semester = select_semester(console);
System.out.print("Section number: ");
String section_no = console.nextLine();
Section section = new Section(course, semester, section_no);
System.out.println(section);
sections.add(section);
}
- select_course has to
- Print a message that the user need to chose a course
- Print a numbered list of courses
- Get the user's choice
- Create a return a Course object
- Getting the user's choice is done with a
while
loop
- We do this because we have to check that the user choice is valid
- Here is
private static Course select_course(Scanner console) {
System.out.println("Select course");
for (int i = 0; i < courses.size() ; i++){
int choice_number = i + 1;
System.out.println(choice_number + ": " + courses.get(i));
}
int user_choice = 0; //dummy value
boolean found_choise = false;
while (! found_choise) {
System.out.print("Choice: ");
user_choice = Integer.parseInt(console.nextLine());
found_choise = choice_good(user_choice, courses.size() +1);
}
return courses.get(user_choice - 1);
}
- And it's helper method
private static boolean choice_good (int user_choice, int max_choice) {
return user_choice >= 1 && user_choice <= max_choice;
}
- select_semester is similar to
to select_course
- Here is what happens when we add a semester
$ java CourseWork
IT 114: Introduction to Java
IT 244: Introduction to Linux/Unix
Fall 2020
1 : Add Course
2 : Add Semester
3 : Add Section
q : Quit
? 3
Select course
1: IT 114: Introduction to Java
2: IT 244: Introduction to Linux/Unix
Choice: 1
Select semester
1: Fall 2020
Choice: 1
Section number: 1
Fall 2020:IT 114: Introduction to Java:1
1 : Add Course
2 : Add Semester
3 : Add Section
q : Quit
? q
- Here are links to the final versions of the code