Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Create Person class with the following members: Private instance variables:

ID: 3794073 • Letter: J

Question

Java
Create Person class with the following members:

Private instance variables: name and age.
A constructor that sets the name and age.
Get and set methods for the name and age;
A toString method that displays the data within a Person object.
**********
Then create a ManyPersons class with the following members:

An encapsulated array that can hold three Person objects.
Get and Set methods that allow you to return or set the Person objects from individual elements of the array.
A getQueue method that returns a Queue object based on the contents of the encapsulated array. The queue should have the functionality of the basic queue from your textbook.
A getArray method the returns a copy of the encapsulated array.
IMPORTANT (and this the point of this exercise!): the array returned by the getArray method and the Queue returned by the getQueue method should contains elements that are COPIES of the Person objects in the encapsulated array of teh ManyPersons class.

**********

Also code a ManyPersonsDriver class that creates a ManyPersons object, extracts an array and a queue from the object, and lists the data in the the extracted array and the queue.

Then change the data in the ManyPersons encapsulated array (you only need to change one element). After that, display the data in the already extracted arrays and queues. If the data displayed from modified Person object's array and the queue, then you haven't followed the exercise instructions properly.

**********

Example (a possible main method in the ManyPersonsDriver class):

Person[] parr = {new Person("John", 25), new Person("Mike", 32), new Person("Sally", 20)};ManyPersons map = new ManyPersons(parr);

Person[] copyarr = map.getArray();
Queue copyq = map.getQueue();

for(int x = 0; x < copyarr.length; x++){
System.out.println(copyarr[x]);
}

/*
displays the following:
John, 25
Mike, 32
Sally, 20
*/

System.out.println(copyq.remove());
System.out.println(copyq.remove());
System.out.println(copyq.remove());

/*
displays the following:
John, 25
Mike, 32
Sally, 20
*/

map.setPerson(0, new Person("Dave", 22)); //change the data in the encapsulated array!

for(int x = 0; x < copyarr.length; x++){
System.out.println(copyarr[x]);
}

/*
displays the following:
John, 25
Mike, 32
Sally, 20
*/

System.out.println(copyq.remove());
System.out.println(copyq.remove());
System.out.println(copyq.remove());

/*
displays the following:
John, 25
Mike, 32
Sally, 20
*/

/* if either of the above display operations show the changed data instead of the old data, then you have a problem. Note that you have not created new array and queue objects before doing the second set of displays. So the data in the existing array and queue objects should not have changed. If the data did change, then your array and queue objects are connected to the encapsulated array by reference -- you didn't correctly copy the data.*/

/*The below demonstrates that the changes made to the encapsulated array will be in new versions of the array and the queue*/

Person[] copyarr2 = map.getArray();

for(int x = 0; x < copyarr2.length; x++)
{
System.out.println(copyarr2[x]);
}

/*
displays the following:
Dave, 22
Mike, 32
Sally, 20
*/

Queue copyq2 = map.getQueue();

System.out.println(copyq2.remove());
System.out.println(copyq2.remove());
System.out.println(copyq2.remove());

/*
displays the following:
Dave, 22
Mike, 32
Sally, 20
*/

Explanation / Answer

You can save this program in pgm.java file . Also you can run several test cases inside the main() and verify.

import java.util.*;

class Person
{
private String name;
private int age;


public void setName(String input){
name = input;
}

public void setAge(int input){
age = input;
}

Person( String inputName, int inputAge){
age = inputAge;
name = inputName;
}

public String getName(){
return name;
}

public int getAge(){
return age;
}

@Override
public String toString() {
return String.format(name + ", " + age);
}
}

class ManyPersons
{
private Person []personList = new Person[3];

public void setPerson(int position, Person obj){
personList[position] = obj;
}

public Person getPerson(int position){
return personList[position];
}

ManyPersons(Person[] list){
personList = list;
}

public Queue getQueue(){

Queue<Person> q = new LinkedList<>();

Person[] temp = new Person[3];

temp[0] = new Person(personList[0].getName(), personList[0].getAge());
temp[1] = new Person(personList[1].getName(), personList[1].getAge());
temp[2] = new Person(personList[2].getName(), personList[2].getAge());

q.add(temp[0]);
q.add(temp[1]);
q.add(temp[2]);

return q;
}

public Person[] getArray(){

Person[] temp = new Person[3];

temp[0] = new Person(personList[0].getName(), personList[0].getAge());
temp[1] = new Person(personList[1].getName(), personList[1].getAge());
temp[2] = new Person(personList[2].getName(), personList[2].getAge());
  
return temp;
}

@Override
public String toString() {
return String.format(personList[0].toString() + " " + personList[1].toString() + " " + personList.toString() +" ");
}
}

class ManyPersonsDriver
{
private ManyPersons obj;

public void setObj(ManyPersons person){
obj = person;
}
  
public void printList(){
System.out.println(obj.getArray());
}
public void printqueue(){
System.out.println(obj.getQueue());
}
}

public class pgm{

public static void main(String [] args){
Person[] parr = {new Person("John", 25), new Person("Mike", 32), new Person("Sally", 20)};
ManyPersons map = new ManyPersons(parr);
Person[] copyarr = map.getArray();
Queue copyq = map.getQueue();

for(int x = 0; x < copyarr.length; x++){
System.out.println(copyarr[x]);
}
System.out.println(copyq.remove());
System.out.println(copyq.remove());
System.out.println(copyq.remove());

for(int x = 0; x < copyarr.length; x++){
System.out.println(copyarr[x]);
}

map.setPerson(0, new Person("Dave", 22)); //change the data in the encapsulated array!
for(int x = 0; x < copyarr.length; x++){
System.out.println(copyarr[x]);
}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote