Create a Stack class and the main(). Stack must contain one of our previous List
ID: 3802130 • Letter: C
Question
Create a Stack class and the main(). Stack must contain one of our previous List classes as a private instance variable. All the methods of class must work by simply calling a method of the contained List class. The Stack can NOT contain ArrayList. It must contain List. Need help in my stack and main.
//inputs:
//outputs:
Expression: 5 2 - 6 7 + * 3 /
Value: 13.0
Expression: 20 2 / 2 / 2 /
Value: 2.5
Expression: 6 2 + 3 - 4 * 7 /
Value: 2.857142857142857
Expression: 90 sin
Value: 1.0
Expression: 180 cos
Value: -1.0
Expression: 270 sin
Value: -1.0
Expression: 45 tan
Value: 0.9999999999999999
Expression: 135 tan
Value: -1.0000000000000002
Expression: 225 tan
Value: 0.9999999999999997
//Class List:
import java.util.ArrayList;
import java.util.Collections;
/**
* @author
* List with using Imported Java Array List
*
*/
public class List
{
private ArrayList<String> mList;
/**
* Create a List with the indicated size
*
* @param size the maximum number of items in the List
*/
public List()
{
mList = new ArrayList<String>();
}
public List(List myList)
{
mList = new ArrayList<String>();
for(int i = 0; i < myList.askCount();i++)
{
mList.add(myList.getMember(i));
}
}
private String getMember(int index)
{
return mList.get(index);
}
/**
* If the List is not full, item becomes the new front element
*
* @param item the item to add
*/
public void addToFront(String item)
{
mList.add(0, item);
}
/**
* If the List is not full, item becomes the new rear element
*
* @param item the item to add
*/
public void addToRear(String item)
{
mList.add(item);
}
/**
* If the List is not full and beforeItem is in the List item becomes the element before beforeItem
* If the List is full, prints "List Full"
* If List is not full but beforeItem is not in List, prints "Item Not Found"
*
* @param beforeItem the item in the list to add item before
* @param item the item to add to the list
*/
public void addBeforeItem(String beforeItem, String item)
{
if (isPresent(beforeItem))
{
int index = 0;
for (String s : mList)
{
if (s.equals(beforeItem))
{
mList.add(index, item);
break;
}
index++;
}
} else {
System.out.println("Item Not found");
}
}
/**
* If the List is not full and afterItem is in the List item becomes the element after afterItem
* If the List is full, prints "List Full"
* If List is not full but afterItem is not in List, prints "Item Not Found"
*
* @param afterItem the item in the list to add item before
* @param item the item to add to the list
*/
public void addAfterItem(String afterItem, String item)
{
if (isPresent(afterItem))
{
int index = mList.indexOf(afterItem);
{
mList.add(index +1, item);
}
}
else {
System.out.println("Item Not found");
}
}
/**
* 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 (mList.size() == 0)
{
System.out.println("List Empty");
return "";
}
return (mList.get(0));
}
/**
* 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 (mList.size() == 0)
{
System.out.println("List Empty");
return "";
}
return (mList.get(mList.size() - 1));
}
/**
* 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)
{
return mList.contains(item);
}
/**
* Returns the number of items in the List
*
* @return the number of items in the List
*/
public int askCount()
{
return mList.size();
}
/**
* 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 (mList.size() > 0)
{
mList.remove(0);
} else
{
System.out.println("List Empty");
}
}
/**
* 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 (mList.size() == 0)
{
System.out.println("List Empty");
}
else
{
mList.remove(mList.size() - 1);
}
}
/**
* 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 (mList.size() == 0)
{
System.out.println("List Empty");
}
else if (mList.contains(item))
{
mList.remove(item);
}
else
{
System.out.println("Item not found");
}
}
/**
* 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(" " + title);
int size = mList.size();
if (size > 0)
{
for (int i = 0; i < mList.size();i++)
{
System.out.print(mList.get(i) + " ");
}
}
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<String> temp = new ArrayList<String> ();
for (String s : mList)
{
temp.add(s);
}
Collections.sort(temp);
System.out.println(" " + title);
for (String s : temp)
{
System.out.print(s + " ");
}
System.out.println();
}
}
//Class Stack:
import java.util.Arrays;
public class Stack
{
private int stack [];
private int top;
public Stack()
{
}
public void push(String item)
{
if (top == stack.length)
{
Stack();
}
stack[top]= i;
top++;
}
public void pop(int i)
{
}
public String getTop()
{
return getTop();
}
public boolean isEmpty()
{
}
}
//main:
import java.util.Scanner;
public class AssignmentSix
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String num = sc.nextLine();
System.out.println("Expression:" + System.in);
System.out.println("Value" + );
}
private boolean isBinary(int number)
{
}
}
Explanation / Answer
Lets take it step by step:
Main Class:
Just take inputs add them to a String List and thats it.
String expresssion;
public ArrayList<String> expressionList= new ArrayList<String>();
while ((expression=r.readLine()) != null) {
expressionList.add(line);
}
//Now you have al the expeessions.
Stack Class :
Declare your stack like this, because stack would be holding an expression string:
Stack<String> s = new Stack<String>();
Now: iterate list add each expression to stack:
for(String exp: expressionList)
s.push(exp);
// Now evaluate the expression and print
while(!s.isEmpty())
{
String currentExp=s.pop();
// print currentexpression
//calculate value using Math Function or desired way.
//print Value
}
Thank you !!