• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tips on how to improve and where to go from here?  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am still pretty new to java so I wanted to write a application that I would have a use for based on my hobbies. My current hobby is powerlifting so I wanted to created a program that makes it easier to calculate progressive overload for that week. The program works perfectly, but I wanted to know how I can better optimize it ( to learn better practices and know what habits I have to change). Sorry I'm still new to this, so please be gentle.

Thanks!


 
Marshal
Posts: 58822
179
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid there is a lot I would change in that code. Have you not been taught how to create an object? Is there any chance of creating a Lift class with weight and whether successful as its fields? That would be something to start with, I think.
 
Jerry Mathew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

I am afraid there is a lot I would change in that code. Have you not been taught how to create an object? Is there any chance of creating a Lift class with weight and whether successful as its fields? That would be something to start with, I think.



Thanks for the reply! I have a vague concept on objects since I'm learning to code java by myself, I can try to create a lift class, but how would "successful" as a field work?
 
Campbell Ritchie
Marshal
Posts: 58822
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this?That tells you whether you lifted it. You can of course enhance the class, maybe with number of tries. I know precious little about weightlifting, as will rapidly become obvious.
 
Jerry Mathew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Like this?That tells you whether you lifted it. You can of course enhance the class, maybe with number of tries. I know precious little about weightlifting, as will rapidly become obvious.



Ohh ok, In that sense successful wouldn't work in my code as its simply a calculator based on the max weight you input. It shows the plates you need to put on both sides. I started looking up objects and see what you mean about it, I think I found a good way to implement a lift class and will paste my new code here. Is it better if I paste my code as a post or edit my original post?
 
Jerry Mathew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I created a lift class, does this look ok?




And this is my main class, I made slight changes but nothing too big



Any suggestions on the code?
 
Saloon Keeper
Posts: 1401
43
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if this became

This would allow you to quickly change the value of SET1MAX once and have that change appear throughout your code.
Especially handy if/when you have more then one line that references SET1MAX.

Generally final is used for values which don't change during the course of the program and they are named uses all caps.
 
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably wanted to add static too, right?
 
Pete Letkeman
Saloon Keeper
Posts: 1401
43
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at your code I can see that you are using a series of variables for things like sets.
You may find it beneficial to explore arrays https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html.
This would allow you to take something like
and replace it with
which could then be accessed in code like
Often people use for loops when using arrays which you can learn about here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

[Edit: corrected the index of the second warmUps element.]
 
Knute Snortum
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jerry Mathew wrote:Any suggestions on the code?


Yes.  First let's get most of the code out of main().  This will allow you to get rid of the static methods.  For a beginner, static methods are usually "wrong", it that it points to procedural programming, as opposed to object oriented programming.  So try this: move all the code inside main() into a method called anything, "run()" is fine.  Then in main() put
This can be shorted to
 
Pete Letkeman
Saloon Keeper
Posts: 1401
43
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:You probably wanted to add static too, right?

Correct.
 
Knute Snortum
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you find yourself concatenating several String objects, it's a good sign that you should use StringBuilder.  Take a look at the documentation.  Here is a mini tutorial:
There's a lot more you can do with StringBuilder, but you'll use append() most of the time.
 
Jerry Mathew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all the replies, I will look at what changes I can do with them and post them here! Thanks for being so helpful!
 
Sheriff
Posts: 11813
192
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I second Knute's advice to move code out of your main() method. Think of main as being similar to the ignition key in your car: It's only purpose is to get the car started. Once the engine is running, it's the other things like the gas pedal, brake pedal, clutch, steering wheel, etc. are really what you use to make the car do things you want it to do. Likewise, main should be small, with just a few lines of very high-level code that will get the program running.

Also, before things get very complex and tightly coupled, start writing tests and refactoring your code. I'll elaborate on that in a follow up post.
 
Ranch Hand
Posts: 398
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code tends to look something like this:

I haven't compiled this, I just deleted a bunch of stuff from working code.  2 things:

1)  You pass the target on the command line instead of using a scanner (also made it an int, doubles are similar)
2)  If you run this standalone you do a self test.  The object can also be used by a larger program without the selftest, but you can re-run the selftest at any time.

Also, the guy who said you should look into arrays was 100% correct.
 
Knute Snortum
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are these things that won't compile:
This method needs to return a boolean type.
This method calls a constructor that hasn't been declared.
Not a compiler error, but this will run without complaint if there are no command line arguments.
 
Campbell Ritchie
Marshal
Posts: 58822
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:. . . This method calls a constructor that hasn't been declared. . . . .

How can you call a constructor from a method? That won't compile even with the appropriate constructor.
JV: If you pass a non‑number via the command‑line arguments, is there any point in catching the number format exception? There might be some point if you got new input.
 
Knute Snortum
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Knute Snortum wrote:. . . This method calls a constructor that hasn't been declared. . . . .

How can you call a constructor from a method?

...um, you can't.  *blush*
 
Jim Venolia
Ranch Hand
Posts: 398
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my defense I did say I hadn't compiled it.  Looking at it 3 seconds after posting I saw all sorts of problems with it (um, if I remember correctly we used to be able to edit our posts) I saw the problems Knute mentioned, but figured I was covered by the "I haven't compiled this but..." phrase.  IMHO, that' a Get Out of Jail Free card in these here parts
 
Campbell Ritchie
Marshal
Posts: 58822
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jim Venolia wrote:. . . . that' a Get Out of Jail Free card in these here parts

. . . where people read this forum who have only been programming for a week? I am not convinced.
 
Jim Venolia
Ranch Hand
Posts: 398
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So let me edit my posts.  Doesn't matter how many times I proofread my submission, when I submit it I almost always find errors.
 
Campbell Ritchie
Marshal
Posts: 58822
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have had lots of trouble with post editing in the past.
 
Pete Letkeman
Saloon Keeper
Posts: 1401
43
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jim Venolia wrote:So let me edit my posts.


Here are a discussion regarding editing posts https://coderanch.com/t/685738/edit-post.
Essentially you can edit your posts if you have earned enough respect/credibility on this site.
 
Knute Snortum
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pete Letkeman wrote:

Jim Venolia wrote:So let me edit my posts.


Here are a discussion regarding editing posts https://coderanch.com/t/685738/edit-post.
Essentially you can edit your posts if you have earned enough respect/credibility on this site.


Until then you can a) ask a moderator (like me) to edit for you, or b) repost the corrected code.  It's also a very good practice to press the Preview button first (next to the Submit button) before posting.

And for the record, I always compile my snippets (expect the most trivial).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!