Results 1 to 8 of 8
  1. #1
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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
    Java 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());
    		}
    }
    Day.java
    Java 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;
    	}
       
    }
    any help on this would be appreciated

    Thanks
    Java N00b
    Russ

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple adding issue

    Where does your code calculate the total?

    Once you've answered that, then answer: where does this code get called?

  3. #3
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Simple adding issue

    // 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;
    }
    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.)

    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.

  4. #4
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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.

  5. #5
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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 :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple adding issue

    Quote Originally Posted by Russd772 View Post
    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.
    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?

  7. #7
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default 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.

  8. #8
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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

  1. Simple Scanner Issue
    By jazzermonty in forum New To Java
    Replies: 7
    Last Post: 07-17-2012, 11:47 PM
  2. Simple Adding Programme
    By ratiug in forum New To Java
    Replies: 5
    Last Post: 04-13-2012, 06:11 AM
  3. Replies: 0
    Last Post: 02-27-2012, 03:39 AM
  4. Conceptual Issue? Please Help, should be simple.
    By justinm231 in forum New To Java
    Replies: 1
    Last Post: 11-18-2011, 11:03 PM
  5. Replies: 21
    Last Post: 10-09-2011, 10:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •