StackGeneric-and-Interfaces-Exceptions
Build on the examples given in 1009-classwork.zip.
1.
Explain why the following code will not compile:
Save your time - order a paper!
Get your paper written from scratch within the tight deadline. Our service is a reliable solution to all your troubles. Place an order on any task and we will take care of it. You won’t have to worry about the quality and deadlines
Order Paper Nowimport StackGeneric.*;_x000D_ _x000D_ public class Test0 {_x000D_ _x000D_ public static void main(String [] args){_x000D_ Stack<Integer> st = new Stack<Integer>();_x000D_ for( int i = 0 ; i < 5 ; i++) {_x000D_ st.push(i);_x000D_ }_x000D_ st.push(5);_x000D_ System.out.println(st.pop());_x000D_ }_x000D_ }_x000D_
2.
Explain why the following two examples throw an exception when run.
import StackGeneric.*;_x000D_ _x000D_ public class Test {_x000D_ _x000D_ public static void main(String [] args){_x000D_ Stack<Integer> st = new IntegerStackArray();_x000D_ int n = st.pop();_x000D_ }_x000D_ }_x000D_
import StackGeneric.*;_x000D_ _x000D_ public class Test2 {_x000D_ _x000D_ public static void main(String [] args){_x000D_ Stack<Integer> st = new IntegerStackArray();_x000D_ for( int i = 0 ; i < 1000 ; i++) {_x000D_ st.push(i);_x000D_ }_x000D_ st.push(5);_x000D_ }_x000D_ }_x000D_
3.
Read the Java tutorial on Exceptions, up to and including the section on finally blocks.
4.
In Problem 2. above, both examples threw ArrayIndexOutOfBoundsException exceptions. Using the following template, modify the method
public void push(Integer value)
so that if pushing results in exceeding the maximum size of the array, the ArrayIndexOutOfBoundsException is caught, an appropriate message is printed, but the program continues, though the requested action is not completed.
Template:
try{ /* ... */ }_x000D_ catch(ArrayIndexOutOfBoundException e){ /* ... */ }_x000D_
5.
In the above example, I referred to the method “push” using the full signature. Why is this good practice, rather than just writing “push”?
6.
Complete the problem described in the picture slide 1(1009 Information) in the folder 10/09.