Question

Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...

Stack2540Array

import java .io .*;
import java . util .*;
public class Stack2540Array {
int CAPACITY = 128;
int top ;
String [] stack ;
public Stack2540Array () {
stack = new String [ CAPACITY ];
top = -1;
}
1
3.1 Implement the stack ADT using array 3 TASKS
public int size () { return top + 1; }
public boolean isEmpty () { return (top == -1); }
public String top () {
if ( top == -1)
return null ;
return stack [ top ];
}
public void push ( String element ) {
top ++;
stack [top ] = element ;
}

3.1.2 Test Your Stack with the Parentheses Matching Problem You will test your Stack2540Array on an application, i.e., parentheses matching. It takes a string that contains parentheses that are often found in arithmetic expressions, and checks whether the brackets are correct (balanced) or incorrect (unbalanced). Example of valid strings are:

final static String [] valid = { "( ) ( ( ) ) {( [ ( ) ] ) } ",

" (3) (3 + (4 - 5) ) {( [ ( ) ] ) } ",

" ((() (() ) {([() ]) }))",

" [(5+ x) -(y+z)]" };

Examples of invalid strings are:

final static String [] invalid ={ ") (() ) {([() ])}",

" ({[]) }",

"(" };

You will write a method named isMatched like below. It will return true for valid strings, and false for invalid strings. You will use the stack named Stack2540Array that is implemented in the rst step. The algorithm itself can be the same as in our lecture slides. Your program does not need to check the correctness of the arithmetic operators/operands. What you need to do is to check for balanced brackets.

public static boolean isMatched ( String expression ) {

final String opening = " ({[ ";

final String closing = ")}]";

Stack2540Array buffer = new Stack2540Array ();

... ... return ...;

}

Please make the answer on java.

Homework Answers

Answer #1

IMPLEMENTED pop() OPERATION IN ABOVE STACK

public String pop()
{

// if the stack is empty
if(top==-1)
return null;

String ans = stack[top];

top--;

return ans;
}
WHOLE PROGRAM WITH pop() FUNCTION :

import java . io .*;

import java . util .*;

class Stack2540Array {

int CAPACITY = 128;

int top;

String[] stack;

public Stack2540Array() {

stack = new String[CAPACITY];
top = -1;
}

public int size() {
return top + 1;
}

public boolean isEmpty() {
return (top == -1);

}

public String top() {

if (top == -1)

return null;

return stack[top];

}

public void push(String element) {

top++;

stack[top] = element;

}

public String pop()
{

//if the stack is empty
if(top==-1)
return null;

String ans = stack[top];

top--;

return ans;
}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) {...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) { return 0; } public static int[] convert1(String str) { return new int[1]; } public static String convert2(int[] v) { return ""; } public static void main(String[] args) { testSmallest(); testConvert(); } public static void testSmallest() { System.out.println("Testing your method smallest(...)"); int[][] testVectors1 = new int[][]{{1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3}, {2, 3, 4}}; int[][] testVectors2 = new...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read the input string String input = sc.nextLine(); BalancedParentheses bp = new BalancedParentheses(); // Print whether the string has balanced parentheses System.out.println(bp.hasBalancedParentheses(input)); } } class BalancedParentheses { public boolean hasBalancedParentheses(String input) { // Remove this and implement code throw new UnsupportedOperationException(); } }
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT