Ch 6 Lab: Fire Extinguisher
Authors: Ralph Morelli
Trinity College, Hartford, CT
For use with Chapter 6
Brief Description
This is a lab that is suitable for Chapter 6. It requires a
conditional loop. It uses a simple applet interface, including
TextArea and TextField components. This lab requires
students to take more of a role in designing their solution.
It asks that they prepare a design document -- including UML diagrams
and pseudocode descriptions of the key algorithms involved -- before
beginning to code.
Objectives
The objectives of this lab are:
- To give practice designing an object-oriented program.
- To give practice using simple conditional loop constructs.
Problem Statement
Suppose you determine that the fire extinguisher in your kitchen loses
X percent of its foam every day. How long before it drops
below a certain threshold (Y percent), at which point it is no
longer serviceable? For example, if your extinguisher loses 0.1% per
day (X = 0.1) and its serviceable threshold is 86% (Y=86), then it
will no longer be serviceable after approximately 14 days, which equals
approximately 2 weeks. (It won't be exactly 14 days because it loses
1% of it current amount of foam each day. For example,
suppose it has 100 ounces of foam to start with. Then on the first
day it will lose 1% of this, which is 1 ounce, leaving it with 99
ounces. On the second day, it will lose 1% of 99 ounces, which is less
than 1 ounce. And so on.)
Write a Java applet that lets the user input the values X
and Y and then reports how many weeks the fire extinguisher
will last.
Before Lab
Study Chapter 6 of Java, Java, Java, 3E and read this document carefully.
Object Oriented Design
Before creating your Codewarrier project and before beginning to code
your solution, you must hand in, for approval, a design document
containing the elements described below. This should be done
within the first hour of lab, and must be approved before you can
start writing code
GUI Design
Follow the GUI design shown in the demo program. This
involves two input TextFields and an output
TextArea. The TextFields should all
be associated with ActionListeners.
Object Decomposition and Class Design
What objects will your solution use? Follow the design shown on
pages 314-318, which utilizes two objects, an applet that serves as a
user interface and a computational object, which handles the
computation of the fire extinguisher's loss. Let's call the
computational object FireExtinguisher. For each object, draw its
UML class diagram showing its name, instance variables, and its
public and private methods.
One public method that your FireExtinguisher class should
have is something like the calculateLifetime() method. This
is the method that the applet will call to calculate the fire
extinguisher's serviceable lifetime. HINT: What additional
parameters does this method need? What type of value will it return?
Method Design
For this project your applet methods, such as init() and
actionPerformed() can be modeled after the applets you
designed in previous labs. The init() method should
instantiate the GUI components used by the applet and add them
to the applet. The actionPerformed() method should input
the values that the user typed into the TextFields and
pass them to the FireExtinguisher object to perform the
calculation.
For the FireExtinguisher you'll need the
calculateLifetime() method. (This might be all you need for
this problem???)
Algorithm Design
There are two algorithms that you need to think about in detail before
you begin coding. One is the algorithm for the applet's
actionPerformed() method. The other is the algorithm for the
FireExtinguisher object's calculateLifetime()
method. Write out pseudocode descriptions of both of these
algorithms.
Implementation
After your design documents have been approved, you may
create your CodeWarrier project and begin coding your solution. Your code should
closely follow your design.
Use the stepwise refinement approach as you develop your code,
compiling and testing short segments of your code at each stage. Here are some
appropriate steps:
- Create the applet's layout, up through init(). Compile and Test.
- Create the applet's event handling, initially just echoing the user's input in the
TextArea. Compile and Test.
- Create the FireExtinguisher object and test passing a value between the applet and
its calculateLifetime() method. This method can just return the value
it is passed for this stage. Compile and Test.
- Develop and test the algorithm to test the fire extinguisher's lifetime.
Optional
Perform error checking on the user's input values. In particular,
the threshold value must be a number between 1 and 100.
Hand in
Hand in your appropriately documented source code.
You're done. Great work!