Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions

Question

4-2. What are four requirements of an effective interview?

Answered: 1 week ago

Question

8. Set goals that relate to practice as well as competition.

Answered: 1 week ago

Question

5. If yes, then why?

Answered: 1 week ago

Question

6. How would you design your ideal position?

Answered: 1 week ago