| 1 | public class Calculator { |
| 2 | private double result; |
| 3 | |
| 4 | public Calculator add(double number) { |
| 5 | result += number; |
| 6 | return this; |
| 7 | } |
| 8 | |
| 9 | public Calculator subtract(double number) { |
| 10 | result -= number; |
| 11 | return this; |
| 12 | } |
| 13 | |
| 14 | |
| 15 | public double getResult() { |
| 16 | return result; |
| 17 | } |
| 18 | |
| 19 | public Calculator reset() { |
| 20 | result = 0; |
| 21 | return this; |
| 22 | } |
| 23 | |
| 24 | public static void main(String[] args) { |
| 25 | Calculator calc = new Calculator(); |
| 26 | calc.add(10).subtract(5); |
| 27 | System.out.println("Result: " + calc.getResult()); |
| 28 | } |
| 29 | } |