• Post Reply Bookmark Topic Watch Topic
  • New Topic

This code is not acting the same way in NetBeans  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the OCA Oracle Certified Associate Java SE 8 Programmer Study Guide, page 174, code lines 5 and 6 are listed as DOES NOT COMPILE. However the exact code in NetBeans IDE 8.2, those lines compile just fine!
Why is that?
 
Ranch Hand
Posts: 204
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:So my first advice would definitely be: get rid of your IDE, only use your favourite text editor, javac and java. Other opinions about using an IDE while preparing for the OCA exam can be found here.

from https://coderanch.com/t/659060/certification/Studying-hard-fail

Jake, That's kind of a non-starter, but without knowing your exact setup, I'd say look at your compatibility settings.
 
author & internet detective
Marshal
Posts: 38144
617
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake,
Welcome to CodeRanch!

Can you copy and paste the exact code from your FatherDuck and BadDuckling classes here? (I'm suspecting a typo at the moment)
 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package pond.duck;

/**
*
* @author
*/
public class PondDuck {
    public class FatherDuck {
        private final String noise = "quack";
        private void quack() {
            System.out.println(noise); // private access is ok
        }
    private void makeNoise() {
         quack();
    }}
    public class BadDuckling {
        public void makeNoise() {
            FatherDuck duck = new FatherDuck();
            duck.quack();
            System.out.println(duck.noise);
    }}
    public class MotherDuck {  
        String noise = "quack";
        void quack() {
            System.out.println(noise); // default access is ok
        }
        private void makeNoise() {
            quack(); // default access is ok
    }}
    public class GoodDuckling { 
        public void makeNoise() {
            MotherDuck duck = new MotherDuck();
            duck.quack(); // default access
            System.out.println(duck.noise); // default access
    } }
 
    public static void main(String[] args) {
   
    }
   
}
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 38144
617
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted does not match the code in the book. In the book, FatherDuck and BadDucking are in separate files in the same package. In your example, they are both inner classes inside the PondDuck class.

Inner classes have different roles regarding scope/access. They are also an OCP topic and therefore not on the OCA exam.
 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the update.

I think that's the problem. I'm still trying to get my head around "classes in separate files in the same package". Is there some place I can look at some examples of this?
 
Marshal
Posts: 58822
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That simply means that class 1 ends before class 2 starts.
 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So since java gives compiler error if  the main class name does not match the package name (i.e. package pond.duck; public class PondDuck {};) and won't allow two files named pond.duck, how would package pond.duck have the two classes out side the main class?
 
Campbell Ritchie
Marshal
Posts: 58822
179
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all those classes are top‑level classes, then the five public classes would be in files called PondDuck.java, FatherDuck.java, BadDuckling.java, MotherDuck, and GoodDuckling.java.
You are, I am afraid, incorrect about matching package names and class names. Also I recommend you forget the concept that there is such a thing as a main class, never mind how many people use that term.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 38144
617
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If all those classes are top‑level classes, then the five public classes would be in files called PondDuck.java, FatherDuck.java, BadDuckling.java, MotherDuck, and GoodDuckling.java.
You are, I am afraid, incorrect about matching package names and class names. Also I recommend you forget the concept that there is such a thing as a main class, never mind how many people use that term.


Correct. If a code example has "package" or line numbers, those are each expected to be in different files.
 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now it makes sense (or should it be, Now I get it). Thanks
 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 189 - System.out.println(name); ignores the update from the method speak.
On page 191 - line  6 of the code does not ignore the update from the method letters.

Pleas further clarify why?
 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 193 - Shouldn't "public class ReferenceTypes {" be "public static class ReferenceTypes {", so it be called from static main?
 
Campbell Ritchie
Marshal
Posts: 58822
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please supply more details; I don't have the book. Please show us the whole code from pages 189, 191, and 193.
 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Page 189



page 191

1: public class ReturningValues {
2:       public static void main(String[] args) {
3:             int number = 1; // 1
4:             String letters = "abc"; // abc
5:             number(number); // 1
6:             letters = letters(letters); // abcd
7:             System.out.println(number + letters); // 1abcd
8:       }
9:       public static int number(int number) {
10:           number++;
11:           return number;
12:     }
13:     public static String letters(String letters) {
14:           letters += "d";
15:           return letters;
16:     }
17: }

page 193

 
Campbell Ritchie
Marshal
Posts: 58822
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:Page 189 . . .

Work out what the different name identifiers in the speak() method mean.

page 191
. . .
6:             letters = letters(letters); // abcd
. . .

There is a difference between that line 6 and the previous code. When you work out the difference, you will probably understand the differences in behaviour.

page 193

What makes you think the class should be marked static? Do you remember what sort of classes can be marked static in the first place?
 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As pages 189 and 191, Yup I get it. One is assigned and one is not.

As for page 193, I know you don't like the referral, but since the class is main class, then it does not need static specifier. If it were a class within another class, then it would have needed the static specifier.

So how did I do?
 
Campbell Ritchie
Marshal
Posts: 58822
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:As pages 189 and 191, Yup I get it. One is assigned and one is not.

It has to do with pass‑by‑value. Remember Java® doesn't support pass‑by‑reference.

. . . I know you don't like the referral . . .

What does that mean? How do you know I don't like something?

since the class is main class, then it does not need static specifier.

That sound incorrect. Try this sort of code:-See what happens.

If it were a class within another class, then it would have needed the static specifier.

What makes you think that? It is quite possible to create an instance of an inner class in a static context.

So how did I do?

Two out of three aren't bad, but at that sort of ratio you are risking averaging 2.9 out of three, which will mean you have to go back and sit the exam again.

What are the answers given for page 193? What do you think the question is really about?
 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for how do I know you don't like something - I was referring to last sentences of your answer to my post about a week ago (‎5‎/‎25‎/‎2018 ‎12‎:‎43‎:‎40‎ ‎AM )
"If all those classes are top‑level classes, then the five public classes would be in files called PondDuck.java, FatherDuck.java, BadDuckling.java, MotherDuck, and GoodDuckling.java.
You are, I am afraid, incorrect about matching package names and class names. Also I recommend you forget the concept that there is such a thing as a main class, never mind how many people use that term. "
If I misinterpreted, or took it out of context, I'm sorry.

As for the code sample you provided, I ran it and yes it does not require static specifier.

Although the code on page 193 is about overloading, looking at the that code and your sample, I wonder if I missing fundamental concept of how a method with return of void vs. a method having return, is handled.

 
author
Sheriff
Posts: 23484
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:
Although the code on page 193 is about overloading, looking at the that code and your sample, I wonder if I missing fundamental concept of how a method with return of void vs. a method having return, is handled.



Method overloading does not take into account the return type, in order to disambiguate the method; so, I am not exactly sure what concept you seem to be missing.

As for the void return type, method that return void can only be used in Java statements, while methods that return a value can be used in Java statements and/or expressions.  Of course, I may have caused more confusion, as you probably haven't learned the subtle difference between a statement and an expression yet....

Henry
 
Master Rancher
Posts: 2533
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this is any more relevant, but this is how I used NetBeans with this exercise. In my project 'OCAJP' I created some sub packages, see print in this link:

dropbox.

I folowed the packages as in the book exactly, and it worked a treat. In the image you see many red blocks with a white exclamation mark, indicating there is something wrong. But that is one advantage of using an IDE this way: if you change some access modifiers from say public to protected or default, you see at once if the lot still keeps working or not...
 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made the following modification to the code on page 205 and the result of all 4 print statement was -1.  So how does the IF condition guards against negative numbers?

 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course line 1 is public class Swan {
 
Sheriff
Posts: 23485
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:I made the following modification to the code on page 205 and the result of all 4 print statement was -1.  So how does the IF condition guards against negative numbers?



The IF-condition indeed doesn't allow you to assign a negative number to the class's numberEggs attribute. That part does guard against negative numbers. But that code implies that the numberEggs attribute should never be negative; given that design, initializing the attribute to -1 was a bad idea. It totally fails to guard against negative numbers. If numberEggs is supposed to represent the number of eggs in some location then initializing it to zero would be a sensible thing to do.

Also if you call a method named setXxxx() and the code refuses to actually set the related attribute in the class, then just continuing without comment is not really a good idea. Throwing an IllegalArgumentException would normally be preferable.
 
Jake Monhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made the following changes and the results changed as follows. Am I correct in saying that because of the IF condition, although the variable numberEggs = -1, the instance variable numberEggs is still zero.

 
Campbell Ritchie
Marshal
Posts: 58822
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:. . . although the variable numberEggs = -1, the instance variable numberEggs is still zero.  . . .

Yes, assuming there were no eggs before.

It might be better to say that numberEggs is a parameter than a variable, but it is technically both. Paul C is right that you should throw an exception if a negative argument is passed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!