Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

use java to solve this! make sure the program can be used in any number of input

ID: 3887632 • Letter: U

Question

use java to solve this! make sure the program can be used in any number of inputs

Problem 2: Maximum Evaluation (ignoring precedence) Given an expression using only * and + binary operators and integer operands we want to "evaluate it" such that we obtain the largest possible result. For our task we do not follow the usual arithmetic precedence of doing multiplication (denoted by *) before addition (denoted by +). For example, 5 32 can be evaluated as (with parentheses used to denote operator ordering) (5+3) *2=16 or 5+(3 2)-11 Input will be one line containing a positive integer n, followed by n lines of test expressions, each with at least 1 and at most 200 operators. Each operator and operand is separated by at least one space character. For each case, output the maximum evaluation value over all possible operator orderings Each integer in the input expression can fit in a 32-bit word. However, note you may need to use a multi-precision data type (e.g., BigInteger) to express some of the answers for the harder test data Sample Input 5 32 100100 100100 100 100 100 1-43 Sample Output 16 15

Explanation / Answer


import java.util.Scanner;
public class Maxevaluation {

   public static void main(String[] args) {
      
      
      
          Scanner in = new Scanner(System.in);
          System.out.println("Please enter number of lines to evaluate: ");
        
          int n=in.nextInt();
      
          String[] str=null;
          StringBuilder sb=new StringBuilder();
          int opt=0;
          while (n>=0)
          {
             String input = in.nextLine();
             input=input.replaceAll("( )+"," ");
              str=input.split(" ");
            
              for(int i=0;i<str.length-2;i=i+2){
              
               int r1=Integer.parseInt(str[i]);
               int r2=Integer.parseInt(str[i+2]);
              
               if(str[i+1].equals("+")){
                   r1=r1+r2;
               }
               else if(str[i+1].equals("-")){
                   r1=r1-r2;
               }
               else if(str[i+1].equals("*")){
                   r1=r1*r2;
               }
               else if(str[i+1].equals("/")){
                   r1=r1/r2;
               }
               else if(str[i+1].equals("%")){
                   r1=r1%r2;
               }
               else{
                   System.out.println("invalid operator: "+str[i+1]);
               }
              
               opt=r1;
               str[2]=r1+"";
              
             }
              sb.append(opt).append(" ");
           
             n--;
          }
         in.close();
          String convert=sb.toString();
          String[] output=convert.split(" ");
          for(int i=1;i<output.length;i++)
      System.out.println(output[i]);
   }
  
}

/*

output:-

==========

Please enter number of lines:
3
5 + 3   *    2
100 * 100 + 100 * 100 * 100 + 100 * 100
-1 + -4 *   -3
16
10000
15


*/