Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need Help With Homework! Create a dictionary. Add some variable names and associated values to it, then modify your evaluator so that the text string

Need Help With Homework!

Create a dictionary. Add some variable names and associated values to it, then modify your evaluator so that the text string can include the names of variables as well as constants. When you encounter a variable name, look it up in the dictionary and use the value you find there. Treat undefined variables as an error.

Finally, modify your text editor from a previous lab to add the instruction LET to its vocabulary. The syntax for LET should be

LET =

The result should be to evaluate the expression and assign its value to the variable on the left hand side of the -.

So, the following sequence of events:

LET A = 10

LET B = 20

LET C = (A + B) / 2.0

should result in C having the associated value 15.0

Add a PRINT command which will print the contents of a variable: PRINT C should give 15.0 in the above example

Finally, If you have time, think about how you could use your editor to create a series of numbered lines

10 LET A = 10

20 LET B = 20

30 LET C = (A + B) / 2.0

40 PRINT C

and then "run" this program by typing RUN

The resut shoud be to evaluate all three expressions and print the result.

DICTIONARY

public class Dictionary {

private HashClass theDict[];

private int size;

private int used;

public Dictionary(int maxSize) {

this.theDict = new HashClass[maxSize];

this.size = maxSize;

this.used = 0;

}

private int hash(String s) {

int retval = 0;

for(int i = 0; i < s.length(); i++) {

retval += (int)s.charAt(i);

}

return retval % size;

}

public boolean isFull() {

return used >= size-1;

}

private int findWhere(String s) // finds s or where it should be {

int where = this.hash(s);

while (theDict[where] != null && !theDict[where].getKey().equals(s)) {

System.out.println("Ouch");

where = (where + 1) % size;

}

return where;

}

public void delete(String s) throws HashException {

int where = findWhere(s);

if(theDict[where] != null) {

theDict[where] = null;

used--;

}

else {

throw new HashException("Missing Key" + s);

}

}

public double lookup(String s) throws HashException {

int where = findWhere(s);

if(theDict[where] != null)

return theDict[where].getValue();

else{

throw new HashException("Missing Key" + s);

}

public void insert(String s, double val) throws HashException {

if(this.isFull()) {

throw new HashException("Hash table overflow");

}

int where = findWhere(s);

if (theDict[where] == null) {

theDict[where] = new HashClass(s, val);

used++;

}

else{

theDict[where].setValue(val);

}

public String toString() {

String retval = "";

for(int i = 0; i < size; i++) {

if (theDict[i] != null) retval += i + " " + theDict[i] + " ";

}

return retval;

}

MY INFIX CALCULATOR

public static String Calculator(String exps){

exps = exps.replaceAll("\\s+", "");

exps="("+exps+")";

Stack stack = new Stack<>();

StringTokenizer t = new StringTokenizer(exps, "()*/+-", true);

while(t.hasMoreTokens()){ String token = t.nextToken();

if(token.equals("(")|| token.matches("[0.0-9.0]+")|| token.equals("*") || token.equals("/") || token.equals("+")|| token.equals("-")){ stack.push(token); } else if( token.equals(")")){

try {

int Operator2 = Integer.parseInt(stack.pop());

String oprnd = stack.pop();

int Operator = Integer.parseInt(stack.pop());

if(!stack.isEmpty()){

stack.pop();

}

int ans = 0;

switch (oprnd) {

case "*":

ans = Operator*Operator2;

break;

case "/":

ans = Operator/Operator2;

break;

case "+":

ans = Operator+Operator2;

break;

case "-":

ans = Operator-Operator2;

break;

default:

break;

}

stack.push(ans+"");

}

catch (Exception e) {

e.printStackTrace();

break;

}

}

}

String value = "";

try {

value = stack.pop();

}

catch (Exception e) {

;

}

return value;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

How will the members be held accountable?

Answered: 1 week ago

Question

a. Do team members trust each other?

Answered: 1 week ago