Question

Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent...

Java:

  1. ModifyStudentList.javato use anArrayListinstead of an array
  2. In StudentList.java, create two new public methods:
    1. The addStudent method should have one parameter of type Student and should add the given Student to the StudentList.
    2. The removeStudent method should have one parameter of type String. The String is the email of the student to be removed from the StudentList.
  3. In Assign5Test.java do the following:
    1. Create a new StudentList containing 3 students.
    2. Print the info of all the Students in the StudentList using the getAllStudentInfo method.
    3. Remove one Student from the StudentList using the removeStudent method.
    4. Add two new Students to the StudentList using the addStudent method.
    5. Print the info of all the Students in the StudentList using the getAllStudentInfo method. Notice that one Student was removed and two were added.

Assign5Test:

public class Assign5Test {

public static void main(String[] args) {

Student aj = new Student("Abdulmuhsin J. Al-Kandari", "[email protected]", "SE", 0);

Student justin = new Student("Justin R. Schlemm", "[email protected]", "SE", 0);

Student mary = new Student("Mary F. Menges", "[email protected]", "SE", 0);

Student nick = new Student("Nicholas-Jason R. Roache", "[email protected]", "CS", 0);

Student taylor = new Student("Taylor J. Klodowski", "[email protected]", "SE", 0);

Student veronica = new Student("Veronica M. Granite", "[email protected]", "CS", 0);

// new students

Student isaiah = new Student("Isaiah K. Bishop" , "[email protected]", "CS", 0);

Student spike = new Student("Spike N. Mike" , "[email protected]", "CS", 0);

Student bishop = new Student("Bishop J. Pastor" , "[email protected]", "SE", 0);

Student kayvonn = new Student("Kayvonn L. Lastor" , "[email protected]", "SE", 0);

Student zay = new Student("Zay A. Hey" , "[email protected]", "CS", 0);

// Graduation year students

Student shawn = new Student("Shawn K. Bill" , "[email protected]", "SE", 2022);

Student sean = new Student("Sean S. Sanders" , "[email protected]", "CS", 2022);

Student Dale = new Student("Dale W. Mail" , "[email protected]", "SE", 2022);

Student[] studentArray = {aj, justin, mary, nick, taylor, veronica, isaiah, spike, bishop, kayvonn, zay };

StudentList myStudentList = new StudentList(studentArray);

System.out.println("Total Studnets in CS: " + myStudentList.studentcount("CS"));

System.out.println("Total Studnets in SE: " + myStudentList.studentcount("SE"));

System.out.println("Total Studnets in HIS: " + myStudentList.studentcount("HIS"));

System.out.println(myStudentList.getAllStudentInfo());

}//end of main

}//end of class

Student
public class Student{

private String name;

private String email;

private String major;

private int graduationYear;

public Student(String name, String email, String majaor, int graduationYear) {

setName(name);

setEmail(email);

setMajor(major);

setGraduationYear(graduationYear);

}

public int getGraduationYear() { // getgraduation method

return graduationYear;

}

public void setGraduationYear(int graduationYear) { // set graduation method

this.graduationYear = graduationYear;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public String getMajor() {

return major;

}

public void setMajor(String major) {

this.major = major;

}

@Override

public String toString() {

return "Name: [" + this.name + "], Email: [" + this.email + "], Major: [" + this.major + "], GraduationYear: [" + this.graduationYear + "]"; // added graduation year to the toString method

}

}

StudentList
public class StudentList {

private static final boolean StudentInfo = false;

private Student[] studentArray;

public String email;

public StudentList(Student[] studentArray) {

setStudentArray(studentArray);

}

public void setStudentArray(Student[] studentArray) {

this.studentArray = studentArray;

}

public Student[] getStudentArray() {

return studentArray;

}

public int studentcount(String major) {

int count = 0;

for(int i = 0; i < studentArray.length; i++) {

if(major.equals(studentArray[i].getMajor())){

count++;

}// end of if

}

return count;

}

public Student getStudentInfo(String email) {

for(int i = 0; i < studentArray.length; i++) {

if(studentArray[i].getEmail() == email) {

return studentArray[i];

}// end of for

}

return null;

}

public String getAllStudentInfo() {

String returnString = "";

for(int i = 0; i < studentArray.length; i++) {

returnString += studentArray[i].toString() + System.lineSeparator();

}//end of for

return returnString;

}

public String studentInfo(String string) {

return null;

}

public boolean updateStudentGraduationYear(String email, int year) {

for(int i = 0; i < studentArray.length; i++) {

if(studentArray[i].getEmail() == email) {

studentArray[i].setGraduationYear(year);

return true; // if true

}// end of if

}

return false; // else false

}

public String getAllStudentInfo1() {

// TODO Auto-generated method stub

return null;

}

}

Homework Answers

Answer #1
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. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!

note: Student.java remains unchanged.
===========================================================================

import java.util.ArrayList;

public class StudentList {

    private static final boolean StudentInfo = false;

    private ArrayList<Student> studentArray;

    public String email;

    public StudentList(ArrayList<Student> studentArray) {

        setStudentArray(studentArray);

    }

    public void addStudent(Student aStudent) {
        studentArray.add(aStudent);
    }

    public void removeStudent(String email) {
        int match = -1;
        for (int i = 0; i < studentArray.size(); i++) {
            if (studentArray.get(i).getEmail().equals(email)) {
                match = i;
                break;
            }
        }
        if (match != -1) studentArray.remove(match);
    }

    public void setStudentArray(ArrayList<Student> studentArray) {

        this.studentArray = studentArray;

    }

    public ArrayList<Student> getStudentArray() {

        return studentArray;

    }

    public int studentcount(String major) {

        int count = 0;

        for (int i = 0; i < studentArray.size(); i++) {

            if (major.equals(studentArray.get(i).getMajor())) {

                count++;

            }// end of if

        }

        return count;

    }

    public Student getStudentInfo(String email) {

        for (int i = 0; i < studentArray.size(); i++) {

            if (studentArray.get(i).getEmail() == email) {

                return studentArray.get(i);

            }// end of for

        }

        return null;

    }

    public String getAllStudentInfo() {

        String returnString = "";

        for (int i = 0; i < studentArray.size(); i++) {

            returnString += studentArray.get(i).toString() + System.lineSeparator();

        }//end of for

        return returnString;

    }

    public String studentInfo(String string) {

        return null;

    }

    public boolean updateStudentGraduationYear(String email, int year) {

        for (int i = 0; i < studentArray.size(); i++) {

            if (studentArray.get(i).getEmail() == email) {

                studentArray.get(i).setGraduationYear(year);

                return true; // if true

            }// end of if

        }

        return false; // else false

    }



}

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

import java.util.ArrayList;

public class Assign5 {

    public static void main(String[] args) {

        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("Abdulmuhsin J. Al-Kandari", "[email protected]", "SE", 0));
        students.add(new Student("Justin R. Schlemm", "[email protected]", "SE", 0));
        students.add(new Student("Mary F. Menges", "[email protected]", "SE", 0));

        StudentList studentList = new StudentList(students);

        //Print the info of all the Students in the StudentList using the getAllStudentInfo method.
        String details = studentList.getAllStudentInfo();
        System.out.println(details);

        //Remove one Student from the StudentList using the removeStudent method.
        studentList.removeStudent("[email protected]");

        //Add two new Students to the StudentList using the addStudent method.
        studentList.addStudent(new Student("Veronica M. Granite", "[email protected]", "CS", 0));
        studentList.addStudent(new Student("Spike N. Mike" , "[email protected]", "CS", 0));

        //Print the info of all the Students in the StudentList using the getAllStudentInfo method.
        System.out.println("\nAfter deleting 1 student and adding 2 students: Updated List:\n");
        details = studentList.getAllStudentInfo();
        System.out.println(details);


    }
}

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
How do I make this: public class Country {     private String name;     private double area;     private...
How do I make this: public class Country {     private String name;     private double area;     private int population;     public Country(String name, double area, int population) {         this.name = name;         this.area = area;         this.population = population;     }     public double getPopulationDensity() {         return population / area;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public double getArea() {         return area;     }     public void setArea(double area) {         this.area = area;     }     public int getPopulation()...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private...
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private int houseNumber;    private int bedrooms;    private int sqFeet;    private int year;    private int cost;    public House(int houseNumber,int bedrooms,int sqFeet, int year, int cost)    {        this.houseNumber = houseNumber;        this.bedrooms = bedrooms;        this.sqFeet = sqFeet;        this.year = year;        this.cost = cost;    }    public int getHouseNumber()    {        return houseNumber;    }   ...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT