[HD Pg 374, Sec. 3.7.0 - exercises]
Posted: Wed Nov 22, 2023 7:32 pm
Hello, my English is not very good, so I hope I can explain my question. This question is about the OCA Java Programmer 8 Book. Section 3.7 Exercises. The problem 3 says:
Can you tell me please if I really grasp the intention of the problem, please? And also if it's a right answer. Thank you very much.
My question is about the underlined section. I don't know if I really understand the point of the problem. What I did was creating an instance of TestClass in TestClass's main method and asign literals to the Bird's instance fields in TestClass instance, then I copied the values of the instance Bird to the static Bird. For better explanation, here's my code:Create and initialize one more instance variable of type Bird in TestClass. Assign values to the members of the Bird instance pointed to by this instance variable in TestClass’s main. Assign values to the members of first Bird using the second Bird. Print the values of the members of both the Bird objects.
Code: Select all
public class TestClass{
public static Bird bs = new Bird();
public Bird b1 = new Bird();
public static void main(String[] args){
TestClass t1 = new TestClass();
t1.b1.numBirds = 1;
t1.b1.height = 2.5f;
t1.b1.weight = 1.8f;
t1.b1.exotic = true;
t1.b1.name = "Loro cabeza de caracol";
t1.b1.doble = 86.5;
bs.numBirds = t1.b1.numBirds;
bs.height = t1.b1.height;
bs.weight = t1.b1.weight;
bs.exotic = t1.b1.exotic;
bs.name = t1.b1.name;
bs.doble = t1.b1.doble;
System.out.println("t1.b1.numBirds: " + t1.b1.numBirds);
System.out.println("t1.b1.height: " + t1.b1.height);
System.out.println("t1.b1.height: " + t1.b1.weight);
System.out.println("t1.b1.exotic: " + t1.b1.exotic);
System.out.println("t1.b1.name: " + t1.b1.name);
System.out.println("t1.b1.doble: " + t1.b1.doble);
System.out.println();
System.out.println("t1.b1: " + t1.b1);
System.out.println("bs.numBirds: " + bs.numBirds);
System.out.println("bs.height: " + bs.height);
System.out.println("bs.height: " + bs.weight);
System.out.println("bs.exotic: " + bs.exotic);
System.out.println("bs.name: " + bs.name);
System.out.println("bs.doble: " + bs.doble);
System.out.println();
System.out.println("bs: " + bs);
}
}