onsider the following class definitions: question 1 public class A { private int
ID: 3535956 • Letter: O
Question
onsider the following class definitions:
question 1
public class A
{
private int number;
public A(int n)
{
number = n;
}
public A(A obj)
{
this.number = obj.number;
}
public String
toString()
{
return "object value: " + number;
}
}
public class B
{
private int number;
private A value;
public B(int n, A
val)
{
number = n;
//-------------- What do we put here?
}
public String toString()
{
return value + " and " + number;
}
}
What is the line of code needed to perfom the deep copy required of
the object B with an object of A as its member?
Answer:
Question
2
Marks: --/14
Consider the following class definitions:
public class A
{
private int number;
private String text;
public A(int n, String txt)
{
number = n;
text = txt;
}
public A(A obj)
{
number = obj.number;
text = obj.text;
}
public String toString()
{
return "The object value: " + number + " and " + text;
}
}
public class B extends A
{
private int number;
public B(int localN, int parentNum,
String parentTxt)
{
//What
goes here?
number = localN;
}
public String toString()
{
return "The child has the value: " + number + " and the parent has
the value: " + //What goes here
}
}
Two questions:
1. The constructor for class B is incomplete because the
call for the parent object is missing. Write that call in the first
blank space.
2. The child wants to output its content and the parent
object's content. Write only the part that calls the
parent's output method in the second blank space.
Answer:
Question
3
Marks: --/25
Consider the following class definitions
public class A
{
private int number;
private String text;
public A()
{
number = 0;
text = "";
}
public void setNum(int n)
{ number
= n; }
public void setText(String t)
{ text = t; }
public
int getNum()
{ return number; }
public String getText()
{ return text; }
public String toString()
{
return "The object value: " + number + " and " + text;
}
}
public class B extends A
{
private int number;
public B()
{ number =
0; }
public int getNumber()
{ return number; }
public void setNumber(int n)
{
number = n;
setNum(12);
setText("Hello");
}
public String toString()
{
return "The child has the value: " + number + " and the parent has
the value: " + getNum() + " and " + getText();
}
}
public class C extends B
{
int double value;
public C()
{ value =
0.0; }
public void setVal(double v)
{
value = v;
setNumber(5);
setNum(8);
setText("We Inherited");
}
public double getValue()
{ return value; }
public String toString()
{
return "This object value is " + value + " The object inherited "
+ getNumber() + " and also "
+ getNum() + "
and " + getText();
}
}
public class Demo
{
public static void main(String[]
args)
{
int
a = 10, b = 14;
double c = 12.8;
String t = "Objects are fun";
//In space 1. Declare an array
called objects that handles 3 objects: 1 of
type A, 1 of type B and 1 of
type C
//Hint:
not time for "new" yet
//In
space 2, set the first element of the
array objects to
type A.
//In
space 3, set the numeric member of the first element of the array
to int variable a.
//In
space 4, set the String member of the first
element of the array to the
String variable t.
//In
space 5, add the second element to
array objects and make that element of
type B.
//In
space 6, set the numeric member of object
type B to the int
variable b.
//In
space 7, add the third element to the
array objects and make that element of
type C.
//In
space 8, set the floating-point member of object
type C to
the double variable c.
//In
space 9, write the header of a for loop to
output the array content with i as the
counter variable set to 0 declared in the
header.
{
//In space 10, write an output statement using the console that
outputs each object and takes advantage of the toString methods
capabilities.
}
}
}
//Do not forget your semicolons where
necessary
Explanation / Answer
answer-
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.