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

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...
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...
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;...
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...
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
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
A Java question. You are given a Student class. A Student has a name and an...
A Java question. You are given a Student class. A Student has a name and an ArrayList of grades (Doubles) as instance variables. Write a class named Classroom which manages Student objects. You will provide the following: 1. public Classroom() a no-argument constructor. 2. public void add(Student s) adds the student to this Classroom (to an ArrayList 3. public String hasAverageGreaterThan(double target) gets the name of the first student in the Classroom who has an average greater than the target...
Make a class called CashRegister that has the method public static double getTotalCostOfOfferings(ArrayList<Offering> o) Make this...
Make a class called CashRegister that has the method public static double getTotalCostOfOfferings(ArrayList<Offering> o) Make this method calculate the total cost of all Offering objects in the ArrayList. Submit Product, Service, and CashRegister. Given Files: import java.util.ArrayList; public class Demo3 { public static void crunch(ArrayList<Offering> o) { System.out.println("Adding up the following offerings:"); for (Offering current : o) { System.out.println(current); } System.out.printf("Total for all: $%,.2f\n", CashRegister.getTotalCostOfOfferings(o)); System.out.println("---------------------------------\n"); } public static void main(String[] args) { ArrayList<Offering> offeringList = new ArrayList<>(); offeringList.add(new Product("iPhone",...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT