Lec2
Lec2
Fang Yu
[source:onanimation.com]
Abstract Data Type
ADT (Abstract Data Type)
¡ Specifies what each operation does, but not how it does (an
interface in Java)
¡ An interface is a list of method declarations without their
method body
[Source: php.jglobal.com]
Encapsulation
¡ Hide the implementation details of a module from
its users
[Source: entertainingcode.com]
Goals
Software implementation should be
¡ Robustness
¡ Adaptability
¡ Reusability
Object-oriented Design
An object
¡ Is the main actor in the object-oriented paradigm
¡ is an instance of a class
A Class
¡ Defines an object
¡ A player has his/her own name and record, and can watch,
talk, jump, and shoot.
Fields: name
Human Methods: watch(), talk(), jump()
extends
Fields: record
Player Methods: shoot()
Overriding
¡ Redefine a method in the subclass
¡ A player has his/her own name and record, and can watch,
talk, jump, and shoot.
Fields: name
Human Methods: watch(), talk(), jump()
extends
Fields: record
Player Methods: jump(), shoot()
Polymorphism
¡ An object can be polymorphic.
For example,
Fields: name
Human Methods: watch(), talk(), jump()
extends
Fields: record
Player Methods: shoot(), shoot(int a)
this
¡ A keyword in Java
¡ Arithmetic Progression
¡ f(n) = f(n-1)+d
¡ f(0) = 1, d =2, we have 1, 3, 5, 7, …
¡ Geometric Progression
¡ f(n) = f(n-1)*r
¡ f(0) = 1, r = 2, we have 1, 2, 4, 8, 16, …
¡ Fibonacci Progression
¡ f(n) = f(n-1)+f(n-2)
¡ f(0) =1, f(1) = 2, we have 1, 2, 3, 5, 8, 13, 21, …
Progression
¡ Fields:
¡ first (the first value)
¡ cur (the current value)
¡ Methods:
¡ Progression(): Initialize the field values (A Constructor
function)
¡ firstValue(): Reset the progression to the first value and return
that value
¡ nextValue(): Step the progression to the next value and return
that value
¡ printProgression(int n): Reset the progression and print the first
n values
Progression
//1, 2, 3, …
public class Progression {
protected long first;
protected long cur;
Progression(){ //Constructor
first = cur = 1;
}
protected long firstValue(){ //Reset cur
cur = first;
return cur;
}
protected long nextValue(){ //cur = cur+1; return cur;
return ++cur;
}
protected long printProgression(int n){ … }
}
Progression
1 3 5 7 9 11 13 15 17 19
1 3 9 27 81 243 729 2187 6561 19683
3 4 7 11 18 29 47 76 123 199
DoubleProgression
//1, 2, 3, …
public class DoubleProgression {
protected double first;
protected double cur;
Progression(){ //Constructor
first = cur = 1;
}
protected double firstValue(){ //Reset cur
cur = first;
return cur;
}
protected double nextValue(){ //cur = cur+1; return cur;
return ++cur;
}
protected double printProgression(int n){ … }
}
DoubleProgression
¡ Example:
if (insertIndex >= A.length){
throw new
BoundaryViolationException(“No element at index ” + insertIndex);
}
Catching Exceptions
¡ When an exception is thrown, it must be caught
Implementing Interfaces
(a class cannot)
…
public static void main(String[] args){
IntPair pair1 = new IntPair();
pair1.set(“age”, 20);
System.out.println(pair1.toString());
DoublePair pair2 = new DoublePair ();
pair2.set(new String(“grade”), new Double(82.53));
System.out.println(pair2.toString());
}
…
Javac Pair.java
Java Pair
[age, 20]
[grade, 82.53]
A Generic Example
public class Pair<K, V>{
K key;
V value;
public void set(K k, V v){
key = k;
value = v;
}
public K getKey(){ return key; }
public V getValue(){ return value;}
public String toString(){
return “[”+getKey()+”, ”+getValue()+” ]”;
}
public static void main(…){…}
}
A Generic Example
…
public static void main(String[] args){
Pair<String, Integer> pair1 = new Pair<String, Integer>();
pair1.set(new String(“age”), new Integer(20));
System.out.println(pair1.toString());
Pair<String, Double> pair2 = new Pair<String, Double>();
pair2.set(new String(“grade”), new Double(82.53));
System.out.println(pair2.toString());
}
…
Javac Pair.java
Java Pair
[age, 20]
[grade, 82.53]
Genetic Progression
//1, 2, 3, …
public class Progression <k> {
protected k first;
protected k cur;
Progression(){ //Constructor
first = cur;
}
protected k firstValue(){ //Reset cur
cur = first;
return cur;
}
protected k nextValue(){ //cur = cur+1; return cur;
return cur;
}
protected k printProgression(int n){ … }
}
Geometric Progression
//refines constructor, replaces nextValue(), and
//inherits Progression(), firstValue(), printProgression(int)
Output: 2, 4, 8, 16, 32
The Growth of Capital
¡ Get initial capital and years from user inputs
¡ Read TB Chapter 12