A01

Due Date: Thursday, January 18
File(s) to be submitted: A01.java, Food.java
Sample Output: SampleOutput.html
Starter Files:


SUBMIT   /   Check

Nutrition Data (Review of CSCI 1226)

Summary

Complete the data class named Food to track some nutrtional data for foods. Have it record the name of the food, the standard serving size (in grams), the number of milligrams of sodium and the number of grams of carbohydrates in one serving.

Use the program TestFood to make sure your Food class is working properly.

Complete the program (A01) to read several foodstuffs into an array, then print them out, then list all low-sodium and low-carb foods.

Make sure that your program follows all style guidelines.

I have provided sample data for you to use with your A01 program.

Details

I have provided you with five (5) files to start you on this assignment.

Food

The Food class keeps track of a kind of food and some of its nutritional information. It tracks the food's name, serving size (in grams), amount of sodium per serving (in milligrams), and the amount of carbohydrates per serving (in grams). The name may change, but the other amounts are fixed (will not change after the object has been created).

The class needs only one constructor (which is given all the required information), a getter for each property (including the ones that don't change), one setter (the name can be changed), a toString method, and a few testing methods (for checking the validity of the name and the amounts).

I have provided stubs for all those methods. Your job is to complete the method definitions according to the documentation provided in the file.

Note that the constructor and setter have a throws clause in the documentation. If the client tries to create a Food item with invalid data, then an exception gets thrown. We covered exceptions in csci1228 (not in csci1226), so you might not expect them here. Nevertheless, I want your methods to throw exceptions when the client tries to do something invalid.

The toString method produces a String with all the information. For example:

Peanut Butter (15g): 17mg sodium; 7g carbs
Note that the sizes are rounded off to the nearest integer value when they're printed out here; but the getter methods (when used) should return exact values.

To help you out with the toString method, I have provided a method called round that rounds a double value off to the nearest int.

TestFood

This program is complete and should not be changed. (You may comment out parts of the program as you complete the testing, but you should remember to run a full test once you have completed the Food class.)

A01

Program A01 has been started for you, but needs to be completed. It must work as follows:

  1. It prints an introduction including a program title, a short description, and the author's name and student number. (Complete the printIntroduction method.)

    It then pauses. (Prompt the user to press the enter key, and wait for them to do so. This method has been provided for you.)

  2. Reads in the Food data. (Complete the getFoods method.) It proceeds in the following way:
    1. It asks the user how many food items they have. (Complete the readgetNumFoods method.)
    2. It creates an array of the appropriate size.
    3. It reads food items into the array. For each food item, it reads in the name (complete the getFoodName method), the serving size (complete the getServingSize method), the sodium content (complete the getSodiumContent method), and the carb content (complete the getCarbContent method).
      Important note: the getXXXContent methods each call the method getNonNegative. All you need to do there is fill in an appropriate prompt.

      You will need to complete the getNonNegative method, however.

      The program then pauses (use the method).

    The methods for reading in Food data each check their input. In each case an attempt by the user to enter inappropriate data is rejected by the method. It continues to prompt for a valid value until the user provides it with one.

    The program then pauses (use the method).

  3. The program prints back the array of foods. The list is to be printed one item per line, indented four spaces. (Complete the printList method.)

    It then pauses.

  4. The program then finds and prints the Foods with the lowest sodium and carbohydrate levels. It does this by (Complete the showLowSodium and showLowCarbs methods.)

    The program pauses one last time. (Use the method.)

Make sure your code complies with all style guidelines for this course. If you don't know the style guidelines, then review them.


SUBMIT   /   Check