[HD Pg 0, Sec. 4.7.0 - exercise]

Oracle Certified Foundations Associate Java Certification Questions and Discussion
1Z0-811

Moderator: admin

Post Reply
NHendy
Posts: 1
Joined: Wed Jun 08, 2022 5:35 pm
Contact:

[HD Pg 0, Sec. 4.7.0 - exercise]

Post by NHendy »

If anyone has screenshots for this exercise's codes, could you please share ? I am struggling here.

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

Re: [HD Pg 0, Sec. 4.7.0 - exercise]

Post by admin »

Which particular exercise are you having trouble doing?
If you like our products and services, please help us by posting your review here.

gaspazet
Posts: 4
Joined: Sun Oct 02, 2022 9:48 am
Contact:

Re: [HD Pg 0, Sec. 4.7.0 - exercise]

Post by gaspazet »

Hello all,
First of all this is a great book for an absolutely begginer as myself.
I am at the same Exercise and i would also like to see the code for the first 3 ones ,that we need to write it up.
In general i would prefer to have the answers of those Exercises somewhere at the end of the book ,so we could compare or get some help incase we cant proceed.
Thank you very much

Haris

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

Re: [HD Pg 0, Sec. 4.7.0 - exercise]

Post by admin »

This is really very very easy exercise. Just do what the problem statement says. For example, in exercise 4.1, here are the steps:

(Assume that we are creating code in c:\temp folder.)

1. Create classes in two different named packages:
So, let's create these two classes :
In file c:\temp\ClassA.java:

Code: Select all

package p1;
public class ClassA {

}
In file c:\temp\ClassB.java:

Code: Select all

package p2;
public class ClassB {

}
So, now, you have two files ClassA.java and ClassB.java in c:\temp folder.

Compile these using this command:

Code: Select all

c:\temp\>javac -d . *.java
After this command, you will see these two class files:

Code: Select all

c:\temp\p1\ClassA.class
and

Code: Select all

c:\temp\p2\ClassB.class
If you like our products and services, please help us by posting your review here.

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

Re: [HD Pg 0, Sec. 4.7.0 - exercise]

Post by admin »

Step 2, let's add a static field in ClassA and access that field in ClassB. So, now we have:
In file c:\temp\ClassA.java:

Code: Select all

package p1;
public class ClassA {
  public static int a = 10;
}
In file c:\temp\ClassB.java:

Code: Select all

package p2;
public class ClassB {
  public static int ba =  p1.ClassA.a; //referring to ClassA's static field a
}
(If you have gone through the chapter, you will know that instead of p1.ClassA.a, you can also do ClassA.a but you will have to add an import statement.)

Use the same command given above to compile the files.
If you like our products and services, please help us by posting your review here.

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

Re: [HD Pg 0, Sec. 4.7.0 - exercise]

Post by admin »

You may keep modifying the code to achieve what the problem statement wants you to do. Post your code here if you face any problem. The objective of these simple exercises is to make you comfortable with java code. So, don't worry about right or wrong. Just write the code and see if you are able to compile and see if you have achieved what the problem statement has asked.
If you like our products and services, please help us by posting your review here.

gaspazet
Posts: 4
Joined: Sun Oct 02, 2022 9:48 am
Contact:

Re: [HD Pg 0, Sec. 4.7.0 - exercise]

Post by gaspazet »

Hello again and thanks for the example.
I did something like the above:

Code: Select all

(A.java)
package p1; 
import p2.B; 
public class A{
	static int W =  B.X +1;
}



(B.java)
package p2; 
public class B{ 
	public static int X = 1;
}
When i tried to run it got the following:

Code: Select all

C:\Users\Haris\jfcja\Chapter4>java A.class
Error: Could not find or load main class A.class
Caused by: java.lang.ClassNotFoundException: A.class
It is really nice to search it for yourself but i would feel more comfortable if i had in this forum an example like the above so i know that my direction is the right one.
Your example made me feel that i understood really well the subject.

No3 ,i have managed but No2 i cant figured out yet( i am trying to google some help also). Maybe i miss or i forgot some fundamental information as i pass the chapters till the excercises.
I gave up doing the following:

Code: Select all

package hr.reporting;
public class TimeCard{
	public static void main(String[] args) {
		TimeCard X = new TimeCard();
			X.add();
				
	}
}	

package hr;
import hr.reporting.*;
public class Admin{
	TimeCard.X = F;
		
}
I know the above is wrong.

Maybe more hands on examples would be more helpful for total noobs like me:)


Thanks again

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

Re: [HD Pg 0, Sec. 4.7.0 - exercise]

Post by admin »

You are having trouble running the program because you most likely missed going through section 4.5 Advanced compilation and execution and specially 4.5.1 Compilation and execution involving packages

Just follow this section and you will see why you are getting that error. It is explained there in detail with example.

The exercises are meant to be done only after reading the chapter thoroughly. Doing exercises before reading and understanding the chapter will not be very beneficial.
If you like our products and services, please help us by posting your review here.

gaspazet
Posts: 4
Joined: Sun Oct 02, 2022 9:48 am
Contact:

Re: [HD Pg 0, Sec. 4.7.0 - exercise]

Post by gaspazet »

I read it normally ,maybe i should spend more time doing hands on alone on each chapter.
I tried some combinations like java package.class etc with no luck.

Anyway thanks for your help again.

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

Re: [HD Pg 0, Sec. 4.7.0 - exercise]

Post by admin »

1. Do not specify .class while running.
2. Since your class belongs to a package, you must specify full name of the class, ie, p1.A
3. Use classpath option if needed (explained in that section)

so, your command would be:

java p1.A
If you like our products and services, please help us by posting your review here.

gaspazet
Posts: 4
Joined: Sun Oct 02, 2022 9:48 am
Contact:

Re: [HD Pg 0, Sec. 4.7.0 - exercise]

Post by gaspazet »

Sorry my mistake for not being more clear:)
By my example above i ment java package(p1).class(A) .

Thank you

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest