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

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Data Structures Lecture 2 Fang Yu Department of Management Information Systems National Chengchi University Fall 2011.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 Inheritance Lecture 9 from Chapter 8. 2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
For use of Cleveland State's IST410 Students only 1 Exception.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Final Review.
Jan Object Oriented Programming Yangjun Chen Dept. Business Computing University of Winnipeg.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Java Programming (Chapter 1). Java Programming Classes, Types, and Objects.
March 2004Object Oriented Design1 Object-Oriented Design.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
In-class exercise 1. 1.downloading Java Java SE Java SE (JDK) 6 JDK 6u2 jdk-6u2-windows-i568-p.exe.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Polymorphism & Interfaces
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
8: Interfaces Interfaces “Multiple inheritance” in Java –Name collisions when combining interfaces Extending an interface with inheritance Grouping constants.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Ade Azurat, Advanced Programming, 2004 Review: Slide 1 Review Why Java ? Application and Applet User Interface Exception Object-Oriented Programming.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Polymorphism CS 162 (Summer 2009).
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Object Oriented Programming Lecture 2: BallWorld.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Chapter 10 – Exception Handling
Chapter 11: Inheritance and Polymorphism
Object Oriented Programming
Computer Science II Exam 1 Review.
Overloading and Constructors
null, true, and false are also reserved.
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Introduction to Java Programming
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Java Exception Dept. Business Computing University of Winnipeg
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

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

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

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;

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

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

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

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

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

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

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

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 }

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();}

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”; }

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

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; bits 16 bits

Data Structure Exercises 2.1

Running Time and Recursion

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

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

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

Data Structure Exercises 2.2