Outlab 1: Writing Your Own Class
Due: Tuesday 05 July at 10 pm
- Create a simple Class on your own
This is an individual assignment, though collaboration (not
solution sharing) is allowed.
You have been contracted by a local grocery store to develop a
software system to hold information about items in the store. You
will create a new class called GroceryItem that will be
able to represent different items for sale in the store. These
items have a name, cost and aisle number it is located at in the
store. You will also need to make a "get method" for each instance
variable as well a constructor that takes the name, cost, and
aisle number as parameters. Finally, there must also be a method
that allows the user to change the cost of an item.
- Create a BlueJ project called Outlab1, create a class
called Driver and type this code
into the Driver class.The code in Driver class should not be modified.
- Create a new class called GroceryItem (spelling and
- Within GroceryItem, declare an instance variable
called name that can contain a value such as "Eggs", an
instance variable called cost that can contain a value
such as "1.21", and an instance variable called aisleNum
that can contain a value such as "5" (but not "5.2").
- Create a constructor that takes the following parameters (in
- The item's name
- The item's cost
- The aisle number where the item is located
- Create the method: getName() that returns the name of the
- Create the method: getCost() that returns the cost of the
- Create the method: getAisle() that returns the aisle number it
is located in.
- Create the method: changeCost(parameter) where "parameter" is
the dataType and variable name of the new cost for that item.
Nothing is returned and the cost is updated.
If GroceryItem has been implemented correctly, this output will be produced when the main method
of Driver is run (right click Driver, select "void
main(String args)", select "Ok").
- Review knowleges in data type
- Look through to understand the code in the Driver class. It will give
you a feel for how your class will be utilized.
- Do not change the Driver. If the Driver class does not compile,
check that if your method names are the same as the task
By Tuesday 05 July at 10:00 pm, submit the file
GroceryItem.java into your D2L dropbox folder. DO NOT SUBMIT
GroceryItem.class, just GroceryItem.java (one file)!
GroceryItem.java can be found in the project folder (Outlab1) you
Grading - 20 points
5 points lost automatically if GroceryItem.java does not compile
with the original Driver class.
- 3 points - name, cost, and aisleNum
are all declared correctly.
- 4 points - The constructor is declared correctly and sets the
- 6 points - The getName(), getCost(), and getAisle() methods
are correct (2 points each).
- 4 points - The changeCost(parameter) method is correct.
- 3 points - GroceryItem.java is commented appropriately.