Question

In: Computer Science

a Composite class representing arithmetic expression. Participants: Expression – interface. Operations: double calculate(), boolean isTree(). Constant...

a Composite class representing arithmetic expression.

Participants:

  • Expression – interface. Operations: double calculate(), boolean isTree().
  • Constant – represent a number. Leaf node.
  • Plus, Minus, Div, Mul – binary operations. Use abstract BinaryOperation class to implement common functionality

Q. Create a code implementing a pattern, Implementing Composite implies multiple class. By my estimate, at least 3. In minimal Composite implementation, classes have two methods (or a constructor and 1 method). extensive coding is not required. (using Java)

Solutions

Expert Solution

public interface Arithmetic {
    double compute();
    default void appendChild(Arithmetic arithmetic) {}
    default void removeChild(Arithmetic arithmetic) {}
}

Next, we have Leaf nodes, which each represent a singular operation.

public class Addition implements Arithmetic {
    private final double x;
    private final double y;

    public Addition(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public double compute() {
        return x + y;
    }
}

public class Subtraction implements Arithmetic {
    private final double x;
    private final double y;

    public Subtraction(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public double compute() {
        return x - y;
    }
}

And finally, a Composite node, which represents multiple operations.

public class CompositeAddition implements Arithmetic {
    private final List<Arithmetic> operations = new ArrayList<>();

    public CompositeAddition(Arithmetic... arithmetics) {
        operations.addAll(Arrays.asList(arithmetics));
    }

    @Override
    public double compute() {
        return operations.stream().mapToDouble(Arithmetic::compute).sum();
    }

    @Override
    public void appendChild(Arithmetic arithmetic) {
        operations.add(arithmetic);
    }

    @Override
    public void removeChild(Arithmetic arithmetic) {
        operations.remove(arithmetic);
    }
}

I leave the remaining arithmetic types as an exercise for the reader. These few classes are sufficient for a demonstration.

public class Main {
    public static void main(String... args) {
        Arithmetic fivePlusTwo = new Addition(5,2);
        Arithmetic fiveMinusTwo = new Subtraction(5,2);
        Arithmetic sevenPlusThree = new CompositeAddition(fivePlusTwo, fiveMinusTwo);
        System.out.println(sevenPlusThree.compute());
    }
}
Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

Related Solutions

In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression...
In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6 2 + 5 * 8 4 / - The program should read the expression into StringBuilder or String infix and use the Stack and Stack Interface class...
. Write a sequence of instructions to calculate the following arithmetic expression and store the result...
. Write a sequence of instructions to calculate the following arithmetic expression and store the result in register CX: 20 – 6 + (-10) - (-8) + 15 Trace the contents of registers, assume initial contents are 0000 ps(there are multiple boxes) Instruction AX BX CX DX Remark initial 0000 0000 0000 0000
create a class in C++ having the following 4 private members: one double variable representing the...
create a class in C++ having the following 4 private members: one double variable representing the balance of a bank account on string variable representing the bank account number a function to deposit money from the bank account a function to withdraw money from the bank account the following 2 public members: -two wrapper functions. - one parameterless constructor - one constructor initializing the account balance and account number - sample: #include <iostream> using namespace std; class account { public:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT