logo Practice-It logo

Playa

Related Links:
Author: Marty Stepp

You have been asked to extend a pre-existing class Person that represents a person used as part of a Gale-Shapley stable marriage program (as seen in your HW3). The Person class includes the following constructors and methods:

Method/Constructor Description
public Person(String name) constructs a person with the given name
public String getName() returns the person's name
public void engageTo(Person partner) sets the person to be engaged to the given partner
public Person getFiancee() returns the person's fiancee, or null if none
public boolean isSingle() returns true if the person has no fiancee
public Queue<String> getPreferences() returns a queue of preferred partners
public Map<String, Integer> getRankings()returns a map of rankings
public String toString() returns a string representation of the person

You are to define a new class called Playa that extends this class through inheritance. A Playa should behave like a Person except that it makes mischief by allowing itself to be engaged to multiple persons at the same time, keeping track of a collection of all such fiancees. You should provide the same methods as the superclass, as well as the following new behavior.

Method/Constructor Description
public Playa(String name) constructs a playa with the given name
public int countFiancees() returns the number of fiancees to which this playa is currently engaged

The behaviors related to preferences and rankings of potential partners are unaffected by this subclass.

Some of the existing behaviors from Person should behave differently on Playa objects:

  • When the engageTo method is called, the Playa should still retain the existing engageTo behavior (because it maintains important internal state), but it should also keep track of a collection of all engagement partners seen so far. Each partner passed to engageTo should become part of this collection. It should not be possible for the same person to appear twice in this collection. If null is passed to engageTo, your Playa should instead clear its collection of partners to become single again. (A Playa can become engaged to any person(s), not just other Playas.)
  • The getFiancee method should return the partner to which the Playa most recently became engaged. This will occur automatically if the original engageTo behavior from Person is retained.
  • The isSingle method should return true only if the Playa has no partners in its engagement collection.

You must also make Playa objects comparable to each other using the Comparable interface. Playas are compared by their number of fiancees, breaking ties by name. In other words, a Playa object with fewer fiancees in its partner collection is considered to be "less than" one with more fiancees in its collection. If two Playa objects have the same number of fiancees, the one whose name comes first in alphabetical order is considered "less." If the two objects have the same number of fiancees and the same name, they are considered to be "equal."

Type your solution here:


This is an inheritance problem. Write a Java class using inheritance. (You do not need to write any import statements.)

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.