Page 1 of 1

Description Parent class has protected int x as instance variable. Parent's data method returns x value. Parent's comput

Posted: Fri Apr 29, 2022 6:37 am
by answerhappygod
Description
Parent class has protected int x as instance variable.
Parent's data method returns x value.
Parent's compute method returns x multiplied by the given dx
value.
A child class is a child of the parent class.
Child class has protected int y as instance variable other than
inherited.
The data method of the child class returns x + y.
Child class compute method receives dx and dy as arguments and
returns x * dx + y * dy.
Complete class Parent and class Child so that the given output
is produced.
[CONDITIONS]
1) Do what is necessary for overloading and overriding, but do
not do what is not necessary.
2) In toString of Child, toString of Parent must be used.
3) [Important] equals exists only in Parent, and cannot be
overridden or overloaded again in Child.
4) [Important] Equals must receive object type argument.
5) The main class of the code template cannot be changed at
all.
Input
no input
Output
Sample output
p1 = x(1)
p2 = x(3)
c1 = x(1) y(2)
c2 = x(1) y(5)
c3 = x(7) y(8)
p2.data() = 3
c3.data() = 15
p2.compute(5) = 15
c3.compute(2, 3) = 38
p2 == p3 ? true
p1 == p2 ? false
p2 == c2 ? false
c2 == c3 ? false
c3 == c4 ? true
c3 == p3 ? false
template:
public class Main {
public static void main(String[] args) {
Parent p1 = new Parent();
Parent p2 = new Parent(3);
Parent p3 = new Parent(3);
Child c1 = new Child();
Child c2 = new Child(5);
Child c3 = new Child(7, 8);
Child c4 = new Child(7, 8);

System.out.println("p1 = " + p1);
System.out.println("p2 = " + p2);
System.out.println("c1 = " + c1);
System.out.println("c2 = " + c2);
System.out.println("c3 = " + c3);

System.out.println("p2.data() = " + p2.data());
System.out.println("c3.data() = " + c3.data());
System.out.println("p2.compute(5) = " +
p2.compute(5));
System.out.println("c3.compute(2, 3) = " + c3.compute(2,
3));

System.out.println("p2 == p3 ? " +
p2.equals(p3));
System.out.println("p1 == p2 ? " +
p1.equals(p2));
System.out.println("p2 == c2 ? " +
p2.equals(c2));
System.out.println("c2 == c3 ? " +
c2.equals(c3));
System.out.println("c3 == c4 ? " +
c3.equals(c4));
System.out.println("c3 == p3 ? " +
c3.equals(p3));
}
}
// your code here