In JAVA I need help on what is asked in the questions that a
In JAVA... I need help on what is asked in the questions that are bolded
For this problem you must define a simple generic interface PairInterface, and two implementations of the interface, BasicPair and ArrayPair.
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 PairInterface interface and the classes that implement it should be generic. Suppose a class named BasicPair implements the PairInterfaceinterface. A simple sample application that uses BasicPair is shown here. Its output would be \"apple orange.\"
Create a class called BasicPair that implements the PairInterface interface. This class should use two instance variables, first and second, to represent the two objects of the pair. Create a test driver application that demonstrates that the BasicPair class works correctly.
Create a class called ArrayPair that implements the PairInterface interface. This class should use an array10 of size 2 to represent the two objects of the pair. Create a test driver application that demonstrates that the ArrayPair class works correctly.
Solution
package javaapplication5;
/**
*
* @author Rashmi Tiwari
*/
interface PairInterface<String>{
void setFirst(String first);
void setSecond(String second);
String getFirst();
String getSecond();
}
class BasicPair<String> implements PairInterface{
String first;
String second;
BasicPair(String first,String second){
this.first=first;
this.second=second;
}
@Override
public String getFirst() {
return first;
}
@Override
public String getSecond() {
return second;
}
@Override
public void setFirst(Object first ) {
this.first=(String)first;
}
@Override
public void setSecond(Object second) {
this.second=(String)second;
}
}
class ArrayPair<String> implements PairInterface{
String first;
String second;
ArrayPair(String first,String second){
this.first=first;
this.second=second;
}
@Override
public String getFirst() {
return first;
}
@Override
public String getSecond() {
return second;
}
@Override
public void setFirst(Object first ) {
this.first=(String)first;
}
@Override
public void setSecond(Object second) {
this.second=(String)second;
}
}
class DriverApplication {
public static void main(String args[]){
PairInterface<String> myPair =
new BasicPair<>(\"apple\", \"peach\");
System.out.print(myPair.getFirst() + \" \");
myPair.setSecond(\"orange\");
System.out.println(myPair.getSecond());
ArrayPair[] array2=new ArrayPair[2];
array2[0]=new ArrayPair<String>(\"apple\",\"peach\");
System.out.print(array2[0].getFirst()+\" \");
array2[0].setSecond(\"orange\");
System.out.println(array2[0].getSecond());
}
}

