CSCE 155

Homework Assignment 3: 

A Measurement Unit Converter

Assigned:  September 27, 2004

Due: Start of Class, October 11, 2004

 

Note: This assignment is to be completed individually - collaboration is strictly prohibited.

Points:  100 points

 

Problem Description

 

The Graphical User Interface (GUI) is an important component in software. It provides a user-friendly communication media between the users and an application. Users provide input to the application; the application processes the input, performs some computations and provides some feedback to the users. The role of the GUI is to provide the bridge between the user and the application.

 

Java provides a variety of tools to help us build a GUI easily.

 

·        The Swing library provides a rich set of graphical components. For example, JFrame, JPanel, and JDialog are some of the classes that can help you put graphical component into your interface. JButton, JTextfield, JRadioButton, and JCheckBox are graphical components you might use for your GUI. Layout Managers help you organize your interface components in a natural and user-friendly manner.

 

·        Java also provides an event-handling mechanism that facilitates user centric programming. Any action performed by the user is captured and notification is send to the application. The application must respond to these actions accordingly. You can write action listeners to process different actions that you think are meaningful to your application.

 

For Assignment 3, you are given a set of requirements to design a GUI for an application called Converter:

 

1.      The application should provide a window for a user to convert between different measures of weight (kilograms—pounds), length (meter—feet) and temperature (centigrade—Fahrenheit). For example, when the user enters the weight in kilograms, your application may convert the weight to the corresponding value in pounds; or vice versa.

 

2.      For your convenience, use Table 1 to implement your conversions. 

 

Measurement

Metric to American

American to Metric

Weight

1 kg à 2.2 lbs

1 lb à 0.45 kg

Length

1 m à 3.28 ft

1 ft à 0.30 m

Temperature

F à (9/5)*C + 32

C à (5/9)*(F – 32)

Table 1.  Conversion equations.

 

3.      You are required to make the appearance of the interface similar to the snapshot shown in Figure 1. Allow the window to be big enough to accommodate all of your components.

 

Figure 1.  Snapshot of a Converter window.

 

4.      First, use three JRadioButton objects as the options for the user.  Each JRadioButton object is for one measurement type: length, weight, or temperature. 

 

5.      Second, for each measurement type, create two JTextField objects so that the user can type into the JTextField objects.

 

6.      Third, create three buttons: “Convert”, “Clear”, and “Quit”.  

 

·        When the “Quit” button is clicked, your application should terminate and the window should disappear. 

 

·        When the “Clear” button is clicked, your application should remove all entered text from the JTextField objects.

 

·        When the “Convert” button is clicked, your application should check for the following:

 

a.       Check to see if one JRadioButton object has been selected.  If no, then send a warning.  Otherwise, move to the next check.

a.       Check to see if only one JTextField object has been modified by the user.  If none or two have been modified, then send a warning.  Otherwise, move to the next check.

b.      Check to see if the entered text is a number.  If no, then send a warning.  Otherwise, move to the next check.

c.       Use the conversion equations in Table 1 to perform the conversion, echo the input number, and output the conversion result.

 

7.      Sending a Warning:  This is shown in Figure 2.  Use a dialog or message box to do this.  You must also be specific in your warning, pointing out specifically what the user has done incorrectly.

Figure 2.  Snapshot of a warning window.

 

8.      Echoing the input number and Outputting the Conversion Result:  This is shown in Figure 3.  You must show what the user has entered and the conversion result.

 

Figure 3.  Snapshot of an echo and conversion result window.

 

9.      Take the user input and store the information internally in your program. Your program should check whether a file called “CSE155.txt” exists in your current directory. If the file exists, open it and append each conversion to the file. You may use a space or comma delimited format to store this text file. Do 10 conversions at least. If the file does not exist, create the file from your program, open it and start appending new records. Submit your test results in hard copy too.

 

Supplied Solution Components

 

None.

 

Challenge - Extra Credit (10 points)

 

Change all the JRadioButton objects to JCheckBox objects, so that the user can convert more than one of the measurement types when clicking the “Convert” button. For example, check both weight and temperature, and do both conversions and show the results in one message/dialog box.

 

Submission Procedure

 

This assignment is due on October 11, 2004 at the start of class. Your grade will be reduced by 10% for each day it is late. It is recommended you read the grading policy and grading guidelines on the course website for a complete explanation of how the assignments will be graded. Remember, your program should follow good programming style, include plenty of comments, and perform all of the functionality outlined above.

After completing the assignment, you must “handin” the following files on-line:

1.      Source files: Converter.java

2.      Compiled files: Converter.class

3.      Readme file: README.TXT(Plus test results)

4.      Data file: CSE155.txt

In addition, you must submit a stapled paper copy of your source code (and README file), please write down your CSE email on your paper copy.  Both of these steps must be done by the start of class on the day the assignment is due.