CS 110: Program 3
Student Learning Objectives
- Demonstrate the ability to write a Java user-defined class:
- Use private instance variables and public methods in your user-defined class
- Write a constructor method that accepts arguments
- Write getter methods that return a value
- Use objects and method calls in your test program
- Use of jGRASP to type in, compile, and execute a simple Java program
- Use formatting and comments to produce readable programs.
- Include a file header comment to identify the author, account, and honor code
- Browse through the Java Style Guide for other standards and conventions to use in CS 110.
Program 3 Assignment
Write a class named
Item that holds data about an item in a retail store.
- The class should have the following three fields:
- name: the name field is a
String object that holds the name of the item.
- price: the price field is a
double variable that holds the item's retail price
- quantity: the quantity field is an
int variable that holds the number of units currently in inventory
- Write a public constructor method that accepts three arguments, name, price, & quantity and stores the values of the arguments passed into it in the object's instance fields
- Write four public methods to retrieve the values from the three fields and their current inventory value
String getName( )
double getPrice( )
int getQuantity( )
double getValue( ) that returns the current inventory value (quantity * price)
Write a separate class called
Inventory with a main method that creates three
Item objects and then produces a neatly formatted table of the store's inventory displaying the three items, their current inventory value, and the total inventory value for the store.
Duplicate the format of the output exactly. Test your output with different items in inventory.
Hint: the tab character (
\t) is used for alignment in the table.
File Header Comment
Include a file header comment at the top of both source code files. Include the assignment number, your cs110 account number, your name, completion time, and the honor code.
Remember, program assignments are to be done on your own. If you receive help understanding or debugging the assignment, you must acknowledge the person who helped. You must do the coding on your own.
// Assignment: Program 3
// Account: (Enter your cs110 account number here)
// Author: (Enter your full name here)
// Completion time: (Enter the total number of hours you
// spent on the total assignment)
// Honor Code: I pledge that this program represents my own
// program code. I received help from (enter the names of
// others that helped with the assignment, write no one if
// you received no help) in understanding and debugging my program.
Add inline comments for every three to five lines of code. Skip a blank line before every inline comment.
Optional Extra Credit
In addition to the jGRASP console output produced above, add the code to also display a
JOptionPane with the inventory.
Duplicate the format of the JOptionPane exactly. Test your output with different items in inventory
Note: For extra credit, your program should produce both sets of output, the neatly formatted table produced with System.out.println statements and the window output produced by the JOptionPane.
What to turn in at the start of the next lab
- Be sure your project is saved in your
U:\programs\program3\ folder in your CS 110 account.
Inventory.java should be saved in program3 folder
- Both files should begin with a file header comment
- Note: if you complete the assignment on your personal computer, you must copy the file(s) to your your CS network account
- An easy way to copy the files is to use a USB portable drive, email, or a digital drop box
- Instructions for accessing your U-Drive remotely is provided if you are unable to physically come into the Hebeler lab to copy the files
- Your program will be checked online. You do not need to print out a copy of your program to turn in.
- The assignment is due at the start of Lab 6.
- If you are unable to finish in time, you may turn in what you have done to receive partial credit for the assignment.
- You are allowed to drop one program assignment score for the quarter
20 points possible plus 2 points extra credit
CS 110 Program 3 Grading Sheet (PDF)
- 10 points - Output Correctness
- The program produces the neatly formatted inventory table with correct values (-1 per error, 4 points total)
- The total inventory value is correctly calculated and displayed (2 points possible)
- Duplicate the sample output exactly; do not make up your own words or formatting. (4 points possible)
- Include the heading line "Name Price ..." (-1 if incorrect)
- Include the divider line "==== ..." (-1 if missing or incorrectly sized)
- Use tab escape sequences (\t) to left-align table columns (-1 if incorrect)
- Total Inventory is displayed correctly (-1 if incorrect)
- 3 points - Class Definition
Item class is defined correctly with
- Three fields (instance variables) declared as
private and the correct type (1 point possible)
- Do not use a fourth field to contain the inventory value. This should be calculated in your getValue() method
- Constructor and methods declared as
public (1 point possible)
Inventory class main method uses the
Item class methods correctly (1 point possible)
- 3 points - Comments and Alignment
- File header comments are present and complete (2 points possible)
- Both Item.java and Inventory.java need complete file header comments (1 point each)
- Single-line comments are present, helpful, and sufficient (1 point possible)
- Skip a blank line before every single-line comment
- Generally one single-line comment is needed for every four to six lines of code
- Use jGRASP Generate CSD to properly align your printout
- Alignment and indentation are important. Read through the Java Style Guide for CS 110 expectations
- 2 points - Identifier Names
- Meaningful identifier names used for all identifiers (-1 per problem, 2 points possible)
- Variable and method names should begin with a lower case letter, and each subsequent word should start with an upper-case
- Class names should begin with an upper case letter, and each subsequent word should start with an upper-case letter.
- Avoid the use of abbreviations in choosing variable names
- Read through the Java Style Guide for CS 110 expectations.
- 2 points - Creation of Assignment Folder and Files
- Program files Item.java and Inventory.java created correctly and saved in
U:\programs\program3\ (2 points possible)
- +2 points - Extra Credit
- JOptionPane output and formatting is correct (-1 per error, 2 points possible)
- Your program should produce both the jGRASP console output table as well as the JOptionPane output