CST141 Project 5: Chapter 12
ArrayIndexOutOfBounds exercise
--or--
CupOfCoffeeClass class
-
Complete Exercise 12.3 exactly as specified in the textbook with the following modifications
(this is a partial-credit 7 point assignment):
-
Use either a
Scanner
object or JOptionPane.showInputDialog()
for input
-
Use either
System.out.println()
or JOptionPane.showMessageDialog()
for output
-
In addition to ArrayIndexOutOfBoundsException, also catch
InputMismatchException or NumberFormatException,
and Exception
-
This assignment is completed by writing a single application (.java file) with
a
main()
method that includes all the elements
-
Create a public class CupOfCoffee with three private data fields
(this is the full-credit 10 point version):
-
A
String
which is coffeeType with valid values
values "Regular", "Decaf", "Latte", "Espresso" and "Cappuccino" (default
is "Regular")
-
An
int
which is ounces, the number of ounces in
the cup (default is 10)
-
An
int
which is temperature, the temperature in
Fahrenheit of the coffee (default is 150)
-
Additionally define a
static
array validCoffeeTypes[]
of type String
with element values "Regular",
"Decaf", "Latte", "Espresso" and "Cappuccino"
-
Also define two
public static int
constants:
(1) TOO_COLD initialized to 150; and (2) TOO_HOT
initialized to 185
-
Create two constructors, the first that takes no parameters and passes
default values to the other constructor that takes three parameters,
one for each of the three instance variables, and passes them to the
set methods for the data fields
-
The set method for the 'coffeeType' field accesses an
element from the String array using the passed
int
parameter and updates the field; it throws an ArrayIndexOutOfBoundsException;
no if
statements are allowed in this method (you do not
need them)
-
The set method for the 'ounces' field validates that
the
int
parameter is a positive number; it throws a
NotPositiveNumberException (defined below) if
the parameter is negative or zero
-
The set method for the 'temperature' field tests the
int
parameter against the TOO_COLD and TOO_HOT constants;
it throws a TooColdException (defined below) if the parameter
is less than the related constant or a TooHotException (also
defined below) if
the parameter is greater than the related constant
- Write get methods for all three fields
-
Write a
static
method of type String
named
getValidCoffeeTypes()
that returns a numbered
list of the coffee types from the String
array; each item
number and coffee name appears on a separate line and could be displayed
as a coffee menu in a JOptionPane.showInputDialog()
as in the
image below (this also would work for a Scanner
object)
-
Write a method toString
()
of type String
which is a String representation of the object fields each on a separate
line with appropriate labels.
-
Create three exception classes, TooColdException, TooHotException,
and NotPositiveNumberException each with two constructors, one that
passes a hard-coded message to its superclass Exception constructor,
and the other that takes a String parameter and passes it to the superclass
constructor.
-
Create a driver class with inputs for the three fields using
either a
Scanner
object or method JOptionPane.showInputDialog()
.
The prompt for 'coffeeType' should display the return value from the
CupOfCoffee.getValidCoffeeTypes()
method (e.g. see the
prompt in the showInputDialog()
above if you choose to
use GUI input). After the three inputs instantiate an object from
class CupOfCoffee and then display the return value from its method
toString()
using either method System.out.println()
or method JOptionPane.showMessageDialog()
. The driver
application handles the following exceptions: InputMismatchException
or NumberFormatException, ArrayIndexOutOfBoundsException, the
programmer-defined exceptions TooColdException, TooHotException and
NotPositiveNumberException, and Exception. Display the return
value of method getMessage()
for each exception object.
-
There are two alternative versions of the CupOfCoffee assignment for
reduced credit (both of which eliminate the CupOfCoffee class and well as the
three programmer-defined exception classes):
-
Eliminate the CupOfCoffee
class and write a single application
(.java file) with a
main()
method that includes inputs for int
variables ounces and
temperature (eliminate the coffeeType variable)
using either a Scanner
object or method JOptionPane.showInputDialog()
.
Include try
...catch
processing with exception handling
for InputMismatchException or NumberFormatException and Exception.
Include a termination message within the finally
block.
Display all output including that for valid input and/or exception messages using either
System.out.println()
or JOptionPane.showMessageDialog()
.
(For a maximum of 8 points)
-
Eliminate the CupOfCoffee
class as above writing a single application (.java file)
with a
main()
method that includes all the elements of the 8 point version
but additionally include the coffeeType variable. Specifically input
int
variable coffeeIndex using either the Scanner
object or method JOptionPane.showInputDialog()
Define a String
array validCoffeeTypes[]
with values "Regular",
"Decaf", "Latte", "Espresso" and "Cappuccino".
Display one of the strings from the 'validCoffeeTypes' array using the input variable
coffeeIndex. Handle an ArrayIndexOutOfBoundsException for an invalid 'coffeeIndex'
and display an exception message. (For a maximum of 9 points)
-
Document the program using the Javadoc standard by:
-
Including a substantive comment statement that describes each class's
purpose, as well as substituting your name for the existing
@author
comment and the assignment due date for the @version
comment that already exist
at the top of new classes
-
Entering a substantive comment before each method (including the
constructor) to describe its function along with
@param
, @return
and @throws
tags
-
Draw the UML diagram for the CupOfCoffee class if you do the ten (10)
point assignment; it is suggested that you do this before you begin
to create the classes as this should make the coding much easier.
-
Students may work on all projects in teams of two or three,
and, if so, all students in the group must submit the same project to their own dropboxes
in Blackboard. In the text box's dropbox, as well as in the source code documentation
(comments), state the name(s) of the classmate(s) with whom you worked.
-
The completed project with a copy of the all five class files (the *.java files)
CupOfCoffee, TooColdException, TooHotException, NotPositiveNumberException and
the "Main.java" class file with the
main()
driver method (for the full credit version) or a single
file (for the 7 or 8 or 9 point
versions) submitted electronically via attachments to a
Blackboard
dropbox is due to Prof. Struck by 11:59 p.m. on the date specified in the course outline.