Lab Assignment 7

Fall Semester, 2006

 

Outcomes:

 

Upon completion of the lab, the student will know how to

 

·       Write simple Java event handlers.

 

·       Write simple Java exception handlers.

 

·       Use JOptionPane methods to display simple dialogs.

 

1

Download the following files to your J: drive from Mr. Clark’s website:

·       Complex.java

·       QuadraticSolution.java

·       QuadraticException.java

·       QuadraticEquation.java

·       Lab7.java

2

Compile and execute the main method of Lab7. You should see the following GUI frame:

 

 

This application has no functionality, but you will rectify that soon.

 

3

Add code to the Lab7 method clearFields() that clears the text in all 5 text fields.

 

4

Add code to actionPerformed to handle the “Clear fields” button.  When this button is pressed, the clearFields() method should be called to clear the text fields of the GUI.  Demonstrate this feature to the lab assistant.

 

5

Add code to actionPerformed to handle the “Find roots” button.  This code should do the following:

·       Get the text from the fields aField, bField, and cField, convert to double and assign to the local variables a, b and c.  Use Double.parseDouble() to do this.

·       Use a, b and c to create a new QuadraticEquation object and assign it to q. 

·       Get the solutions of q and display them in the fields labeled R1 and R2.  If the roots are real, display the value of the solution’s r1 attribute in r1Field and the value of the r2 attribute in r2Field.  If the roots are complex, display the Complex value (r1, r2) in r1Field and the Complex value (r1, -r2) in r2Field.

·       Here are a couple of screen shots to illustrate this:

 

  Roots are real

 

  Roots are complex

 

Demonstrate this code to the lab assistant.

 

6

Add exception handlers (using try-catch) to the “Find roots” code in actionPerformed to handle the following exceptions

·       QuadraticException.  In this case, display a JOptionPane message dialog that displays the message from the thrown exception, and then calls clearFields() to clear the text fields in the GUI.
The code for showing a message dialog is:

JOptionPane.showMessageDialog(null, message);

 

    

 

 

·       NumberFormatException.  In this case, display a message dialog that informs the user that the coefficients are invalid, then clear the text fields of the dialog.  For example,

 

    

 

Demonstrate this code to the lab assistant.

 

7

Print a listing of your Lab7 class and have the lab instructor/assistant sign it.  Hand in this printed listing.