Fantasy Food Class
CS 160 In-Lab 3
September 17, 2007
The
purpose of this laboratory is to make sure that you can create and use a simple
class within the BlueJ environment.
Problem Description
Activate the BlueJ environment. Within the
environment, create a new project called inlab3. Add a new class to the
project called Food. The Food class should contain the following
information.
- An instance variable called name that can store the name of the food.
- An instance variable called calories that can store the amount of a calories.
- A constructor that takes no parameters. The constructor should set the
name of the food to "unknown" and the amount of calories to 0.
- A method called setName that takes a String parameter. The method
should assign the value of the parameter to the name instance variable.
- A method called setCalories that takes an integer parameter. The method
should assign the value of the parameter to the calories instance variable.
- A method called print that prints out both the name and
calories in a readable format.
Creating Instances by Hand
Once the above Food class is
finished, create two instances manually (by right clicking on the class). Call
the first instance food1 and fill in the name with doughnuts and the
calories to 5. Call the second instance food2 and fill in the name
with rice cake and the calories with 488. (It's a fantasy program).
Once the two instances are created, call over either your lab TA or a
consultant. He will call the print method on each of the two instances to
see whether everything has been set up correctly. He or she will record whether
you have done this part correctly.
Creating Instances by Code
Add another class to the project
called Driver. Fill in the public static void main method with
code so that it (1) creates the doughnut instance above and prints it and
(2) creates the rice cake instance above and prints it.
When you are finished with this part, submit it to
your lab TA before you leave.
Before Leaving Lab
Delete your inlab3 project so that
students in the next lab section will not have a head start on the assignment.
Remember to empty the recycle bin, too. Thanks!