Skip to content
Snippets Groups Projects
Commit 32df3be3 authored by Siyu Lin's avatar Siyu Lin
Browse files

ExceptionExample of propagating and Problem 13 Answer from Review Sheet

parent c5ade4c5
No related branches found
No related tags found
No related merge requests found
package exam3;
public class Book extends LibraryItem implements Item {
public class Book extends LibraryItem {
public Book(String title) {
super(title);
super(title, 21);
}
public int getCheckOutPeriod() {
return 21; // three weeks
protected Book(String title, int checkOutPeriod) {
super(title, checkOutPeriod);
}
}
\ No newline at end of file
}
......@@ -4,7 +4,7 @@ public class DVD extends LibraryItem {
private int duration; // duration in minutes
public DVD(String title, int duration) {
super(title);
super(title, 7);
this.duration = duration;
}
......@@ -12,11 +12,7 @@ public class DVD extends LibraryItem {
return "DVD: " + super.getTitle();
}
public int getCheckOutPeriod() {
return 7; // DVDs check out for one week
}
public int getDuration() {
return duration;
}
}
\ No newline at end of file
}
package exam3;
/**
* Illustration of exception handling.
*/
public class ExceptionExample
{
/**
* @param args not used
*/
public static void main(String[] args)
{
ExceptionExample t = new ExceptionExample();
System.out.println("main calling m1()");
t.m1();
}
// Called by main()
public void m1()
{
System.out.println("Starting m1 ");
try
{
m2();
// remaining code is not executed if m2 throws an exception
System.out.println("Finishing try block of m1");
}
catch (MyException e)
{
System.out.println("m1 caught exception: " + e);
// if we want, look at the stack trace
//e.printStackTrace(System.out);
}
// we could also catch other exception types
catch (NullPointerException npe)
{
System.out.println("m1 caught a NullPointerException");
}
// remaining code is still executed if m2 throws MyException,
// which we caught and handled above
System.out.println("Executing last line of m1");
}
// Called by m1()
public void m2() throws MyException
{
try
{
System.out.println(" Starting m2");
m3();
// remaining code is not executed if m3 throws an exception
System.out.println(" Finishing try block of m2");
}
finally
{
System.out.println(" Finally block of m2 executes no matter what");
}
// remaining code is not executed if m3 throws an exception
System.out.println(" Executing last line of m2");
}
// Called by m2()
public void m3() throws MyException
{
System.out.println(" Starting m3");
m4();
// remaining code is not executed if m4 throws an exception
System.out.println(" Executing last line of m3");
}
// Called by m3()
public void m4() throws MyException
{
System.out.println(" Starting m4");
// throw a checked exception...
// System.out.println(" About to throw MyException from m4");
// throw new MyException("This happened in m4");
// or, throw a runtime exception...
//System.out.println(" About to throw NullPointerException from m4");
//Object obj = null;
//obj.toString();
// or, just return normally
//System.out.println(" Returning normally from m4");
}
}
/**
* A simple subclass of Exception. Note this is a "checked"
* exception, since we aren't extending RuntimeException.
*/
class MyException extends Exception
{
public MyException(String message)
{
super(message);
}
public MyException()
{
super();
}
}
......@@ -20,11 +20,18 @@ public class InheritancePart {
// System.out.println(li.getDuration());//Compile error, No getDuration method in
// LibraryItem
System.out.println(((DVD) li).getDuration());
i = b;
// b = i;//Compile error, No Downcast
i = (Item) b;
// b = i;//Compile error, Declared type of i is Item and No Downcast
System.out.println(b.getClass() == i.getClass());
System.out.println(b instanceof Book);
System.out.println(i instanceof Book);
System.out.println(i.getTitle());
ReferenceBook rb = (ReferenceBook) b;
System.out.println(rb.getTitle());
// ReferenceBook rb = (ReferenceBook) b;
// System.out.println(rb.getClass() == b.getClass());
// System.out.println(rb.getClass() == i.getClass());
// System.out.println(rb instanceof Item);
// System.out.println(rb instanceof Book);
// System.out.println(rb.getTitle());
// rb = (ReferenceBook) new Book("Big Java"); // Exception
// ClassCastException Book
// cannot be cast to
......
......@@ -2,14 +2,18 @@ package exam3;
public abstract class LibraryItem implements Item {
private String title;
private int checkOutPeriod;
protected LibraryItem(String title) {
protected LibraryItem(String title, int checkOutPeriod) {
this.title = title;
this.checkOutPeriod = checkOutPeriod;
}
public String getTitle() {
return title;
}
public abstract int getCheckOutPeriod();
public int getCheckOutPeriod() {
return checkOutPeriod;
}
}
\ No newline at end of file
......@@ -74,20 +74,16 @@ public class RecursionPart {
return number;
}
public static int tryStuff(String text) {
int total = 0;
int i = 0;
Scanner scanner = new Scanner(text);
while (scanner.hasNext()) {
try {
String s = scanner.next();
i = Integer.parseInt(s);
total += i;
} catch (NumberFormatException nfe) {
total -= i;
public static void mysteryWithoutRecursion(int x) {
while (x != 0) {
if (x % 2 == 0) {
System.out.println(x);
x /= 2;
} else {
x -= 1;
}
}
return total;
System.out.println("pooh");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment