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 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
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
//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.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
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.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
System.out.println("Number of students after: " + students.getNumStudents()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started