Write a code for a constructor PairOfInts that receives a single int value (valu
ID: 3773376 • Letter: W
Question
Write a code for a constructor PairOfInts that receives a single int value (value) as its argument. It will construct a PairOfInts object such that its two instance variable are both assigned the value received into the input parameter value.
Write another constructor of PairOfInts that has a single input parameter ( other) thats a reference to an object of type PairOfInts. this constructor assigns the corresponding values of the instance variables in other tothe instance variables of the object being constructed. The snipped given below (a demonstration showing the use of this overloaded) will produce a single line of display:
PairOfInts pairD = new PairOfInts(7,9);
PaifOfInts pairE = new PairOfInts(pair D);
if(pairD.equals(pairE))
System.out.println("pairD and pairE are equal to each other");
Explanation / Answer
public class PairOfInts
{
int a;
int b;
public PairOfInts(int a, int b)
{
this.a = a;
this.b = b;
}
public PairOfInts(PairOfInts d)
{
this.a=d.a;
this.b=d.b;
}
public static void main(String args[])
{
PairOfInts pairD = new PairOfInts(7,9);
PairOfInts pairE = new PairOfInts(pairD);
if(pairD.equals(pairE))
System.out.println("pairD and pairE are equal to each other");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.