RHS   Allan   SW construction Exercises ReadInput02


 

Read Input 02

In this exercise we'll extend the simple form for reading input we developed in the exercise ReadInput01.
We will add three text fields and change some labels (and names) so the form can be used to enter four numbers (two integer and two floating-point).
These values will be used to create an object from a class called SimpleCalc. This class is a silly, simple class we will create with the sole purpose of getting some experience with performing various calculations.


First start NetBeans, create a new project "Read Input 02" and mount the folder where you'll store the files for this exercise (remember to store each application (or program) in individual folders).

  As usual I'm working on my D: drive - you should use your network drive (H:).

First we'll copy the java-file from the former exercise so we don't have to start from scratch:

 

From the menu choose File -> Open File, find and open the file InputForm.java from project "Read Input 01".
Note that when you open a file from a folder that is not mounted, NetBeans mounts it automatically.

Make sure the InputForm element is selected in the Filesystems area (to select it click on it with the mouse).

To make the copy you can choose one of the following three methods:

  • Right-click on the InputForm element and choose Copy
  • Press the Ctrl and c key simultaneously
  • From the menu choose Edit -> Copy

Now select the folder you just created for this exercise and insert your copy.
Again you can choose one of three methods:

  • Right-click on the folder and choose Paste -> Copy
  • Press the Ctrl and v key simultaneously
  • From the menu choose Edit -> Paste -> Copy

Finally as we won't risk changing the original file you should remove the folder for "Read Input 01" from this project: Right-click on the folder and choose Unmount Filesystem

 

To show the InputForm in the editing area double-click on the element in the Filesystems area.

OK - finally we're set to start!

Now InputForm should look something like this:

 

You should be able to compile (F6) and execute (F9) but as we removed the code for the button nothing will happen when you click the button.

Before we add this action we will create the class SimpleCalc.
An object from this class is meant to hold four values (entered in the form) and do some calculations with these numbers and finally show the result.

Right-click on the folder in the Filesystems column, choose New -> Java Class, give it the name SimpleCalc.

Inside the class there is only put code for a single "method". In fact it is not a method - it is a constructor which is used when objects from this class is created.
We want the four values from the forms textfields to be used when an object is created so they must be passed to the constructor.
Values in a textfield is always a String (even if the content of the String expresses a numerical value) so we would like the constructor to accept four String-values.
Adapt the code so the constructor lokks like:

public SimpleCalc(String val1, String val2, String val3, String val4) {
}

We'll add some code to the constructor in a while, but first we need four variables to store the values that will be passed to the four String-parameters. The variables should store the numerical values not the strings - so we need two int variables and two double variables.

Between the closing curly-bracket (}) of the constructor and the closing curly bracket of the class itself add the following declarations:

private int int1;
private int int2;
private double double1;
private double double2;

Now we have variables to hold the values so we can add code to the constructor that will transfer numerical values expressed as Strings to variables of the correct numerical type.

Inside the constructor add the following code:

int1 = Integer.parseInt(val1);
int2 = Integer.parseInt(val2);
double1 = Double.parseDouble(val3);
double2 = Double.parseDouble(val4);

The Java-library holds two classes (Integer and Double) that each has a method for converting a String into its numerical value (as int or double). Note that we assume that the two first parameters (val1 and val2) holds the int-values and the last two parameters (val3 and val4) holds the double-values. We must respect this when we activate the constructor!

Now we're able to create an object of the SimpleCalc class by passing four strings (holding numerical values) to the constructor. The four numerial values will be stored in four variables of the right type. We only miss to do something with these four numerical values.

For a start we'll just add a method that returns a String containing the sum of the two int-values and the sum of the two double-values.

Before the constructor add the following method:

public String getResult() {
  int intResult = int1 + int2;
  double doubleResult = double1 + double2;

  return "Sum of int's : " + intResult + "  " +
         "Sum of double's : " + doubleResult;
}

You should now be able to compile (F9) but you cannot execute this class (it has no main-method!).
This is fine as objects from the class is only supposed to hold values and perform some calculation. We'll create an object from the class in InputForm.

So go back to the InputForm [Form] tab and double-click on the button.

Add the code that will read the four text-fields and place the values as arguments to the constructor of the SimpleCalc class as we create an object of the class (and store it in a variable called calcObj).:

SimpleCalc calcObj = new SimpleCalc(int1Fld.getText(), int2Fld.getText(),
                                    double1Fld.getText(), double2Fld.getText());

Note that we put the two int-values first and then the two double-values in order to match the declaration of the constructor.

Add one more line to set the text of outputLbl to be the result of the method getResult() defined in the class SimpleCalc (and thus available in the object calcObj):

outputLbl.setText(calcObj.getResult());

Compile and execute!

In the next exercise we'll make inputForm a little more advanced and make SimpleCalc objects perform some more calculations.


Maintained by: Allan Helboe Nielsen
Updated: 5 December, 2006 15:48