JAVA programming question using Data Structures

JAVA programming question using Data Structures

HW1 Java Code File:

Use the following files from the Stack Code Files folder as well as the code below:

  • • StackInterface.java
  • • ArrayStack.java (but with the methods given in the Answer to Lab Exercise 2.1, AND as specified on the assignment)
  • • LinkedStack.java (but the FIXED version as shown in the Answer to Lab Exercise 2.2)

// YOU WRITE THE WHOLE PostfixExpression.java class as described on the assignment

// Use this method for tokenizing the postfix expression String

// (remember, it’s a private instance method in the PostfixExpression class):

// tokens is the name of my instance ArrayList of Strings variable

private void tokenize()

{

String [] tokenArray = wholeExpr.split(“[ t]+”);

tokens.clear(); // clear the ArrayList

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

{

tokens.add(tokenArray[i]); // add the next token to the ArrayList

}

} // end tokenize

ALGORITHM FOR CONVERTING A POSTFIX EXPRESSION TO AN INFIX EXPRESSION (IF IMPLEMENTED CORRECTLY, THIS WILL NOT ALLOW TOO MANY OPERANDS OR TOO MANY OPERATORS):

  1. 1. Initialize empty stack (of Strings)
  2. 2. For every token in the postfix expression list:
    1. 1. If the token is an operator:
      1. 1. Check if the stack contains the sufficient number of values (usually two) for given operator
      2. 2. If there are not enough values, return a String with “error”
      3. 3. Remove the top 2 items (pop the right operand, then left operand)
      4. 4. Create a string with “(“+left+ token+right+”)”
      5. 5. Push the string on the stack
    2. 2. If the token is an operand (number), push it on the stack
    3. 3. If the stack contains only one value, return it as a final result of the conversion

Otherwise, return a String with “error”

ALGORITHM FOR EVALUATION THE POSTFIX EXPRESSION (this method returns a boolean), GIVEN A LIST OF Strings (IF IMPLEMENTED CORRECTLY, THIS WILL NOT ALLOW TOO MANY OPERANDS OR TOO MANY OPERATORS):

  1. 1. Initialize empty stack (LinkedStack of Doubles)
  2. 2. For every token in the postfix expression list:
  3. 1. If the token is an operator:
    1. 1. Check if the stack is empty (break from the loop it’s in)
    2. 2. Get the value from the top of the stack and store in a variable for the right operand
    3. 3. Pop the top of the stack
    4. 4. Check if the stack is empty (break from the loop it’s in)
    5. 5. Get the value from the top of the stack and store in a variable for the left operand
    6. 6. Pop the top of the stack
    7. 7. Evaluate the operator using the left and right operand values and push the single result on the stack (SHOULD USE A SWITCH)
  4. 2. If the token is an operand (number), push it on the stack (converted to a Double) (optional: check
  5. 3. If the stack contains only one value, the top value is assigned to the result and return true
  6. 4. Otherwise, assign Double.NaN to the result and return false

//—————————————————————————–

// WRITE MAIN as described on the assignment and using the following method & variable:

//USE THE FOLLOWING IN YOUR MAIN FILE for opening the input file:

public static Scanner userScanner = new Scanner(System.in);

public static Scanner openInputFile()

{

String filename;

Scanner scanner=null;

System.out.print(“Enter the input filename: “);

filename = userScanner.nextLine();

File file= new File(filename);

try{

scanner = new Scanner(file);

}// end try

catch(FileNotFoundException fe){

System.out.println(“Can’t open input filen”);

return null; // array of 0 elements

} // end catch

return scanner;

} // end openInputFile

// call this method as indicated in HW#1, and don’t change the method NOR

//the size of your ArrayStack:

public static void stackTester()

{

ArrayStack<String> arrStack = new ArrayStack<>();

LinkedStack<String> linkStack = new LinkedStack<>();

String [] strArray = {“A”,”B”,”C”,”D”,”E”,”F”,”G”,”H”,”I”,”J”};

String temp;

// Testing ArrayStack

System.out.println(“nTesting the ArrayStack:”);

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

{

temp = strArray[i]+” 1″;

if(arrStack.push(temp))

System.out.println(“Pushed “+temp+”, size is now= “+arrStack.size());

else

System.out.println(“Error: couldn’t push “+ temp+”, size is now= “

+arrStack.size());

}

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

{

temp = strArray[i]+” 2″;

System.out.println(“Trying to push “+temp);

if(!arrStack.push(temp))

{

System.out.println(“Out of space, removing ” +

arrStack.pop());

if(arrStack.push(temp))

System.out.println(“Pushed ” + temp);

else

System.out.println(“Error pushing “+temp);

}

}

System.out.println(“The size of the ArrayStack is now ” + arrStack.size());

for( int i=0; i < strArray.length/2; ++i )

{

System.out.println(“Popping “+ arrStack.pop());

}

System.out.println(“The size of the ArrayStack is now ” + arrStack.size());

temp = strArray[0] + ” 3″;

if(arrStack.push(temp))

System.out.println(“Pushed “+temp+”, size is now= “+arrStack.size());

else

System.out.println(“Error: couldn’t push “+ temp+”, size is now= “

+arrStack.size());

while( !arrStack.isEmpty() )

{

System.out.println(“Popping “+ arrStack.pop());

}

System.out.println(“The size of the ArrayStack is now ” + arrStack.size());

// testing LinkedStack

System.out.println(“nTesting the LinkedStack:”);

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

linkStack.push(strArray[i] + ” 4″);

System.out.println(“The size of the LinkedStack is ” + linkStack.size());

for( int i=0; i < strArray.length/2; ++i )

{

System.out.println(“Popping ” + linkStack.pop());

}

System.out.println(“The size of the LinkedStack is now ” + linkStack.size());

while( !linkStack.isEmpty() )

{

System.out.println(“Popping “+ linkStack.pop());

}

System.out.println(“The size of the LinkedStack is now ” + linkStack.size());

} // end stackTester

"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.


Discount Code: CIPD30



Click ORDER NOW..

order custom paper