Addition.java Addition.java public class Addition extends Component { public Addition(Component numberOne, Component numberTwo) { this.left = numberOne; this.right = numberTwo; } @Override...

1 answer below »


For all of the components: delete getType() completely.
Implement the Visitor pattern using Double Dispatch.



When all of the tests pass, the code is functionally correct.



Feeback from last week's;


-15 Component should not have left/right


-10 Component should be an interface


-10 getValue should not be in Component












Addition.java Addition.java public class Addition extends Component {     public Addition(Component numberOne, Component numberTwo) {         this.left = numberOne;         this.right = numberTwo;     }     @Override     int getValue() {         return getLeft().getValue() + getRight().getValue();     }     @Override     String getType() {         return "addition";     } } __MACOSX/._Addition.java ComplexAdditionTests.java ComplexAdditionTests.java import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class ComplexAdditionTests {     CalculatorVisitor calculatorVisitor = new CalculatorVisitor();     Component component;     @Test     void one_plus_one_plus_five() {         component = new Addition(new Addition(new Number(1), new Number(1)), new Number(5));         int actual = component.accept(calculatorVisitor);         assertEquals(7, actual);     }     @Test     void two_plus_negative_six_plus_fifteen() {         component = new Addition(new Number(2), new Addition(new Number(-6), new Number(15)));         int actual = component.accept(calculatorVisitor);         assertEquals(11, actual);     }     @Test     void negative_four_plus_three_minus_one() {         component = new Addition(new Number(-4), new Subtraction(new Number(3), new Number(1)));         int actual = component.accept(calculatorVisitor);         assertEquals(-2, actual);     }     @Test     void negative_six_minus_two_plus_five_minus_negative_three() {         component = new Addition(new Subtraction(new Number(-6), new Number(2)),                 new Subtraction(new Number(5), new Number(-3)));         int actual = component.accept(calculatorVisitor);         assertEquals(0, actual);     } } __MACOSX/._ComplexAdditionTests.java ComplexSubtractionTests.java ComplexSubtractionTests.java import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class ComplexSubtractionTests {     CalculatorVisitor calculatorVisitor = new CalculatorVisitor();     Component component;     @Test     void one_plus_one_minus_five() {         component = new Subtraction(new Addition(new Number(1), new Number(1)), new Number(5));         int actual = component.accept(calculatorVisitor);         assertEquals(-3, actual);     }     @Test     void two_minus_negative_six_plus_fifteen() {         component = new Subtraction(new Number(2), new Addition(new Number(-6), new Number(15)));         int actual = component.accept(calculatorVisitor);         assertEquals(-7, actual);     }     @Test     void negative_four_minus_three_minus_one() {         component = new Subtraction(new Number(-4), new Subtraction(new Number(3), new Number(1)));         int actual = component.accept(calculatorVisitor);         assertEquals(-6, actual);     }     @Test     void negative_six_minus_two_minus_five_minus_negative_three() {         component = new Subtraction(new Subtraction(new Number(-6), new Number(2)),                 new Subtraction(new Number(5), new Number(-3)));         int actual = component.accept(calculatorVisitor);         assertEquals(-16, actual);     } } __MACOSX/._ComplexSubtractionTests.java Component.java Component.java public abstract class Component {     public Component left;     public Component right;     public Component getLeft() {         return left;     }     public Component getRight() {         return right;     }     abstract int getValue();     abstract String getType(); } __MACOSX/._Component.java 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";     }      } __MACOSX/._Number.java SimpleAdditionTests.java SimpleAdditionTests.java import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class SimpleAdditionTests {     CalculatorVisitor calculatorVisitor = new CalculatorVisitor();     Component component;     @Test     void add_one_and_one() {         component = new Addition(new Number(1), new Number(1));         int actual = component.accept(calculatorVisitor);         assertEquals(2, actual);     }     @Test     void add_two_and_fifteen() {         component = new Addition(new Number(2), new Number(15));         int actual = component.accept(calculatorVisitor);         assertEquals(17, actual);     }     @Test     void add_negative_four_and_three() {         component = new Addition(new Number(-4), new Number(3));         int actual = component.accept(calculatorVisitor);         assertEquals(-1, actual);     }     @Test     void add_negative_six_and_negative_three() {         component = new Addition(new Number(-6), new Number(-3));         int actual = component.accept(calculatorVisitor);         assertEquals(-9, actual);     } } __MACOSX/._SimpleAdditionTests.java SimpleNumberTests.java SimpleNumberTests.java import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class SimpleNumberTests {     CalculatorVisitor calculatorVisitor = new CalculatorVisitor();     Component component;     @Test     void number_one() {         component = new Number(1);         int actual = component.accept(calculatorVisitor);         assertEquals(1, actual);     }     @Test     void number_nine() {         component = new Number(9);         int actual = component.accept(calculatorVisitor);         assertEquals(9, actual);     }     @Test     void number_negative_five() {         component = new Number(-5);         int actual = component.accept(calculatorVisitor);         assertEquals(-5, actual);     } } __MACOSX/._SimpleNumberTests.java SimpleSubtractionTests.java SimpleSubtractionTests.java import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class SimpleSubtractionTests {     CalculatorVisitor calculatorVisitor = new CalculatorVisitor();     Component component;     @Test     void subtract_one_and_one() {         component = new Subtraction(new Number(1), new Number(1));         int actual = component.accept(calculatorVisitor);         assertEquals(0, actual);     }     @Test     void subtract_two_and_fifteen() {         component = new Subtraction(new Number(2), new Number(15));         int actual = component.accept(calculatorVisitor);         assertEquals(-13, actual);     }     @Test     void subtract_negative_four_and_three() {         component = new Subtraction(new Number(-4), new Number(3));         int actual = component.accept(calculatorVisitor);         assertEquals(-7, actual);     }     @Test     void subtract_negative_six_and_negative_three() {         component = new Subtraction(new Number(-6), new Number(-3));         int actual = component.accept(calculatorVisitor);         assertEquals(-3, actual);     } } __MACOSX/._SimpleSubtractionTests.java Subtraction.java Subtraction.java public class Subtraction extends Component {     public Subtraction(Component numberOne, Component numberTwo) {         this.left = numberOne;         this.right = numberTwo;     }     @Override     int getValue() {         return (getLeft().getValue() - getRight().getValue());     }     @Override     String getType() {         return "subtraction";     } } __MACOSX/._Subtraction.java public class Subtraction extends Component { public Subtraction(Component numberOne, Component numberTwo) { this.left = numberOne; this.right = numberTwo; } @Override int getValue() { return (getLeft().getValue() - getRight().getValue()); } @Override String getType() { return "subtraction"; } } public class Number extends Component{ int number; public Number(int number) { this.number = number; } @Override int getValue() { return number; } @Override String getType() { return "number"; } } public class Addition extends Component { public Addition(Component numberOne, Component numberTwo) { this.left = numberOne; this.right = numberTwo; } @Override int getValue() { return getLeft().getValue() + getRight().getValue(); } @Override String getType() { return "addition"; } } public abstract class Component { public Component left; public Component right; public Component getLeft() { return left; } public Component getRight() { return right; } abstract int getValue(); abstract String getType(); } import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class ComplexAdditionTests { CalculatorVisitor calculatorVisitor = new CalculatorVisitor(); Component component; @Test void one_plus_one_plus_five() { component = new Addition(new Addition(new Number(1), new Number(1)), new Number(5)); int actual = component.accept(calculatorVisitor); assertEquals(7, actual); } @Test void two_plus_negative_six_plus_fifteen() { component = new Addition(new Number(2), new Addition(new Number(-6), new Number(15))); int actual = component.accept(calculatorVisitor); assertEquals(11, actual); } @Test void negative_four_plus_three_minus_one() { component = new Addition(new Number(-4), new Subtraction(new Number(3), new Number(1))); int actual = component.accept(calculatorVisitor); assertEquals(-2, actual); } @Test void negative_six_minus_two_plus_five_minus_negative_three() { component = new Addition(new Subtraction(new Number(-6), new Number(2)), new Subtraction(new Number(5), new Number(-3))); int actual = component.accept(calculatorVisitor); assertEquals(0, actual); } } import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class ComplexSubtractionTests { CalculatorVisitor calculatorVisitor = new CalculatorVisitor(); Component component; @Test void one_plus_one_minus_five() { component = new Subtraction(new Addition(new Number(1), new Number(1)), new Number(5)); int actual = component.accept(calculatorVisitor); assertEquals(-3, actual); } @Test void two_minus_negative_six_plus_fifteen() { component = new Subtraction(new Number(2), new Addition(new Number(-6), new Number(15))); int actual = component.accept(calculatorVisitor); assertEquals(-7, actual); } @Test void negative_four_minus_three_minus_one() { component = new Subtraction(new Number(-4), new Subtraction(new Number(3), new Number(1))); int actual = component.accept(calculatorVisitor); assertEquals(-6, actual); } @Test void negative_six_minus_two_minus_five_minus_negative_three() { component = new Subtraction(new Subtraction(new Number(-6), new Number(2)), new Subtraction(new Number(5), new Number(-3))); int actual = component.accept(calculatorVisitor); assertEquals(-16, actual); } } import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class SimpleAdditionTests { CalculatorVisitor calculatorVisitor = new CalculatorVisitor(); Component component; @Test void add_one_and_one() { component = new Addition(new Number(1), new Number(1)); int actual = component.accept(calculatorVisitor); assertEquals(2, actual); } @Test void add_two_and_fifteen() { component = new Addition(new Number(2), new Number(15)); int actual = component.accept(calculatorVisitor); assertEquals(17, actual); } @Test void add_negative_four_and_three() { component = new Addition(new Number(-4), new Number(3)); int actual = component.accept(calculatorVisitor); assertEquals(-1, actual); } @Test void add_negative_six_and_negative_three() { component = new Addition(new Number(-6), new Number(-3)); int actual = component.accept(calculatorVisitor); assertEquals(-9, actual); } } import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class SimpleNumberTests { CalculatorVisitor calculatorVisitor = new CalculatorVisitor(); Component component; @Test void number_one() { component = new Number(1); int actual = component.accept(calculatorVisitor); assertEquals(1, actual); } @Test void number_nine() { component = new Number(9); int actual = component.accept(calculatorVisitor); assertEquals(9, actual); } @Test void number_negative_five() { component = new Number(-5); int actual = component.accept(calculatorVisitor); assertEquals(-5, actual); } } import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class SimpleSubtractionTests { CalculatorVisitor calculatorVisitor = new CalculatorVisitor(); Component component; @Test void subtract_one_and_one() { component = new Subtraction(new Number(1), new Number(1)); int actual = component.accept(calculatorVisitor); assertEquals(0, actual); } @Test void subtract_two_and_fifteen() { component = new Subtraction(new Number(2), new Number(15)); int actual = component.accept(calculatorVisitor); assertEquals(-13, actual); } @Test void subtract_negative_four_and_three() { component = new Subtraction(new Number(-4), new Number(3)); int actual = component.accept(calculatorVisitor); assertEquals(-7, actual); } @Test void subtract_negative_six_and_negative_three() { component = new Subtraction(new Number(-6), new Number(-3)); int actual = component.accept(calculatorVisitor); assertEquals(-3, actual); } }
Answered 1 days AfterNov 10, 2021

Answer To: Addition.java Addition.java public class Addition extends Component {...

Saima answered on Nov 11 2021
125 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here