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 fora class that implements a bag of T. // A bag is

//---------------------------------------------------------------------------- // 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 extends CollectionInterface { T grab(); // If this bag is not empty, removes and returns a random element of the bag; // otherwise returns null. int count(T target); // Returns a count of all elements e in this collection such that e.equals(target). int removeAll(T target); // Removes all elements e from this collection such that e.equals(target) // and returns the number of elements removed.

void clear(); // Empties this bag so that it contains zero elements. }

FamousPerson.java

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

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!