Can you guys help me out with these? 1.) Consider the following class definition
ID: 3535863 • Letter: C
Question
Can you guys help me out with these?
1.) Consider the following class definitions:
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 goes here?
}
public String toString()
{
return value + " and " + number;
}
}
What is the line of code needed to perform the deep copy required of the object B with object of A as its member?
2.) 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 " + test;
}
}
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:
2A) The constructer for class B is incomplete because the call for the parent object is missing. Write that call in the first space.
2B) 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 space.
3) 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 get Value()
{ 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 and 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 number member of 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 numberic 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 method capabilities
}
}
//Do not forget your semicolons where necessary
Explanation / Answer
public class AA{ private int x; private int y; publicvoid print() { System.out.println(x + "" + y); } publicint sum() { return x + y; } publicAA() { x = 0; y = 0; } publicAA(int a, int b) { x = a; y =b; }}You had to get rid ofthe word int in public int AA(int a, int b);
or visit
http://answers.yahoo.com/question/index?qid=20120508084917AAni3zB
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.