Question
//---------------------------------------------------------------------------- // BagInterface.java by Dale/Joyce/Weems Chapter 5 // // Interface for a class that implements a bag of T. // A bag is a collection
//---------------------------------------------------------------------------- // BagInterface.java by Dale/Joyce/Weems Chapter 5 // // Interface for a class that implements a bag of T. // A bag is a collection that supports a few extra operations. //----------------------------------------------------------------------------
package ch05.collections;
public interface BagInterface
void clear(); // Empties this bag so that it contains zero elements. }
FamousPerson.java
package support;
import java.util.Comparator;
public class FamousPerson implements Comparable
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
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started