Answer to Question #282526 in Java | JSP | JSF for طالب

Question #282526

1/Write java application with the following information:


- Class Name ; Student


- Method1: AddStudent


- Method2: Print Students


- Method 3: totalStudent


- Method 4: Delete Student

1
Expert's answer
2021-12-25T01:48:16-0500
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        StudentsCollection studentsCollection = new StudentsCollection();

        Student student1 = new Student("John", "Smith");
        Student student2 = new Student("Thomas", "Anderson");
        Student student3 = new Student("Angelina", "Jolie");

        System.out.println("Add student: " + student1);
        studentsCollection.addStudent(student1);
        System.out.println("Add student: " + student2);
        studentsCollection.addStudent(student2);
        System.out.println("Add student: " + student3);
        studentsCollection.addStudent(student3);
        System.out.println("");

        System.out.println("Total of students: " + studentsCollection.totalStudents());
        System.out.println("\tStudents:");
        System.out.println("\t" + studentsCollection.printStudents());
        System.out.println("");

        System.out.println("Student John Smith is removed.");
        studentsCollection.deleteStudents(student1);

        System.out.println("");
        System.out.println("Total of students: " + studentsCollection.totalStudents());
        System.out.println("\tStudents:");
        System.out.println("\t" + studentsCollection.printStudents());
    }

    public static class Student {

        private final String firstName;
        private final String lastName;

        public Student(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "firstName='" + firstName + '\'' +
                    ", lastName='" + lastName + '\'' +
                    '}';
        }
    }

    public static class StudentsCollection {

        private final ArrayList<Student> studentArrayList = new ArrayList<>();

        public ArrayList<Student> addStudent(Student student) {
            studentArrayList.add(student);
            return studentArrayList;
        }

        public String printStudents () {
            return studentArrayList.toString();
        }

        public int totalStudents () {
            return studentArrayList.size();
        }

        public void deleteStudents(Student student) {
            studentArrayList.remove(student);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog