This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Objects Questions  RSS feed

 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've been doing some tutorials and had a few questions about some things I am learning.



I'm not sure where to draw up the boundaries on what my classes do as opposed to my main method.  The main thing I am trying to achieve is a good basic understanding of OOP.

1. How would I alter this program to return a calculation (e.g. "rating" is temp divided by 10.)

2. What other things can I do with my methods? Is there a better way to organise this?

3. Any suggestions on how I can improve this code?

 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by making all fields in the drink class private. Then create one constructor which sets those fields to sensible values. You should be able to write
Drink icedTea = new Drink(1, 15, "Iced tea", "Kitchen table"); // °C presumed
or similar. You shou‍ld never be able to gain access to variables from outside the class. Provide methods if required to access individual fields.

Override the toString method; you can then write
System.out.println(icedTea);
or similar and get a sensible output on screen.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And repost your code after you're done!  We want to see how you progress.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!