Consider the following class definitions public class A { private int number; pr
ID: 3535950 • Letter: C
Question
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.
}
}
}
Explanation / Answer
//1
Object[] objects;
//2
objects[1] = new A();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.