Review 3

Write one or more drivers that test the implementations of the classes and interfaces described below.

Create an interface named Tuple that requires the following methods:

  1. a method named getFirst that has no parameters and returns an Object
  2. a method named getLast that has no parameters and returns an Object
  3. a method named getElements that returns an array of Objects

Create an class named Pair that implements the Tuple interface and meets the following requirements:

  • The class has two fields, named first and second, that hold Objects.
  • The class should have a constructor that has 2 Object parameters whose values are stored in the fields.
  • The getFirst method returns the value stored in the field named first.
  • The getLast method returns the value stored in the field named second.
  • The getElements method returns an array of length 2 that holds the values stored in the two fields.
  • The class overrides toString and returns a string of the form “(x,y)” where x and y are the string representations of the values stored in first and second respectively.
  • The class overrides equals assuming both fields are significant.
  • The class overrides hashCode.

Create a class named Triple that implements the Tuple interface and meets the following requirements:

  • The class has one field named elements that holds an array of 3 Objects.
  • The class should have a constructor that has 3 Object parameters whose values are stored in the fields.
  • The getFirst method returns the first element in the array.
  • The getLast method returns the last element in the array.
  • The getElements method returns the array field.
  • The class overrides toString and returns a string of the form “(x,y,z)” where x,y, and z are the string representations of the values of the first, second, and third elements in the elements array.
  • The class overrides equals assuming all elements in the array are significant and order matters.
  • The class overrides hashCode.

Write a class named PositivePair that extends Pair and meets the following requirements:

  • The class has one constructor that takes two Objects as parameters.  If either object is not an Integer or is negative, the constructor throws an IllegalArgumentException exception.  Otherwise, the constructor sets the fields with the values passed in.

Write a class named GreekGod that meets the following requirements:

  • The class has 3 named instances: APOLLO, ARES, and DIONYSUS.
  • Each instance has two String fields named mother and father which are set via a constructor.  Use Wikipedia to find the parents of these gods.
  • The class has getters for the mother and father fields.

Write a class named JigglyPuff that meets the following requirements:

  • The class allows only one instance to be created but is not an enumerations.
  • The one instance is accessible only via a method named getInstance.
  • The instance has an readable field named HP that is initialized to 5.

Recite and use Bloch’s advice for designing methods.

Write a function named foo that takes 0 or more integers as arguments and returns the average of the integers that are passed into the function.

Write a function named bar that takes an instance of Tuple as an argument and returns a string representation of the first and last element of the object passed into the function by calling the object’s getFirst and getLast methods.  Demonstrate that you can pass an instance of Pair and an instance of Triple to bar.

© 2018 – 2019, Eric. All rights reserved.