Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. package com.company;
  2.  
  3. /*
  4. Create a Calculator class with members: a and b using the appropriate data type
  5. The Calculator class must implement an interface called Calculable with the following method signatures
  6. double add(double num1, double num2);
  7. double subtract(double num1, double num2);
  8. double multiply(double num1, double num2);
  9. double divide(double num1, double num2);
  10. Create parameterized constructor which initializes the instance variables
  11. Modify the Calculator so that any of the methods can accept an array as a parameter
  12. */
  13.  
  14. public class Calculator implements Calculable {
  15.  
  16. private double a, b;
  17. private double[] numbers;
  18.  
  19. public Calculator(double a, double b) {
  20. this.a = a;
  21. this.b = b;
  22. }
  23.  
  24. public Calculator(double[] numbers) {
  25. this.numbers = numbers;
  26. }
  27.  
  28. public double add() {
  29. return a + b;
  30. }
  31.  
  32. public double addNumbers() {
  33. double sum = 0;
  34.  
  35. for(int i = 0; i < numbers.length; i++) {
  36. sum = sum + numbers[i];
  37. }
  38.  
  39. return sum;
  40. }
  41.  
  42. public double subtract() {
  43. return a - b;
  44. }
  45.  
  46. public double multiply() {
  47. return a * b;
  48. }
  49.  
  50. public double divide() {
  51. return a / b;
  52. }
  53.  
  54. public void someMethod(Calculable c) {
  55.  
  56. }
  57.  
  58. public static void main(String[] args) {
  59.  
  60. double num1 = 5.0, num2 = 10.0;
  61.  
  62. Calculator c = new Calculator(num1, num2);
  63.  
  64. System.out.printf("Adding: %f and %f = %f", num1, num2, c.add());
  65. System.out.printf("Subtracting: %f and %f = %f", num1, num2, c.subtract());
  66. System.out.printf("Multiplying: %f and %f = %f", num1, num2, c.multiply());
  67. System.out.printf("Dividing: %f and %f = %f", num1, num2, c.divide());
  68. }
  69. }
  70.  
  71. interface Calculable {
  72. double add();
  73. double subtract();
  74. double multiply();
  75. double divide();
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement