Question: You are being given four files for this problem: People.java, UsePeople.java, Students.java and UseStudents.java. People.java defines a class containing a generic (templated) ArrayList that can

You are being given four files for this problem: People.java, UsePeople.java, Students.java and UseStudents.java. People.java defines a class containing a generic (templated) ArrayList that can hold objects of different types (defined by the in the class definition). The class provided methods for adding and retrieving object references to/from the ArrayList, and for returning a reference to the inner ArrayList as a whole. In UsePeople.java a People object is instantiated that can work with Person-type objects. Two Person objects are added to the People object and then their data is retrieved and displayed on-screen. The Students class defined in Students.java inherits from the People class. It adds two methods used simply to clean up the API (they mention Student rather than Person in their names), and a method for returning the number of Student objects currently stored in the Students object (getNumStudents()). Otherwise it works the same as People.

In the UseStudents.java file there is a main() method. In there the program instantiates a Students object that can store and manage Student-type objects. Two Student objects are added to the Students object. The program then retrieves a reference to the inner ArrayList from Students and calls the clear() method to delete everything from the Students object. This is not supposed to be allowed for Students! Why was this possible to do, and how would you change the classes to stop this from being possible in future?

People.java:

import java.util.ArrayList;

public class People { ArrayList people = new ArrayList();

public People() {}

public People(T p) { people.add(p); }

public void addPerson(T p) { people.add(p); }

public T getPerson(int index) { return people.get(index); }

public ArrayList getPeople() { return people; }

//getNumPeople method is commented out, not available. /*public int getNumPeople() { return people.size(); }*/

}

UsePeople.java :

public class UsePeople { public static void main(String args[]) { People p = new People();

p.addPerson(new Person("adam","1234")); p.addPerson(new Person("betty","2345"));

//System.out.println("Number of people: " + p.getNumPeople()); System.out.println("1) " + p.getPerson(0).getName() + ", " + p.getPerson(0).getPhone()); System.out.println("2) " + p.getPerson(1).getName() + ", " + p.getPerson(1).getPhone()); } }

Students.java :

import java.util.ArrayList;

public class Students extends People {

public Students() { }

public Students(T p) { people.add(p); }

//API changes from People superclass... public void addStudent(T p) { people.add(p); }

public T getStudent(int index) { return people.get(index); }

//Additional method for subclass public int getNumStudents() { return people.size(); }

}

UseStudents.java:

import java.util.ArrayList;

public class UseStudents { public static void main(String args[]) { Students students = new Students();

students.addStudent(new Student("adam","1234",3.5)); students.addStudent(new Student("betty","2345",4.0));

//How many students in the list before? System.out.println("Number of students before: " + students.getNumStudents());

//Get reference to internal ArrayList of all Student objects (inherited method from People) Object allStudents = (Object)students.getPeople(); //Clear all students from list (should not be able to do this, according to API of Students)! ((ArrayList)allStudents).clear();

System.out.println("Number of students after: " + students.getNumStudents()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!