Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the follow 3 JAVA classes, complete the code and fix any syntax errors. NOTE: THE PROGRAM SHOULD CRASH AT THE LAST PEEK IN THE

Using the follow 3 JAVA classes, complete the code and fix any syntax errors. NOTE: THE PROGRAM SHOULD CRASH AT THE LAST PEEK IN THE MAIN CLASS.....

import java.util.EmptyStackException;

public class Lab3 {

public static void main(String[] args) throws EmptyStackException {

int maxSize = 5;

MyStack personStack;

personStack = new MyStack(maxSize);

personStack.push(new Person("Jonh", "Smith", 33));

personStack.pop();

// !!!call four more push() with different person objects

personStack.push(new Person("Mike", "Jones", 36));

personStack.push(new Person("Jane", "Doe", 48));

personStack.push(new Person("James", "Joyce", 36));

personStack.push(new Person("Bob", "Johnson", 54));

// !!!call pop() and print the popped element

System.out.println(personStack.pop());

System.out.println(personStack); // print the stack object

// !!!call pop() and print the popped element

System.out.println(personStack.pop());

System.out.println(personStack); // print the stack object

// !!!call two more pop()

personStack.pop();

personStack.pop();

System.out.println(personStack); // print the stack object

// !!!call peek()

System.out.println(personStack.peek());

}

}

public class MyStack {

private Object[] elem;

private int size;

int top;

public MyStack(int max) {

// complete

}

public void push(Object elem) // put element into array

{

// complete

// avoid pushing if stack is full

}

public Object pop() {

// complete

// throw EmptyStackException if stack empty

if (!isEmpty())

return array[top];

else

throw new IllegalStateException("Stack empty");

}

return null;

}

public Object peek() {

// complete

// throw EmptyStackException if stack empty

if (!isEmpty())

return array[top];

else

throw new IllegalStateException("Stack empty");

}

return null;

}

public String toString() {

return "My Stack info"; // complete: show the size and

capacity

}

}

public class Person {

private String lastName; private String firstName; private int age;

public Person(String last, String first, int age) { // complete }

@Override public String toString() { return "my person info"; // complete: show first & last name and age }

public String getLast() { return lastName; } }

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

Students also viewed these Databases questions