Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Java Design, implement, and test the SOP expression hierarchy. Sums of Products The SOP processor executes SOP expressions. class Processor { public Double execute

Using Java Design, implement, and test the SOP expression hierarchy.

image text in transcribedimage text in transcribed

Sums of Products The SOP processor executes SOP expressions. class Processor { public Double execute (Expression exp) { return exp.execute(); The SOP language currently has three types of expressions: numbers, sums, and products. expression ::= number | sum product A sum is the token "sum" followed by a list of 2 or more expressions: sum(a, b, ...) A product is the token "mul" followed by a list of 2 or more expressions: mul (a, b, ...) A number has the format num(x) where x is any Java Double: num(-3.14) Here are a few examples of SOP expressions and their values: num (5.1) // = 5.1 sum (num (3.0), num (4.0), num (5.0)) // = 12 mul (num (2.0), sum (num (3.0), num (4.0), num (5.0))) // - 24 Design, implement, and test the SOP expression hierarchy. Notes Here's a start on a test harness. Add more tests: TestSOP.java. My constructors for Sum and Product use Java's varargs feature: Sum (Expression operand ...) (...) For example new Sun (a, b, c) gathers the inputs into an array (a, b, c). public class Test SOP { public static void main(String[] args) { Processor proc = new Processor(); Expression el = new Number(2.0); Expression e2 = new Number(3.1); Expression e3 = new Number(-5.0); Sum sl = new Sum(el, e2, e3); System.out.println("sl - " + proc.execute(sl)); Product pl = new Product(si, e3); System.out.println("sl - " + proc.execute(sl)); 11 etc. Sums of Products The SOP processor executes SOP expressions. class Processor { public Double execute (Expression exp) { return exp.execute(); The SOP language currently has three types of expressions: numbers, sums, and products. expression ::= number | sum product A sum is the token "sum" followed by a list of 2 or more expressions: sum(a, b, ...) A product is the token "mul" followed by a list of 2 or more expressions: mul (a, b, ...) A number has the format num(x) where x is any Java Double: num(-3.14) Here are a few examples of SOP expressions and their values: num (5.1) // = 5.1 sum (num (3.0), num (4.0), num (5.0)) // = 12 mul (num (2.0), sum (num (3.0), num (4.0), num (5.0))) // - 24 Design, implement, and test the SOP expression hierarchy. Notes Here's a start on a test harness. Add more tests: TestSOP.java. My constructors for Sum and Product use Java's varargs feature: Sum (Expression operand ...) (...) For example new Sun (a, b, c) gathers the inputs into an array (a, b, c). public class Test SOP { public static void main(String[] args) { Processor proc = new Processor(); Expression el = new Number(2.0); Expression e2 = new Number(3.1); Expression e3 = new Number(-5.0); Sum sl = new Sum(el, e2, e3); System.out.println("sl - " + proc.execute(sl)); Product pl = new Product(si, e3); System.out.println("sl - " + proc.execute(sl)); 11 etc

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions