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

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...
3. Which statement about methods in an interface is true? A.)All methods in an interface must...
3. Which statement about methods in an interface is true? A.)All methods in an interface must be explicitly declared as private or public. B.)All methods in an interface are automatically public. C.)All methods in an interface are automatically private. D.)All methods in an interface are automatically static.
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...
Make the Measurable interface from Chapter 10 into a generic class. Provide a static method that...
Make the Measurable interface from Chapter 10 into a generic class. Provide a static method that returns the largest element of an ArrayList, provided that the elements are instances of Measurable. Be sure to return a value of type T. Measurable interface: public interface Measurable { double getMeasure(); } Using JAVA please
The following is a Java programming question: Define a class StatePair with two generic types (Type1...
The following is a Java programming question: Define a class StatePair with two generic types (Type1 and Type2), a constructor, mutators, accessors, and a printInfo() method. Three ArrayLists have been pre-filled with StatePair data in main(): ArrayList> zipCodeState: Contains ZIP code/state abbreviation pairs ArrayList> abbrevState: Contains state abbreviation/state name pairs ArrayList> statePopulation: Contains state name/population pairs Complete main() to use an input ZIP code to retrieve the correct state abbreviation from the ArrayList zipCodeState. Then use the state abbreviation to...
A standard and generic simple question in economics are: Would you rather receive $100 today or...
A standard and generic simple question in economics are: Would you rather receive $100 today or $100 a year from now? What about $105 a year from now? Go beyond the simple answer to explain TVOM using an example from your experience or a scenario based on material. PLEASE TYPE OUT THE RESPONSE
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT