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

Write a class to evaluate arithmetic expressions, using the generic stack class

ID: 3672595 • Letter: W

Question

Write a class to evaluate arithmetic expressions, using the generic stack class from Part 1. The arithmetic operators are +, -,   *, /, %, and ^ . The minus sign always represents a subtraction. The order of evaluation can be altered by using brackets. Assume that all values involved are integer values and that the result of any operation is also an integer. The highest priority is ^ and lowest priority is +.

Data members:

-A String to store the arithmetic expression

-An integer to store the result of the evaluation of the expression

Public methods:

-A zero-parameter constructor to set the arithmetic expression to the null String and the result to zero

-void evaluate (String) method to evaluate the given arithmetic expression (parameter) to obtain the result. Make sure to add a blank at the end of the given String before you start processing it.

-String toString() returns the arithmetic expression followed by the equal sign and then the result.:

Evaluate as you process the string, taking into consideration the priority of operator and brackets, making use of an Integer stack and a Character stack.

Only go through the String with the arithmetic expression once, from left to right.

Code private methods to efficiently produce the result.

Part 1 is already done as posted below, minus the copy constructor which I'm still trying to figure out.

import java.util.ArrayList;

public class MyStack<T>

{
   private ArrayList<T> stack;

   public MyStack()
   {
       stack = new ArrayList<T>();
   }

  
   public MyStack (MyStack<T> oldStack)
   {

   }


   public boolean isEmpty()
   {
       return size()==0;

   }

   public void push (T value)
   {
       stack.add(value);

   }


   public T pop()
   {
       if (size() != 0)
       {
           return stack.remove(size()-1);
       }
       else
       {
           System.out.println("Empty list, cannot list.");
           return null;
       }

   }

   public T lookUp()
   {
       if (size() != 0)
       {
           return stack.get(size()-1);
       }
       else
       {
           System.out.println("Empty list, cannot look up.");
           return null;
       }
   }

   public int size()
   {
       return stack.size();
   }
}

Explanation / Answer

step 2:

Step 2.1:

}

step 3:

step 4:

step 5:

step 6:

step 7:

step 8:

step 9:

step 10:

  public abstract class Expression  {      public abstract decimal Evaluate();      public abstract string toString();  }  

step 2:

  public abstract class ValueNode:Expression  {      public int intvalue;      public decimal decvalue;      public abstract decimal TEvaluate();      public abstract string TtoString();        public override decimal Evaluate()      {          return TEvaluate();      }        public override string toString()      {          return TtoString();      }  }  

Step 2.1:

  public abstract class OperationNode:Expression  {     public Expression left;     public Expression right;     public override decimal Evaluate()      {          return this.EEvaluate();      }      public override string toString()      {          return this.EtoString();      }      public abstract decimal EEvaluate();      public abstract string EtoString();  

}

step 3:

  public class UnaryMinus:OperationNode  {      public UnaryMinus(Expression Left)       {          this.left = Left;        }      public override decimal EEvaluate()      {          return -(this.left.Evaluate());      }        public override string EtoString()      {          return string.Format("(-({0}))",left.toString()); ;      }    }  

step 4:

  public class DecimalClass:ValueNode  {      public DecimalClass(decimal decimalValue)      {          this.decvalue = decimalValue;      }        public override decimal TEvaluate()      {          return this.decvalue;      }        public override string TtoString()      {          return this.decvalue.ToString();      }  }  

step 5:

  public class Integer : ValueNode  {      public Integer(int decimalValue)        {          this.intvalue = decimalValue;      }     public override decimal TEvaluate()        {          return this.intvalue;      }        public override string TtoString()        {          return this.intvalue.ToString();      }  }  

step 6:

  public  class Addition:OperationNode   {       public Addition(Expression Left, Expression Right)        {          this.left = Left;          this.right = Right;      }      public override decimal EEvaluate()      {          return left.Evaluate()+ right.Evaluate();      }      public override string EtoString()      {          return string.Format("({0}+{1})",left.toString(),right.toString()); ;      }  }  

step 7:

  public class Multiplication : OperationNode    {      public Multiplication(Expression Left, Expression Right)      {          this.left = Left;          this.right = Right;      }      public override decimal EEvaluate()      {          return left.Evaluate()* right.Evaluate();      }        public override string EtoString()      {          return string.Format("({0}*{1})",left.toString(),right.toString()); ;      }  }  

step 8:

  public class Substraction:OperationNode  {     public Substraction(Expression Left, Expression Right)      {          this.left = Left;          this.right = Right;      }      public override decimal EEvaluate()      {          return left.Evaluate()- right.Evaluate();      }        public override string EtoString()      {          return string.Format("({0}-{1})",left.toString(),right.toString()); ;      }  }  

step 9:

  public  class Division: OperationNode  {     public Division(Expression Left, Expression Right)      {          this.left = Left;          this.right = Right;      }      public override decimal EEvaluate()      {          return left.Evaluate()/ right.Evaluate();      }        public override string EtoString()      {          return string.Format("({0}/{1})",left.toString(),right.toString()); ;      }  }  

step 10:

  class Program  {      static void Main(string[] args)      {            callComposit();          Console.ReadKey();      }        private static void callComposit()      {          //Expression ((2.5+3.5)*(-(5)))          Multiplication multiplication = new Multiplication(new Addition(new DecimalClass(2.5m), new DecimalClass(3.5m)), new UnaryMinus(new Integer(5)));          Console.WriteLine(string.Format("   Expression {0} resulted in {1}", multiplication.toString(), multiplication.Evaluate()));                //Expression (5/6)          Division division = new Division(new Integer(5), new Integer(6));          Console.WriteLine(string.Format("   Expression {0} resulted in {1}", division.toString(), division.Evaluate()));          //Expression ((2.5-3.5)*(-(5)))          Multiplication multiplication2 = new Multiplication(new Substraction(new DecimalClass(2.5m), new DecimalClass(3.5m)), new UnaryMinus(new Integer(5)));          Console.WriteLine(string.Format("   Expression {0} resulted in {1}", multiplication2.toString(), multiplication2.Evaluate()));          //Expression ((2.5/3.5)*(-(5)))          Multiplication multiplication3 = new Multiplication(new Division(new DecimalClass(2.5m), new DecimalClass(3.5m)), new UnaryMinus(new Integer(5)));          Console.WriteLine(string.Format("   Expression {0} resulted in {1}", multiplication3.toString(), multiplication3.Evaluate()));            //Expression ((2.5/3.5)*(-(5))* 3.5)          Multiplication multiplication4 = new Multiplication(new Multiplication(new Division(new DecimalClass(2.5m), new DecimalClass(3.5m)), new UnaryMinus(new Integer(5))), new DecimalClass(3.5m));          Console.WriteLine(string.Format("   Expression {0} resulted in {1}", multiplication4.toString(), multiplication4.Evaluate()));              //Expression ( 3.5*(2.5/3.5)*(-(5)))          Multiplication multiplication5 = new Multiplication(new Multiplication(new DecimalClass(3.5m), new Division(new DecimalClass(2.5m), new DecimalClass(3.5m))), new UnaryMinus(new Integer(5)));          Console.WriteLine(string.Format("   Expression {0} resulted in {1}", multiplication5.toString(), multiplication5.Evaluate()));            //Expression ( 3.5*(2.5/3.5)+ 3.5 *(-(5)))          Multiplication multiplication6 = new Multiplication(new Addition(new Multiplication(new DecimalClass(3.5m), new Division(new DecimalClass(2.5m), new DecimalClass(3.5m))), new DecimalClass(3.5m)), new UnaryMinus(new Integer(5)));          Console.WriteLine(string.Format("   Expression {0} resulted in {1}", multiplication6.toString(), multiplication6.Evaluate()));      }  }  
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote