In: Computer Science
Required Tasks:
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]
Implementation of above program in JAVA:
import java.util.ArrayList;
import java.util.Scanner;
//class Assign5Test.java
public class Assign5Test {
// Scanner class declared as Static
static Scanner s= new Scanner(System.in);
// driver method
public static void main(String[] args) {
StudentList sl = new
StudentList();
System.out.println("Add 3 Students
:");
System.out.println();
add(sl);
add(sl);
add(sl);
System.out.println();
print(sl.list);
System.out.println();
System.out.println("Remove 1
Student : ");
remove(sl);
System.out.println();
System.out.println("Add 2 Students
:");
System.out.println();
add(sl);
add(sl);
System.out.println();
print(sl.list);
}
// print the list by using the object of
StudentList
// and getters of Student class
public static void print(ArrayList<Student1>
list) {
System.out.println("-------------------List of
Students-----------------");
System.out.println();
for(int i=0;i<list.size();i++)
{
Student1 st=
list.get(i);
System.out.println("Name: ["+st.getname()+"], Email:
["+st.getemail()+"], Major: ["+st.getmajor()+"], Graduation Year:
["+st.getyear()+"]");
}
}
// user enter input and call add method in StudentList
class
public static void add(StudentList sl) {
System.out.print("Enter the name of
Student : ");
String name=s.next();
s.nextLine();
System.out.print("Enter the Email
of Student : ");
String email=s.next();
s.nextLine();
System.out.print("Enter the major
of Student : ");
String major=s.next();
s.nextLine();
System.out.print("Enter the
Graduation-Year of Student : ");
String year=s.next();
s.nextLine();
Student1 st= new
Student1(name,email,major,year);
sl.addstudent(st);
}
// user enter input and call remove method in
StudentList class
public static void remove(StudentList sl) {
System.out.print("Enter the Email
of Student you want to remove : ");
String email=s.next();
sl.remove(email);
}
}
//----------------------------------------------------
// class StudentList.java
class StudentList{
// Arraylist
ArrayList<Student1> list= new
ArrayList<Student1>();
// add Student in arrayList
public void addstudent(Student1 st) {
list.add(st);
System.out.println("Student
Added!!!");
System.out.println();
}
// remove Student in arrayList
public void remove(String email) {
// traverse whole list and try to
found mail if dound then remove
// else print "Email not
found..."
for(int i=0;i<list.size();i++)
{
Student1
st=list.get(i);
if(st.getemail().equals(email)) {
list.remove(i);
System.out.println("Student removed!!!");
return;
}
}
System.out.println("Email not
found...");
}
}
//--------------------------------------------------------
//class Student1.java
class Student1{
String name;
String email;
String major;
String year;
// Constructor
public Student1(String name, String email ,String major, String
year) {
this.name=name;
this.email=email;
this.major=major;
this.year=year;
}
// getters Methods
public String getname() {
return name;
}
public String getemail() {
return email;
}
public String getmajor() {
return major;
}
public String getyear() {
return year;
}
}
SAMPLE OUTPUTS;
If you have any doubt regarding this question ask me in comments
//THANK YOU:-)