Outlab 1: Writing Your Own Class

Due: Friday 29 January at 6 pm

Purpose

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

  1. Create a BlueJ project called Outlab1, add a class called Driver and paste this code into it. Driver 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.

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

  1. Look through and understand the Driver. It will give you a feel for how your class will be utilized.
  2. 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

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