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...
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...
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.       ...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
based on the code below, answer the questions Question 1: The LinkedList class uses another class...
based on the code below, answer the questions Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are the fields in the Node class? Question 2: The Node class uses generics. Why? Question 3: The linkFirst method contains the following lines of code: if (f == null) last = newNode; else f.prev = newNode; What is the value of the linked list’s size attribute at this point in the code if f ==...