Question

In: Computer Science

class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top...

class ArrayStringStack
{
  String stack[];

  int top;

  public arrayStringStack(int size)
  {
    stack = new String[size];
    top = -1;
  }

  //Assume that your answers to 3.1-3.3 will be inserted here, ex:

  // full() would be here

  //empty() would be here...

  //push() 

  //pop() 

  //displayStack() 
}

1. Write two boolean methods, full( ) and empty( ).

2. Write two methods, push( ) and pop( ). Note: parameters may be expected for push and/or pop.

3. Write the method displayStack( ) that prints the stack like a stack, from top to bottom.

In java

Solutions

Expert Solution

public class ArrayStringStack {
String stack[];
int top;

public ArrayStringStack(int size)
{
stack = new String[size];
top = -1;
}
// full() would be here
boolean full(){
// check top is equal size-1
return top==stack.length-1;
}
//empty()
boolean empty(){
// check top is -1 return true
return top==-1;
}
//push()
void push(String elem){
if(full()){ // check full print
System.out.println("Stack is full");
}else{ // add element to stack
stack[++top] = elem;
}
}
//pop()
void pop(String elem){
if(empty()){ // check empty
System.out.println("Stack is empty");
}else{ // decrease top
elem = stack[top];
top--;
}
}
void displayStack(){
// If stack is empty then return
if (empty())
return;

String x = stack[top];
String elem=""; // to store poped element
// Pop the top element of the stack
pop(elem);

  

// Print the stack element starting
// from top to bottom
System.out.println(x);
// Recursively call the function displayStack
displayStack();
// Push the same element onto the stack
// to preserve the order
push(x);
}
}

/* USE SCREENSHOT TO ALIGN CODE */

/* Driver class to test code */ (if want to check)


public class Driver {
public static void main(String[] args) {
// create object
ArrayStringStack s = new ArrayStringStack(5);
for (int i = 0; i < 5; i++) {
// convert i to string and add to stack
s.push(String.valueOf(i));
}
// print stack
s.displayStack();
  
}

}
/* OUTPUT */ TOP TO BOTTOM


Related Solutions

public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20,...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20, 31, 40, 55, 60, 65525}; System.out.println(findPattern(array)); } private static int findPattern(int[] arr) { for (int i = 0; i < arr.length - 2; i++) { int sum = 0; for (int j = i; j < i + 2; j++) { sum += Math.abs(arr[j] - arr[j + 1]); } if (sum == 20) return i; } return -1; } } QUESTION: Modify the given...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT