[HD Pg 374, Sec. 3.7.0 - exercises]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
jjoseorione
Posts: 3
Joined: Tue Oct 11, 2022 1:47 pm
Contact:

[HD Pg 374, Sec. 3.7.0 - exercises]

Post by jjoseorione »

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:
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.
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:

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);
	}
}
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.

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 3.7.0 - exercises]

Post by admin »

Yes, it is fine. The objective is to just to make sure you know how to access member fields (instance as well as static) of a class and realize that the static field values are shared by all instances of a class. In this case, you should have a few static as well as instance fields in the Bird class.

Then create one instance and one static fields of type Bird in TestClass. Initialize all the values inside those bird instances. Then see the values of instance as well as static fields of both bird instances. Copy values from one to another.

There is no fixed correct answer. Just play around with the code and see how things work. That is the main objective. If you have any doubt, you can ask here.
If you like our products and services, please help us by posting your review here.

jjoseorione
Posts: 3
Joined: Tue Oct 11, 2022 1:47 pm
Contact:

Re: [HD Pg 374, Sec. 3.7.0 - exercises]

Post by jjoseorione »

Thank you very much! :cheers:

Post Reply

Who is online

Users browsing this forum: No registered users and 243 guests