In Java SOP expressions are sums of products of doubles Exp

In Java :

SOP expressions are sums of products of doubles:

Expression ::= Sum | Product

Sum ::= Product~(\"+\"~Product)*

Product ::= Double~(\"*\"~Double)*

The SOP console reads a string from the keyboard and breaks it into a list of sub-strings separated by + signs. These sub-strings are broken into a list of sub-sub-strings separated by * signs. The sub-sub-strings should be parsable as doubles. (Otherwise and exception should be thrown.)

The lists of sub-sub-strings should be converted into instances of a Product class. Lists of products should be converted into lists of expressions

Hint

You can modify the console presented in class.

You could break a string such as 1+2*3 + 4 * 5 * 6 into the lists of lists [1, [2, 3], [4, 5, 6]] by creating scanners using the right delimiter pattern. For example:

Scanner tokens1 = new Scanner(input).useDelimiter(\"\\\\s*\\\\+\\\\s*\");

«interface» Expression +execute0: Double Sum Product -operands: Product(1. -operands: Double[1. Console +repl0 +parse(line: String): Expression

Solution

import java.util.Scanner;

interface Expression {
   public Double execute();
}

class Sum implements Expression {
   private Product[] operands;
  
   public Product[] getOperands() {
       return operands;
   }
   public void setOperands(Product[] operands) {
       this.operands = operands;
   }
   @Override
   public Double execute() {
       Double sum = 0.0;
      
       for(int i=0;i<operands.length;i++){
           sum += operands[i].execute();
       }
       return sum;
   }
}

class Product implements Expression {
   private Double[] operands;
  
   public Double[] getOperands() {
       return operands;
   }

   public void setOperands(Double[] operands) {
       this.operands = operands;
   }

   @Override
   public Double execute() {
       Double product = 1.0;
       for(int i=0;i<operands.length;i++){
           product *= operands[i];
       }
       return product;
   }
}

class Console {
   public void repl() {
       // I have used console for input
       Scanner tokens1 = new Scanner(System.in);
       // You can use a file even by
       //Scanner tokens1 = new Scanner(new file(\"input.txt\")).useDelimiter(\"\ \");

      
       // this follows the REPL = read-eval-print-loop flow, to check many expression in one
       // execution
       while (tokens1.hasNextLine()) {
           String sopExpression = tokens1.nextLine();
           //System.out.println(\"sopExpression\"+sopExpression);
           if(sopExpression.equals(\"\"))
               break;
          
           Expression exp = parse(sopExpression);
           Double ans = exp.execute();
           System.out.println(\"ans = \"+ans);
       }
   }

   public Expression parse(String line) {
       Expression exp;
       if(line.contains(\"+\")){
          
           Sum sum = new Sum();
           String proList[] = line.split(\"\\\\+\");
           Product[] operands = new Product[proList.length];
          
           for(int i=0;i<proList.length;i++){
               operands[i] = (Product) parse(proList[i]);
           }
           sum.setOperands(operands);
           exp = sum;
          
       }else{
           Product pro = new Product();
          
           String doubsList[] = line.split(\"\\\\*\");
           Double[] operands = new Double[doubsList.length];
          
           for(int i=0;i<doubsList.length;i++){
               operands[i] = Double.parseDouble(doubsList[i]);
           }
           pro.setOperands(operands);
           exp = pro;
       }
       return exp;
   }
}

public class TestExecution {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Console obj = new Console();
       obj.repl();
   }

}

In Java : SOP expressions are sums of products of doubles: Expression ::= Sum | Product Sum ::= Product~(\
In Java : SOP expressions are sums of products of doubles: Expression ::= Sum | Product Sum ::= Product~(\
In Java : SOP expressions are sums of products of doubles: Expression ::= Sum | Product Sum ::= Product~(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site