Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- /*
- Create a Calculator class with members: a and b using the appropriate data type
- The Calculator class must implement an interface called Calculable with the following method signatures
- double add(double num1, double num2);
- double subtract(double num1, double num2);
- double multiply(double num1, double num2);
- double divide(double num1, double num2);
- Create parameterized constructor which initializes the instance variables
- Modify the Calculator so that any of the methods can accept an array as a parameter
- */
- public class Calculator implements Calculable {
- private double a, b;
- private double[] numbers;
- public Calculator(double a, double b) {
- this.a = a;
- this.b = b;
- }
- public Calculator(double[] numbers) {
- this.numbers = numbers;
- }
- public double add() {
- return a + b;
- }
- public double addNumbers() {
- double sum = 0;
- for(int i = 0; i < numbers.length; i++) {
- sum = sum + numbers[i];
- }
- return sum;
- }
- public double subtract() {
- return a - b;
- }
- public double multiply() {
- return a * b;
- }
- public double divide() {
- return a / b;
- }
- public void someMethod(Calculable c) {
- }
- public static void main(String[] args) {
- double num1 = 5.0, num2 = 10.0;
- Calculator c = new Calculator(num1, num2);
- System.out.printf("Adding: %f and %f = %f", num1, num2, c.add());
- System.out.printf("Subtracting: %f and %f = %f", num1, num2, c.subtract());
- System.out.printf("Multiplying: %f and %f = %f", num1, num2, c.multiply());
- System.out.printf("Dividing: %f and %f = %f", num1, num2, c.divide());
- }
- }
- interface Calculable {
- double add();
- double subtract();
- double multiply();
- double divide();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement