Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Stack class and the main(). Stack must contain one of our previous List

ID: 3803493 • 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
{
}

Explanation / Answer

public class Stack
{
//private int stack [];
private int top;
private List classList;

public Stack()
{
classList = new List();
}
public Stack(List myList)
{
classList = new List(myList);
top = classList.askCount();
}
/*Pushes the element onto the stack.*/
public void push(String item)
{
/*if (top == stack.length)
{
Stack();
}
stack[top]= i;
top++;*/
classList.addToRear(item);
top = getTop());
}
/*Removes the element on the top of the stack.*/
public void pop()
{
classList.removeRear();
top = getTop();
  
}
/*Returns the value of top*/
public int getTop()
{
int temp;
temp = classList.askCount();
return temp;

}
/*Returns the element on the top of the stack, but does not remove it. If the Stack is empty returns List Empty*/
pubic String peek() {
String temp = classList.getRear();
return temp;

}
/*Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements.*/
public boolean isEmpty()
{
top = getTop();
if(top == 0)
return true;
else
return false;
}
/*Performs the operation passed in the parameter to the top two elements in the stack and returns the result*/
public String operate(String operator) {
Integer num1 = Integer.valueOf(classList.peek());
classList.pop();
Integer num2 = Integer.valueOf(classList.peek());
classList.pop();
Integer result;
switch(operator) {
case EXP: {
result = pow(num2,num1);
}
case MULT: {
result = num2 * num1;
}
case DIV: {
result = num2/num1;
}
case PLUS: {
result = num2 + num1;
}
case MINUS: {
result = num2 - num1;
}
return String.valueOf(result);
}
}
/*Prints the elements in the stack in the order in which they were pushed*/
public void printStack(String title) {
classList.print(title);
}
}
//main:
import java.util.Scanner;
public class AssignmentSix
{
public static void main() {
List myList = new List();
Stack myStack = new Stack(myList);
myStack.push('5');
myStack.push('2');
myStack.push('6');
myStack.push('7');
myStack.push('3');
String toBePushed = myStack.operate('MINUS'); // 7-3
myStack.push(toBePushed); // 4
toBePushed = myStack.operate('PLUS'); // 6+4
myStack.push(toBePushed); // 10
toBePushed = myStack.operate('MULT'); // 10*2
myStack.push(toBePushed); // 20
toBePushed = myStack.operate('DIV'); // 20/5
myStack.push(toBePushed); // 4
myStack.printStack();
}
}