I'm writing some code for a project for my intro to OOP course. The program is to write a report based on some courses inputted by the user. My test class looks like this
import java.util.*;
public class CourseReport {
private ArrayList<Showable> courses = new ArrayList<Showable>();
ArrayList<Showable> students = new ArrayList<Showable>();
Scanner keyboard = new Scanner(System.in);
public CourseReport() {
char cont = 'y';
while(cont == 'y') {
courses.add(addCourse());
System.out.println("Need to enter another course? (y.n)");
cont = keyboard.next().charAt(0);
}
}
public Showable addCourse() {
System.out.println("Please enter the Course ID: ");
int courseID = keyboard.nextInt();
while((courseID < 200) || (courseID >= 300)) {
System.out.println("Invaild course ID");
courseID = keyboard.nextInt();
}
Showable instructor = addInstructor();
char cont = 'y';
while(cont == 'y') {
students.add(addStudent());
System.out.println("Need to enter another student? (y,n)");
cont = keyboard.next().charAt(0);
}
Showable course = (Showable) new Course(courseID, instructor,
students);
return course;
}
public Showable addStudent() {
System.out.println("Please input the Student ID: ");
int studentID = keyboard.nextInt();
while (studentID < 0) {
System.out.println("Invalid ID number, try again");
studentID = keyboard.nextInt();
}
System.out.println("Please input the first name of the student");
keyboard.nextLine();
String firstName = keyboard.nextLine();
System.out.println("Please input the last name of the student");
String lastName = keyboard.nextLine();
System.out.println("Please input the grade of the student");
int grade = keyboard.nextInt();
while ((grade > 100) || (grade <= 35)) {
System.out.println("Invalid grade, try again");
grade = keyboard.nextInt();
}
Showable student = new Student(firstName, lastName, studentID, grade);
return student;
}
public Showable addInstructor() {
System.out.println("Please input the first name of the professor");
String firstName = keyboard.nextLine();
System.out.println("Please input the last name of the professor");
String lastName = keyboard.nextLine();
System.out.println("Please input the statues of the professor");
String statues = keyboard.nextLine();
Showable instructor = new Instructor(firstName, lastName, statues);
return instructor;
}
public void printReport() {
Showable course;
for(int i = 0; i < courses.size(); i++) {
course = courses.get(i);
course.show();
((Course) course).getInstructor().show();
for(int j = 0; j < students.size(); j++) {
((Course) course).getStudents().get(j);
}
}
}
public static void main(String[] args) {
CourseReport course = new CourseReport();
course.printReport();
}
}
The line Showable course = (Showable) new Course(courseID, instructor, students);
is giving me a ClassCastException by telling be that Course cannot be casted to Showable. Showable is an interface with the method show. The class Course looks like this
import java.util.*;
public class Course {
private int courseID;
private Showable instructor;
private ArrayList<Showable> students = new ArrayList<Showable>();
public int getCourseID() {
return courseID;
}
public Showable getInstructor() {
return instructor;
}
public void setCourseID(int courseID) {
courseID = this.courseID;
}
public void setInstructor(Showable instructor) {
instructor = this.instructor;
}
public void setStudents(ArrayList<Showable> students) {
this.students = students;
}
public ArrayList<Showable> getStudents() {
return students;
}
public Course() {
}
public Course(int courseID, Showable instructor, ArrayList<Showable> students) {
courseID = this.courseID;
instructor = this.instructor;
students = this.students;
}
public String show() {
return "courseID is: " + courseID;
}
}
Also would anyone know why my inner for loop in the PrintReport method would throw a NullPointerExeception?
public void printReport() {
Showable course;
for(int i = 0; i < courses.size(); i++) {
course = courses.get(i);
course.show();
((Course) course).getInstructor().show();
for(int j = 0; j < students.size(); j++) {
((Course) course).getStudents().get(j);
}
}
}