In: Computer Science
Locate your Student project from the previous in-class assignment. You will add a new class to that project.
1. Create a class called Course. It should have a field to hold an ArrayList of Student objects.
2. Create a constructor for Course that accepts an ArrayList of Student objects, and set field to the parameter.
3. In the Course class, create a method called addStudent(Student s). This should add the parameter "s" to the ArrayList of Student objects.
4. In the Course class, create a method called removeStudent(Student s). This method should remove the parameter"s" from the ArrayList of Student objects.
5. In the Course class, create a method called toString(). This method should return a String showing the information from all the Student objects in the ArrayList. It should be formatted in the following way:
"Student 0: *Student 0 toString()*
Student 1: *Student 1 toString()*
...
Student n: *Student n toString()*"
This method should call each Student object's toString() method, as shown in the above example.
MY CODE :
student.java
package student;
public class Student {
  
public static void main(String[] args) {
  
  
Course myNewCourse = new Course(2) ;
System.out.println(myNewCourse.toString());
}
  
private int idNum;
private String firstN;
private String lastN;
  
public Student(int studentId, String firstName, String lastName ){
  
idNum = studentId;
firstN = firstName;
lastN = lastName;
}
  
public String toString(){   
  
String allData = "Student"+"1"+":ID: "+getIdNum()+ ", First name:
"
+ getFirstName()+ ", Last name: " +getLastName()+ "." ;
  
return allData;
}
  
public void setIdNum(int studentId){
  
this.idNum = studentId;
  
  
  
}
  
public void setFirstName(String firstName){
  
this.firstN = firstName;
  
  
}
  
public void setLastName(String lastName){
  
this.lastN = lastName;
  
}
  
public int getIdNum(){
  
  
return idNum;
  
}
  
  
public String getFirstName(){
  
return firstN;
  
}
  
public String getLastName() {
  
return lastN;
}
}
course.java
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package student;
import java.util.ArrayList;
/**
*
* @author farha
*/
public class Course {
  
ArrayList<Student> students = new
ArrayList<Student>();
  
  
  
public Course(ArrayList<Student> students){
  
this.students = students;
  
  
}
  
public void addStudent(Student s){
  
students.add(0, s);
  
  
  
}
  
public void removeStudent(Student s){
  
students.remove(s);
  
}
  
public String toString(){
  
String information = ("");
  
return information;
}
  
  
  
}
Thanks for the question.
Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.
Thank You !!
===========================================================================
import java.util.ArrayList;
public class Student {
    private int idNum;
    private String firstN;
    private String lastN;
    public Student(int studentId, String firstName, String lastName) {
        idNum = studentId;
        firstN = firstName;
        lastN = lastName;
    }
    public String toString() {
        String allData = "Student" + "1" + ":ID: " + getIdNum() + ", First name: "
                + getFirstName() + ", Last name: " + getLastName() + ".";
        return allData;
    }
    public void setIdNum(int studentId) {
        this.idNum = studentId;
    }
    public void setFirstName(String firstName) {
        this.firstN = firstName;
    }
    public void setLastName(String lastName) {
        this.lastN = lastName;
    }
    public int getIdNum() {
        return idNum;
    }
    public String getFirstName() {
        return firstN;
    }
    public String getLastName() {
        return lastN;
    }
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<Student>();
        students.add(new Student(1, "Barack", "Obama"));
        students.add(new Student(2, "Marsha", "Walker"));
        students.add(new Student(3, "David", "Lisenko"));
        Course myNewCourse = new Course(students);
        System.out.println(myNewCourse.toString());
    }
}
==================================================================
import java.util.ArrayList;
/**
 * @author farha
 */
public class Course {
    //1. Create a class called Course. It should have a field to hold an ArrayList of Student objects.
    ArrayList<Student> students = new ArrayList<Student>();
    //2. Create a constructor for Course that accepts an ArrayList of Student objects, and set field to the parameter.
    public Course(ArrayList<Student> students) {
        this.students = students;
    }
    //3. In the Course class, create a method called addStudent(Student s).
    // This should add the parameter "s" to the ArrayList of Student objects.
    public void addStudent(Student s) {
        students.add(s);
    }
    //4. In the Course class, create a method called removeStudent(Student s).
    // This method should remove the parameter"s" from the ArrayList of Student objects.
    public void removeStudent(Student s) {
        students.remove(s);
    }
    //5. In the Course class, create a method called toString().
    // This method should return a String showing the information from all
    // the Student objects in the ArrayList. It should be formatted in
    // the following way:
    public String toString() {
        String information = ("");
        for (int i = 0; i < students.size(); i++) {
            information += "Student " + i + ": " + students.get(i) + "\n";
        }
        return information;
    }
}
==================================================================
