Results 1 to 8 of 8
Thread: Simple adding issue
- 12-21-2012, 01:00 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
Simple adding issue
Hey everyone,
I'm brand new to Java and just started working with it about a week ago, so i apoligize if this is a n00b question.
im working on making an app that keeps track of dailey calorie intake for dieting, im just trying to write the code in Java and not using any android SDK or anything just Eclipse.
basically my program gets user input and then keeps a running tally and outputs that tally ( or thats what its supposed to do ) but all i keep getting for an output for total is 0. I checked to make sure the variables were being assigned correctly by outputting the "getter" for each one so thats not the issue. anyways heres the code im working with im sure that will be a big help
CalorieCounter.java
Day.javaJava Code:import java.util.Scanner; public class CalorieCounter { public static void main(String[] args) { Scanner input = new Scanner(System.in); Day monday = new Day(); System.out.println("How many calories did your lunch have?"); monday.setLunch(input.nextInt()); // System.out.println(monday.getLunch()); System.out.println("How Many calories did your Dinner have?"); monday.setDinner(input.nextInt()); System.out.println("Your total dailey calories so far today are: " + monday.getTotal()); } }
any help on this would be appreciatedJava Code:public class Day { int breakfast, morningSnack, lunch, afternoonSnack, dinner, bedTimeSnack, total; public int getBreakfast() { return breakfast; } public void setBreakfast(int breakfast) { this.breakfast = breakfast; } public int getMorningSnack() { return morningSnack; } public void setMorningSnack(int morningSnack) { this.morningSnack = morningSnack; } public int getLunch() { return lunch; } public void setLunch(int lunch) { this.lunch = lunch; } public int getAfternoonSnack() { return afternoonSnack; } public void setAfternoonSnack(int afternoonSnack) { this.afternoonSnack = afternoonSnack; } public int getDinner() { return dinner; } public void setDinner(int dinner) { this.dinner = dinner; } public int getBedTimeSnack() { return bedTimeSnack; } public void setBedTimeSnack(int bedTimeSnack) { this.bedTimeSnack = bedTimeSnack; } public int getTotal() { return total; } // this is the code im having issue with, im not sure if this is even how i should do it, it was just an attempt // of many. i got it to work when putting it in the main class but i would prefer to keep it in the Day class // to keep less clutter . public void setTotal(int total) { this.total = breakfast + morningSnack + lunch + afternoonSnack + dinner + bedTimeSnack; } }
Thanks
Java N00b
Russ
-
Re: Simple adding issue
Where does your code calculate the total?
Once you've answered that, then answer: where does this code get called?
- 12-21-2012, 01:42 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Simple adding issue
Your intuition is quite correct. The total should not be set by the main() class: it would make no sense for the "caller" - in this case the main() method - to do the work of adding the items together or of "sending" all the individual items again. The job of adding everything together should be the job of the Day class. (I think DayIntake might be a better name, but that's just being picky. A "day" is a thing with a sunrise, sunset etc whereas what's being dealt with here is a specific day's calorie intake.)// this is the code im having issue with, im not sure if this is even how i should do it, it was just an attempt
// of many. i got it to work when putting it in the main class but i would prefer to keep it in the Day class
// to keep less clutter .
public void setTotal(int total) {
this.total = breakfast + morningSnack + lunch + afternoonSnack + dinner + bedTimeSnack;
}
It's also a matter of integrity: we want an accurate calorie count with no lying (by the caller) or arithmetic mistakes (in the Day class). To avoid mistakes the code all goes in the Day class (less clutter, as you suggest). To avoid lying there should be no setter method. The caller says how much they ate at each meal and that's all. The Day class should be solely responsible for figuring out the total. So remove the setTotal() method altogether.
Which leaves us with getTotal(). As things stand total will be zero and that will be returned, which is no good. In fact it should be the getTotal() method that does the sum (total=breakfast+morningSnack+...) and then returns total.
If that makes sense, try it.
- 12-21-2012, 01:43 AM #4
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
Re: Simple adding issue
i am trying to calculate on line 63 and 64 of Day.java, and it is being called by monday.getTotal() on line 18 of CalorieCounter.java , at least thats what my intention is.
- 12-21-2012, 01:47 AM #5
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
Re: Simple adding issue
pbrockway2 - worked perfectly, i didnt think of using the get like that but i will keep that in mind. Thanks again i appreciate the quick response :)
-
Re: Simple adding issue
pbrockway's answer is the correct one, but I'm still just trying to get you to understand your original error (in a longer round about way, admittedly), and that requires that you fully understand your code. Note that getTotal() doesn't do any adding at all, and that the method that does adding is never called. Do you see why your code didn't work now?
- 12-21-2012, 02:04 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Simple adding issue
To elaborate on Fubarable's point there are maybe a couple of morals:
(1) There is no connection between setXXX() and getXXX(). They are just method names and could be anything at all. Carefully defining setXXX(), but then calling getXXX() is not going to fly.
(2) setXXX() and getXXX() typically do more than merely set and get some field in a class. It's a pity that they are introduced by simple examples along the lines of your setBreakfast()/getBreakfast(): their power comes from the other things they might do. The "pattern" you see here is that of providing a getter but no setter which is a nice way of dividing up responsibility. The caller is responsible for the individual values while the Day class, alone, is responsible for the total.
- 12-24-2012, 04:26 AM #8
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
Re: Simple adding issue
i see what he was saying, and i understand what happened. my thought process was that setTotal() was kinda working on its own in there and pulling the variables from the above code. I do understand what Fubarable was doing, i knew why he was asking like that, because it was something obvious i was not seeing i just didnt know what it was. anyways sorry for posting again on this thread i know it will probably bump it up even though its resolved
thanks again for the help going to post another question here in a min so hopefully i can get some input there.
Similar Threads
-
Simple Scanner Issue
By jazzermonty in forum New To JavaReplies: 7Last Post: 07-17-2012, 10:47 PM -
Simple Adding Programme
By ratiug in forum New To JavaReplies: 5Last Post: 04-13-2012, 05:11 AM -
Issue adding elements to an ArrayList while looping through a HashMap
By vb89 in forum New To JavaReplies: 0Last Post: 02-27-2012, 02:39 AM -
Conceptual Issue? Please Help, should be simple.
By justinm231 in forum New To JavaReplies: 1Last Post: 11-18-2011, 10:03 PM -
Issue with adding a JPanel and add a mouselistener to it
By gargamel7 in forum AWT / SwingReplies: 21Last Post: 10-09-2011, 09:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks