Question: Using Java please help with step 2 public class Test2 { public static void main (String[] args) { System.out.println(Test 2: Step 1 by (Your Name));

Using Java please help with step 2

public class Test2 {

public static void main (String[] args) {

System.out.println("Test 2: Step 1 by (Your Name)");

Step1 s1 = new Step1();

//Step 2

System.out.println(" Test 2: Step 2 by (Your Name)");

String fileName = "xxxxx.txt";

Step2 s2 = new Step2(fileName);

//Step3

System.out.println(" Test 2: Step 3 by (Your Name)");

int stackSize = 30;

String expression = (a valid expression);

String infix = (will be given);

SymbolTable symbolTable = new SymbolTable(expression);

System.out.println(symbolTable);

String postfix = Step3.convertToPostfix(infix, stackSize);

System.out.println("infix = " + infix);

System.out.println("postfix = " + postfix);

double result = Step3.evaluatePostfix(symbolTable, postfix, stackSize);

System.out.println("Result = " + String.format("xxxxx",result));

}

This program produces the following output:

Test 2: Step 1 by (Your Name)

//one line of output

Test 2: Step 2 by (Your Name)

//Many line of outut

MyArtStack =

//Contents of the stack

Test 2: Step 3 by (Your Name)

//See the main program

infix =

postfix =

Result =

Details:

Step 1:

The class file Step1.java will contain a string and you will split it and produce the result indicated in the output.

Step 2:

The constructor of this class will take the name of the file and put the contents of the file into a stack and then print the stack out.

Step 3:

You will modify the textbooks Postfix.java to work with your MyStack.java and SymbolTable to process the infix and postfix. The main program should give you enough information.

-----------------

postfix.java

------------

/** A class that represents a postfix expression. Based on pseudocode in Segments 5.16 and 5.18. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ public class Postfix { /** Creates a postfix expression that represents a given infix expression. Segment 5.16. @param infix A string that is a valid infix expression. @return A string that is the postfix expression equivalent to infix. */ public static String convertToPostfix(String infix) { StackInterface operatorStack = new LinkedStack(); StringBuilder postfix = new StringBuilder(); int characterCount = infix.length(); char topOperator; for (int index = 0; index < characterCount; index++) { boolean done = false; char nextCharacter = infix.charAt(index); if (isVariable(nextCharacter)) postfix = postfix.append(nextCharacter); else { switch (nextCharacter) { case '^': operatorStack.push(nextCharacter); break; case '+': case '-': case '*': case '/': while (!done && !operatorStack.isEmpty()) { topOperator = operatorStack.peek(); if (getPrecedence(nextCharacter) <= getPrecedence(topOperator)) { postfix = postfix.append(topOperator); operatorStack.pop(); } else done = true; } // end while operatorStack.push(nextCharacter); break; case '(': operatorStack.push(nextCharacter); break; case ')': // Stack is not empty if infix expression is valid topOperator = operatorStack.pop(); while (topOperator != '(') { postfix = postfix.append(topOperator); topOperator = operatorStack.pop(); } // end while break; default: break; // Ignore unexpected characters } // end switch } // end if } // end for while (!operatorStack.isEmpty()) { topOperator = operatorStack.pop(); postfix = postfix.append(topOperator); } // end while return postfix.toString(); } // end convertToPostfix // Indicates the precedence of a given operator. // Precondition: operator is a character that is (, ), +, -, *, /, or ^. // Returns an integer that indicates the precedence of operator: // 0 if ( or ), 1 if + or -, 2 if * or /, 3 if ^, // -1 if anything else. */ private static int getPrecedence(char operator) { switch (operator) { case '(': case ')': return 0; case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; } // end switch return -1; } // end getPrecedence private static boolean isVariable(char character) { return Character.isLetter(character); } // end isVariable /** Evaluates a postfix expression. Segment 5.18 @param postfix a string that is a valid postfix expression. @return the value of the postfix expression. */ public static double evaluatePostfix(String postfix) { StackInterface valueStack = new LinkedStack(); int characterCount = postfix.length(); for (int index = 0; index < characterCount; index++) { char nextCharacter = postfix.charAt(index); switch(nextCharacter) { case 'a': case 'b': case 'c': case 'd': case 'e': valueStack.push(valueOf(nextCharacter)); break; case '+': case '-': case '*': case '/': case '^': Double operandTwo = valueStack.pop(); Double operandOne = valueStack.pop(); Double result = compute(operandOne, operandTwo, nextCharacter); valueStack.push(result); break; default: break; // Ignore unexpected characters } // end switch } // end for return (valueStack.peek()).doubleValue(); } // end evaluatePostfix private static double valueOf(char variable) { switch (variable) { case 'a': return 2.5; case 'b': return 3.0; case 'c': return 4.0; case 'd': return 12.0; case 'e': return 16.5; } // end switch return 0; // Unexpected character } // end valueOf private static Double compute(Double operandOne, Double operandTwo, char operator) { double result; switch (operator) { case '+': result = operandOne.doubleValue() + operandTwo.doubleValue(); break; case '-': result = operandOne.doubleValue() - operandTwo.doubleValue(); break; case '*': result = operandOne.doubleValue() * operandTwo.doubleValue(); break; case '/': result = operandOne.doubleValue() / operandTwo.doubleValue(); break; case '^': result = Math.pow(operandOne.doubleValue(), operandTwo.doubleValue()); break; default: // Unexpected character result = 0; break; } // end switch return result; } // end compute } // end Postfix 

------------

hint

------------

The main program was given as follow:

//Step 2

System.out.println(" Test 2: Step 2 by (Your Name)");

String fileName = "xxxxx.txt";

Step2 s2 = new Step2(fileName);

//Step3

System.out.println(" Test 2: Step 3 by (Your Name)"); int stackSize = 30;

String expression = (a valid expression);

String infix = (will be given);

SymbolTable symbolTable = new SymbolTable(expression);

System.out.println(symbolTable);

String postfix = Step3.convertToPostfix(infix, stackSize);

System.out.println("infix = " + infix); System.out.println("postfix = " + postfix);

double result = Step3.evaluatePostfix(symbolTable, postfix, stackSize);

System.out.println("Result = " + String.format("xxxxx",result));

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

I.On Step 2:

From line 4, you should know that Step2 is a class that takes a file name as the only parameter. I also stated that this step is going to read the file into a stack and print out the stack. Since this is the only Step2-related statement and it produces output, so all actions will be handled in the constructor.

II.On Step3:

b.From line 8, a valid expression is given so you dont have to worry about spaces and invalid character. (Obviously, this expression will be something like 3+5/4.3*5.) This tests if you can build a symbol table from a given expression. (from line 10).

c.Again, we skip some details, I gave you the infix using variables for the expression (in our case here, the infix will be A+B/C*D and each variable represents a different value. Again, in our case, A will be 3, B will be 5,.etc Note that infix and expression are basically same except infix uses variables instead of values.)

d.Given an infix that uses variable, the test checks to see if you can have it converted to its postfix form. And then, check to see if you can evaluate the postfix. In the evaluate process, you will need to get the values of variables from the symbol table that you built.

---------------------

STEP 2

------------

1. Assign "e2arts.txt" to the file Name. i.e., filename = "2earts.txt";

2. Write a simple class called MyArt which contains the following:

a. Two fields: artName, ArtistName.

b. constructor that takes the values of above two fields.

c. toString that prints these two values separated by a tab.

3. Compelete Step2 class according to the following.

a. which contains the following lines inside your program:

int STACK_SIZE = 20;

MyStack myArtStack = new MyStack(STCK_SIZE);

MyArtistList myArtistList = new MyArtistList("e2artists.txt");

e2arts.txt

1038+Spring Flowers*1$800.99

1050+Cattle Ranch*1$10000.99

1103+Trail End*1$8000.50

1042+Coffee on the Trail*2$7544.50

1013+Superstitions*3$78000.40

1021+Bead Wall*3$14000.00

1034+Beaver Pole Jumble*3$28000.00

1063+Asleep in the Garden*3$110000

1070+Beginnings*4$27500.00

1036+Blackhawk*5$25500.00

e2artists.txt

1 Acconci

2 Budd

3 Carpenter

4 Dill

5 Edwards

6 Fleming

7 Garber

8 Higgins

9 Ibe

10 Kollasch

11 Lerman

12 Metz

13 Novarre

14 Ortega

15 Parker

16 Penn

17 Pierobon

18 Prinzen

19 Quiroz

20 Rath

----------------------

MyArtistList class there should be a method called "find" that takes artistID and returns artistName. MyStack this is the generic stack, print out each record of "e2arts.txt" as you read it with '-' character separating two adjacent fields and then push the pair into myArtStack.

-----------------------

Out Put

------------------

Test 2: Step 2 1038-Spring Flowers-1-800.99 1050-Cattle Ranch-1-10000.99 etc

MyArtStack = Blackhawk Edwards Beginnings Dill etc // the first field is artName and the second one is artistName

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!