Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions

Question

2. What is an EHR?

Answered: 1 week ago