RHS   Allan   SW construction Exercises ReadInput04


 

Read Input 04

In this exercise we'll adapt our project "Read Input 03" a little and add some more calculations to see how different types of values act.

Let's add some more operations and let's mix the types of values used. Most of the new operations we'll get from the class Math (see page 87 in Koffman).

Stretch the form and the panel (buttonPanel) a little and add 9 buttons. Give the buttons the names modulusBtn, sqrtBtn, powerBtn, logBtn, floorBtn, ceilBtn, roundBtn, maxBtn and minBtn. Change the text of the buttons into "mod","sqrt","pow","log", "floor", "ceil", "round", "max" and "min".

Now add code to be executed when the buttons are clicked.

Don't worry that the methods (e.g. getModulusResult()) doesn't exist in the SimpleCalc class yet - the next thing we'll do is to add these methods.

Before we add the new methods we'll extend the existing methods so they also do some calculation with values of mixed type:

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


  return "Addition of int's : " + intResult + "\n" +
         "Addition of double's : " + doubleResult + "\n" +
         "Addition of 1st int and 1st double : " + mixedResult
;

}

We declared a new variable (mixedResult) of type double and assigned to it the result of adding the first int variable (int1) and the first double variable (double1). The result is added to the return-statement.


Now add methods to SimpleCalc for the new operations we want to use. Note that some of the operations only require one value. Also note that some operations only can be performed on floating-point values (e.g. double). In the table below is some comments for each method to help you implement them:

 
Method
Comment
 
getModresult()
In java the modulus operator (which finds the remainder in division) is: %
E.g. to find the remainder when dividing the first int value by the second int value do the following: int intResult = int1 % int2;
 
getSqrtResult()
In the class Math you find the method sqrt() that calculates the square root of a positive value. Note that the result type always is double.
As the method only requires one argument you should only do calculation for int1 and double1. E.g. to find the square root of int1 do the following: double intResult = Math.sqrt(int1);
 
getPowerResult()
In Math you also find pow() that takes two arguments. The return value is the first argument to the power of the second. The return type is double.
 
getLogResult()
Math.log() only takes one argument (only use int1 and double1) and returns the natural logarithm of this value - return type is double.
 
getFloorResult()
Math.floor() returns the largest (closest to positive infinity) value that is not greater than the argument (only use int1 and double1) and is equal to a mathematical integer. Return type is double.
 
getCeilResult()
Math.ceil() returns the smallest (closest to negative infinity) value that is not less than the argument (only use int1 and double1) and is equal to a mathematical integer. Return type is double.
 
getRoundResult()
Math.round() returns the closest int to the argument (only use int1 and double1). Return type is int for integer argument and long for floating-point argument.
 
getMaxResult()
Math.max() returns the greater of two values. Return type is same as arguments.
 
getMinResult()
Math.min() returns the greater of two values of same type. Return type is same as arguments.

If you want more detailed information about the functions you should look in the API-documentation for java - you can browse it online here: java.sun.com/j2se/1.4.2/docs/api/index.html


Maintained by: Allan Helboe Nielsen
Updated: 3 November, 2005 1:04