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

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

ID: 3733700 • Letter: F

Question

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 birth—decreasing

c. Order by length of “fact”—increasing

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

Explanation / Answer

Please find the program below which has agumented methods having comparators.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package support;

import java.util.Comparator;
import java.util.ArrayList;
import java.util.Collections;

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

//a. Order alphabetically by name (first name, then last name)
public static void OrderBynameComparator(ArrayList al){
Collections.sort(al,new FullNameComparator());
}

//b. Order by year of birth—decreasing
public static void OrderByYearOfBirthComparator(ArrayList al){
Collections.sort(al,new AgeComparator());  
}

//c. Order by length of “fact”—increasing
public static void OrderByLengthOfFactComparator(ArrayList al){
Collections.sort(al,new AgeComparator());  
}
}

class FullNameComparator implements Comparator<FamousPerson> {
@Override
public int compare(FamousPerson a, FamousPerson b) {
return a.firstName.compareToIgnoreCase(b.firstName);
}
}

class AgeComparator implements Comparator<FamousPerson> {
@Override
public int compare(FamousPerson a, FamousPerson b) {
return a.yearOfBirth > b.yearOfBirth ? 1 : 0;
}
}

class FactComparator implements Comparator<FamousPerson> {
@Override
public int compare(FamousPerson a, FamousPerson b) {
return a.fact.length() > b.fact.length() ? 1 : 0;
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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? 3: Order alphabetically by name (first name, then last name) 4: Order by year of birth—decreasing 5: Order by length of “fact”—increasing");
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 if (choice == 2)
people = new SortedABList(FamousPerson.yearOfBirthComparator());
else if (choice == 3)
FamousPerson.OrderBynameComparator(people);
else if (choice == 4)
FamousPerson.OrderByYearOfBirthComparator(people);
else if (choice == 5)
FamousPerson.OrderByLengthOfFactComparator(people);
  
// 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);
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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