April 6 Lab

Instructions

Please log into cs.bridgewater.edu or your AWS VM and rename the repository directory csci102/labs/apr1 to csci102/labs/apr6.

Please place all of your code for the following assignment in csci102/labs/apr6.

Program Specifications

A tuple is a finite ordered list of elements and a k-tuple is an ordered list of k elements.  For example, 2-tuple is a pair of ordered elements and a 3-tuple is an ordered list of 3 elements.

We use tuples all the time without referring to them as tuples.  For example, a 2D point (x,y) on a plane is a 2-tuple and a 3D point (x,y,z) is a 3-tuple.  They each have a k elements and their order matters.

Create a generic abstract class named Tuple that satisfies the following.

    • The class has a private field named arr that holds a reference to an array of Objects.
    • The class contains a constructor that has an integer parameter.  When the constructor is called, the field arr is initialized to an array whose size is equal to the value of the integer passed to the constructor.
    • The class has a method named size() that returns the length of arr.
    • The class contains a method named get() that has an integer parameter and returns the element in arr at the index specified by the value passed to the method. If the value of the first parameter is not a valid index an IndexOutOfBoundsException is thrown.
    • The class contains a method named set() that has an integer parameter and a parameter of the generic type.  The method sets the generic element in arr at the position specified by the value of the first parameter. If the value of the first parameter is not a valid index an IndexOutOfBoundsException is thrown.
    • The class overrides toString() and returns a string displaying a comma separated list of elements within parenthesis.
    • The class overrides equals() and considers an Object passed into the method equal to this, if the Object passed into the method is a Tuple having the same length and all the elements in the Tuples are equal.
    • The class overrides hashCode().
    • The class implements the Iterable interface.

Create a generic class named Pair that satisfies the following.

    • The class extends Tuple.
    • The class contains a constructor that has two parameters of the generic type.  The constructor passes the value 2 to its superclass’ constructor, then sets the two elements passed into the constructor into the tuple by calling set().

Create a driver named Driver that satisfies the following.

    • In main, create numerous Pair objects holding various types and test the methods in the Tuple class.

© 2021, Eric. All rights reserved.