Question

Saw this code online an im trying to understand each step. Can someone add comments to...

Saw this code online an im trying to understand each step. Can someone add comments to this ?

package javaapplication5;


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());

}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package javaapplication5;


interface PairInterface<String>{//basically this is an interface and it will not have any concrete methods as the name suggests as this is a pair here we have two getter methods and two setter methods
//setters used to set a value
void setFirst(String first);
void setSecond(String second);
//getters use to get the value
String getFirst();
String getSecond();
  
}
class BasicPair<String> implements PairInterface{//here with clas BasicPair we are implementing the abive interface, as we are implementing the interface we need to implement the methods in that interface
//defined two variables
String first;
String second;
//coonstructor to initialize thw variables
BasicPair(String first,String second){
this.first=first;
this.second=second;
}
//as we said we need to implement the methods in above interface we are overriding the methods @override annotation indicating the same
@Override
public String getFirst() {
return first;
}
//as we said we need to implement the methods in above interface we are overriding the methods @override annotation indicating the same
@Override
public String getSecond() {
return second;
}
//as we said we need to implement the methods in above interface we are overriding the methods @override annotation indicating the same
@Override
public void setFirst(Object first ) {
this.first=(String)first;
}
//as we said we need to implement the methods in above interface we are overriding the methods @override annotation indicating the same
@Override
public void setSecond(Object second) {
this.second=(String)second;
}

  

  
}
class ArrayPair<String> implements PairInterface{//here with clas ArrayPair we are implementing the abive interface, as we are implementing the interface we need to implement the methods in that interface

//to variables
String first;
String second;
//constructor to initialize thw variables
ArrayPair(String first,String second){
this.first=first;
this.second=second;
}
//as we said we need to implement the methods in above interface we are overriding the methods @override annotation indicating the same
@Override
public String getFirst() {
return first;
}
//as we said we need to implement the methods in above interface we are overriding the methods @override annotation indicating the same
@Override
public String getSecond() {
return second;
}
//as we said we need to implement the methods in above interface we are overriding the methods @override annotation indicating the same
@Override
public void setFirst(Object first ) {
this.first=(String)first;
}
//as we said we need to implement the methods in above interface we are overriding the methods @override annotation indicating the same
@Override
public void setSecond(Object second) {
this.second=(String)second;
}
  
}
//this a class with main method used to test the above classes
class DriverApplication {
  
public static void main(String args[]){
//here cretaed the objects for the above two class and printe dthose object svalues
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());

}
}

Add a comment
Know the answer?
Add Answer to:
Saw this code online an im trying to understand each step. Can someone add comments to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • Add appropriate descriptive comments to each line of the code, explaining why the code is in...

    Add appropriate descriptive comments to each line of the code, explaining why the code is in the application. import java.text.NumberFormat; public class LineItem implements Cloneable { private Product product; private int quantity; private double total; public LineItem() { this.product = new Product(); this.quantity = 0; this.total = 0; } public LineItem(Product product, int quantity) { this.product = product; this.quantity = quantity; } public void setProduct(Product product) { this.product = product; } public Product getProduct() { return product; } public void...

  • Why doesn't this code let me run it. I just finished it really... public class ComboLock...

    Why doesn't this code let me run it. I just finished it really... public class ComboLock { int secret1, secret2, secret3; // secret keys int dial1, dial2, dial3; // variables used to store dialed ticks boolean first, second, third; // variables used to store current turn number(how many numbers entered already) public ComboLock(int secret1, int secret2, int secret3) { super(); this.secret1 = secret1; this.secret2 = secret2; this.secret3 = secret3; this.dial1 = 0; this.dial2 = 0; this.dial3 = 0; this.first =...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • //MultiValuedTreeMap.java import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; //import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>>...

    //MultiValuedTreeMap.java import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; //import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> {    private static final long serialVersionUID = -6229569372944782075L;       public void add(K k, V v) { // Problem 1 method        // empty linked list, with key=k         if (!containsKey(k)) {               put(k, new LinkedList<V>());         }         // adding v to the linked list associated with key k         get(k).add(v);    }    public V removeFirst(K k)...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • In each of the following questions, first use NetBeans IDE to run the code (if there...

    In each of the following questions, first use NetBeans IDE to run the code (if there is an error fix it) then take a screenshot of the output and paste it under (A). In (B), justify the output you obtained in (A). - 9. Given the following: public class WorkingSheet {     public static void main(String[] args) {        B myB = new B();        A myA = new B();        System.out.print(myB instanceof A);        System.out.print(myB instanceof C);        System.out.print(myA...

  • java problem here is the combination class class Combination {    int first,second,third, fourth;    public...

    java problem here is the combination class class Combination {    int first,second,third, fourth;    public Combination(int first, int second, int third,int fourth)    {        this.first=first;        this.second=second;        this.third=third;        this.fourth=fourth;    }    public boolean equals(Combination other)    {               if ((this.first==other.first) && (this.second==other.second) && (this.third==other.third) && (this.fourth==other.fourth))            return true;        else            return false;    }    public String toString()    {   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT