Addition and Subtraction - Composite You will have a parent abstraction: Component. Every Component can getType() which will return its name. Next, you will have a Number, which is a Component. A...

1 answer below »

Addition and Subtraction - Composite




You will have a parent abstraction: Component. Every Component can getType() which will return its name.


Next, you will have a Number, which is a Component. A Number can be created with any number and has a getter to get that number. You can use an int here, nothing clever needed. Follow the tests. Number is our leaf.


Next, you will have Addition, which is a Component. It also has 2 Components. If you think of an operator, like addition, there are always 2 sides of that operation. A constructor must set both Components and must have getters for both. Again, nothing clever here.


Last, you will have Subtraction, which is a Component. It also has 2 Components. If you think of an operator, like subtraction, there are always 2 sides of that operation. A constructor must set both Components and must have getters for both. Again, nothing clever here.


Follow the tests, they will tell you exactly what getType() should return for each of the classes.








you are expected to optimize design.


Educator notes:
There is no functionality to this, For now, we are just building out the structural pattern.


All tests must pass.







import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class AdditionTest { Addition addition; @Test void get_type() { addition = new Addition(new Number(1), new Number(2)); assertEquals("addition", addition.getType()); } @Test void number_children_have_correct_assignments() { addition = new Addition(new Number(1), new Number(2)); assertEquals(1, ((Number) addition.getLeft()).getValue()); assertEquals(2, ((Number) addition.getRight()).getValue()); } @Test void component_children_have_correct_assignments() { Addition left = new Addition(new Number(1), new Number(2)); Subtraction right = new Subtraction(new Number(1), new Number(2)); addition = new Addition(left, right); assertEquals("addition", addition.getLeft().getType()); assertEquals("subtraction", addition.getRight().getType()); } } import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class NumberTest { Number number; @Test void one() { number = new Number(1); assertEquals(1, number.getValue()); } @Test void get_type() { number = new Number(1); assertEquals("number", number.getType()); } } import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class SubtractionTest { Subtraction subtraction; @Test void get_type() { subtraction = new Subtraction(new Number(1), new Number(2)); assertEquals("subtraction", subtraction.getType()); } @Test void number_children_have_correct_assignments() { subtraction = new Subtraction(new Number(1), new Number(2)); assertEquals(1, ((Number) subtraction.getLeft()).getValue()); assertEquals(2, ((Number) subtraction.getRight()).getValue()); } @Test void component_children_have_correct_assignments() { Subtraction left = new Subtraction(new Number(1), new Number(2)); Addition right = new Addition(new Number(1), new Number(2)); subtraction = new Subtraction(left, right); assertEquals("subtraction", subtraction.getLeft().getType()); assertEquals("addition", subtraction.getRight().getType()); } }

Answered Same DayNov 02, 2021

Answer To: Addition and Subtraction - Composite You will have a parent abstraction: Component. Every Component...

Kondalarao answered on Nov 03 2021
117 Votes
Number.java
Number.java
public class Number extends Component{
    int number;

    public 
Number(int number) {
        this.number = number;
    }
    @Override
    int getValue() {
        return number;
    }
    @Override
    String getType() {
        return "number";
    }

}
Subtraction.java
Subtraction.java
public class Subtraction extends Component{

    public Subtraction(Component n1, Component n2) {
        this.left = n1;
        this.right = n2;
    }
    @Override
    int getValue() {
        return (getLeft().getValue()-getRight().getValue());
    }
    @Override
    String getType() {...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here