Question: ***** BagDriver.java ***** public class BagDriver { public static void main(String[] args){ Bag myBag = new Bag(10); FamousPerson p1, p2, p3; p1 = new FamousPerson(John,

 ***** BagDriver.java ***** public class BagDriver { public static void main(String[]

***** BagDriver.java *****

public class BagDriver {

public static void main(String[] args){

Bag myBag = new Bag(10);

FamousPerson p1, p2, p3;

p1 = new FamousPerson("John", "McEnroe", 1980, "Tennis star.");

p2 = new FamousPerson("Herman", "Hollerith", 1860, "American scientist.");

p3 = new FamousPerson("John", "McEnroe", 2017, "Tennis announcer.");

System.out.println(myBag);

myBag.add(p1);

myBag.add(p2);

myBag.add(p3);

System.out.println(myBag);

System.out.println(myBag.grab());

System.out.println(myBag.count(p1));

System.out.println(myBag.removeAll(p1));

System.out.println(myBag);

myBag.clear();

System.out.println(myBag);

}

}

***** ArrayCollection.java *****

//--------------------------------------------------------------------------- // ArrayCollection.java by Dale/Joyce/Weems Chapter 5 // // Implements the CollectionInterface using an array. // // Null elements are not allowed. Duplicate elements are allowed. // // Two constructors are provided: one that creates a collection of a default // capacity, and one that allows the calling program to specify the capacity. //---------------------------------------------------------------------------

public class ArrayCollection implements CollectionInterface { protected final int DEFCAP = 100; // default capacity protected T[] elements; // array to hold collections elements protected int numElements = 0; // number of elements in this collection

// set by find method protected boolean found; // true if target found, otherwise false protected int location; // indicates location of target if found

public ArrayCollection() { elements = (T[]) new Object[DEFCAP]; }

public ArrayCollection(int capacity) { elements = (T[]) new Object[capacity]; }

protected void find(T target) // Searches elements for an occurrence of an element e such that // e.equals(target). If successful, sets instance variables // found to true and location to the array index of e. If // not successful, sets found to false. { location = 0; found = false;

while (location

public boolean add(T element) // Attempts to add element to this collection. // Returns true if successful, false otherwise. { if (isFull()) return false; else { elements[numElements] = element; numElements++; return true; } }

public boolean remove (T target) // Removes an element e from this collection such that e.equals(target) // and returns true; if no such element exists, returns false. { find(target); if (found) { elements[location] = elements[numElements - 1]; elements[numElements - 1] = null; numElements--; } return found; } public boolean contains (T target) // Returns true if this collection contains an element e such that // e.equals(target); otherwise, returns false. { find(target); return found; }

public T get(T target) // Returns an element e from this collection such that e.equals(target); // if no such element exists, returns null. { find(target); if (found) return elements[location]; else return null; } public boolean isFull() // Returns true if this collection is full; otherwise, returns false. { return (numElements == elements.length); }

public boolean isEmpty() // Returns true if this collection is empty; otherwise, returns false. { return (numElements == 0); }

public int size() // Returns the number of elements in this collection. { return numElements; } }

***** BagInterface.java *****

//----------------------------------------------------------------------------

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

//----------------------------------------------------------------------------

public interface BagInterface extends CollectionInterface

{

Object 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.

}

***** CollectionInterface.java *****

//---------------------------------------------------------------------------- // CollectionInterface.java by Dale/Joyce/Weems Chapter 5 // // Interface for a class that implements a collection of T. // A collection allows addition, removal, and access of elements. // // Nullelements are not allowed. Duplicate elements are allowed. //----------------------------------------------------------------------------

package Lab5_Kuhns;

public interface CollectionInterface { boolean add(T element); // Attempts to add element to this collection. // Returns true if successful, false otherwise.

T get(T target); // Returns an element e from this collection such that e.equals(target). // If no such e exists, returns null.

boolean contains(T target); // Returns true if this collection contains an element e such that // e.equals(target); otherwise returns false.

boolean remove (T target); // Removes an element e from this collection such that e.equals(target) // and returns true. If no such e exists, returns false.

boolean isFull(); // Returns true if this collection is full; otherwise, returns false.

boolean isEmpty(); // Returns true if this collection is empty; otherwise, returns false. int size(); // Returns the number of elements in this collection. }

***** FamousPerson.java *****

package Lab5_Kuhns; //---------------------------------------------------------------------- // FamousPerson.java by Dale/Joyce/Weems Chapter 5,6 // // Supports famous people objects having a first name, last name, a year // of birth and a short tidbit of factual information. //----------------------------------------------------------------------

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!