Question

Code in JAVA The requirements are as follows: The input will be in a text file...

Code in JAVA

The requirements are as follows:

The input will be in a text file whose name is given by arg[0] of main().

It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers.

Need help on the main and fixing the Queue.

//Input:

( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) )
( ( 1 + 2 ) - ( 3 - 4 ) )
( ( ( 1 + 2 ) - 3 ) - 4 )
( ( 1 + ( 2 - 3 ) - 4 ) )
( 1 + ( 2 - ( 3 - 4 ) ) )

//output:

Expression is: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) )
Postfix is 1 2 + 3 4 - 7 2 - + -
Value is -1.0

Expression is: ( ( 1 + 2 ) - ( 3 - 4 ) )
Postfix is 1 2 + 3 4 - -
Value is 4.0

Expression is: ( ( ( 1 + 2 ) - 3 ) - 4 )
Postfix is 1 2 + 3 - 4 -
Value is -4.0

Expression is: ( ( 1 + ( 2 - 3 ) - 4 ) )
Postfix is 1 2 3 - 4 - +
Value is -4.0

Expression is: ( 1 + ( 2 - ( 3 - 4 ) ) )
Postfix is 1 2 3 4 - - +
Value is 4.0

//List:

public class List
{
   /**
   * Create an empty List
   */
   public List()
   {
       mFront = new Node(Node.DUMMY);
       mRear = new Node(Node.DUMMY);
       mFront.mNext = mRear;
       mRear.mPrev = mFront;
   }
  
   /**
   * Create a new List that is a copy of the list input
   * @param list the List to copy
   */
   public List(List list)
   {
       this();
       for(Node current = list.mFront.mNext; current != list.mRear; current = current.mNext)
       {
           addToRear(current.mData);
       }
   }
      
   /**
   * Item becomes the new front element
   * @param item the item to add
   */
   public void addToFront(String item)
   {
       Node newNode = new Node(item);
       newNode.mPrev = mFront;
       newNode.mNext = mFront.mNext;
       newNode.mPrev.mNext = newNode;
       newNode.mNext.mPrev = newNode;
   }
  
   /**
   * Item becomes the new rear element
   * @param item the item to add
   */
   public void addToRear(String item)
   {
       Node newNode = new Node(item);
       newNode.mNext = mRear;
       newNode.mPrev = mRear.mPrev;
       newNode.mPrev.mNext = newNode;
       newNode.mNext.mPrev = newNode;
   }
  
   /**
   * If beforeItem is not in List, prints "Item Not Found" else add item in List before beforeItem
   * @param beforeItem the item in the list to add item before
   * @param item the item to add
   */
   public void addBeforeItem(String beforeItem, String item)
   {
       Node beforeNode = find(beforeItem);
       if(beforeNode == null)
       {
           sop("Item Not Found");
           return;
       }
  
       Node newNode = new Node(item);
       newNode.mPrev = beforeNode.mPrev;
       newNode.mNext = beforeNode;
       newNode.mPrev.mNext = newNode;
       newNode.mNext.mPrev = newNode;
   }
  
   /**
   * Item becomes the element after afterItem
   * @param afterItem the item in the list to add item before
   * @param item the item to add
   */
   public void addAfterItem(String afterItem, String item)
   {
       Node afterNode = find(afterItem);
       if(afterNode == null)
       {
           sop("Item Not Found");
           return;
       }
      
       Node newNode = new Node(item);
       newNode.mPrev = afterNode;
       newNode.mNext = afterNode.mNext;
       newNode.mNext.mPrev = newNode;
       newNode.mPrev.mNext = newNode;
   }
  
   /**
   * Returns the item at the front of the List (List is not altered)
   * @return the item at the front of the List
   */
   public String getFront()
   {
       if(askCount() == 0)
       {
           sop("List Empty");
           return "";
       }
      
       return(mFront.mNext.mData);
   }
  
   /**
   * Returns the item at the rear of the List (List is not altered)
   * @return the item at the rear of the List
   */
   public String getRear()
   {
       if(askCount() == 0)
       {
           sop("List Empty");
           return "";
       }
      
       return(mRear.mPrev.mData);
   }
  
   /**
   * Return true if item is in List, false otherwise
   * @param item to check presence in List
   * @return true if item is in List, false otherwise
   */
   public boolean isPresent(String item)
   {
       if(find(item) != null) return true;
       else return false;
   }
  

//Queue:


public class Queue
{
   private List mQueue;
  
   public Queue()
   {
       mQueue= new List();
   }
  
   public void add(String item)
   {
       mQueue.addToRear(item);
   }

   public String remove()
   {
       return mQueue.removeRear();

   }
  
   public boolean isEmpty()
   {
       return mQueue.askCount() == 0;
   }
  
   public void dump(String title)
   {
       mQueue.print(title);
   }
}


   /**
   * Returns the number of items in the List
   * @return the number of items in the List
   */
   public int askCount()
   {
       int count = 0;
       Node current = mFront.mNext;
       while(current != mRear)
       {
           current = current.mNext;
           ++count;
       }
      
       return count;
   }
  
   /**
   * If the List is empty, prints "List Empty"
   * If the List is not empty, removes the item at the front of the List
   */
   public void removeFront()
   {
       if(askCount() == 0)
       {
           sop("List Empty");
           return;
       }
       removeItem(mFront.mNext.mData);
   }
  
   /**
   * If the List is empty, prints "List Empty"
   * If the List is not empty, removes the item at the rear of the List
   */
   public void removeRear()
   {
       if(askCount() == 0)
       {
           sop("List Empty");
           return;
       }
       removeItem(mRear.mPrev.mData);
   }
  
   /**
   * If the List is empty, prints "List Empty"
   * If item is not present in List, prints "Item not found"
   * Otherwise, item is removed from the List
   * @param item the item to remove
   */
   public void removeItem(String item)
   {
       if(askCount() == 0)
       {
           sop("List Empty");
           return;
       }
      
       Node removeNode = find(item);
       if(removeNode == null)
       {
           sop("Item not found");
           return;
       }
      
       removeNode.mPrev.mNext = removeNode.mNext;
       removeNode.mNext.mPrev = removeNode.mPrev;
   }
  
   /**
   * Print title on a line by itself
   * Prints the List from front to rear with 1 space between each item
   * @param title the description of the List
   */
   public void print(String title)
   {
       System.out.println("\n" + title);
       for(Node current = mFront.mNext; current != mRear; current = current.mNext)
       {
           System.out.print(current.mData + " ");
       }
       System.out.println("");
   }
  
   /**
   * Print title on a line by itself
   * Prints the Sorted List with 1 space between each item
   * Does not alter the List
   * @param title the description of the List
   */
   public void printSorted(String title)
   {
       ArrayList tempList = new ArrayList();
       for(Node current = mFront.mNext; current != mRear; current = current.mNext)
       {
           tempList.add(current.mData);
       }
      
       Collections.sort(tempList);
       System.out.println("\n" + title);
       for(String s : tempList)
       {
           System.out.print(s + " ");
       }
      
       System.out.println("");
      
   }
  
   private class Node
   {
       public Node(String data)
       {
           mData = data;
           mPrev = null;
           mNext = null;
       }
      
       private static final String DUMMY= "dummy";
       private String mData;
       private Node mPrev;
       private Node mNext;
   }
  
   private Node find(String item)
   {
       for(Node current = mFront.mNext; current != mRear; current = current.mNext)
       {
           if(current.mData.equals(item)) return current;
       }
      
       return null;
   }
  
   private static void sop(String s)
   {
       System.out.println(s);
   }
      
   private Node mFront;
   private Node mRear;
}

//Stack:

public class Stack
{
   private List mStack;
   /**
   * Create an empty Stack
   * @param myList Create an empty Stack
   */
   public Stack()
   {
       mStack = new List();
   }
   /** Make item the Top of the Stack
   *
   * @param item Make item the Top of the Stack
   */
   public void push(String item)
   {
       mStack.addToRear(item);
}
  
   /**
   * Remove the Top of the Stack
   * @param i
   */
   public void pop()
{
       mStack.removeRear();
}
   /**
   *
   * @return the Top of the Stack –do not remove it
   */
   public String getTop()
   {
       return mStack.getRear();
   }
   /**
   *
   * @return true if Stack is empty, false otherwise
   */
   public boolean isEmpty()
   {
       return mStack.askCount() == 0;
   }
}

main:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

public class AssignmentSeven
{
  
   public static void main(String[] args)
   {
       ArrayList input = getInput(args[0]);
       printInput(input);
   }

   private static ArrayList getInput(String fileName)
   {
   try
   {
       ArrayList input = new ArrayList();
       Scanner sc = new Scanner(new BufferedReader(new FileReader(fileName)));
  
   while(sc.hasNextLine()) input.add(sc.nextLine());
   sc.close();
  
   return input;
  
   }
   catch(Exception exc)
   {
       System.out.println("Exception: " +exc.getMessage() + " opening input file " + fileName);
       System.exit(0);
   }
     
       return null;
   }

Homework Answers

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may not use the original methods of the stack api to answer. import java.util.NoSuchElementException; import edu.princeton.cs.algs4.Stack; public class StringQueue {    //You may NOT add any more fields to this class.    private Stack stack1;    private Stack stack2;    /**    * Initializes an empty queue.    */    public StringQueue() { //TODO    }    /**    * Returns true if this queue...
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Using SLLStack.java, implement an application, LineReverser.java, that reads a line from keyboard (user enters one line),...
Using SLLStack.java, implement an application, LineReverser.java, that reads a line from keyboard (user enters one line), insert each separate word into the stack, and read/print on the screen. So, if the user enters: This is a test the application output should be: test a is This. This is SLLSTACK.java below package linked_lists; public class SLLStack {    //instance variabels    private SLLNode top;    private int numOfItems;       //constructors    public SLLStack() {        top = null;   ...
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node...
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node next; } public boolean isEmpty() { return (first == null); } public void push(String item) { // Insert a new node at the beginning of the list. Node oldFirst = first; first = new Node(); first.item = item; first.next = oldFirst; } public String pop() { // Remove the first node from the list and return item. String item = first.item; first = first.next;...
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is: 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 -...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT