Write a program that allows the user to calculate his or her running pace, given the numbers of miles run and the time. The user should be able to input total running time in terms of hours, minutes and seconds. The user should be able to input total miles run in terms of miles or kilometers. Upon user's request, the program should report the runner's pace in minutes per mile.
The RunnersCalculator Applet.The user interface for this program should be a Java applet.
Problem Decomposition. Your program should consist of two classes, the RunnersCalculatorApplet, which serves as the user interface, and the RunnersCalculator class, which performs the calculations. The purpose of the applet is just to get the user's input and to display the results of calculations, which are done by the RunnersCalculator object.
GUI Design. The applet should use JLabels to prompt the user for the various kinds of input needed. It should use JTextFields for the user's input (miles, kilometers, hours, minutes, and seconds). The results of the calculations should be displayed in a JTextArea. JButtons should be used to control the calculation and to convert kilometers to miles. The JButtons should use the applet itself as their ActionListener.
Design of The RunnersCalculator Class. The RunnersCalculator class represents the computational object for this program. It contains variables and methods needed to perform the necessary calculations. The following UML diagram provides the details of its design:
| RunnersCalculator |
|
- hours: int
- minutes: int - seconds: int - miles: double |
|
+ RunnersCalculator(h:int, mins:int, secs:int, mi:double)
+ minsPerMile(): double + static kmToMiles(km:double): double |
Note that the kmToMiles() method is declared static. This is like methods in the Math class and means that this method can be called using the class as a reference rather than using an object as a reference--e.g., RunnersCalculator.kmToMiles(10). This is a useful part of the design because it allows you to use that method without having to create a RunnersCalculator object.
Design of the GUI Classes. The GUI classes should be modeled after the GUI design described in Chapter 4. It will require two Java classes, the RunnersCalculatorApplet class and the RunnerCalcGUIPanel class. Review Section 4.4.10.
The source code for the Applet class is very simple and is given here:
import javax.swing.*;
public class RunnersCalculatorApplet extends JApplet
{ public void init()
{ getContentPane().add(new RunnerCalcGUIPanel());
}
}
The following UML diagram provides the design details for the RunnerCalcGUIPanel class:
| RunnerCalcGUIPanel |
|
- display: JTextArea
- hoursField, minutesField, secondsField: JTextField - milesField: JTextField - minutesPerMileButton, kmToMilesButton: JButton - calculator: RunnersCalculator |
|
+ RunnerCalcGUIPanel()
+ buildGUI(): + actinPerformed(e:ActionEvent) |
Recall that this class must be a subclass of JPanel and must implement the ActionListener interface.
Note: One difference between this class and the GreeterGUIPanel described in the text is that in this case you cannot instantiate the RunnersCalculator object (the computational object) in the buildGUI(). Instead you must wait until you get the user's inputs to use as arguments in the RunnersCalculator() constructor.
Algorithm Design. In the actionPerformed() method, it will be necessary to distinguish between a click on the Minutes Per Mile button and click on the Convert Km to Miles button. Depending on which button is clicked, the program should then get the information from the appropriate text fields, convert it into the proper form (int or double), and call the appropriate methods in the RunnersCalculator object to perform the desired calculations. Each action (button click) by the user should result in an updated message in the TextArea.
Evaluation Criteria. Your program will be evaluated on the following basis:
General Hints and Suggestions.
Stepwise refinement. Write the program in stages. The first stage should be to implement the RunnersCalculator class. This class can be designed, implemented, and tested---by giving it a main() method--before you start working on the GUI.
The next stage should be to create the necessary GUI components and get the layout and appearance of the applet correct. This requires coding the buildGUI(), which should be done in a stepwise fashion.
The next step should be to get the applet to communicate with the RunnersCalculator and to perform calculations. This work will focus on the actionPerformed() method. It too should be done in a stepwise fashion, testing each new bit as you proceed.
Use the appletviewer program to test your applet, rather than opening it in a browser. Remember that you will need an HTML file to run the applet. You can use the demo HTML file (above) for that purpose.
Document your program as you build it. Don't leave it for the end. The documentation requirements are the same as what we have developed in lab.
Test each stage of your program thoroughly.
Get started early. Don't wait till the night before it is due.
Use the Trinity College Blackboard CourseInfo Digital Drop Box to submit your files: RunnersCalculator.java and RunnerCalcGUI.java classes. NOTE: Always select the LOGOUT option to exit CourseInfo. Closing the browser window Does Not terminate your CourseInfo session
This is an individual or partner programming project. You may choose to work on it completely by yourself or with 1 other classmate as your partner. If you choose to work alone, it is expected that the work you submit will be your work entirely. If you choose to work with a partner, then the work you submit should be done entirely by you and your partner. It is assumed that you and your partner worked on every aspect of the program together. In this case a single copy of the program can be handed in. If you feel you can write the program by yourself, you should do so since that will probably give you the most effective learning experience.
This does not mean that you cannot talk to classmates about your work. It is okay to discuss in general how you are tackling a particular problem -- e.g., "I used a String to store the value input by the user." You may also give help to or receive help from a classmate about a particular error message or program bug. "That error means you must have forgotten a semicolon."However, you may not copy another person's code or solution, whether from a classmate or from someone who took the course before or from someone you find on the Internet, in whole or in part, either by hand or electronically, and submit it as your own. If you need help with either design or programming, you should consult with the Instructors or TAs or with the CPSC Study Hall Consultants. If you receive substantial help from another student or a TA or an Instructor, you must document it in your program's header by providing the name of the person who helped you and a brief description of the type of help you received. Getting help on an assignment and not documenting it is a form of plagarism.