I have 7 classes with the following requirements: An ABSTRACT class Operator tha
ID: 3630180 • Letter: I
Question
I have 7 classes with the following requirements:
An ABSTRACT class Operator that has:
-a private String called name
- a public abstract method performOperation.
- a public method getName that returns the name
- a public equals method.
Classes: Add, Subtract, Multiply, Divide, which all EXTEND the Operator class:
- Each constructor should take no parameters. Instead, use super to pass in the name of the operator (e.g., "Add").
The Calculator class that has:
- a private instance variable called operators that is an array of Operator objects.
- This array should be set using the Calculator constructor
- A calculate method. parameter: double array called values. return type: double.
NOTE: If there are too few or too many elements in the value array, return Double.NEGATIVE_INFINITY.
- An equals method. parameter: A Calculator object called c. return type: boolean
A class called Main that has:
- a testCalculator method. parameters: Calculator called c, double array called values, double called expected.
- a main method
There are a LOT of errors (work in progress), but my main question is this: How do I access the private String name in the Operator class so I can use the super keyword to pass in the names of the operator in other classes, like "Add", etc. Thanks!!!
This is what I have so far:
Operator.java
public abstract class Operator {
private String name;
abstract public void performOperation(double n1, double n2);
public String getName() {
return name;
}
public boolean equals(Operator op) {
return op.toString().equals(toString());
}
}
Add.java
public class Add extends Operator{
public double performOperation(double n1, double n2) {
super.getName();
return n1 + n2;
}
}
Subtract.java
public class Subtract extends Operator{
public double performOperation(double n1, double n2) {
super.getName();
return n1-n2;
}
}
Multiply.java
public class Multiply extends Operator{
public double performOperation(double n1, double n2) {
super.getName();
return n1*n2;
}
}
Divide.java
public class Divide extends Operator{
public double performOperation(double n1, double n2) {
return n1/n2;
}
}
Calculator.java
public class Calculator {
private Operator[] operators;
Calculator(Operator[] o) {
Operator[] operators = o; }
double calculate(double[] values) {
for (double i = 0; i < values.length; i++ ) {
o.performOperation(values); }
if ((values.length > values.length - 1) ^ (values.length < values.length + 1)) // this is confusing to me.
{ return Double.NEGATIVE_INFINITY; } }
public boolean equals(Calculator c) {
if (c instanceof Calculator) {
} if (c.equals(c)) {
return true; }
}
Main.java
public class Main {
public static boolean testCalculator(Calculator c, double c[], double expected) {
return true;
}
}
Explanation / Answer
//Operator.java public abstract class Operator { private String name; abstract public double performOperation(double n1, double n2); public Operator(String opname) { name= opname; } public String getName() { return name; } public boolean equals(Operator op) { return op.toString().equals(toString()); } } //Add.Java public class Add extends Operator { public Add() { super("Add"); } @Override public double performOperation(double n1, double n2) { return n1+n2; } } //Subtract.Java public class Subtract extends Operator { public Subtract() { super("Subtract"); } @Override public double performOperation(double n1, double n2) { return n1-n2; } } //Multiply.Java public class Multiply extends Operator { public Multiply() { super("Multiply"); } @Override public double performOperation(double n1, double n2) { return n1*n2; } } //Divide.Java public class Divide extends Operator { public Divide() { super("Divide"); } @Override public double performOperation(double n1, double n2) { return n1/n2; } } //Calculator.Java public class Calculator { private Operator[] opearators; public Calculator(Operator[] op) { opearators = op; } public double calculate(double[] values) { double result = 0; if(opearators.length == (values.length - 1)) { for(int i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.