Java please PART C: Now we will add functionality that allows students to regist
ID: 3734694 • Letter: J
Question
Java please
PART C: Now we will add functionality that allows students to register for courses . Add an instance variable to the Student class called courses which should maintain an ArrayList of courses that the student has registered in. Create a correspondng get method and initialze it to an empty list withn your constructors Create methods in the Student class called addCourse(Course c) and removeCourse(Course c) that add and remove courses from the Student's list of registered courses . Similarly, add an instance variable to the Course class called students which should maintain an ArrayList of students that are registered in the course. Create a correspondng get method and initialze it to an empty list withn your constructors Create methods in the Course class called addStudent(Student s) and removeStudent(Student s) that add and remove courses from the Student's list of registered courses Test vour code before continuing using the program below: public class studentCourseTestProgram2 I public static void main(String args[]) Student s1 - new Student ("Jackie Chen", 100123456, 2015) Student s2 new student ("Bruce Willis", 100234567, 2017); Student s3 = new Student ("Keanu Reeves", 100345678, 2017); Course c1 = new Course ("BIOL", 10100, "Introductory Biology I");Explanation / Answer
import java.util.ArrayList;
public class Student {
private ArrayList<Course> courses;
private String name;
private int id;
private int year;
public Student(String name, int id, int year) {
this.courses = new ArrayList<Course>();
this.name = name;
this.id = id;
this.year = year;
}
public void addCourse(Course c){
courses.add(c);
}
public void removeCourse(Course c){
int i=0;
for(Course cc : courses){
if(cc==c){
break;
}
i++;
}
courses.remove(i);
}
public ArrayList<Course> getCourses(){
return this.courses;
}
public static void main(String[] args){
Student s1 = new Student("randi", 1, 5);
Student s2 = new Student("randi1", 2, 5);
Course c1 = new Course("BIO", 12, "random c1");
Course c2 = new Course("MATH", 13, "random c2");
s1.addCourse(c1);
s1.addCourse(c2);
c1.addStudent(s1);
c1.addStudent(s2);
System.out.println(s1.getCourses());
s1.removeCourse(c1);System.out.println(s1.getCourses());
}
}
import java.util.ArrayList;
public class Course {
private ArrayList<Student> students;
private String courseCode;
private int courseId;
private String courseName;
public Course(String code, int id, String name) {
this.students = new ArrayList<Student>();
this.courseCode = code;
this.courseId = id;
this.courseName = name;
}
public void addStudent(Student c){
students.add(c);
}
public void removeStudent(Student c){
int i=0;
for(Student cc : students){
if(cc==c){
break;
}
i++;
}
students.remove(i);
}
public ArrayList<Student> getStudents(){
return this.students;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.