Using Java please help with step 2 public class Test2 { public static void main
ID: 3600162 • Letter: U
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)");
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 textbook’s 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
------------
------------
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 don’t 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 <artName, artistName> 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
----------------
MyStack.java
-----------
import java.util.Arrays;
import java.util.EmptyStackException;
public final class MyStack<T>
{
private T[] stack;
private int topIndex;
private boolean initialized = false;
private static final int DEFAULT_CAPACITY = 20;
private static final int MAX_CAPACITY = 5000;
public MyStack()
{
this(DEFAULT_CAPACITY);
} // end default constructor
public MyStack(int initialCapacity)
{
checkCapacity(initialCapacity);
@SuppressWarnings("unchecked")
T[] tempStack = (T[])new Object[initialCapacity];
stack = tempStack;
topIndex = -1;
initialized = true;
}
public void push(T newEntry)
{
checkInitialization();
ensureCapacity();
stack[topIndex + 1] = newEntry;
topIndex++;
}
public T peek()
{
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
return stack[topIndex];
}
public T pop()
{
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
{
T top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
return top;
}
}
public boolean isEmpty()
{
return topIndex < 0;
}
public void clear()
{
checkInitialization();
while (topIndex > -1)
{
stack[topIndex] = null;
topIndex--;
}
}
private void checkInitialization()
{
if (!initialized)
throw new SecurityException ("ArrayStack object is not initialized properly.");
}
private void checkCapacity(int capacity)
{
if (capacity > MAX_CAPACITY)
throw new IllegalStateException("Attempt to create a stack " +
"whose capacity exceeds " +
"allowed maximum.");
}
private void ensureCapacity()
{
if (topIndex >= stack.length - 1)
{
int newLength = 2 * stack.length;
checkCapacity(newLength);
stack = Arrays.copyOf(stack, newLength);
}
}
}
//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));
Explanation / Answer
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 textbook’s 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
------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.