• 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

Caffeine Calculator

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// Create a simple caffeine calculator
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 10705
86
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
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.
 
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll scrap this as its so bad!
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic