Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hey guys. so i have the following program in java ( eclipse ) for impleminting stack i did everything as it asked but i got

hey guys. so i have the following program in java ( eclipse ) for impleminting stack

i did everything as it asked but i got stucked in the last part .. please help me out as soon as possible

// 1- how to traverse through the user input and push charachters into the stack??

// 2- write a loop that pops a character from the stack and prints that character until the stack is empty.

// 3-If your stack is correct, then the string the user enters will be printed backwards

please the answer should be well orieted and clear becasue i need to understand!

thank you in advance!

here is the code:

import java.util.EmptyStackException;

import java.util.Scanner;

public class ArrayStack implements Stack{ //implementing the stack

private E[] a;

private int size;

private String convert;

public ArrayStack() {

a = (E[]) new Object[10];

size = -1;

}

public void push(E e) { //to add to the stack

if (size == a.length) {

throw new IllegalStateException();

}

else {

a[size] = e;

size++;

}

}

public E pop() { //delete and returns the value

if (size == 0) {

throw new EmptyStackException();

}

E top = a[size-1];

size--;

return top;

}

public E peek() { //check the value of the stack

if (size == 0) {

throw new EmptyStackException();

}

return a[size -1];

}

public boolean empty() { //to check if the stack is empty.

if(size == 0) {

return true;

}else {

return false;

}

}

public static void main(String[] args) {

//Read a string entered by the user

ArrayStack a = new ArrayStack();

System.out.println("Enter a string:" );

Scanner scanner = new Scanner(System.in);

String tra =scanner.nextLine();

// 1- how to traverse through the user input and push charachters into the stack??

// 2- write a loop that pops a character from the stack and prints that character until the stack is empty.

// 3-If your stack is correct, then the string the user enters will be printed backwards

for (int i = 0 ; i < tra.length(); i++) {

char c = tra.charAt(i);

a.push(c);

System.out.println(c);

}

while(!a.empty()) {

char b = a.pop();

System.out.println(b);

}

// THIS PART IS A SAPERATE METHOD THAT IT ONLY NEEDS TO MATCH THE PARENTHESIS

/**char c;

for( int i = 0 ; i < tra.length(); i++) {

c = tra.charAt(i);

if( c== '[' || c == '{' || c == '(') {

a.push(c);

} else if( c == ']' || c == '}' || c == ')' && !a.empty()) {

if((char)a.peek() == '(' && c == ')' || (char)a.peek() == '{' && c == '}' || (char)a.peek() == '[' && c == ']'){

a.pop();

} else {

System.out.println("not balanced");;

}

} else {

if (( c == ']' || c == '}' || c == ')')) {

System.out.println("not balanced");

}

}

}

if(a.empty()) {

System.out.println("Balanced Parenthisis");

}else {

System.out.println("not balanced");

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Is the person willing to deal with the consequences?

Answered: 1 week ago

Question

Was there an effort to involve the appropriate people?

Answered: 1 week ago