Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

This code is not acting the same way in NetBeans

 
Marshal
Posts: 69779
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:. . . OCA Study Guide pages 279 and 280.  . . .

Thank you, but pleasee give more details, e.g. author's name, because there are several books which would fit that description.

You are right: lemurs don't have striped tails. They have ringed tails .
 
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Authors are and Jeanne Boyarsky & Scott Selikoff.
As for the lemurs' tail situation, I would suggest a hint to the authors:)
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the code on pages 285 & 286 of the book mentioned in the last posting, how come the code variation in the class ZooWorker produces compiler error? I'm sure that I'm missing something basic, so appreciate a hint.

 
Marshal
Posts: 25671
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with the compiler: I don't see those methods either. Perhaps if you told us where you see a method named "Reptile2" we could see where you are going wrong.
 
Paul Clapham
Marshal
Posts: 25671
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You are right: lemurs don't have striped tails. They have ringed tails .



The stripes on their tails (at least those of the Ring-tailed Lemur, which appears to be the species you're talking about) aren't longitudinal, it's true. They are very short stripes which go around the tail.
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok since it seems like I'm coming around correcting my understanding of this in a long about way, let me ask,

1.  Lets take line 41,  why is removing "new" will cause the same compiler error as line 40?
2.  In lines 41, 44, and 47, aren't classes and subclasses being passed to the method? If so, how is "new" in these lines acting different than in line 38?
 
Campbell Ritchie
Marshal
Posts: 69779
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by going through the bytecode, maybe:-Then work out what Reptile2() is. The fact that new Reptile2() compiles at all should give you a big hint.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mooo!

Your posting was just mentioned in the July 2018 CodeRanch Journal and for that you get a cow.
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pete Letkeman wrote:Mooo!

Your posting was just mentioned in the July 2018 CodeRanch Journal and for that you get a cow.



Well I guess that means that I've caused enough commotion:)
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Start by going through the bytecode, maybe:-Then work out what Reptile2() is. The fact that new Reptile2() compiles at all should give you a big hint.



Thanks for the hint. Your hint and rethinking what I posted originally - I am wondering that I've been overthinking the process of parameter passing vs. instances!
 
Campbell Ritchie
Marshal
Posts: 69779
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:. . . I've been overthinking the process of parameter passing vs. instances!

No, more missing the source of the error: confusing constructors and methods.
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Jake Monhan wrote:. . . I've been overthinking the process of parameter passing vs. instances!

No, more missing the source of the error: confusing constructors and methods.



I missed this basic rule in the above code, didn't I?

"If the parent class doesn’t contain a no argument constructor, an explicit call to the parent constructor must be provided."
 
Campbell Ritchie
Marshal
Posts: 69779
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:. . . I missed this basic rule in the above code, didn't I?

"If the parent class doesn’t contain a no argument constructor, an explicit call to the parent constructor must be provided."

Don't say parent class: say superclass. What you missed is even more basic; you posted the name of a constructor where should have posted the name of a method.
You can only call the superclass' constructor with the super(...); construct, and that can only be written as the first line of a constructor.
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it comes down to - looking at the main() method in the ZooWorker class, the new Reptile2() being passed to feed() method as parameter, is the direct reference to the superclass's constructor?

 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Continuing the last posting, am I getting the terminology in the total of both postings right?

In this updated code, passing the constructors to the feed() method is saving us from having to create the other 3 methods to get the same results, correct?

 
Campbell Ritchie
Marshal
Posts: 69779
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:. . . the new Reptile2() . . . is the direct reference to the superclass's constructor? . . .

No.

That is an instruction to create a Reptile2 object, calling its constructor taking no arguments. How that constructor calls its superclass' constructor is a detail in the implementation of Reptile2 which you might not know about. Actually, I think you do know about it. But maybe you should marshall your thoughts about that point.
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the key I've been looking for - "instruction to create a Reptile2 object, calling its constructor taking no arguments."

I can finally move to the last chapter of the study book.
 
Campbell Ritchie
Marshal
Posts: 69779
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:This is the key I've been looking for . . . .

Success
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously the first concrete subclass is not implementing the abstract method. I was wondering what would be simplest change, for the code to compile?

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

Jake Monhan wrote:Obviously the first concrete subclass is not implementing the abstract method. . . .

How on earth would you get that sort of thing past the compiler? A concrete class must implement all methods declared or inherited from an interface or a superclass. Your Insect class is abstract.
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Oh, please ignore the last posting, I totally blew the way I posted what I was really after. This is what I had intended to ask,

Given the original code below that obviously does not implement the abstract methods and its further modification right after, I was wondering what would be simplest change, for the modified code to compile?





 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll rephrase the question from my last two postings, hoping for a hint so I can get this concept straight in my head.

If the "static abstract class Insect implements HasExoskeleton {" in superclass loses the "static", then in the subclass the compiler error requires an enclosing instance of Chpt5Q6.Insect.

Questions,

1. abstract class Insect implements HasExoskeleton must be a static one? Why?
2. If not, how can the code in subclass be corrected for the compiler error to go away?


 
Campbell Ritchie
Marshal
Posts: 69779
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you made that class and interface nested rather than top‑level?
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I confess - not having them nested was the code snippet in the original question that did not implement all the abstract methods (page 293). I've been looking to see after getting passed the implementation issue error (which was obvious), in a nested situation, how to get past the "enclosing instance of Chpt5Q6.Insect" error in the subclass without having to make the abstract class in the superclass a static abstract class.

So if the nesting of the abstract class and interface in this way, is just a bad (or not doable) design, then should I stop?

Original code snippet from the book,
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how my code in yesterday's posting will work in nested version without the abstract class in the superclass being set to static. Which then looks to me like setting it to static just to force the nesting to compile doesn't accomplish anything either.  Is this correct, or am I missing something?
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that writing a try/finally block without a catch block, is nothing more than writing extra code for sequences of code that would have executed anyways. Or is there a reason to write a try/finally block without the catch block?
 
Paul Clapham
Marshal
Posts: 25671
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:It seems to me that writing a try/finally block without a catch block, is nothing more than writing extra code for sequences of code that would have executed anyways. Or is there a reason to write a try/finally block without the catch block?



Yes, there is. If a line of code in the try block throws an uncaught exception then all code after that will be skipped. Using a finally-block ensures that essential code will be executed regardless of whether or not an exception was thrown. (Typically that essential code is there to close resources such as database connections.)

Note that this reason applies whether or not a catch block is present -- the presence or absence of a catch block has nothing to do with why you need a finally block.
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then is it correct to say that the finally block is a fail-safe code section of the program?
 
Paul Clapham
Marshal
Posts: 25671
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you could say that, but I'd have to look up the word "fail-safe" to be sure.
 
Marshal
Posts: 3145
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Monhan wrote:Then is it correct to say that the finally block is a fail-safe code section of the program?


Fail-safe usually means something that it is only invoked in the event of a failure.  In Java, a finally block is always executed, regardless of whether an exception occurred or not.
 
Paul Clapham
Marshal
Posts: 25671
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:In Java, a finally block is always executed, regardless of whether an exception occurred or not.



This is just how I think of it. If I have some code which I want executed regardless of whether an exception occurred in earlier code, I think of a finally block. I don't find other descriptions useful, so even if "fail-safe" were a correct description I wouldn't bother with it.
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well thanks a bunch fellows. Is not everyday that one gets a helping hand from sheriff and saloon keeper to set the marbles straight
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking more into enhanced for loops and was wondering in the following code snippet I coded, which enhanced for loop is a better design?


 
Paul Clapham
Marshal
Posts: 25671
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really like the first version (the enhanced for-loop) because it looks like it can be used for all kinds of lists, but then it turns out that it can actually only be used for an array. Not to mention that if the last entry of the array is duplicated by an earlier entry, you're going to miss a comma. That workaround you had to use so that you could write an enhanced for-loop is flawed.

But consider this instead:



This also fixes the bug in both of your versions which occurs when the array only has one entry.
 
Jake Monhan
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup, your design will definitely take care of duplicates and make the code easier to follow. I just made a couple modifications to it to make the outcome look better.

 
Campbell Ritchie
Marshal
Posts: 69779
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two alternatives:-
  • 1: After the loop, System.out.println("\b ");
  • 2: Probably a lot better: use a StringJoiner object.
  •  
    Bartender
    Posts: 3994
    156
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also: do have a look at the static String method String.join
     
    Jake Monhan
    Ranch Hand
    Posts: 121
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, I did come across stringjoiner objects. But I held off looking into them, till I finish with the OCA exam.


    p.s.

    In table 2.5 (page 91) of the OCA 8 prep book, it is listed that if statements do not allow break and continue statements.

    1. However the following code snippet did not give me any compiler error or run time exception for using break statement.
    2. when replaced break with continue, it did not like the continue statement, calling it "unnecessary continue statement".



    So what is correct for the answer for the OCA exam?
     
    author & internet detective
    Posts: 40035
    809
    Eclipse IDE VI Editor Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The book is correct. This code compiles:


    This does not:


    Which shows the break is allowed in the first example because it is inside of a loop. The fact that it is also inside an if statement isn't relevant to compilation.
     
    Saloon Keeper
    Posts: 12132
    258
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see Jake's point though. If the book says that break is not allowed an if-statement, that's patently untrue because you can put a break inside an if-statement as long as it's somewhere inside a loop.

    More correct would be to say you can only use break inside a loop or switch case (regardless of the scope of the block that contains the break statement).
     
      Bookmark Topic Watch Topic
    • New Topic