In this part you will first design using pseudo code two ver

In this part, you will first design using pseudo code two versions of arithmetic calculators. The first version will be based on the pseudo code that uses two different stacks (Array Stack based and Linked list stack based). The second version must be completely based on recursion and must not use any stack (however, we know by now that recursion will implicitly use JVM’s own stack as in regular function calls). You will implement the first version in Java. Your arithmetic calculators must read lines of text from a text file, where each line contains a syntactically correct arithmetic expression. Your output file must repeat the input line and print the computed result on the next line.

Your calculators must support the following operators on integers and observe the standard operator precedence as shown in the following (1 to 8: 1 is highest and 8 is lowest. Operators of same precedence are evaluated from left to right).

1. Parentheses (possibly nested ones): ( , ) Unary operators:

2. factorial: !

3. minus: - Binary operators:

power function: x^y.

operators: *, /

operators: +, -

operators: >, , , <

operators: ==, !=

As part of this programming assignment, you will do the following:

a) Design and submit the pseudo-code for both the versions of arithmetic calculator.

b) Implement a Java program for the first version. In your program, you will implement and use your own array-based stack (and not the built-in Java stack) of variable size based on the doubling strategy.

c) Briefly explain the time and memory complexity of both versions of your calculator. You can

write your answer in a separate file and submit it together with the other submissions.

d) Provide test logs for the first version for at least 20 different and sufficiently complex arithmetic expressions that use all types of operators (including parentheses) in varying

combinations.

Solution

import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class Cal extends Applet implements ActionListener { String msg=\" \"; int v1,v2,result; TextField t1; Button b[]=new Button[10]; Button add,sub,mul,div,clear,mod,EQ; char OP; public void init() { Color k=new Color(120,89,90); setBackground(k); t1=new TextField(10); GridLayout gl=new GridLayout(4,5); setLayout(gl); for(int i=0;i<10;i++) { b[i]=new Button(\"\"+i); } add=new Button(\"add\"); sub=new Button(\"sub\"); mul=new Button(\"mul\"); div=new Button(\"div\"); mod=new Button(\"mod\"); clear=new Button(\"clear\"); EQ=new Button(\"EQ\"); t1.addActionListener(this); add(t1); for(int i=0;i<10;i++) { add(b[i]); } add(add); add(sub); add(mul); add(div); add(mod); add(clear); add(EQ); for(int i=0;i<10;i++) { b[i].addActionListener(this); } add.addActionListener(this); sub.addActionListener(this); mul.addActionListener(this); div.addActionListener(this); mod.addActionListener(this); clear.addActionListener(this); EQ.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); char ch=str.charAt(0); if ( Character.isDigit(ch)) t1.setText(t1.getText()+str); else if(str.equals(\"add\")) { v1=Integer.parseInt(t1.getText()); OP=\'+\'; t1.setText(\"\"); } else if(str.equals(\"sub\")) { v1=Integer.parseInt(t1.getText()); OP=\'-\'; t1.setText(\"\"); } else if(str.equals(\"mul\")) { v1=Integer.parseInt(t1.getText()); OP=\'*\'; t1.setText(\"\"); } else if(str.equals(\"div\")) { v1=Integer.parseInt(t1.getText()); OP=\'/\'; t1.setText(\"\"); } else if(str.equals(\"mod\")) { v1=Integer.parseInt(t1.getText()); OP=\'%\'; t1.setText(\"\"); } if(str.equals(\"EQ\")) { v2=Integer.parseInt(t1.getText()); if(OP==\'+\') result=v1+v2; else if(OP==\'-\') result=v1-v2; else if(OP==\'*\') result=v1*v2; else if(OP==\'/\') result=v1/v2; else if(OP==\'%\') result=v1%v2; t1.setText(\"\"+result); } if(str.equals(\"clear\")) { t1.setText(\"\"); } } }
In this part, you will first design using pseudo code two versions of arithmetic calculators. The first version will be based on the pseudo code that uses two d

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site