Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

From Object-Oriented Data Structures with Java 4th Edition. Chapter 6 Excercise 30. Currently the FamousPerson class defines the natural order of its elements to be

From Object-Oriented Data Structures with Java 4th Edition. Chapter 6 Excercise 30.

Currently the FamousPerson class defines the natural order of its elements to be based on the alphabetical order of people names (last name, then first name). It also provides a Comparator that defines order based on increasing year of birth. Augment the class to include more public static methods that return Comparators as described below. Augment the CSPeople application to demonstrate that the new Comparators work properly. These must be added to the program, not just changed.

a. Order alphabetically by name (first name, then last name)

b. Order by year of birthdecreasing

c. Order by length of factincreasing This is the Famous Person class file :

package support;

import java.util.Comparator;

public class FamousPerson implements Comparable { protected String firstName, lastName, fact; protected int yearOfBirth;

public FamousPerson(String first, String last, int yob, String f) { firstName = first; lastName = last; fact = f; yearOfBirth = yob; }

public String getFirstName() {return firstName ;} public String getLastName() {return lastName;} public String getFact() {return fact;} public int getYearOfBirth() {return yearOfBirth;}

@Override public boolean equals(Object obj) // Returns true if 'obj' is a FamousPerson with same first and last // names as this FamousPerson, otherwise returns false. { if (obj == this) return true; else if (obj == null || obj.getClass() != this.getClass()) return false; else { FamousPerson fp = (FamousPerson) obj; return (this.firstName.equals(fp.firstName) && this.lastName.equals(fp.lastName)); } } public int compareTo(FamousPerson other) // Precondition: 'other' is not null // // Compares this FamousPerson with 'other' for order. Returns a // negative integer, zero, or a positive integer as this object // is less than, equal to, or greater than 'other'. { if (!this.lastName.equals(other.lastName)) return this.lastName.compareTo(other.lastName); else return this.firstName.compareTo(other.firstName); }

@Override public String toString() { return (firstName + " " + lastName + "(Born " + yearOfBirth + "): " + fact); } public static Comparator yearOfBirthComparator() { return new Comparator() { public int compare(FamousPerson element1, FamousPerson element2) { return (element1.yearOfBirth - element2.yearOfBirth); } }; }

}

And this is the CSPeople application :

package ch06.apps;

import java.io.*; import java.util.*; import ch06.lists.*; import support.*;

public class CSPeople { public static void main(String[] args) throws IOException { // Get user's display preference Scanner scan = new Scanner(System.in); int choice; System.out.println(" 1: Sorted by name? 2: Sorted by year of birth?"); System.out.print(" How would you like to see the information > "); choice = scan.nextInt(); // Instantiate sorted list SortedABList people; if (choice == 1) people = new SortedABList(); // defaults to natural order else people = new SortedABList(FamousPerson.yearOfBirthComparator()); // Set up file reading FileReader fin = new FileReader("input/FamousCS.txt"); Scanner info = new Scanner(fin); info.useDelimiter("[,\ ]"); // delimiters are commas, line feeds FamousPerson person; String fname, lname, fact; int year;

// Read the info from the file and add it to the list while (info.hasNext()) { fname = info.next(); lname = info.next(); year = info.nextInt(); fact = info.next(); person = new FamousPerson(fname, lname, year, fact); people.add(person); } // Display the list, using the advanced for loop System.out.println(); for (FamousPerson fp: people) System.out.println(fp); } }

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

Beginning C# 2005 Databases

Authors: Karli Watson

1st Edition

0470044063, 978-0470044063

More Books

Students also viewed these Databases questions