Outlab 1: Writing Your Own Class
Due: Friday 29 January at 6 pm
Purpose
- Create a simple Class on your own
Partners
This is an individual assignment, though collaboration (not solution sharing) is allowed.
Problem Statement
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.
Assignment
- Create a BlueJ project called Outlab1, add a class called Driver and paste this code into it. Driver should not be modified.
- Create a new class called GroceryItem (spelling and capitalization important).
- 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 order):
- 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 item.
- Create the method: getCost() that returns the cost of the item.
- 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.
Output
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").
Hints
- Look through and understand the Driver. It will give you a feel for how your class will be utilized.
- Do not change the Driver. If it will not compile, check that your method names are the same as the assignment directs.
Submission
By Friday 29 January at 6:00 pm, submit the file GroceryItem.java into your lab's appropriate D2L dropbox folder. DO NOT SUBMIT GroceryItem.class, just GroceryItem.java (one file)!
GroceryItem.java can be found in the project folder (Outlab1) you created.
Grading - 20 points
- 3 points - name, cost, and aisleNum are all declared correctly.
- 3 points - The constructor is declared correctly and sets the appropriate variables.
- 6 points - The getName(), getCost(), and getAisle() methods are correct (2 points each).
- 3 points - The changeCost(parameter) method is correct.
- 2 point - GroceryItem.java is commented appropriately.
5 points lost automatically if GroceryItem.java does not compile with the original Driver file.