Question: Implement the Person object class, such that Person implements Comparable : Data Fields: String name int age Constructor: Person(String, int) Methods: getName() String getAge() int

Implement the Person object class, such that Person implements Comparable:

Data Fields:

String name

int age

Constructor:

Person(String, int)

Methods:

getName() String

getAge() int

toString() "name (age)"

compareTo(Person other)

< 0 if this.age < other.age

> 0 if this.age > other.age

== 0 if this.age == other.age

Implement the following client program:

public static void main(String[] args) { final String FILE_NAME = "names.txt"; PriorityQ waitLine = new PriorityQ(); Random randy = new Random(); // make sure to import java.util.*; try { ArrayList names = readNames(FILE_NAME); // have randy add 100 people, each aged between 1 and 100, to the list int numNames = names.size(); for (int count=0; count<100; count++) { int age = 1 + randy.nextInt(100); // pick random age int index = randy.nextInt( numNames ); // pick random name String name = names.get( index ); waitLine.enqueue( new Person(name, age) ); } // end creation loop System.out.print("Waiting Line: "); System.out.println(waitLine); System.out.println(); // empty the queue and output each person served while (!waitLine.isEmpty()) { Person nextPerson = waitLine.dequeue(); System.out.println( nextPerson.getName() + ", who is " + nextPerson.getAge() + " years old, was just served."); } // end removal loop } catch (IOException e) { e.printStackTrace(); } // end try/catch } // end main /** * method reads names from a text file * @param the text file name * @return list of names */ static ArrayList readNames(String fileName) { /*** ADD METHOD CODE HERE ***/ } // end readNames

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The provided question is incomplete It contains an opening instruction for implementing the client program but does not include the full details or lo... View full answer

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!