Java:
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;
}
}
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); } }
==============================================================
Get Answers For Free
Most questions answered within 1 hours.