Question

In: Computer Science

JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...

JAVA LANGUAGE

Required Tasks:
In Eclipse, create a Java Project as CS-176L-Assign5.
Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java.
Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method.
Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp
In StudentList.java, create two new public methods:
The addStudent method should have one parameter of type Student and should add the given Student to the StudentList.
The removeStudent method should have one parameter of type String. The String is the email of the student to be removed from the StudentList.
In Assign5Test.java do the following:
Create a new StudentList containing 3 students.
Print the info of all the Students in the StudentList using the getAllStudentInfo method.
Remove one Student from the StudentList using the removeStudent method.
Add two new Students to the StudentList using the addStudent method.
Print the info of all the Students in the StudentList using the getAllStudentInfo method. Notice that one Student was removed and two were added.
Code must compile and run without warnings or errors.
Final output should look something like the following:
Name: [Abdulmuhsin J.Al-Kandari], Email: [[email protected]], Major: [SE], GraduationYear: [2017]
Name: [Justin R. Schlemm], Email: [[email protected]], Major: [SE], GraduationYear: [2016]
Name: [Mary F. Menges], Email: [[email protected]], Major: [SE], GraduationYear: [2017]

Name: [Abdulmuhsin J.Al-Kandari], Email: [[email protected]], Major: [SE], GraduationYear: [2017]
Name: [Justin R. Schlemm], Email: [[email protected]], Major: [SE], GraduationYear: [2016]
Name: [Nicholas-Jason R. Roache], Email: [[email protected]], Major: [CS], GraduationYear: [2017]
Name: [Taylor J. Klodowski], Email: [[email protected]], Major: [SE], GraduationYear: [2016]

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) { // added graduation year in Constructer
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
}
}

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

return count;
}

public Student getStudentInfo(String email) {
for(int i = 0; i < studentArray.length; i++) {
if(studentArray[i].getEmail() == email) {
return studentArray[i];
}
}
return null;
}

public String getAllStudentInfo() {
String returnString = "";

for(int i = 0; i < studentArray.length; i++) {
returnString += studentArray[i].toString() + System.lineSeparator();
}
return returnString;
}

public String studentInfo(String string) {
return null;
}

public boolean updateStudentGraduationYear(String email, int year) { // added method to update the graduation year of a student using the email.

for(int i = 0; i < studentArray.length; i++) {
if(studentArray[i].getEmail() == email) {
studentArray[i].setGraduationYear(year);
return true; // if found updated and returned true
}
}
return false; // else false
}
}

Solutions

Expert Solution

Here is the Java code to the given question.

Three files are provided i.e., Student.java ,StudentList.java and Assign5Test.java.

Output screenshot is added at the end.

Code:

Student.java:

public class Student{

    private String name;
    private String email;
    private String major;
    private int graduationYear;

    public Student(String name, String email, String major, int graduationYear) { // added graduation year in Constructor
        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.java:

import java.util.ArrayList;

public class StudentList {

    private static final boolean StudentInfo = false;
    private ArrayList<Student> studentList;    /* ArrayList to store Student objects*/
    public String email;

    public StudentList(){

        studentList=new ArrayList<Student>();
    }

    public StudentList(ArrayList<Student> studentList) {
        setStudentList(studentList);
    }

    public void setStudentList(ArrayList<Student> studentList) {
        this.studentList = studentList;
    }

    public ArrayList<Student> getStudentList() {
        return studentList;
    }

    public int studentcount(String major) {
        int count = 0;

        for(Student student : studentList) {      /*for each student in studentList*/
            if(major.equals(student.getMajor())){    /*if the major of student equals major passed as parameter*/
                count++;    /*increment count by 1*/
            }
        }

        return count;    /*returns count*/
    }

    public Student getStudentInfo(String email) {
        for(Student student : studentList) {      /*for each student in studentList*/
            if(student.getEmail().equals(email)) {     /*if emain of the student equals email passed as parameter*/
                return student;     /*returns student object*/
            }
        }
        return null;   /*returns null if no student is found*/
    }

    public String getAllStudentInfo() {
        String returnString = "";

        for(Student student : studentList) {      /*for each student in studentList*/
            returnString += student.toString() + System.lineSeparator();     /*append student string to returnString*/
        }
        return returnString;
    }


    public boolean updateStudentGraduationYear(String email, int year) { // added method to update the graduation year of a student using the email.

        for(Student student: studentList){      /*for each student in student list*/

            if(student.getEmail().equals(email)){     /*if email of the student equals email passed to the method*/

                student.setGraduationYear(year);    /*sets student graduation year to the year passed as parameter*/

                return true;    /*if found then return true*/
            }
        }
        return false; // else false
    }


    /*method that adds student to studentList*/
    public void addStudent(Student student){

        studentList.add(student);    /*adds student to studentList*/

    }

    /*method that removes student from studentList*/
    public boolean removeStudent(String email){

        for(Student student : studentList){    /*for each student in studentList*/

            if(student.getEmail().equals(email)){     /*if email of the student equals email passed to the method*/

                studentList.remove(student);    /*removes student from studentList*/

                return true;    /*returns true if student is removed*/
            }
        }
        return false;   /*returns false if the student is not found*/
    }
}

Assign5Test.java:

import java.util.ArrayList;

public class Assign5Test {

    public static void main(String[] args){

        Student student1=new Student("Watson","[email protected]","SE",2020);   /*creates new Student object*/

        Student student2=new Student("Curran","[email protected]","ME",2019);    /*creates new Student object*/

        Student student3=new Student("James","[email protected]","SE",2019);     /*creates new Student object*/

        Student student4=new Student("Robert","[email protected]","ME",2020);   /*creates new Student object*/

        Student student5=new Student("Chris","[email protected]","SE",2019);    /*creates new Student object*/

        ArrayList<Student> studentArrayList=new ArrayList<Student>();    /*creates a new ArrayList that stores Student objects*/

        studentArrayList.add(student1);    /*adds student1 to studentArrayList*/

        studentArrayList.add(student2);    /*adds student2 to studentArrayList*/

        studentArrayList.add(student3);     /*adds student3 to studentArrayList*/

        StudentList studentList=new StudentList(studentArrayList);   /*creates a new StudentList object with studentArrayList passed to the constructor*/

        System.out.println(studentList.getAllStudentInfo());    /*prints all students info*/

        studentList.removeStudent("[email protected]");    /*removes student with email "[email protected]" from studentList*/

        studentList.addStudent(student4);  /*adds student4 to studentList*/

        studentList.addStudent(student5);   /*adds student5 to studentList*/

        System.out.println(studentList.getAllStudentInfo());  /*prints all students info*/


    }
}

Output:


Related Solutions

Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add...
For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java,...
GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java, BubbleSortTestCaseMaker.java, and Statistician.java. PART 1: Implementing BubbleSorter Implement a very simple BubbleSorter class that records how many array visits and how many swaps are performed. Look at the starter file before reading on. This class has an instance variable called “a”. Its type is int[]. This is the array that will be bubble-sorted in place. Usually a single letter is a bad variable name,...
Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week...
Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week 6 lecture, create six packages: Main, add, subtract, multiply, divide, and modulo. ALL of these packages should be within the “src” folder in the Eclipse package Explorer. ALL of the packages should be on the same “level” in the file hierarchy. In the “Main” package, create the “Lab04.java” file and add the needed boilerplate (“Hello, World!” style) code to create the main method for...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
PLZ USE JAVA ECLIPSE AND EXPLAIN Create a GUI which works as an accumulator:  There...
PLZ USE JAVA ECLIPSE AND EXPLAIN Create a GUI which works as an accumulator:  There is a textfield A which allows user to enter a number  There is also another textfield B with value start with 0.  When a user is done with entering the number in textfield A and press enter, calculate the sum of the number in textfield B and the number user just entered in textfield A, and display the updated number in textfield...
JAVA Practice Exam 2 The project practice_exam_2 contains three (3) classes: Testing – This is the...
JAVA Practice Exam 2 The project practice_exam_2 contains three (3) classes: Testing – This is the file you will use to execute your program and test your cases. Each section refers to one or more specific array(s). From here, you can run the whole program or one section at a time. TestCases – This file contains all test cases. NO NEED FOR YOU TO PAY ATTENTION TO IT. YourCode – This is where you will be writing your code. Implement...
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A....
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A. Two bags per person are free. B. The Excess Bag Charge is $75 per bag. The program needs to do the following: 1. In your main method(),    Create integers to store the number of Passengers and also the total number of bags    Prompt for the number of passengers on a ticket.    Prompt for the total number of bags for all passengers...
Create a new project in BlueJ. Create two new classes in the project, with the following...
Create a new project in BlueJ. Create two new classes in the project, with the following specifications: Class name: Part Fields: id (int) description (String) price (double) onSale (boolean) 1 Constructor: takes parameters partId, partDescrip, and partPrice to initialize fields id, description, and price; sets onSale field to false. Methods: Write get-methods (getters) for all four fields. The getters should be named getId, getDescription, getPrice, and isOnSale.
Create a moderately complex java program that utilises 2 or more classes. Within these classes: -...
Create a moderately complex java program that utilises 2 or more classes. Within these classes: - have one that defines an exception - have that exception throw(n) in one method and handled in another -has the program continue even if the user inputs incorrect data -be creative/unique in some way
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT