Question

In: Computer Science

***Given a class called Student and a class called Course that contains an ArrayList of Student....

***Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called dropStudent() as described below. Refer to Student.java below to learn what methods are available.***

Course.java

import java.util.*;
import java.io.*;
/******************************************************
* A list of students in a course
*****************************************************/
public class Course{

/** collection of Students */
private ArrayList<Student> roster;

/*****************************************************
Constructor for objects of class Course
*****************************************************/
public Course(){
roster = new ArrayList<Student>();
}

/*****************************************************
Remove student with the provided last name. Do
nothing if last name not found.
*****************************************************/
public void dropStudent(String last){
/** Your code goes here */
  
}
  
/*****************************************************
Add a student to the course
*****************************************************/
public void addStudent(Student s){
roster.add(s);
}
  
public int countStudents(){
return roster.size();
}
  
/*****************************************************
Main method for testing
*****************************************************/
public static void main(String args[]){
Course cis162 = new Course();
cis162.addStudent(new Student("Henry", "Cabot", 3.5));
cis162.addStudent(new Student("Brenda", "Stern", 2.0));
cis162.addStudent(new Student("Lynda", "Robison", 3.2));
cis162.addStudent(new Student("Jane", "Flynn", 3.9));
cis162.dropStudent("Stern");
}
}

Student.java

import java.text.*;
/*************************************************
* A simple student class including name and gpa.
*
* @author Scott Grissom
* @version March 21, 2016
*************************************************/
public class Student{
  
/** student name */
private String first, last;
  
/** student GPA */
private double gpa;

/************************************************
Constructor for Student
************************************************/
public Student(String f, String l, double d){
first = f;
last = l;
gpa = d;
}
  
/************************************************
@return GPA
************************************************/
public double getGPA(){
return gpa;
}

/************************************************
@return last name
************************************************/
public String getLast(){
return last;
}
  
/************************************************
to String
@return String representation of the object
************************************************/   
public String toString(){
DecimalFormat fmt = new DecimalFormat("#.0");
return first + " " + last + " " + fmt.format(gpa);
}
  
public static void main (String [] args){
Student s = new Student ("Henry", "Walker", 3.6);
System.out.println(s);
}

}

Solutions

Expert Solution

import java.util.*;
import java.io.*;

/******************************************************
 * A list of students in a course
 *****************************************************/
public class Course {

    /**
     * collection of Students
     */
    private ArrayList<Student> roster;

    /*****************************************************
     Constructor for objects of class Course
     *****************************************************/
    public Course() {
        roster = new ArrayList<Student>();
    }

    /*****************************************************
     Remove student with the provided last name. Do
     nothing if last name not found.
     *****************************************************/
    public void dropStudent(String last) {
        int index = -1;
        for (int i = 0; i < roster.size(); i++) {
            if (roster.get(i).getLast().equals(last)) {
                index = i;
            }
        }
        if (index != -1) {
            roster.remove(index);
        }
    }

    /*****************************************************
     Add a student to the course
     *****************************************************/
    public void addStudent(Student s) {
        roster.add(s);
    }

    public int countStudents() {
        return roster.size();
    }

    /*****************************************************
     Main method for testing
     *****************************************************/
    public static void main(String args[]) {
        Course cis162 = new Course();
        cis162.addStudent(new Student("Henry", "Cabot", 3.5));
        cis162.addStudent(new Student("Brenda", "Stern", 2.0));
        cis162.addStudent(new Student("Lynda", "Robison", 3.2));
        cis162.addStudent(new Student("Jane", "Flynn", 3.9));
        cis162.dropStudent("Stern");
    }
}

Related Solutions

Language C++ Student-Report Card Generator: create a class called student and class called course. student class...
Language C++ Student-Report Card Generator: create a class called student and class called course. student class this class should contain the following private data types: - name (string) - id number (string) - email (s) - phone number (string) - number of courses (int) - a dynamic array of course objects. the user will specify how many courses are there in the array. the following are public members of the student class: - default constructor (in this constructor, prompt the...
Write a for-each loop that prints all of the Student objects in an ArrayList<student> object called...
Write a for-each loop that prints all of the Student objects in an ArrayList<student> object called roster. Write this piece in Java. Write the correct code to execute.
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings representing the names of staff members. The constructor takes an ArrayList of String names as a parameter. In addition to the constructor, you need to implement the following methods The methods 1. public boolean equals(Staff other) - Determines if the other Staff contains all the same elements in the same order as this Staff 2. public boolean sameContents(Staff other) - Determines if the other...
In a BestFriendSimulation driver class, define and initialize a reference variable called myBestFriends referencing an ArrayList...
In a BestFriendSimulation driver class, define and initialize a reference variable called myBestFriends referencing an ArrayList ofBestFriend objects. Then, create a menu that will continue to display itself, and process the requests of the user, until the user requests to exit the menu (loop). The menu should have 5 menu options: 1.) Add a BestFriend to the arrayList called myBestFriends; 2.) Change a BestFriend in the arrayList; 3.) Remove a BestFriend from the arrayList; 4.) Display all the objects in...
Create a class called Student which stores • the name of the student • the grade...
Create a class called Student which stores • the name of the student • the grade of the student • Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into the grade field. • Calculate the...
In order to access this course, attend this class as an on-line student, and participate in...
In order to access this course, attend this class as an on-line student, and participate in class discussions, you need to use a specific course management system. Answer the following questions about this system. 1. What is the name of the course management system? 2. Who created this system? 3. Other than your instructor, name three ways to get technical help with this product. 4. Other than class discussions, talk about one function of this product. Define its purpose and...
We have created an ArrayList of Person class. write a method called push that pushes all...
We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList Content of the ArrayList before push [alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte] content of the ArrayList after the push method [alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa khan, Jose Martinex] import java.util.*; class Person { private String name;...
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the...
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the method main. The program should ask for information from the user and then print it. Look at the examples below and write your program so it produces the same input/output. Examples (the input from the user is in bold face) % java AskInfoPrintInfo enter your name: jon doe enter your address (first line): 23 infinite loop lane enter your address (second line): los angeles,...
Write a class called Animal that contains a static variable called count to keep track of...
Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT