Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//---------------------------------------------------------------------------- // BagInterface.java by Dale/Joyce/Weems Chapter 5 // // Interface for a class that implements a bag of T. // A bag is a collection

image text in transcribedimage text in transcribed

//---------------------------------------------------------------------------- // 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

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_2

Step: 3

blur-text-image_3

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

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions

Question

6. List outcomes of the adaptation process.

Answered: 1 week ago