Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Caffeine Calculator  RSS feed

 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to build a caffeine calculator that takes input from the user (numbers of cups of coffee drank and time awake)

And then tells the user if they are over the RDA based on how many cups on average they have had.

I've read textbooks and still feel lost! Anyone willing to give any tips?

Thanks!
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?  Post the code you are working on and ask some questions about where you are having problems.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start small.

Do one thing at at time, get it to work, and make SURE it works before you do anything else.

Don't start with a computer, start with pencil and paper.

Coding is 90% THINKING, and 10% typing.

Once you do start writing code, compile often - every 2-3 new lines of code AT MOST.
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

When Fred says 3 lines, that doesn't include blank lines of lines comprising only braces.
 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Create a simple caffeine calculator
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that code doing what you want? 
Otherwise copy and paste any error messages or the program's output and ask any questions you have.
 
Carey Brown
Bartender
Posts: 2992
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "rda" (recommended daily allowance) really variable, or is it a constant?

If it is a constant you should replace
with

Magic numbers like '5' should be avoided because they give you no clue as to what they represent.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another reason to avoid "magic numbers" is that you might want to change the value sometime later, It is very easy to change some of the values but miss some others. And just making a global edit of, say, 12 to 13, can cause other problems since occurrences of the token "12" might exist somewhere else where they have nothing to do with that instance (for example, 12 months in a year, and 12 eggs in a carton).
 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice guys, I was looking to maybe take user input i,e how many cups they've had and use that but its a little tricky.

also thanks for the headsup on encapsulation.
 
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
Would you like to post your improved code here?
 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some elements are not in use yet, I'm still exploring with this.
 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Feeling a lot more confident after more practice, but could do with suggestions as to how to improve my completely now revamped caffeine calculator.
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afraid that doesn't look at all good to me. You have static fields in one class, which would mean all coffee tastes the same. You have fields declared and initialised in another class, which means all coffee tastes the same.
I suggest you find out about instance fields, making sure to give all instance fields private access, so your coffee can have different characteristics from cup to cup.
 
Junilu Lacar
Sheriff
Posts: 11139
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple of conceptual inconsistencies in this most recent iteration of your code that stem from your improper use of inheritance relationships.

The "extends" keyword establishes a specialization-generalization relationship between two classes. For example, Truck-LandVehicle, Car-LandVehicle, Motorcycle-LandVehicle, Boat-WaterVehicle, etc. In other words, you can think of "extends" as being synonymous to "is a kind of", that is, a Truck is a kind of LandVehicle, etc. Caffeine and Coffee do not have this kind of relationship though so declaring that Caffeine extends Coffee doesn't make sense.

Declaring that CoffeeBeans extends Coffee doesn't make sense either.  A list of Cars is not a specialization of Vehicle. Conversely, while a general description of a Vehicle may apply to each individual member/element of a list of vehicles, the generalization does not apply to the list itself. Just as a flock of geese is not a bird or a school of herring is not a fish, it doesn't make sense to say that a list of different kinds of coffee beans is coffee which is essentially what your saying with that declaration.

 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll scrap this as its so bad!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We all write bad code in the beginning of our careers, and hopefully it improves as we go along.

The decision to scrap and rewrite a class or patch it is a difficult one.  I usually say, If there are structural problems with the class, rewrite.  And sometimes there's a psychological boost of just starting clean.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's almost a fixed feature of programming that you will throw away your first attempt at implementing something. That's just part of the learning process.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!