• Post Reply Bookmark Topic Watch Topic
  • New Topic

first program in Java, how to make it less procedural and more OOP  RSS feed

 
sum ding wong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any tips on how i can improve this simple resource calculator for this game called Clan of Clans.

It might even look more functional than OOP. Any help with be appreciated!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Start by expunging the word static from your vocabulary. There are good reasons why something might be static, but it is the exception. If you write static frequently, then you are not writing OO.
Avoid parallel arrays like the plague. For one thing they are not OO. For another they are very error‑prone. Create a Monster class with name and cost. Find out about enumerated types and consider whether you can use an enumerated type to implement your Monster class.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly tell us and the people on the other website if you post in several places.
 
sum ding wong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops, it's my first java app. I was excited to get a second opinion and wasn't aware that cross posting was an offending issue. I know better now.

I don't fully understand instances, static, when to make their own classes yet but i'll try reading some more and post my second draft following your suggestions.

Thank you
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the IDE is leading you to make everything static so it doesn't show an error. As was said, get rid of all your statics. Besides being extremely odd it is helping it be C like and procedural because you never have to make an instance of anything. So somewhere near the top of main put ClashOfClan coc = new ClashOfClan(); to create an instance of the class. After that you can call your methods the normal Java way using say coc.finalValue(); Where coc is the instance you created, and the right of the dot is the method you want.

Once you do that, I think OOP is more of a concept that leads to a style rather than a style itself. The concept is that everything your are dealing with is a "thing" with certain attributes. The usual example is say a class Animal. That class would have methods like findfood() and reproduce() makesound() and fields like int bodyweight int height and so on, all the things you needed for your particular "animal-ness". So the focus is on the Object and its characteristics rather than a bunch of procedures than aren't particularly related to each other. One benefit of this is you can have a classes dog and cat that inherit from animal and in those two classes you would only have to create code that distinguishes them from animals in general rather than start over for every new animal. So if you have a Clan class and you want to Clash several Clans all the clans can inherit from the idea of Clan which would be a class of the characteristics the clans have in common, s. Say shoot move and communicate It's a lot less coding and much easier to think about in the long run. I've done it both ways.

 
sum ding wong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guillermo, I threw the units to another class but i still need to insert static. Also is there a way to pass dynamic enum list to the method argument so i don't need to create 2 methods which have similar instructions?



 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the reason you still need static there. You aren't creating any instances. Static doesn't require an instance, so the IDE doesn't complain.

Put this near the top of main
TrainingCost tc = new TrainingCost();

thereafter you can go like
tc.unitInput();

and unitInput() will no longer have to be declared static.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!