Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Homework Help Java Beginner

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    36
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Homework Help Java Beginner

    Hello,

    I need some help with my java code about a calorie tracker. Basically what the program should do is have four items with their calorie totals. Then the program should display the highest calories, the name of the menu item with the highest calories, and display the average of all four item calories. What I'm having trouble with is the addToCounter part of the code. I can't get the values to store in the instance variables defined earlier in the code as private and values of zeros and empty string. Here is my code so far.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Homework Help Java Beginner

    I'm confused. What is wrong with the addToCounter() method? I am noticing that you are pretty much ignoring the values of the parameters you pass into the method (since you are setting their values in the method without ever reading them first).
    Is the problem that you don't understand how to use a mutator method in general?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    36
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Homework Help Java Beginner

    Yeah I'm not understanding what is supposed to be done in the mutator method. How am I supposed to go about adding these items in because this is where I'm lost. I have no idea what to do within mutator method (addToCounter) I know I have to add the items in just don't know how. This was basically an attempt of me failing to do so.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Homework Help Java Beginner

    Ok, well first of all, take out the code in your addToCounter() method where you are setting the parameter values:
    calories = 900;
    // and
    itemName = "Subway BMT Italian Sandwich";
    // and 
    itemName=nameOfHighest;
    The values of the parameters are determined when the mutator method is called, you do not need to (and you should not) set them in the method itself.
    The values of the parameters are used to set your instance variables. So, instead of hardcoding the name, make this change:
    // Change this:
    nameOfHighest = "Subway BMT Italian Sandwich";
    // Into this:
    nameOfHighest = itemName;
    // And change this:
    totalCalories = 900;
    // Into this:
    totalCalories = calories;
    Now, in your main, we call the addToCounter() method and pass it the variables by saying:
    counter1.addToCounter("Subway BMT Italian Sandwich",900);
    This call passes the desired parameter variables to the addToCounter() method.
    After we have all of that sorted out, we can continue from there.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Homework Help Java Beginner

    Ok, very good. Now, let's look at your requirements for this method:
    The addToCounter method will add calories of the added item to the totalCalories and increment the countOfItems for the instance of CalorieCounter. It will also compare the value of the item passed into the method with the current caloriesOfHighest. If the added itme has greater calories than the current highest item it must update the variables nameOfHighest and caloriesOfHighest. (Note that the first item added will automatically be the highest.)
    So our goals for this method are:
    1. Add calories to the totalCalories instance variable
    2. Increment the count of items instance variable
    3. Adjust the highest values if need be

    You already did #2 with this line: countOfItems++;
    So now we just have to worry about #1 and #3.
    Currently, your code just sets the totalCalories variable to the value you passed in. According to your requirements, you need to add the passed in calories to your totalCalories instance variable. This is simple enough to accomplish. Depending on preference either of these two lines would work:
    // you can do it this way:
    totalCalories += calories;
    // or this way:
    totalCalories = totalCalories + calories
    // the two lines of code do the exact same thing
    So you would want to replace your totalCalories = calories; line with one of the above. This covers goal #1.
    Now for the hardest part of this method: goal #3. I will explain to you how to do it, and you can try to code this part on your own. Your requirements tell you to compare the value of the current highest with the value of the passed in item. This is telling you that you should compare the caloriesOfHighest instance variable with the calories parameter variable. If the value of calories is larger, you want to set caloriesOfHighest to be equal to calories AND you want to set the nameOfHighest instance variable to be equal to the itemName parameter variable. If the value of calories is smaller, you don't have to do anything.
    Your requirements also mention one more condition: the first item added will automatically be the highest. This means that if the item that was passed into the method is the first item, you want to set caloriesOfHighest to be equal to calories and you want to set the nameOfHighest instance variable to be equal to the itemName parameter variable. The question you should ask here is: but how do I know it is the first item? There are a few ways you can do this. You can check if the value of the totalCalories instance variable is 0 (before adding the value of calories to it). If it is 0, this indicates that no items have been added yet. Alternatively, you can check the value of your nameOfHighest instance variable. If it is set to " ", this indicates that no items have been added yet (see my NOTE below regarding default values of Strings). There are a handful of ways you can check this.
    So, considering all of your requirements for updating the largest value, here is the order you want to do them:
    1. Check if no items have been added yet. If no items have been added, set the highest values to the ones provided by the method parameters.
    2. Check if the calories parameter variable is larger than the caloriesOfHighest instance variable. If the calories parameter variable is larger, set the highest values to the ones provided by the method parameters.

    Try to see how much of that you can do on your own, and comment if you have any trouble.

    NOTE: I noticed you set the nameOfHighest instance variable's default value to " " (you did this in the constructor). While there is nothing wrong with that, normal java convention suggests you should set the default value of Strings to be either: null or "". Notice the second one: "", is known as: empty String. The only difference between that and what you have now is that your default value has a space. The reason empty String is used instead of " " is mainly because of the String.length() method. In the case of an empty String, this method will return: 0. However, if you add the space, it has a value of: 1. Most professionally developed programs would probably trim away any of the leading and following spaces anyway, but that extra space could give the false impression that the String has some value other than the conventional default value. This is just something to consider when programming. Like I said, this is not something that would be considered "wrong" with your program, and it will not change how the program operates, but it is a generally frowned upon practice.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Edmacelroy (October 21st, 2013)

  7. #6
    Member
    Join Date
    Sep 2013
    Posts
    36
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Homework Help Java Beginner

    ok, here is what i have got done so far. I tried to get the name of the highest calorie item to display but still get the last item displayed. Everything else seems to be working, still struggling a little bit with this part but looking into it more.

    Here is the code now

  8. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,543
    Thanks
    24
    Thanked 298 Times in 281 Posts

    Default Re: Homework Help Java Beginner

    You know, since you are setting caloriesOfHighest to 0 initially, and we can assume you will never have an item with negative calories, your first if statement would actually cover the situation where it is the first item. This is because: if caloriesOfHighest has never been set, then calories will obviously be larger.
    So, you can actually get rid of the two if statements after your comment where you say you are unsure how to display the highest calorie item.
    Now, as for why it doesn't work: that is because you are setting the nameOfHighest and caloriesOfHighest every time (before checking).
    Look at my comments below on your code:
    public void addToCounter(String itemName, int calories)
       {
     
          nameOfHighest = itemName; // Remove this
          totalCalories += calories;
          caloriesOfHighest = calories; // Remove this
          countOfItems++;
     
          if (calories > caloriesOfHighest)
          {
             caloriesOfHighest = calories;
             nameOfHighest = itemName;
          }  
     
          // Remove everything from here...
          if(nameOfHighest.equals(""))
          {
              caloriesOfHighest = calories;
             nameOfHighest = itemName;
          }   
     
         if(totalCalories == 0)
          {
           caloriesOfHighest = calories;
             nameOfHighest = itemName;
          }
        // ... to here
        }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Edmacelroy (October 21st, 2013)

  10. #8
    Member
    Join Date
    Sep 2013
    Posts
    36
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Homework Help Java Beginner

    This code seems to work well

    Now I really just have to study this code because I did so much wrong. I can't thank you enough for all of your help and taking the time to walk me through this so patiently and explaining everything to me. This will definitely be a reference for me as my studies progress.

Similar Threads

  1. Beginner struggling with homework question
    By mets3214 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 6th, 2013, 08:10 PM
  2. Help with Java homework
    By BoDuke1835 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2013, 08:39 AM
  3. Can you someone help me with my homework for java... Please
    By surfelijo in forum What's Wrong With My Code?
    Replies: 15
    Last Post: February 25th, 2013, 09:01 PM
  4. Replies: 8
    Last Post: February 12th, 2013, 04:45 AM
  5. Homework assignment (beginner)
    By z3ohyr84 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 30th, 2011, 01:32 PM