CS 5010: Problem Set 09

Out: Monday, 6 November 2017
Due: Monday, 13 November 2017 at 6pm local time

This is an individual assignment. You are not allowed to discuss this problem set with any other person. You are also not allowed to search for or to view any solutions to similar problems that may be available on the World-Wide Web or in other resources that might otherwise have been available to you.

The main purpose of this problem set is to give you practice using interfaces and defining classes in Java.

We will test your program using Java 8, which is the version of Java installed on CCIS Linux machines. You will therefore want to use Java 8 when developing and testing your program.

For these problems, we are giving you the following files:

Three of those files are marked as incomplete. Your job is to complete those three files.

As you complete those three files, you should leave them in the default package. In other words, your Java files should not contain any package declarations.

You must not change any of the other files in any way.

You should start by downloading the files we are giving you, unpacking them into a set09 directory, and pushing that directory to your GitHub repository.

We are giving you a choice of two formats in which to download the files we are giving you. Both of these links will unpack to the same set09 directory containing exactly the same Java files:

Remember that you must follow the design recipe, which is a process, not a list of deliverables.

Be sure to fill out a work session report at the end of each work session. Be sure to report only the hours spent in that work session; do not report the cumulative time. Tell git to add your work session report to the files you will commit, and then commit and push (sync) that report in addition to committing and pushing your entire set09 directory. Do this at the end of every work session.


This problem set asks you to define three Java classes that implement the Java interfaces we are giving you. The three classes you define will perform essentially the same computations as the program you wrote for Problem Set 08.

There are some differences, however.

In Problem Set 08, you defined functions named tie and defeated whose results represent an Outcome. For Problem Set 09, you will define classes named Tie1 and Defeat1 that implement the interface given in Outcome.java:

      // An Outcome is an object of any class that implements Outcome
      //
      // Interpretation: the two competitors have engaged in a contest
      //   If this.isTie() is true, the contest ended in a tie.
      //   Otherwise the contest did not end in a tie, and
      //       this.winner() defeated this.loser()
      
      interface Outcome {
      
          // RETURNS: true iff this outcome represents a tie
      
          boolean isTie ();
      
          // RETURNS: one of the competitors
      
          Competitor first ();
      
          // RETURNS: the other competitor
      
          Competitor second ();
      
          // GIVEN: no arguments
          // WHERE: this.isTie() is false
          // RETURNS: the winner of this outcome
      
          Competitor winner ();
      
          // GIVEN: no arguments
          // WHERE: this.isTie() is false
          // RETURNS: the loser of this outcome
      
          Competitor loser ();
      }

In Problem Set 08, a Competitor was just a String. For Problem Set 09, you will define a class named Competitor1 that implements the interface given in Competitor.java:

      // A Competitor is an object of any class that implements Competitor
      //
      // Interpretation: the competitor represents an individual or team
      //     that may have engaged in one or more contests
      
      // Note:  In Java, you cannot assume a List is mutable, because all
      // of the List operations that change the state of a List are optional.
      // Mutation of a Java list is allowed only if a precondition or other
      // invariant says the list is mutable and you are allowed to change it.
      
      import java.util.List;
      
      interface Competitor {
      
          // returns the name of this competitor
      
          String name ();
      
          // GIVEN: another competitor and a list of outcomes
          // RETURNS: true iff one or more of the outcomes indicates this
          //     competitor has defeated or tied the given competitor
      
          boolean hasDefeated (Competitor c2, List<Outcome> outcomes);
      
          // GIVEN: a list of outcomes
          // RETURNS: a list of the names of all competitors mentioned by
          //     the outcomes that are outranked by this competitor,
          //     without duplicates, in alphabetical order
      
          List<String> outranks (List<Outcome> outcomes);
      
          // GIVEN: a list of outcomes
          // RETURNS: a list of the names of all competitors mentioned by
          //     the outcomes that outrank this competitor,
          //     without duplicates, in alphabetical order
      
          List<String> outrankedBy (List<Outcome> outcomes);
      
          // GIVEN: a list of outcomes
          // RETURNS: a list of the names of all competitors mentioned by
          //     one or more of the outcomes, without repetitions, with
          //     the name of competitor A coming before the name of
          //     competitor B in the list if and only if the power ranking
          //     of A is higher than the power ranking of B.
      
          List<String> powerRanking (List<Outcome> outcomes);
      }

To help you plan your work, we have divided this problem into two questions. However, you should submit a single set09 directory containing your class definitions and other deliverables for both questions 1 and 2.

  1. (Outcome)

    For this first part of Problem Set 09, you will complete the Tie1 and Defeat1 classes we started to define for you in the Tie1.java and Defeat1.java files.

    You will not be able to test your Tie1 and Defeat1 classes until you define a class that implements the Competitor interface. For testing, however, you can define a Competitor1 class that supports the name method but throws an UnsupportedOperationException whenever the other methods of the Competitor interface are called. The Competitor1.java file we are giving you already throws that exception whenever the hasDefeated, outranks, outrankedBy, or powerRanking methods are called.

  2. (Competitor)

    For this second part of Problem Set 09, you are to replace the stubs we have given you for the hasDefeated, outranks, outrankedBy, and powerRanking methods by definitions that satisfy the contracts and purpose statements given in the Competitor interface.

    The concepts used in the descriptions of those methods are the same as in Problem Set 08. If your solution for Problem Set 08 was correct, you can just translate your solution into Java. If your solution for Problem Set 08 was incorrect, you will probably want to debug it in Racket's ISL+Lambda language before you try to translate it into Java.