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 Book{     public String title;     public String author;     public int year;    ...
public class Book{     public String title;     public String author;     public int year;     public String publisher;     public double cost;            public Book(String title,String author,int year,String publisher,double cost){        this.title=title;         this.author=author;         this.year=year;         this.publisher=publisher;         this.cost=cost;     }     public String getTitle(){         return title;     }         public String getAuthor(){         return author;     }     public int getYear(){         return year;     }     public String getPublisher(){         return publisher;...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
Given this class: class Issue { public:                 string problem; int howBad; void setProblem(string problem); };...
Given this class: class Issue { public:                 string problem; int howBad; void setProblem(string problem); }; And this code in main: vector<Issue> tickets; Issue issu; a) tickets.push_back(-99); State whether this code is valid, if not, state the reason b) Properly implement the setProblem() method as it would be outside of the class. You MUST name the parameter problem as shown in the prototype like: (string problem) c) Write code that will output the problem attribute for every element in the...
Stack Class //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Queue Class public class Stack { private java.util.ArrayList pool = new java.util.ArrayList(); pu
Stack Class //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Queue Class public class Stack { private java.util.ArrayList pool = new java.util.ArrayList(); public Stack() { }    public Stack(int n) { pool.ensureCapacity(n); }    public void clear() { pool.clear(); }    public boolean isEmpty() { return pool.isEmpty(); }    public Object topEl() { if (isEmpty()) throw new java.util.EmptyStackException(); return pool.get(pool.size()-1); }    public Object pop() { if (isEmpty()) throw new java.util.EmptyStackException(); return pool.remove(pool.size()-1); }    public void push(Object el) { pool.add(el); }    public String toString() {...
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...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
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 Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...
public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /** * Constructor for objects of class Classroom */ public Classroom() { this.capacity = 0; }    /** * Constructor for objects of class Classroom * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Classroom(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT