Assignment : Nutrition Analyzer

Assignment Setup

To create your repository go here. Then follow the same accept/import process described in Assignment 0.

Nutrition Analyzer

In this assignment you develop a tool that prints nutrition information. By completing this assignment you demonstrate that you can:

  • Create a Java class on your own
  • Arrange for the class to take inputs of interest
  • Compute output values of interest
  • Produce meaningful output based on your computations
  • Work with Java expressions

Procedure

  1. Create a Nutrition Java class in the assignment01 package (assignment01 is in the src folder).
    1. The assignment01 package is currently empty, but don’t let that bother you. Right (control) click on assignment01 and create a new class.
    2. Type the name Nutrition in the Name: field (please use Nutrition, capitalized and spelled correctly)
    3. Click the box that says public static void main(String[] args)
    4. Click the Finish button, which will create the new file
  2. Arrange for your program to accept the following inputs:

    Variable Value
    name The name of this food, provided as a String
    carbs The number of grams of carbohydrates in this food
    fat The number of grams of fat in this food
    protein The number of grams of protein in this food
    statedCals The number of calories stated on this food's label

    For example, the values for a Snicker bar are:

    Variable Value
    name Snickers
    carbs 34.5
    fat 13.6
    protein 4.3
    statedCals 271

    You should be thinking about the data types you would use to represent each of the above items. We tell you that the name is a String, but what about the other inputs? The values given in the Snicker bar example are provided to inspire your thinking. To accept the inputs use the same code we used in studio.
    Just after the main method declaration, you should have the line:

    ArgsProcessor ap = new ArgsProcessor(args);

    It it shows an error it can be fixed via the approach described in Studio 1.

    After that, you should initialize your inputs using code modeled after the code you read and wrote in [studio] (studio.html “studio 01”) — see the section on Average.

    Eclipse will help you out here: If you have typed ap. and Eclipse will show you the possible ways of continuing. For example, nextInt(String) is one way to keep going. That call will prompt the user for an int displaying the provided String as the prompting message.

  3. Arrange for your program to produce output such as the following:
Snickers has
34.5 grams of carbohydrates = 138.0 Calories
13.6 grams of fat = 122.4 Calories
4.3 grams of protein = 17.2 Calories

This food is said to have 271 (available) Calories.
With 6.6 unavailable Calories, this food has 1.65 grams of fiber

Approximately
  50.9% of your food is carbohydrates
  45.2% of your food is fat
  6.3% of your food is protein

This food is acceptable for a low-carb diet?  false
This food is acceptable for a low-fat diet?  false
By coin flip, you should eat this food?   true

Notes

  • Don’t try to write this all at once. Instead, build one piece at a time and test it. This will make it easier to figure out what’s going wrong when your program doesn’t work as you expect. (This is called incremental development and this approach will serve you well all semester).

  • Here’s some data you’ll need for your computations:
    • There are 4 Calories in a gram of carbohydrates or protein.
    • There are 9 Calories in a gram of fat.

  • The stated caloric content of some foods may be less than the value you obtain by adding the calories due to carbohydrates, fat, and protein. This is because some of the carbohydrates may be dietary fiber, and as such they are unavailable for your body to use as energy. In the above example, the snickers bar contains 277.6 Calories based on carbohydrates, fat, and protein.However, the label claims a modest 271 Calories. Thus, 6.6 Calories are unavailable and are attributed to dietary fiber.

From this we can compute 6.6 / 4 = 1.65 grams of the stated carbohydrates are fiber.


  • The percentages shown in the output above feature at most a single digit of precision after the decimal point. You print percentages in exactly this format. It’ll take some thought, but it can be done using things that have been covered: int operations, double operations, casting, and Math.round(..). (You are not allowed to use printf or other functions you may find that would accomplish this task. You have to figure out how to do it using what you have learned in this module.)

  • The following are taken from this online resource:
    • A food is a low-carb food if no more than 25% of its calories come from carbohydrates.
    • A food is a low-fat food if no more than 15% of its calories come from fat. For the output lines whose conclusions are true or false, define an appropriately named boolean variable in your program and set it equal to the expression that determines its value. When you print the value it will print as true or false.

  • Review how the the random-number generator, Math.random() works. Define a boolean heads and assign its value by comparing a random number with 0.5.
  • Your strings can include the character sequences \t and \n, which have the effect of inserting a tab and a newline, respectively. A tab will cause the output to shift right to a predetermined position. A newline will end the current line of output and begin a new line.

Prepare for demoing your work by finding some food labels (real labels or on the web) and run your program with the appropriate values for those foods.

Submitting your work

This assignment is scheduled to be due on a “studio day”. We would really like to spend most of class on the Studio activity. If at all possible, please demo the assignment during Tuesday class sessions (11:30-2:20pm in the Urbauer Labs). This week you can demo on Tuesday even if it isn’t your normal class. You may also demo during TA office hours Tuesday. (If the assignment isn’t due until Thursday, you may also demo during office hours Wednesday night without a penalty)

To submit your work come to office hours or class on an “Assignment day” and sign up for a demo via 131list.com.