Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Design Running Time Recursion - Ed. 2 and 3.: Chapter 2, 3 - Ed. 4: Chapter 2, 3, 4.

Similar presentations


Presentation on theme: "Object-Oriented Design Running Time Recursion - Ed. 2 and 3.: Chapter 2, 3 - Ed. 4: Chapter 2, 3, 4."— Presentation transcript:

1 Object-Oriented Design Running Time Recursion - Ed. 2 and 3.: Chapter 2, 3 - Ed. 4: Chapter 2, 3, 4

2 Object-Oriented Design Inheritance Polymorphism method overriding method overloading Keyword: this Exception Interface, Abstract Classes Type casting

3

4

5

6

7 class S { int x, y; public void a(int x, int y){ this.x = x; this.y = y; }

8

9

10 An example: import java.lang.*; //Here we define some exception types of our own. //Exception classes generally have constructors but no data //or other methods. All these do is call their superclass //constructors. class MyException extends Exception { public MyException() {super();} public MyException(String s) { super(s); } } class MyOtherException extends Exception { public MyOtherException() { super();} public MyOtherException(String s) { super(s); } }

11 class MySubException extends MyException { public MySubException() { super(); } public MySubException(String s) { super(s); } } public class Throwtest { //This is the main() method. Note that it uses two //catch clauses to handle two standard Java exceptions. public static void main(String argv[]) { int i = 2;

12 //First, covert our argument to an integer. //Make sure we have an argument and that it is convertible. try { i = Integer.parseInt(argv[0]); } catch (ArrayIndexOutOfBoundsException e) {//argv is empty System.out.println("Must specify an argument"); return; } catch (NumberFormatException e) {//argv[0] is not an integer System.out.println("Must specify an integer argument"); }

13 //Now, pass that integer to method a(). a(i); } //This method invokes b(), which is declared to throw //one type of exception. We handle that one exception. public static void a(int i) { try { b(i); }

14 catch (MyException e) {//Point 1 //Here we handle MyException and its subclass MySubException if (e instanceof MySubException) System.out.print("MySubException: "); else System.out.print("MyException: "); System.out.println(e.getMessage()); System.out.println("Handle at point 1"); }

15 public static void b(int i) throws MyException { int result; try { System.out.print("i = " + i); result = c(i); System.out.print(" c(i) = " + result); } catch (MyOtherException e) {//Point 2 //Handle MyOtherException: System.out.println("MyOtherException: " + e.getMessage()); System.out.println("Handle at point 2"); } finally { //Terminate the output we printed above with a newline. System.out.print("\n"); }

16 public static int c(int i) throws MyException, MyOtherException { switch (i) { case 0: //processing resumes at point 1 above throw new MyException("input too low"); case 1: //processing resumes at point 1 above throw new MyException("input still too low"); case 99: //processing resumes at point 2 above throw new MyOtherException("input too high"); default: return i*i;}}}

17 If you use JCreator to compile and run your program, the above program Should be changed as follows: import javax.swing.JOptionPane; … public static void main(String argv[]) { int i = 2; try {i = (int) new Integer(JOptionPane.showInputDialog("Enter an integer"));} … try { i = Integer.parseInt(argv[0]); }

18

19

20 An example: import java.util.*; import java.lang.*; interface CanFight { void fight ( );} interface CanSwim { void swim ( );} interface CanFly {void fly ( );} class ActionCharacter { public void fight( ) { }} class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly { public void fight ( ) {System.out.println(“Can fight!”);} public void swim ( ) {System.out.println(“Can swim!”); } public void fly ( ) {System.out.println(“Can fly!”);} }

21 public class Adventure { static void t(CanFight x) { x.fight();} static void u(CanSwim x) { x.swim();} static void v(CanFly x) { x.fly();} static void w(ActionCharacter x) { x.fight();} public static void main (String[ ] args) { Hero h = new Hero( ); t(h); //Treat it as a CanFight u(h); //Treat it as a CanSwim v(h); //Treat it as a CanFly w(h); //Treat it as an ActionCharacter }

22 HeroActionCharacterCanFightCanSwimCanFly subclassimplementation h instantiation Hero h = new Hero( ) t(h); //Treat it as a CanFight u(h); //Treat it as a CanSwim v(h); //Treat it as a CanFly w(h); //Treat it as an ActionCharacter static void t(CanFight x) { x.fight();} static void u(CanSwim x) { x.swim();} static void v(CanFly x) { x.fly();} static void w(ActionCharacter x) { x.fight();}

23 Abstract Classes We’ve reviewed this before. An abstract class contains both abstract methods and normal methods. The abstract methods must be overridden in subclasses. Because of the abstract methods, an abstract class cannot be instantiated. An example: import java.util.*; import java.lang.*; abstract class Instrument { int i; public abstract void play( ); public String what( ) { return “instrument”; } class Wind extends Instrument { public void play( ){ System.out.println(“Wind.play()”); public String what( ) { return “Wind”; }

24 S T o so extends assign type casting (T) so

25 I T o io Impl.assign type casting (T) io A possible error by basic data type casting: int i = 258; byte b = (byte) i; 0 0110... 8 bits 16 bits

26 Data Structure Exercises 2.1

27 Running Time and Recursion

28

29

30

31

32

33

34

35

36

37

38

39

40 public static long factorial( long n ) { if( n <= 1 ) return 1; else return n * factorial( n - 1 ); } n = 1 Return 1 Return 2 Return 6 Return 24

41 public static long factorial( long n ) { if( n <= 1 ) return 1; else return n * factorial( n - 1 ); } n = 4 public static long factorial( long n ) { if( n <= 1 ) return 1; else return n * factorial( n - 1 ); } n = 3 public static long factorial( long n ) { if( n <= 1 ) return 1; else return n * factorial( n - 1 ); } 2 * 1 = 2

42 public static long factorial( long n ) { if( n <= 1 ) return 1; else return n * factorial( n - 1 ); } n = 4 public static long factorial( long n ) { if( n <= 1 ) return 1; else return n * factorial( n - 1 ); } 3 * 2 = 6

43

44

45 Data Structure Exercises 2.2


Download ppt "Object-Oriented Design Running Time Recursion - Ed. 2 and 3.: Chapter 2, 3 - Ed. 4: Chapter 2, 3, 4."

Similar presentations


Ads by Google