import java.util.Scanner;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintWriter;/*Tiffany Miller * tmm211 * February 20 2008 * The purpose of this program is to define a class that encapsulates the implementation * of a stack, use that stack in the applications of previous assignements, and practice * arrays. */public class P5{ public static void main(String[] args) { Scanner inputFile = null; PrintWriter outputFile = null; boolean error = false; char operand = ' '; String operation = ""; String line =""; int linecounter= 2; Stack[] Hanoi= new Stack[3]; //The try, catch loops are used to input file hanoi.txt and output //the file to P5.txt. try { inputFile = new Scanner(new FileInputStream("hanoi.txt")); } catch(FileNotFoundException e) { System.out.println("hanoi.txt was not found or could not be opened"); System.exit(0); } try { outputFile = new PrintWriter(new FileOutputStream("P5.txt")); } catch(FileNotFoundException e) { System.out.println("P5.txt could not be opened or created"); System.exit(0); } for( int i = 0; i< Hanoi.length; i++) Hanoi[i] = new Stack(); line= inputFile.nextLine(); Hanoi[0]= new Stack(line); while ((inputFile.hasNextLine()) && (error == false)) { operation = inputFile.next(); inputFile.nextLine(); if(operation.equals("push0") || operation.equals("push1") || operation.equals("push2")) { int n = operation.charAt(5); if(operand!=' ' && (Hanoi[n].isEmpty() || operand < Hanoi[n].getTop())) { Hanoi[n].push(operand); operand = ' '; } else { outputFile.println("Invalid push at line: " + linecounter +". Letter is too big to be pushed onto stack0"); error = true; } } else if(operation.equals("pop0") || operation.equals("pop1") || operation.equals("pop2")) { int n = operation.charAt(5); if (!Hanoi[1].isEmpty()) { Hanoi[n].pop(); } else { outputFile.println("Invalid pop at line: " + linecounter + ". Stack is empty"); error= true; } } outputFile.println("operand=" + operand); linecounter++; } outputFile.println("Stack0: " + Hanoi[0].toString()); outputFile.println("Stack1: " + Hanoi[1].toString()); outputFile.println("Stack2: " + Hanoi[2].toString()); inputFile.close(); outputFile.close(); }}class Stack { private String stack; public Stack () //constructor creates empty stack { stack = ""; } public Stack (String St) //constructor a stack to its initial contents { stack = St; } public boolean isEmpty () //returns a boolean that says whether there is data in the stack { if(stack.length() == 0) return true; else return false; } public char getTop () // an accessor that returns the char at the top of the stack { char operand; operand = stack.charAt(0); return operand; } public void push (char c) // void method with a char argument that pushes char on stack { stack = c + stack; } public char pop() // does implementation of pop method, returning a char { char temp; temp = stack.charAt(0); stack = stack.substring(1); return temp; } public String toString() //needed for printing stack to file { return stack; } public boolean equals(Stack other) //helps compare two stacks { if(stack.equals(other.stack)) return true; else return false; } }