In: Computer Science
***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);
}
}
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"); } }