object-oriented design running time recursion
TRANSCRIPT
-
7/29/2019 Object-Oriented Design Running Time Recursion
1/45
Object-Oriented Design
Running Time
Recursion- Ed. 2 and 3.: Chapter 2, 3
- Ed. 4: Chapter 2, 3, 4
-
7/29/2019 Object-Oriented Design Running Time Recursion
2/45
Object-Oriented Design
In an object-oriented programming language, we are dealing
with class and objects. Here we review briefly some major
concepts of object-oriented programming.
InheritancePolymorphism
method overriding
method overloading
Keyword: this
Exception
Interface, Abstract Classes
Casting
-
7/29/2019 Object-Oriented Design Running Time Recursion
3/45
Inheritance
Inheritance allows classes (subclasses) to use the members of
another class (superclass) as their own members.
We use the keyword extends to make a class a subclass of a
superclass.
Example:
public class SalaryEmployee extends NewEmployee {
// class body
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
4/45
Inheritance
Inheritance allows classes (subclasses) to use the members of
another class (superclass) as their own members.
We use the keyword extends to make a class a subclass of a
superclass.
Example:
public class SalaryEmployee extends NewEmployee {
// class body
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
5/45
Polymorphism
Polymorphism adds flexibility to the programming. Java
provides two kinds of polymorphism: method overriding andoverloading.
override: A method of a superclass is re-defined in the
subclass.
Example:
class S {
//. . .
public void a() {// . . .
}
}
class T extends S {
//. . .
public void a() {// . . .
}
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
6/45
overloading: A method name is used by more than one method
in the same class.
Example:
class T {
// . . .
public int a() {
// . . .}
public void a( int x ) {
// . . .
}
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
7/45
The Keyword this
In Java, the keywork this is a reference to the current object.We may see code like the example below.
class T {
private double x;
public T() {this( 0.0 );
}
public T( double y ) {
x = y;
}
}
class S {
int x, y;
public void a(int x, int y){
this.x = x;
this.y = y;
}
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
8/45
Exceptions
In Java, exceptions are objects that can be thrown and then
caught.
Example:
void a() throws myException {
//. . .
if( . . . ) {
throw new
myException( "Error" );
}
}
class B {
// . . .
public int b() {
try {
// . . .
a();
}
catch( myException e )
{
// do something
}
}
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
9/45
Because an exception is a class, we need also a file that
contains this class.
Example:
class myException extends RuntimeException {
public myException() {}
public myException( String s ) {
super( s );}
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
10/45
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); }}
-
7/29/2019 Object-Oriented Design Running Time Recursion
11/45
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;
-
7/29/2019 Object-Oriented Design Running Time Recursion
12/45
//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 integerSystem.out.println("Must specify an integer argument");
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
13/45
//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);
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
14/45
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");
}
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
15/45
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");
}
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
16/45
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;}}}
-
7/29/2019 Object-Oriented Design Running Time Recursion
17/45
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]); }
-
7/29/2019 Object-Oriented Design Running Time Recursion
18/45
Interfaces
Java provides a structure called interface for the
implementation ofapplication programming interface (API).An interface defines the methods that a class must implement.
If a class implements a given interface, it must have all the
methods defined in the interface.
Example:
public interface CarDriver {
public static final int speed = 100;
public Reading panel();
public void shiftGear( int n );public void brake( int n );
public void fuel( int n );
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
19/45
public class pickupTruck implements CarDriver {
// . . .
public Reading panel() {
// . . .
return rd;}
public void shiftGear( int n ) {
// . . .
}
public void brake( int n ) {
// . . .}
public void fuel( int n ) {
// . . .
}
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
20/45
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!);}
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
21/45
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
}
}
-
7/29/2019 Object-Oriented Design Running Time Recursion
22/45
Hero
ActionCharacter CanFight CanSwim CanFly
subclass implementation
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();}
-
7/29/2019 Object-Oriented Design Running Time Recursion
23/45
Abstract Classes
Weve reviewed this before. An abstract class contains both
abstract methods and normal methods. The abstract methodsmust be overridden in subclasses. Because of the abstract
methods, an abstract class cannot be instantiated.
-
7/29/2019 Object-Oriented Design Running Time Recursion
24/45
Casting
Recall that casting can be used to convert data types. Now let
us see how it works with objects.
If class T extends class S, then we can do the following
reference:
T o = new T();
S so = o;
S so1 = new T();
or
S so = new T();
T o = ( T )so;
S
To
so
extends assign
type casting
(T)so
-
7/29/2019 Object-Oriented Design Running Time Recursion
25/45
Interfaces can be treated similar to superclasses. If class T
implements interface I, we can do the following:
T o = new T();
I io = o;
I io1 = new T();
T o1 = ( T )io1;
I
To
io
Impl. assign
type casting
(T)io
A possible error by basic data type casting:
int i = 258;byte b = (byte) i;
0 011 0... ...
8 bits
16 bits
-
7/29/2019 Object-Oriented Design Running Time Recursion
26/45
Data Structure Exercises 2.1
-
7/29/2019 Object-Oriented Design Running Time Recursion
27/45
Running Time and Recursion
Suppose we have the following two pieces of code.
Fragment 1:
i = 0;
z = x + y;
n = i * i;
Fragment 2:
for( int i = 0; i < n; i++ ) {
a[ i ] = i * i;
}
Which one takes shorter time to run?
-
7/29/2019 Object-Oriented Design Running Time Recursion
28/45
Running Time
In this course, we want to compare algorithms. We want to
know which algorithm is more efficient.
By efficiency, we mean that an algorithm either takes less time
to run or uses less memory than the other.
Let us look at the running time here.
Running time depends on many factors. Since we will be
dealing with a collection of objects most of the time, what
interests us is the relationship between running time and thenumber of objects.
-
7/29/2019 Object-Oriented Design Running Time Recursion
29/45
Lets say we have n objects. We want to find out the function
)(nfT
Here Trepresents running time. In fact, we are interested in the
situation when the number of objects becomes very large.
-
7/29/2019 Object-Oriented Design Running Time Recursion
30/45
Some simple relationships are listed below.
Relationship )(nf Running time does not change (constant time) O(1)
Running time is proportional to logn O(logn)
Running time is proportional to n O(n)
Running time is proportional to the square of n O(n2)
Running time is proportional to n3
O(n3
)
Remember that these relationships hold only when n is very
large.
-
7/29/2019 Object-Oriented Design Running Time Recursion
31/45
Now let us compare these relationships.
constant c c c c cn 1 2 3 4 5
n2 1 4 9 16 25
n3 1 8 27 64 125
Therefore, if running time is proportional to n3
, the code willtake very long time to run. Next is proportional to n
2and so on.
If running time is a constant, it is fast. In fact, it is the fastest
type.
-
7/29/2019 Object-Oriented Design Running Time Recursion
32/45
Suppose that we have a task that can be done by one of the two
algorithms,A andB. The running time of algorithmA is
proportional to the number of objects, n while the running time
of algorithmB is proportional to the square ofn.
Which algorithm we should use?
-
7/29/2019 Object-Oriented Design Running Time Recursion
33/45
A Simple Rule to determine Running Time
There is a simple rule to estimate running time (or the
relationship, so to speek)
If we have a loop like this:
for( int i = 0; i < n; i++ ) {
a[ i ] = i * i;}
The running time is proportional to n.
-
7/29/2019 Object-Oriented Design Running Time Recursion
34/45
If we have nested loops like this:
for( int i = 0; i < n; i++ ) {
for( int j = i; j < n; j++ ) {a[ j ] = a[ i ] * j;
}
}
The running time is proportional to the square ofn (when n isery large).
Now we see a pattern here.
-
7/29/2019 Object-Oriented Design Running Time Recursion
35/45
Mathematicians have a fancy symbol for running-time
relationships when n is large. They call it big-Oh.
Relationship big-Oh Symbol
constant )1(O On the order of logn O(logn)
on the order ofn )(nO on the order ofn
2)( 2nO
on the order ofn3
)(3nO
In fact, O stands for order. These relationships belong
to different orders.
-
7/29/2019 Object-Oriented Design Running Time Recursion
36/45
Recursion
In Java course, we have learned recursion.
recursion: A method calls itself.
Example:
Factorial function can be expressed as
0! = 1,
1! = 1,
n! = n * (n1)!, n > 1.
We can use this relationship to calculate factorial function.
-
7/29/2019 Object-Oriented Design Running Time Recursion
37/45
Here is the method factorial().
public static long factorial( long n ) {
if( n
-
7/29/2019 Object-Oriented Design Running Time Recursion
38/45
public static long factorial( long n ) {
if( n
-
7/29/2019 Object-Oriented Design Running Time Recursion
39/45
public static long factorial( long n ) {if( n
-
7/29/2019 Object-Oriented Design Running Time Recursion
40/45
public static long factorial( long n ) {if( n
-
7/29/2019 Object-Oriented Design Running Time Recursion
41/45
public static long factorial( long n ) {
if( n
-
7/29/2019 Object-Oriented Design Running Time Recursion
42/45
public static long factorial( long n ) {if( n
-
7/29/2019 Object-Oriented Design Running Time Recursion
43/45
public static long factorial( long n ) {if( n
-
7/29/2019 Object-Oriented Design Running Time Recursion
44/45
Running Time of a Recursion
Let us take the factorial method as an example.
n Number of Function Calls
1 1
2 2
3 3
4 4
Therefore, the running time of a recursion is proposional to
the number of function calls. In this case, it is propotional to n,the size of the problem.
-
7/29/2019 Object-Oriented Design Running Time Recursion
45/45
Data Structure Exercises 2.2