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); } }

---I should add that the list the Comparator is checking is a Notepad file with this information, otherwise I don't think the program would find the information. This is what the Comparators are working with in the program. It is named as FamousCS.---

Herman,Hollerith,1860,Developed mechanical counting machine based on punched cards. Edsger,Dijkstra,1930,1972 Turing Award winner. John,Atanasoff,1903,Invented digital computer in 1930. John,McCarthy,1907,Helped invent the Eniac in 1943. Blaise,Pascal,1623,One of inventors of mechanical calculator. Jeannette,Wing,1956,Promoter of computational thinking. Alan,Turing,1912,Defined a formalism of a computation machine. Steven,Jobs,1955,Cofounder of Apple. Charles,Babbage,1791,Originated concept of machine that could be programmed. Barbara,Liskov,1939,Distributed computing researcher and more. Tim,Berners-Lee,1955,"Inventor" of the World Wide Web. Alonzo,Church,1903,Many contributions to mathematical logic. Ada,Lovelace,1815,Considered by many to be first computer programmer. Joseph,Jacquard,1752,Developed programmable loom. Grace,Hopper,1906,Compiler and high-level language pioneer. Alan,Kay,1940,One of developers of idea of object-oriented programmming. Donald,Knuth,1938,"Father of analysis of algorithms". Niklaus,Wirth,1934,Designed several widely used programming languages. James,Gosling,1955,"Father of java". Linus,Torvalds,1969,Main developer of Linux kernal. Marissa,Mayer,1975,Executive with Google and Yahoo. Steve,Wozniak,1950,Cofounder of Apple. Anita,Borg,1949,Founding director of IWT

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

What is the cerebrum?

Answered: 1 week ago