Question

In: Computer Science

Locate your Student project from the previous in-class assignment. You will add a new class to...

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;
}
  
  
  
}

Solutions

Expert Solution

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;
    }


}

==================================================================


Related Solutions

For this programming assignment, you will use your previous code that implemented a video game class...
For this programming assignment, you will use your previous code that implemented a video game class and objects with constructors. Add error checking to all your constructors, except the default constructor which does not require it. Make sure that the high score and number of times played is zero or greater (no negative values permitted). Also modify your set methods to do the same error checking. Finally add error checking to any input requested from the user. #include <iostream> #include...
You are to use your code from part A of this assignment and add to it....
You are to use your code from part A of this assignment and add to it. In this part of the project, you are to do the following: 1. In the count_voters function you are to add code to: • Count the number of votes for Trump • Count the number of votes for Biden • Count the number of females • Count the number of males • Count the number of Democrats • Count the number of Republicans •...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
JAVA The previous assignment for the semester project required that you submit a document that included...
JAVA The previous assignment for the semester project required that you submit a document that included class diagram document that used UML notation. By now (if not sooner), you should have a good idea of what classes you will need to include in your Java Application.You may, in fact, had identified additional classes you need to implement. For this assignment, you are to write your classes that are required for your Java application (minimum of three). Additionally, you will need...
C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new...
C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new behavior as well: add(Section s) this behavior will add a section to the Person’s schedule. The Person’s display() function will also need to be modified to display() the Person’s Schedule. Schedule class has Properties: An array of Sections and a Count. Constructors: only a default constructor is needed. Behaviors: add() and display(). This is my schedule class class Schedule     {         private int...
Intercultural Communication. A college professor has a new student from China in the class. The student...
Intercultural Communication. A college professor has a new student from China in the class. The student does not speak English fluently. Name and discuss three cultural factors that might influence classroom interaction
Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
For this program you will add and test 2 new member functions to the class in...
For this program you will add and test 2 new member functions to the class in //************************ intSLList.h ************************** // singly-linked list class to store integers #ifndef INT_LINKED_LIST #define INT_LINKED_LIST class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(int el, IntSLLNode *ptr = 0) { info = el; next = ptr; } int info; IntSLLNode *next; }; class IntSLList { public: IntSLList() { head = tail = 0; } ~IntSLList(); int isEmpty() { return head == 0; }...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include the following three static methods described below. However, don't implement them right away. Instead, start by returning the default value followed by a // TODO comment. public static int sumOfDigits(int n) This method returns the sum of all the digits. sumOfDigits(-34) -> 7 sumOfDigits(1038) -> 12 public static int countSmiles(char[] letters, int index) This method counts the number of colons followed by a closing...
You are completing a software development project. This project is expected to add SIX new features...
You are completing a software development project. This project is expected to add SIX new features to a popular software program you sell to customers. The projected cost for the project is $100,000 and the development is expected to take 5 weeks. At the end of the second week, you spent $17,500 per new software feature and you completed the development of two new features. You are about to start the development of the third new software feature. Based on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT