• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
Ranch Hand
Posts: 86
1
  • 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: 232
5
  • 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
Posts: 38917
686
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
Ranch Hand
Posts: 86
1
  • 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
Posts: 38917
686
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
Ranch Hand
Posts: 86
1
  • 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: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That simply means that class 1 ends before class 2 starts.
 
Jake Monhan
Ranch Hand
Posts: 86
1
  • 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: 61766
193
  • 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
Posts: 38917
686
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
Ranch Hand
Posts: 86
1
  • 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
Ranch Hand
Posts: 86
1
  • 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
Ranch Hand
Posts: 86
1
  • 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: 61766
193
  • 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
Ranch Hand
Posts: 86
1
  • 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: 61766
193
  • 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
Ranch Hand
Posts: 86
1
  • 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: 61766
193
  • 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
Ranch Hand
Posts: 86
1
  • 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
Posts: 23811
140
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: 3002
105
  • 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
Ranch Hand
Posts: 86
1
  • 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
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course line 1 is public class Swan {
 
Sheriff
Posts: 23876
50
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
Ranch Hand
Posts: 86
1
  • 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: 61766
193
  • 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.
 
Jake Monhan
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 220 question 9, answer A is incorrect according to the answer section. However according to many other sources on the internet, "is" is as acceptable as "get" when it comes to type boolean in JavaBeans naming convention, just not that common. And I did not get any error in NetBeans. So for clarity purposes, in the OCA exam, can I assume that "get" with type boolean is always wrong, when asking about JavaBeans naming?
 
Jeanne Boyarsky
author & internet detective
Posts: 38917
686
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaBeans aren't currently on the exam. We included them in the book in case they came back. However, they didn't come back to the exam so you don't need to worry about it at all!
 
Jake Monhan
Ranch Hand
Posts: 86
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well every little break helps!
 
Jake Monhan
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Are the concepts of class overloading and Inheritance the same thing?
2. If not, what is the difference?
3. If not, is class overloading on the OCA exam?

Thanks
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you hear somebody say, “class overloading”?
 
Jake Monhan
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was doing a Google search on inheritance in java and came across,

https://stackoverflow.com/questions/9319802/java-class-overloading
 
Jake Monhan
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I modified the code on page 207 as follows,


Since this is a way of creating immutable classes, how come all the print statements print 5? How come 6 is not being preserved?
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That class isn't immutable.
Go through the flow of execution through the constructor and you should find out why you are getting five eggs.
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:I was doing a Google search on inheritance in java and came across,

https://stackoverflow.com/questions/9319802/java-class-overloading

That isn't some sort of authorative reference; Joe Coder there is the only person I have seen say class overloading, so I suspect it is a phrase he uses himself. I do that myself; I have recently started thinking we should call the Equality Operator the “same object operator”.
 
Jeanne Boyarsky
author & internet detective
Posts: 38917
686
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake,
You override methods, not classes. In the thread you point to, class overloading is in quotes. Not an actual term. And the replies signify that it isn't a term either.
 
Jake Monhan
Ranch Hand
Posts: 86
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With so many new terms to know in Java, every official sounding term looks real
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately people create their own official‑sounding terms.
 
Jake Monhan
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must be missing a fundamental concept. Besides the book, I've looked at many examples of setting up an immutable class, and they pretty much have the same constructor design as mine.  I figured if nothing else, trying to set kk.numberEggs = 7 in main should cause an exception, but didn't! So, what am I missing?

 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a start, an immutable class should always be tagged final. Otherwise it is possible to create subclasses which are mutable; you now can create mutable instances of a so‑called immutable class.
Your code demonstrates that you have written the class in such a way as to be mutable. Your class contains code which has access to its private members and uses that access to change its state. It will become less mutable if you move that code into a different class, which is probably better design; you will have some compile‑time errors, however.
Don't write print statements in the constructor. Use the constructor to get your object into a consistent state full stop. Those print statements might be useful for debugging, but they shouldn't appear in the final product.
Give the class a toString() method and print it with System.out.println(myObject);. Don't allow other code access to the fields. Least of all in the main method, which is where the problem is occurring.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!