Due Date:
Thursday, January 18
File(s) to be submitted:
A01.java, Food.java
Sample Output:
SampleOutput.html
Starter Files:
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.
I have provided you with five (5) files to start you on this assignment.
round
,
which I have completed for you.
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 carbsNote 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
.
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.)
Program A01 has been started for you, but needs to be completed. It must work as follows:
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.)
Important note: thegetXXXContent
methods each call the methodgetNonNegative
. 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.
Important note: the sodium is measured in milligrams (mg), whereas the serving size and carbs are measured in grams (g). There are 1000 milligrams in each gram, so it's possible that the number recorded for sodium might actually be larger than the number recorded for serving size. For example, 200mg is less than 5g, so it's OK if the sodium content is 200 and the serving size is only 5.
The program then pauses (use the method).
It then pauses.
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.