[HD Pg 0, Sec. 2.4.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
aluken
Posts: 3
Joined: Sun May 24, 2020 10:30 pm
Contact:

[HD Pg 0, Sec. 2.4.0 - exercises]

Post by aluken »

I am trying to do Exercise #1 and I have created the two files with the two different package names but I run it and it says that the package in File 1 (package abc) that it is trying to import DOES NOT EXIST - this is so confusing, any help would be much appreciated!

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

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by admin »

It would take two minutes for me to post the solution here but the idea is that you really need to do it yourself. Try to follow the hint, "Hint: If you have trouble compiling classes, check out “Compilation and Execution” section in Kickstarter for Beginners”.

If you still have a problem, please post complete code, file names, directory names where you have them and the command that you are running. Without this info, it is not possible to figure out what is wrong.
If you like our products and services, please help us by posting your review here.

aluken
Posts: 3
Joined: Sun May 24, 2020 10:30 pm
Contact:

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by aluken »

I have TestClass1.java:
package xyz;
import abc.*;

public class TestClass1 {

public static String MY_NAME = "Alvaro";

public int x = 10;



public static void main(String[] args) {
double imp = abc.TestClass2.X_TAX;
System.out.println(imp);
}
}

and TestClass2.java :

package abc;

public class TestClass2 {

public static double X_TAX = 0.70;
public String diff_name = "Eddie";

public static void main(String[] args) {
System.out.println("2 is a go!");
}
}

They are both in the same directory.


I am just trying to use a variable from one package in either one, in the other -> I went back to the help compiling section and I tried all the classpath -cp and -d commands and still it says the variables do not exist... I am honestly trying it, I am certain it has something to do with class path?

aluken
Posts: 3
Joined: Sun May 24, 2020 10:30 pm
Contact:

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by aluken »

And I am running java TestClass1.java

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

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by admin »

1. You said both the java files are in the same directory. Which directory? Please post complete path.

2. What command did you use to compile them?

3 You said, "And I am running java TestClass1.java". Were you able to compile both the java files?

4. You said, you are getting message " DOES NOT EXIST". What exact error message are you getting at what point?

I saw your code. There is nothing wrong with it. You are just getting confused for no reason. Just follow this:

1. Open cmd prompt and go to an empty directory, c:\temp\test\src and create both the java files in this src directory.
2. Compile them like this: java -d ..\classes *.java (you will not see classes created in c:\temp\test\classes\xyz\TestClass1.class and c:\temp\test\classes\abc\TestClass2.class
3. cd c:\temp\test
4. java -classpath classes xyz.TestClass1
If you like our products and services, please help us by posting your review here.

abashdesh
Posts: 14
Joined: Sat Jul 17, 2021 8:22 am
Contact:

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by abashdesh »

Hi,

I was trying to set a value to a static variable in class1 and try to access it from class2. Is that even possible? When I run the code, the value I set to the variable in the other class is ignored and null value is printed. Here are my two classes -

Code: Select all

package C1;

class class1{
	
	public String inst_1 = "test instance class 1";
        public static String st_1 = "test static class 1";

public static void main(String[] args){

	    C2.class2.st_1 = "test static";
	    C2.class2 objClass2 = new C2.class2();
	    objClass2.inst_1 = "test instance";

	    System.out.println(C2.class2.st_1);
	    System.out.println(objClass2.inst_1);
	
        }

} 

package C2;

public class class2{

	public static String  st_1 = "";
	public String inst_1 = "";

	public static void main(String[] args){
	        System.out.println(C2.class2.st_1);

	        C2.class2 class2Obj = new class2();
	        class2Obj.inst_1 = "test value";
	        System.out.println(class2Obj.inst_1);
	
        }
} 
I compiled and ran using following commands -

Code: Select all

javac -d . class1.java class2.java

java C2.class2
This is the output I get -

Code: Select all

$ java C2.class2

test value
Last edited by admin on Sat Aug 21, 2021 10:54 pm, edited 1 time in total.
Reason: Please put code inside [code] [/code]

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

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by admin »

1. Yes, it is possible.
2. In the code that you have shown here, you are not printing the static value of C1.class1.st_1 anywhere. So, you are not seeing any output for that.
3. >The value I set to the variable in the other class is ignored and null value is printed.
Can you point the lines in the above code where you are setting the variable and where you are printing it? Also, where exactly do you see "null" value getting printed?

BTW, Just a suggestion:
Please follow conventions while writing code. Package names should start with lower case and class names should start with upper case.
If you like our products and services, please help us by posting your review here.

abashdesh
Posts: 14
Joined: Sat Jul 17, 2021 8:22 am
Contact:

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by abashdesh »

Yes my mistake. It worked correctly when I replaced the static variable from c1 package instead of c2 package.

By the way, I cannot refer to instance variable from c1 package Class1 inside c2 package Class2 right? As instance variable will have to be instantiated and it will end after the scope ends?

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

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by admin »

>By the way, I cannot refer to instance variable from c1 package Class1 inside c2 package Class2 right? As instance variable will have to be instantiated and it will end after the scope ends?

You can do that too. Like this:

Code: Select all

//in c2.Class2's main method:
c1.Class1 var1 = new c1.Class1();
System.out.println(var1.inst_1);
If you like our products and services, please help us by posting your review here.

abashdesh
Posts: 14
Joined: Sat Jul 17, 2021 8:22 am
Contact:

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by abashdesh »

Yes, this worked. Thank you very much.

abashdesh
Posts: 14
Joined: Sat Jul 17, 2021 8:22 am
Contact:

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by abashdesh »

Book:
Deshmukh, Hanumant. OCP Oracle Certified Professional Java SE 11 Programmer I Exam Fundamentals 1Z0-815: Study guide for passing the OCP Java 11 Developer Certification Part 1 Exam 1Z0-815 . Enthuware. Kindle Edition.

Chapter 2 >> Exercises >> 2.

2. Define a local variable in a method. Update this variable in a while loop and print it out after the while loop ends. Check what happens when you define a variable by the same name within the while loop.

Can you please confirm that we will get a compilation error when we define the same variable inside the while loop?

Code:

Code: Select all

public class WhileLoop{
	
	public static void main(String[] args){
		
		int i=3;
		while(i>0){
			int i = 4;
			i--;
		}
		System.out.println(i);
	}
}
Output:
WhileLoop.java:7: error: variable i is already defined in method main(String[])
int i = 4;
^
1 error
Thanks.

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

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by admin »

Right and also try removing the line int = 3; and see what happens.
If you like our products and services, please help us by posting your review here.

abashdesh
Posts: 14
Joined: Sat Jul 17, 2021 8:22 am
Contact:

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by abashdesh »

Ok tried that it gives compilation error -
WhileLoop.java:5: error: cannot find symbol
while(i>0){
^
symbol: variable i
location: class WhileLoop
WhileLoop.java:9: error: cannot find symbol
System.out.println(i);
^
symbol: variable i
location: class WhileLoop
2 errors
I think it only gives error for statements outside the While Loop as i is defined inside the while loop.

abashdesh
Posts: 14
Joined: Sat Jul 17, 2021 8:22 am
Contact:

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by abashdesh »

In the Time-Card and Admin class example, when I used hyphen in Time-Card then I am getting following compilation error -
$ javac -d . Time-Card.java
Time-Card.java:3: error: '{' expected
public class Time-Card{
^
1 error
The ^ is not placed correctly when I paste it, its actually pointing to '-' location. Is hyphen not allowed in class name?

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

Re: [HD Pg 0, Sec. 2.4.0 - exercises]

Post by admin »

Yes, - is not allowed.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 50 guests