Question

In: Computer Science

For this problem you must define a simple generic interface PairInterface, and two implementations of the...

  1. For this problem you must define a simple generic interface PairInterface, and two implementations of the interface, BasicPair and ArrayPair.

a. Define a Java interface named PairInterface. A class that implements this interface allows creation of an object that holds a “pair” of objects of a specified type—these are referred to as the “first” object and the “second” object
of the pair. We assume that classes implementing PairInterface provide constructors that accept as arguments the values of the pair of objects. The PairInterface interface should require both setters and getters for the first and second objects. The actual type of the objects in the pair is specified when the PairInterface object is instantiated. Therefore, both the PairInter- face interface and the classes that implement it should be generic. Suppose a class named BasicPair implements the PairInterface interface. A simple sample application that uses BasicPair is shown here. It's output would be "apple orange."

public class Sample {

public static void main (String[] args) {

PairInterface myPair<String> =
new BasicPair<String>("apple", "peach");

System.out.print(myPair.getFirst() + " ");

myPair.setSecond("orange");

System.out.println(myPair.getSecond()); }

}

b. Create a class called ArrayPair that implements the PairInterface inter- 10 face.

- This class should use an array of size 2 to represent the two objects of the pair.

- Create a test driver application that demonstrates that the ArrayPair class works correctly.

Solutions

Expert Solution

PairInterface.java


public interface PairInterface<T> {
   // declare abstract methods
   public void setFirst(T first);
   public void setSecond(T second);
   public T getFirst();
   public T getSecond();
}

BasicPair.java

// implement PairInterface interface
public class BasicPair<T> implements PairInterface<T> {
   // instance variables
   private T first;
   private T second;
   // constructor
   public BasicPair(T first, T second) {
       super();
       this.first = first;
       this.second = second;
   }
   // setters and getters
   @Override
   public T getFirst() {
       return first;
   }
   @Override
   public void setFirst(T first) {
       this.first = first;
   }
   @Override
   public T getSecond() {
       return second;
   }
   @Override
   public void setSecond(T second) {
       this.second = second;
   }
}

ArrayPair.java

// implement PairInterface interface
public class ArrayPair<T> implements PairInterface<T> {
   // instance variable
   private T array[];
   // constructor
   public ArrayPair(T[] array) {
       super();
       this.array = array;
   }
   // setters and getters
   @Override
   public void setFirst(T first) {
       array[0]=first;
   }
   @Override
   public void setSecond(T second) {
       array[1]=second;
   }
   @Override
   public T getFirst() {
       return array[0];
   }
   @Override
   public T getSecond() {
       return array[1];
   }
}

Sample.java


public class Sample {
   public static void main(String[] args) {
       System.out.println("BasicPair class: ");
       // create BasicPair class object with generic type String
       PairInterface<String> myPair =new BasicPair<>("apple", "peach");
       System.out.print(myPair.getFirst() + " "); // get first
       myPair.setSecond("orange"); // set second
       System.out.println(myPair.getSecond()); // get second
      
       System.out.println("\nArrayPair class: ");
       // create 2 size array
       String array[]= {"apple","peach"};
       // create ArrayPair class object with generic type String
       PairInterface<String> myPair2=new ArrayPair<>(array);
       System.out.print(myPair2.getFirst() + " ");
       myPair2.setSecond("orange");
       System.out.println(myPair2.getSecond());
   }
}

Output


Related Solutions

The implementations of the methods addAll, removeAll, retainAll are omitted in the MyList interface. Implement these...
The implementations of the methods addAll, removeAll, retainAll are omitted in the MyList interface. Implement these methods. /** Adds the elements in otherList to this list. * Returns true if this list changed as a result of the call */ public default boolean addAll(Collection<? extends E> c) /** Removes all the elements in otherList from this list * Returns true if this list changed as a result of the call */ public default boolean removeAll(Collection<?> c) /** Retains the elements...
Generic types A class or interface that declares one or more generic variables is called a...
Generic types A class or interface that declares one or more generic variables is called a generic type. In this portion of the activity, you will make a class generic. Open GenericsC.java in jGRASP then compile it. At this point you should be familiar with the two type-safety warnings given by the compiler. You should be able to understand the source of the error: the use of the raw types List and Collection. Since the List being declared (al) is...
INTERFACE (BinaryTreeInterface): BinaryTreeInterface.java This interface represents all the functionalities a generic binary tree. Operations + getRootData():...
INTERFACE (BinaryTreeInterface): BinaryTreeInterface.java This interface represents all the functionalities a generic binary tree. Operations + getRootData(): T - This method returns the data stored in the root node of a tree. Note that you cannot return the data of an empty tree. + getRootNode(): BinaryNode<T> - This method returns the reference to the root node of a tree. + setRootNode(BinaryNode<T>):void - This method sets the root node of a tree. + getHeight():int - This method returns the height of a...
Java Programming Problem: Define a generic method called checkOrder() that checks if four items are in...
Java Programming Problem: Define a generic method called checkOrder() that checks if four items are in ascending, neither, or descending order. The method should return -1 if the items are in ascending order, 0 if the items are unordered, and 1 if the items are in descending order. The program reads four items from input and outputs if the items are ordered. The items can be different types, including integers, Strings, characters, or doubles. Ex. If the input is: bat...
Define and discuss Porters’ Generic Strategies
Define and discuss Porters’ Generic Strategies
Forward prices of a generic asset The purpose of these problem is to guide you and...
Forward prices of a generic asset The purpose of these problem is to guide you and introduce you the “no-arbitrage” condition required to compute forward prices. For the following problems, assume the following information: There is an asset A. The price of the asset today, denoted by ?0, is ?0 = $100. The CCIR (yearly)(continuously compounded interest rate) is 6%. Problem 3: No storage cost, and a convenience yield. Assume that asset A has no storage cost and there is...
Forward prices of a generic asset The purpose of these problem is to guide you and...
Forward prices of a generic asset The purpose of these problem is to guide you and introduce you the “no-arbitrage” condition required to compute forward prices. For the following problems, assume the following information: There is an asset A. The price of the asset today, denoted by ?0, is ?0 = $100. The CCIR (yearly) is 6%. Problem 1: No storage cost, and no convenience yield. Assume that asset A has no storage cost and there is no convenience yield....
Forward prices of a generic asset The purpose of these problem is to guide you and...
Forward prices of a generic asset The purpose of these problem is to guide you and introduce you the “no-arbitrage” condition required to compute forward prices. For the following problems, assume the following information: There is an asset A. The price of the asset today, denoted by ?0, is ?0 = $100. The CCIR (yearly) is 6%. Problem 4: No storage cost, and a convenience yield. Assume that asset A has no storage cost and there is a convenience yield....
Forward prices of a generic asset The purpose of these problem is to guide you and...
Forward prices of a generic asset The purpose of these problem is to guide you and introduce you the “no-arbitrage” condition required to compute forward prices. For the following problems, assume the following information: There is an asset A. The price of the asset today, denoted by ?0, is ?0 = $100. The CCIR (yearly) is 6%. Problem 3: No storage cost, and a convenience yield. Assume that asset A has no storage cost and there is a convenience yield....
Forward prices of a generic asset The purpose of these problem is to guide you and...
Forward prices of a generic asset The purpose of these problem is to guide you and introduce you the “no-arbitrage” condition required to compute forward prices. For the following problems, assume the following information: There is an asset A. The price of the asset today, denoted by ?0, is ?0 = $100. The CCIR (yearly) is 6%. Problem 2: Storage cost, and no convenience yield. Assume that asset A has a storage cost and there is no convenience yield. Every...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT