1 / 45

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

Object-Oriented Design Running Time Recursion Ed. 2 and 3.: Chapter 2, 3 Ed. 4: Chapter 2, 3, 4. Object-Oriented Design. Inheritance Polymorphism method overriding method overloading Keyword: this Exception Interface, Abstract Classes Type casting. class S { int x, y;

lpatricia
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. class S { int x, y; public void a(int x, int y){ this.x = x; this.y = y; } }

  4. 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); } }

  5. 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;

  6. //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"); }

  7. //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); }

  8. 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"); } }

  9. 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"); } }

  10. 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;}}}

  11. 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]); }

  12. 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!”);} }

  13. 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 } }

  14. ActionCharacter CanFight CanSwim CanFly Hero h subclass implementation 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();} 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

  15. 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. class Wind extends Instrument { public void play( ){ System.out.println(“Wind.play()”); public String what( ) { return “Wind”; } } An example: import java.util.*; import java.lang.*; abstract class Instrument { int i; public abstract void play( ); public String what( ) { return “instrument”; } }

  16. S so type casting extends assign (T)so T o

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

  18. Data Structure Exercises 2.1

  19. Running Time and Recursion

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

  21. 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 }

  22. 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 }

  23. Data Structure Exercises 2.2

More Related