June 8, 2024 Deadline: Jun 24 (inclusive)
Create three classes:
- Constructor: Takes a
name(String). - Method:
This method calculates the sum of its arguments and returns a string (not a number) with the name of the machine and the result of the addition.
public String calculate(double x, double y)
- Static Method:
This method takes an array of calculating machines and arguments
public static void printRes(CalculatingDevice[] a, double x, double y)
xandy. It prints the results of calculations with these arguments for each machine from the array.
- Extends
CalculatingDevice. - Override Method:
This method gets the result of addition by invoking
public String calculate(double x, double y)
super.calculateand adds the result of subtraction (as a string).
- Extends
Calculator. - Override Method:
This method gets the result of addition and subtraction by invoking
public String calculate(double x, double y)
super.calculateand adds, as a string, the results of multiplication and division.
public class Computers {
public static void main(String[] args) {
CalculatingDevice[] arr = {
new Computer("Cray"),
new CalculatingDevice("Abacus"),
new Calculator("HP")
};
CalculatingDevice.printRes(arr, 21, 7);
}
}Cray: 21.0+7.0=28.0; 21.0-7.0=14.0; 21.0*7.0=147.0; 21.0/7.0=3.0
Abacus: 21.0+7.0=28.0
HP: 21.0+7.0=28.0; 21.0-7.0=14.0
public class CalculatingDevice {
private String name;
public CalculatingDevice(String name) {
this.name = name;
}
public String calculate(double x, double y) {
double sum = x + y;
return name + ": " + x + "+" + y + "=" + sum;
}
public static void printRes(CalculatingDevice[] a, double x, double y) {
for (CalculatingDevice device : a) {
System.out.println(device.calculate(x, y));
}
}
}public class Calculator extends CalculatingDevice {
public Calculator(String name) {
super(name);
}
@Override
public String calculate(double x, double y) {
String result = super.calculate(x, y);
double difference = x - y;
return result + "; " + x + "-" + y + "=" + difference;
}
}public class Computer extends Calculator {
public Computer(String name) {
super(name);
}
@Override
public String calculate(double x, double y) {
String result = super.calculate(x, y);
double product = x * y;
double quotient = x / y;
return result + "; " + x + "*" + y + "=" + product + "; " + x + "/" + y + "=" + quotient;
}
}public class Computers {
public static void main(String[] args) {
CalculatingDevice[] arr = {
new Computer("Cray"),
new CalculatingDevice("Abacus"),
new Calculator("HP")
};
CalculatingDevice.printRes(arr, 21, 7);
}
}-
CalculatingDevice Class:
- Has a constructor to initialize the name.
- The
calculatemethod sums the two numbers and returns a formatted string with the result. - The
printResstatic method iterates over an array ofCalculatingDeviceobjects and prints their calculations.
-
Calculator Class:
- Extends
CalculatingDevice. - Overrides the
calculatemethod to include subtraction in the result string.
- Extends
-
Computer Class:
- Extends
Calculator. - Overrides the
calculatemethod to include multiplication and division in the result string.
- Extends
-
Computers Class:
- Contains the
mainmethod. - Creates an array of
CalculatingDeviceobjects including instances ofComputer,CalculatingDevice, andCalculator. - Calls the
printResmethod to print the results for each device with the provided arguments.
- Contains the
When you run the Computers class, it should produce the following output:
Cray: 21.0+7.0=28.0; 21.0-7.0=14.0; 21.0*7.0=147.0; 21.0/7.0=3.0
Abacus: 21.0+7.0=28.0
HP: 21.0+7.0=28.0; 21.0-7.0=14.0
Computers.java
public class Computers {
public static void main(String[] args) {
CalculatingDevice[] arr = {
new Computer("Cray"),
new CalculatingDevice("Abacus"),
new Calculator("HP")
};
CalculatingDevice.printRes(arr, 21, 7);
}
}
// Base class
class CalculatingDevice {
private String name;
public CalculatingDevice(String name) {
this.name = name;
}
public String calculate(double x, double y) {
double sum = x + y;
return name + ": " + x + "+" + y + "=" + sum;
}
public static void printRes(CalculatingDevice[] a, double x, double y) {
for (CalculatingDevice device : a) {
System.out.println(device.calculate(x, y));
}
}
}
// Subclass extending CalculatingDevice
class Calculator extends CalculatingDevice {
public Calculator(String name) {
super(name);
}
@Override
public String calculate(double x, double y) {
String result = super.calculate(x, y);
double difference = x - y;
return result + "; " + x + "-" + y + "=" + difference;
}
}
// Subclass extending Calculator
class Computer extends Calculator {
public Computer(String name) {
super(name);
}
@Override
public String calculate(double x, double y) {
String result = super.calculate(x, y);
double product = x * y;
double quotient = x / y;
return result + "; " + x + "*" + y + "=" + product + "; " + x + "/" + y + "=" + quotient;
}
}