Spaces:
Running
Running
void add(int number); double getResult();
Browse files
Week 6: Methods of OO Programming/05B. Write an interface class Calculator
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write an interface class Calculator.
|
| 2 |
+
|
| 3 |
+
The class has the following properties:
|
| 4 |
+
Methods add, subtract, multiply, and divide.
|
| 5 |
+
All methods are of void type and each takes a single integer as a parameter.
|
| 6 |
+
A method getResult, which has no parameters.
|
| 7 |
+
This method returns a double-type floating-point number.
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
import java.util.Random;
|
| 13 |
+
|
| 14 |
+
public class Test{
|
| 15 |
+
public static void main(String[] args){
|
| 16 |
+
final Random r = new Random();
|
| 17 |
+
|
| 18 |
+
System.out.println("Testing the calculator interface...");
|
| 19 |
+
|
| 20 |
+
Calculator c = new CalculatorTest();
|
| 21 |
+
|
| 22 |
+
c.add(1);
|
| 23 |
+
System.out.println("add(int number) ok");
|
| 24 |
+
|
| 25 |
+
c.subtract(1);
|
| 26 |
+
System.out.println("subtract(int number) ok");
|
| 27 |
+
|
| 28 |
+
c.multiply(1);
|
| 29 |
+
System.out.println("multiply(int number) ok");
|
| 30 |
+
|
| 31 |
+
c.divide(1);
|
| 32 |
+
System.out.println("divide(int number) ok");
|
| 33 |
+
|
| 34 |
+
double d = c.getResult();
|
| 35 |
+
System.out.println("getResult() ok");
|
| 36 |
+
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
//ADD
|
| 44 |
+
interface Calculator {
|
| 45 |
+
void add(int number);
|
| 46 |
+
void subtract(int number);
|
| 47 |
+
void multiply(int number);
|
| 48 |
+
void divide(int number);
|
| 49 |
+
double getResult();
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
Testing the calculator interface...
|
| 56 |
+
add(int number) ok
|
| 57 |
+
subtract(int number) ok
|
| 58 |
+
multiply(int number) ok
|
| 59 |
+
divide(int number) ok
|
| 60 |
+
getResult() ok
|
| 61 |
+
|
| 62 |
+
|