class ArrayStackException extends Exception {
ArrayStackException(String m) {
super(m);
}
}
public class ArrayStack {
private int s[] = new int[100];
private int top=0;
public void push(int data) throws ArrayStackException {
if (top>=s.length) throw new ArrayStackException("Push overflow: " + data);
s[top]=data;
top++;
}
public int pop() throws ArrayStackException {
if (top<=0) throw new ArrayStackException("Pop underflow: ");
int value = s[top-1];
top--;
return value;
}
public int peek() {
int value = s[top-1];
return value;
}
public void clear() {
top=0;
}
public int size() {
return top;
}
public void display() {
System.out.print( "size =" +top + ": ");
for(int i =0; i<top;i++) {
System.out.print( s[i] + " ");
}
System.out.println( );
}
public static void main(String[] s) {
ArrayStack mystack = new ArrayStack();
try {
mystack.push(30);
mystack.push(20);
mystack.push(10);
mystack.display();
mystack.pop();
mystack.display();
mystack.pop();
mystack.pop();
mystack.pop();
mystack.pop();
} catch (ArrayStackException e) {
System.out.println("Exception "+e.getMessage());
}
}
}
Modify the code to store a character instead of a String. Name it CharStack.
Use CharStack to implement a nesting bracket validator.
Examples:
( x+y / { a } ) + [ X ] // valid
{ f + ( c + g } ) // not valid
Name the program Nest.java create a method:
int nested( String s )
The method will return the location of a nesting failure or -1 if the string s is
correctly nested.
Your program should check for the following pairs of nesting characters: {} [] ()
Hint: Parse the string one character at a time from beginning to end using a
loop.
When you encounter a opening character [{( push it on the stack.
When you encounter a closing character ]}) pop the stack and check to make sure that the closing
character matches the opening character. If they do not match return the character location.
If you process the entire string and the stack is empty then you have a valid matched expression
return -1.
If you ever throw a StackException then the expression is not balanced.
Your test driver should test your nested() method with the expressions
Step by stepSolved in 5 steps with 3 images
- int [] arrayInt={3,54,90,22,32}; To print the last element in the arrayInt you will write: a. System.out.println(arrayInt[5]); b. System.out.println(arrayInt[0]); c. System.out.println(arrayInt[length]); d. System.out.println(arrayInt[4]);arrow_forward2 = 0 ) { numList [ i ] i } } } [1, 2, 3, 4, 5] [0, 2, 4, 6, 8] [2, 4, 6, 8, 10] [1, 3, 5, 7, 9]arrow_forwardWrite a loop that counts how many elements in an array are equal to zero. arrays.cpp 1 #include // sizet 2 int countZeros (const int values[], size_t size) { int count = 0; 3 4 for (int i; i using namespace std; 3 2 4 int countZeros(const int values[], size_t size); 5 int main() { int a[] = {1, 2, 0, 3}; cout <« countZeros (a, 4) <« endl; cout « "Expected: 1" « endl; 6 7 8 9 10 11 int b[] = {0, 2, 0, 3}; cout <« countZeros (b, 4) <« endl; cout « "Expected: 2" « endl; 12 13 14 15 int cl] -{1, 0, θ, 0, 0 ; cout <« countZeros (c, 5) <« endl; cout « "Expected: 4" « endl; 16 17 18 19 } CodeCheck Reset Testers Running Tester.cpp pass fail fail 1 Expected: 1 Expected: 2 Expected: 4 Score 1/3arrow_forward
- vWhat happens when an object such as an array is no longer referenced by a variable?arrow_forward5arrow_forwardpublic int binarySearch(int[]array, int num) {int low = 0;//low rangeint high = array.length -1; //high range int mid; //mid rangewhile () //while low is less than or equal to high{mid = ; //set middle range to be (low + high) /2if () { //if the array in the middle range = input number//return mid range }elseif () { //if the array in the middle range > input number//set the high value to be the mid value minus 1 }else{//set low value to be mid value plus one } }//return -1 here because that would mean that the number is not found in the loop}arrow_forward
- Method Details getCountInRange public static int getCountInRange(int[] array, int lower, int upper) Returns the number of values in the range defined by lower (inclusive) and upper (inclusive) Parameters: array - integer array lower - lower limit upper - upper limit Returns: Number of values found Throws: java.lang.IllegalArgumentException - if array is null or lower is greater than upper Any error message is fine (e.g., "Invalid parameters(s)")arrow_forwardX40: sum3 Given an array containing three ints, return the sum of all the elements. Examples: sum3({1, 2, 3}) -> 6 sum3({5, 11, 2}) -> 18 Your Answer: 1 public int sum3(int[] nums) 2{ 3 4} Check my answer! Reset Next exercisearrow_forward3- Determine the output of the following code JavaScript Array Methods pop() The pop() method removes the last element from an array. The return value of the pop() method is the removed item. var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML document.getElementById("demo2").innerHTML document.getElementById("demo3").innerHTML = fruits; = fruits.pop(); = fruits.shift();arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY