Page 1 of 1

[HD Pg 0, Sec. 4.7.0 - exercise]

Posted: Mon Jun 13, 2022 5:16 pm
by NHendy
If anyone has screenshots for this exercise's codes, could you please share ? I am struggling here.

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

Posted: Thu Jun 16, 2022 2:41 am
by admin
Which particular exercise are you having trouble doing?

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

Posted: Sun Oct 02, 2022 9:53 am
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

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

Posted: Mon Oct 03, 2022 9:58 am
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

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

Posted: Mon Oct 03, 2022 10:02 am
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.

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

Posted: Mon Oct 03, 2022 10:03 am
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.

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

Posted: Tue Oct 04, 2022 3:34 pm
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

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

Posted: Tue Oct 04, 2022 11:51 pm
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.

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

Posted: Wed Oct 05, 2022 8:41 am
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.

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

Posted: Wed Oct 05, 2022 9:29 pm
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

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

Posted: Thu Oct 06, 2022 12:56 pm
by gaspazet
Sorry my mistake for not being more clear:)
By my example above i ment java package(p1).class(A) .

Thank you