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()); } }