In: Computer Science
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.
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