• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Where am I going wrong in Java journey?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have been on my java journey for some time now. I currently spend the best part of 3 hours a day reading and coding in Java and covering the material of OCP JAVA SE11 DEVELOPER study guide. I have covered chapters 1 - 8 thus far. In the Wiley Efficient Learning Study Guide, I have scored 100% for Welcome to Java, 95% Java Building Blocks, 80% Operators. I then fall off the cliff at chapter 4 in Making Decisions with 20%, Core Java APIS 16%, Methods and Encapsulation 14%, Class Design at 3%.

I know the easiest answer would be to "study harder" but I am trying to get all the basics correct. Is there something else or another set of books I can read to get all the material to be absorbed in my head. Sometimes I feel I am going down a rabbit hole.
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the bottom of the hole you may find bottles marked, “drink me,” or cakes marked, “eat me.”

Chester Dal wrote:. . . I know the easiest answer would be to "study harder" . . . .

That might be the easiest answer, but it is by no means the correct answer. If you have got into a hole, working harder will simply make the hole deeper and you wearier. You might do well to leave off the exams, particularly if they test the rules and don't test whether you can program.
I suggest you show us a little program you have written and explain why you made the decisions you did, and how it works.
 
Chester Dal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the feedback; below is a program i made to help with me planning a trip. Its by no means "professional". I have a number of other classes in this package. Its somewhat "untidy" with my naming conventions. I may be on the right track on further down the rabbit hole. Thanks in advance.

Forgot to add my explanations - I added the "import java.time" class to ascertain the local time in the country of Belize plus to also learn brut force how this concept works. I also added single line comments in my code so I am aware of what I was doing. I think i need to delete the java.security class as I believe that was included in error. I used the "extend" to inherit the Central America class and its attributes instead of a lot of recode usage. I assume I used object orientation instead of a procedural code in the code below by creating the objects in the "crossing the border and entry requirements section". The methods created were to make the code more functional in returning the correct output "setters and getters" to get the correct data.

 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Study Harder" sounds a lot like the Management Myth of "work harder/longer". It's what I call the Hamburger Grinder theory of software development and it's (ahem), baloney. Software is not hamburger (well, it shouldn't be, at least!). The more you stare at it, the less sense it makes. It's more akin to winemaking in that it takes its own time and cannot be forced. Even a hamburger grinder doesn't produce more hamburger if it's run over-speed, it produces cooked ground beef.

Software pros know that a lot of our real work doesn't come from sitting at a desk 10 hours or more a day and pressing keys, but instead in bed at 3:00 AM or in the shower with no keyboard in sight. The "Eureka" moment isn't something that went out with Aristotle. In fact, one of my biggest annoyances on the ranch is a User Who Shall Remain Nameless. This person flails about trying random things because they cannot be bothered to sit down, read the freakin' manual and slowly think things through. They're doing a lot of work, but none of it works.

Sometimes if you simply back off and go gardening, play the violin (a là Sherlock Holmes), or whatever, pieces will gradually fit into place. Don't try and force them.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . a User Who Shall Remain Nameless. . . .

At least it isn't me (I hope).

I am afraid that explanation doesn't tell me how the program works. An import declaration shouldn't need any explanation in the first place.
Only use the reversed webname format for a package name if you own that website. But at this stage, you should probably not be using package names yet.
The book hasn't taught you about encapsulation; none of your variables should be accessible outwith the class, which means they should all have private access. Reserve public access for global constants
Avoid setXXX() methods; initialise the costs in the constructor, or create an addCost(Cost) method to add costs.
Don't put so much code into the main() method, whose ideal length is one statement.
Afraid I don't like the names of your classes; a Belize ISN'T‑A SouthAmerica, so I would change the names. In fact I think you want a Trip class with an instance you call belize.

I think that example is proving a burden to you, and I think you should revert to something simpler. Start by deciding what you want the program to do without using any computing names, before you try implementing anything. I am afraid that isn't a good object‑oriented example. I worry about books that tell you all about the language before they teach object‑orientation.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at your code the thing that strikes me the most is that it doesn't really take advantage of an object oriented approach. A tell-tail sign is that you've got everything in main(). As I read it I was imagining what would generic requirements for ANY trip be? And also treating a trip with sub-trips (day jaunts, etc).

Currency List
FromCurrencyCode, ToCurrencyCode, Rate

Trip:
Date of trip
From
To
isRoundTrip
List<Prerequisites>
List<Trip> // subtrips

Prerequisites:
Description, Required, RequiredByDate, Satisfied, Cost, CurrencyCode

The trick is to take your specific problem and abstract it so that it could be easily applied to similar problems without the need for re-writes.

You might have a text based user interface that allows entry/editing of the parameters. You could save/load to flat files. Generate reports.

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . what would generic requirements for ANY trip be? . . .

In my case, beer, beer, beer, beer, and beer (). On the last trip I took (I left on Monday and got home yesterday), I did manage to find some good beer. So I would start my Trip class with one attribute. Let's say it's travel costs. I shall leave you to work out what the cost‑per‑mile/km for my 61 miles yesterday (=99km), or what I do about my train ticket, but I might call a method like this:-Start off with a very simple implementation: record the description of the latest part of the journey and the total price. Get that working. Show us it. Then we can suggest some enhancements. Get a good OO application running first, then the enhancements will be simpler to implement.
I shan't implement a beerCost field in case Ruth finds out how much I spent!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Software pros know that a lot of our real work doesn't come from sitting at a desk 10 hours or more a day and pressing keys, but instead in bed at 3:00 AM or in the shower with no keyboard in sight.


I always say software development is 90% thinking, and 10% typing.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:. . . software development is 90% thinking, and 10% typing.

But it's actually 90% drinking, and 9% thinking, and 1% typing.

The drinking is usually (fortunately) full of caffeine rather than ethanol
 
Chester Dal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your comments and feedback. Most appreciated.

Campbell Ritchie wrote:Please avoid quoting the whole of a previous post; that usually adds little to the discussion and is liable to deletion. Only quote the relevant parts please.

 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:none of your variables should be accessible outwith the class…



"outwith"? If you're going to go Scottish on us, I'm going to have to check my ancestry to see if there's a blood feud between our clans. Or should I just take it as given?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . Or should I just take it as given?

Take it as given.
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be noted that Campbell and I generally differ on the use of setters versus omnibus constructors. However, as this is both the bean and the main class, I, too would tend to favor constructor parameters.

More importantly, we both strongly agree that the primary logic for the app should NOT be in the main method or constructor. I usually have main() construct an instance, and the instance does its work in a method with a name of doIt() or something similarly original.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would be the point in any of these classes being Beans? Introdcuing Beans would also make their leaning curve steeper
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you can argue that the class object isn't technically a bean, although in many cases that's what it is for me, since for a serious Java command-line app I will often take parameters as arguments to the main() method and so I'll instantiate, inject override values from the command line options, and then doIt().
 
Chester Dal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So leave the main method on its own and utilise the other classes accordingly.
All this does help. Thank you.

Tim Holloway wrote:It should be noted that Campbell and I generally differ on the use of setters versus omnibus constructors. However, as this is both the bean and the main class, I, too would tend to favor constructor parameters.

More importantly, we both strongly agree that the primary logic for the app should NOT be in the main method or constructor. I usually have main() construct an instance, and the instance does its work in a method with a name of doIt() or something similarly original.

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chester Dal wrote:So leave the main method on its own and utilise the other classes accordingly. . . .

Don't know; please explain more about what you intend to do.

Thank you.

That's a pleasure

Tim Holloway wrote:. . . the primary logic for the app should NOT be in the main method or constructor. I usually have main() construct an instance, and the instance does its work in a method with a name of doIt() or something similarly original.

More advice about the main() method from Winston Gutkowski here. I would write something like this:-The reason for making the method and constructor private is that they don't need to be called from outwith this class: note link to JLS (=Java® Language Specification). The app's logic can be run from execute(), and you can expand that class as you develop your other code.
Note that if you analyse that class and compare it to what Tim recommends, it will constitute exactly the same technique. And you will find my XYZDemo classes are the equivalent of Winston's XYZLauncher class and my main() method is an abbreviated version of Winston's main() method. That proves that, “great minds think alike,” “fools never differ,” as we said when I was a young schoolboy.
Joking apart, I recommend you write code like that above, and a Trip class, keeping it to the simple state I told you about yesterday. Then show us how far you have got, remembering that you now have an incomplete product, like when I reassembled the bicycle I rode earlier this week. I think I started by fitting the seat. The seat can be used as a handle for the rest of the bicycle. That is one stage more than a load of separate parts, but still only a start and it couldn't be ridden until other parts were added. As I said, enhance your work in stages. Get one piece working and then add the next piece.

Challenge: find the mistake I made in my advice to you yesterday. I gave you conflicting advice, and something could (should) be changed to make the solution more object‑oriented.
 
Chester Dal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have now realised i should used access modifiers on my variables, ideally the private access modifier. In addition to this getters and setters to make sure encapsulation of the data is utilised.

With regards to the main method on its own, I have noticed some books have the main method in the body of just one class along with the rest of the code. Other books have the main class in one file and the rest of the data in another file.

Challenge: find the mistake I made in my advice to you yesterday. I gave you conflicting advice, and something could (should) be changed to make the solution more object‑oriented.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chester Dal wrote:. . . Other books have the main class in one file and the rest of the data in another file.

I don't like the concept that there is such a thing as a “main class” at all. The main() method is called that for historic reasons, to be similar to C and C++, but all it does is start the program off. I prefer not to think that classes live in some sort of hierarchy of importance.

How have you got on with writing your code, even if a very basic form of it? And did you work out what my mistake yesterday was?
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Chester Dal wrote:. . . Other books have the main class in one file and the rest of the data in another file.

I don't like the concept that there is such a thing as a “main class” at all. The main() method is called that for historic reasons, to be similar to C and C++, but all it does is start the program off. I prefer not to think that classes live in some sort of hierarchy of importance.

How have you got on with writing your code, even if a very basic form of it? And did you work out what my mistake yesterday was?



Actually, there isn't any such thing as a "main class" in Java. Well, yes, you can define a Main-Class in an executable JAR, but what it really is is the default execution class. Every freaking class in a JAR could contain a main() method and that's not only perfectly legal, but having multiple classes with main methods in them is not that uncommon for systems where one JAR serves multiple applications and/or test services.

Why should you be constrained to have the top-level method called main()? Well, as noted, it's modelled after C/C++ (and PL/1), but primarily because it's one less variable to have to chase down when trying to make sense of a program, just like in Maven, you know to look in srv/main/java for application source. Or use run() for Threads.

As to why main() has to have a class at all, that comes down to the fact that Java isn't merely Object-Oriented, it's Object based. You cannot have a free-floating entry procedure in Java like you can in C.
 
Chester Dal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[/code]


I can figure out the mistake you mentioned however i have encapsulated the data.

How have you got on with writing your code, even if a very basic form of it? And did you work out what my mistake yesterday was?
 
Chester Dal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"cant" figure out the mistake.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chester Dal wrote: . . . however i have encapsulated the data. . . .

I am afraid I am not convinced you have. All you did was to write a load of setXXX() and getXXX() methods, which can expose implementation details. Most of the names of the fields can become obvious to other code. Your object doesn't do anything more than record the last data it was presented with. It fails to take care of itself because it doesn't do anything with those data; it doesn't even work out the total cost of the trip. If you wanted the total cost, you would have to add them up from outwith your object.
As Tim said, he and I disagree about setXXX() and getXXX() methods. If you don't set all those data in the constructor, you can create an object with its fields set to their default values. Tim describes how he would call the setXXX() methods, but he is working in a protected environment, particularly if using a framework like Spring, and can ensure that all the setXXX() methods have been called before the object is used. You cannot rely on those methods being called., and there are advantages to setting fields once only: for example, you can make your datatype immutable.
You are right to run the code from elsewhere, but you should move all that code out of the main() method. You have set some of the data, but you haven't shown how you can view those data, nor how you can verify that your code is working correctly.
Didn't your books tell you not to use doubles for anything requiring precision, which includes money? I have my own opinion about the ideal place to use the float datatype: nowhere. Unfortunately there is still API which requires floats.
Why didn't you try to implement my suggestion, which would have required less code?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chester Dal wrote:"cant" figure out the mistake.

I said to create a cost class and then used two parameters to the method, a String and an int.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are making the  code very brittle in that if you have to add another prerequisite then you'll need to change a bunch of code. Instead, make your  code data driven. Here's an example where your hard coded fields are replaced by "Expense" data. Total expenses can then be computed by looping through the Expense List and applying any currency rates.
reply
    Bookmark Topic Watch Topic
  • New Topic