|
RHS |
Read Input 03
In this exercise we'll adapt our project "Read Input 02" a little and add some more calculations to see how different types of values act.
So start by creating the project "Read Input 03" and mount a new folder.
Also mount the folder where you stored the project "Read
Input 02" and
copy both InputForm and SimpleCalc to
the new folder - then Unmount the folder for "Read
Input 02" again.
First we'll make it possible to show some more output in a better way in the window than we can now - a JLabel is not very suitable for several lines of text.
Open InputForm in the graphical form-editor.
Stretch the form a little so there is room below the outputLbl.
Add a JTextArea, name it outputTxtArea.
Add a JPanel below the JTextArea - make it a little bigger so we can drop
some components on it.
Name it buttonPanel, select an AbsoluteLayout and attach it to the panel.
Click on the button to the right of the "border" property
and choose "BevelBorder"
Now add 5 JButtons to the panel, name them addBtn, subtractBtn, multiplyBtn, divideBtn and clearOutputBtn. Set their text attribute to "+", "-", "*", "/" and "C" .
Resize and arrange so the frame looks something like
![]() |
Before we add code for the buttons we'll make some changes in the class
SimpleCalc.
We'll add methods
to perform the operations (+, -, * and /) on the two set of numbers and return
the result as a String.
Thus the existing method getResult() is not needed anymore - instead we'll
add a general method to return all values from an object - the toString() method.
Open SimpleCalc in the code-editor.
Rename the method getResult() into toString().
Change the code so the method
looks like:
public String toString() {
return int1 + ", " + int2 + ", " +
double1 + ", " + double2;
}
Now add four methods getAddResult(), getSubtractResult(), getMultiplyResult() and getDivideresult().
These methods is to be activated from the buttons on the panel in InputForm.
They'll perform an arithmetic operation
on the two int's and on the two double's and return the two results as a
String.
Below you find the code for the two first methods:
public String getAddResult() {
int intResult = int1 + int2;
double doubleResult = double1 + double2;
return "Addition of int's : " + intResult + "\n" +
"Addition of double's : " + doubleResult;
}
public String getSubtractResult() {
int intResult = int1 - int2;
double doubleResult = double1 - double2;
return "Subtraction of int's : " + intResult + "\n" +
"Subtraction of double's : " + doubleResult;
}
Add the code for the two last methods (getMultiplyResult() and getDivideresult()).
Compile (F9) - if you get any error: correct it
and compile again.
Now go back to InputForm in the form-editor.
Doubleclick on addBtn. In the code-editor the cursor is placed in the method
addBtnActionPerformed().
Add the following code that will activate the getAddResult() from the SimpleCalc object and place the result in the output JTextArea:
outputArea.setText(calcObj.getAddResult());
Go back to the form-editor and add similar code for the three buttons subtractBtn,
divideBtn and multiplyBtn.
Off course their code should activate the method from the SimpleCalc object
that performs the actual operation.
For the clearOutputBtn add the code: outputTxtArea.setText("");
This code sets the content of the JTextArea to an empty string ("")
which gives the effect of clearing the content.
Now doubleclick od the actionBtn.
In the method actionBtnActionPerformed() method
the variable calcObj (an object of the
type SimpleCalc) is declared.
Now we need to access this object from the other methods so we have to move the declaration outside of the method, but we still want to create the object itself inside theis method (which is activated when the actionBtn is clicked).
So simply delete the word "SimpleCalc" in the first line.
The next java-statement used to get the result string from the object using the method getResult() - now this method doesn't exist anymore so you should change the statement into using the toString() method instead.
Finally we need to make the declaration of the SimpleCalc variable so it
can be accessed from everywhere in the class.
Go to the bottom of the file - right after the blue area with the declarations
of all the graphical components (but before the final right curly-bracket
}) add the declaration of the variable calcObj of the type SimpleCalc:
private SimpleCalc calcObj;
Now compile and run the program.
Enter 4 values and click the "Read input" button.
Then see the results
of the different arithmetic operations by clicking the appropriate button.
Clear
the output area by the "C" button.
If you want to change values just enter new ones in the text fields and click the "Read input" button again.