Outlab 1: Writing Your Own Class

Due: Tuesday 05 July at 10 pm



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.


  1. 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.
  2. Create a new class called GroceryItem (spelling and capitalization important).
  3. 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").
  4. Create a constructor that takes the following parameters (in order):
    1. The item's name
    2. The item's cost
    3. The aisle number where the item is located
  5. Create the method: getName() that returns the name of the item.
  6. Create the method: getCost() that returns the cost of the item.
  7. Create the method: getAisle() that returns the aisle number it is located in.
  8. 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").


  1. Review knowleges in data type
  2. Look through to understand the code in the Driver class. It will give you a feel for how your class will be utilized.
  3. Do not change the Driver. If the Driver class does not compile, check that if your method names are the same as the task directs.


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 created.

Grading - 20 points

5 points lost automatically if GroceryItem.java does not compile with the original Driver class.