CSC 213- Final Handout
1) Exception: Trace the program and fill out the table. Show which one produces the Stack Trace.
/*Exception*/
class TestException {
public static void m1() {/*throw new ArithmeticException();/* Code
is not given...*/}
public static void m2() {/*throw new ArithmeticException();/* Code
is not given...*/}
public static void m3() {/*throw new IllegalArgumentException();/*
Code is not given...*/}
public static void main(String[] args) {
[Link]("A, ");
m1();
try {
[Link]("B, ");
m2();
[Link]("C, ");
m3();
[Link]("D, ");
}
catch (ArithmeticException e) {
[Link]("E, ");
return;
}
catch (Exception e) {
[Link]("F, ");
}
finally {
[Link]("H, ");
}
[Link]("G. ");
}
}
description output
Call to m1 and m2 complete normally and, if called,
m3 completes normally
Call to m1 is complete, and m2 throws an
ArithmeticException and, if called, m3 complete
normally
Call to m1 and m2 complete normally and, if called,
m3 throws IllegalArgumentException
Call to m1 and m2 complete normally and, if called,
m3 throws an ArithmeticException
Call to m1 throws an ArithmeticException and, if
called, others complete normally
2) Hierarchy/ Inheritance: Trace the code and print the correct message when the greet method is
called.
public class M_Animal {
public void greet (M_Animal a){
[Link]("Sniff");
}
}
class Person extends M_Animal {
public void greet (M_Animal a){
[Link]("Grrf");
}
public void greet (Person a){
[Link]("hi");
}
}
class Professor extends Person {
public void greet (Professor a){
[Link]("Good Day");
}
public void greet (Person a){
[Link]("hello");
}
public void greet (Student a){
[Link]("How can I help you?");
}
}
class Student extends Person {
public void greet (Professor a){
[Link]("What's the answer?");
}
public void greet (Person a){
[Link]("hey");
}
public void greet (Student a){
[Link]("yo");
}
}
class Driver {
public static void main(String[] args) {
M_Animal rex = new M_Animal();
M_Animal bob = new Professor();
Professor sara = new Professor();
Person jane = new Student();
[Link](bob);
//1)What will print
[Link](rex);
//2)What will print
[Link](bob);
//3)What will print
[Link](sara);
//4)What will print
[Link](jane);
//5)What will print
}
}
3) Lambda Expression/ Sort
For List of students use predicate to filter students with gpa>3.
ArrayList<Student> filtered;
//a) use Predicate and lambda expression to filter students
with gpa>3.0
Predicate< Student> pred =
[Link]("Print the Filtered Students:");
//b) use Predicate and lambda expression to filter based on age
Predicate< Student> pred2 =
[Link]("Print the Filter Students with age>22");
//c) Sort the students list based on name and for same names
sort based on age
4) Stream
List <Integer> numbers = [Link](1,2,3,4,5,6);
/* find the double of the first even number that is>3*/
List <Integer> numbers2 =
[Link](1,2,3,5,4,6,7,8,9,10);
int res=0;
for (int e:numbers2) {
if (e>3 && e%2==0){
res = e*2;
break;
}
}
//Convert the code above to use the stream
[Link]("Stream: "+
[Link]()
);