ADT
ADT
Lecture Note #5
Understanding exceptions
Motivation
1. Software Engineering Issues (1/5)
Section 5.1 – 5.5 Program Design Principles
o Abstraction
Concentrate on what it can do and not how it does it
Use of Java Interface
o Coupling
Restrict interdependent relationship among classes to the
minimum
o Coherent
A class should be about a single entity only
There should be a clear logical grouping of all functionalities
o Information Hiding
Expose only necessary information to outside
class Employee {
or static final int MAX_NUMBER = 500;
private String names;
(better
private double salaries;
choice) }
...
Employee[] workers = new Employee[Employee.MAX_NUMBER];
Collection Spec. of a
ADT of data set of
operations
interface
--
||
–
*
int && boolean
+
/
++ !
int type with the operations boolean type with the operations
(e.g.: --, /) defined on it. (e.g.: &&) defined on it.
b a + bi
Real
0 a
[CS1020 Lecture 5 AY2013/4 S2]
19
2. ADT Eg: Complex Number as ADT (2/6)
User-defined data types can also be organized as ADTs
Let’s create a “Complex” ADT for complex numbers
add(c)
Complex(r,i)
minus(c)
imagpart()
Testing ComplexCart:
a=a+b is 11.0+14.0i
a-b (which is the original a) is 10.0+12.0i
Angle of a is 0.8760580505981934
a=a*b is -14.0+32.0i
System.out.println("\nTesting ComplexPolar:");
System.out.println("c is " + c);
System.out.println("d is " + d);
c.add(d);
System.out.println("c=c+d is " + c);
c.minus(d);
System.out.println("c-d (which is the original c) is " + c);
c.times(d);
System.out.println("c=c*d is " + c);
Testing ComplexPolar:
c is 8.660254037844387+4.999999999999999i
d is 5.000000000000001+8.660254037844386i
c=c+d is 13.660254037844393+13.660254037844387i
c-d (which is ... c) is 8.660254037844393+5.000000000000002i
c=c*d is 2.83276944823992E-14+100.00000000000007i
Practice Exercises
4. Pract. Ex. Fraction as ADT (1/3)
We are going to view Fraction as an ADT, before we
proceed to provide two implementations of Fraction
Qn: What are the data members (attributes) of a
fraction object (without going into its implementation)?
Qn: What are the behaviours (methods) you want to provide
for this class (without going into its implementation)?
Data Behaviors
members Add
Numerator Minus
We will leave out divide
Denominator Times for the moment
Simplify
[CS1020 Lecture 5 AY2013/4 S2]
39
4. Pract. Ex. Fraction as ADT (2/3)
How do we write an Interface for Fraction? Let’s call it
FractionI
You may refer to interface Complex for idea
But this time, we wants add(), minus(), times() and simplify() to
return a fraction object
FractionI.java
public interface FractionI {
public int getNumer(); //returns numerator part
public int getDenom(); //returns denominator part
public void setNumer(int numer); //sets new numerator
public void setDenom(int denom); //sets new denominator
throw ExceptionObject;
Constructor
ExceptionClassName(String Msg)
Construct an exception object with the error message Msg
void printStackTrace()
Print the calling stack
Note:
A method can throw more than one type of exception
[CS1020 Lecture 5 AY2013/4 S2]
53
5. Exceptions Exception Handling: Syntax
As the user of a method that can throw exception(s):
It is your responsibility to handle the exception(s)
Also known as exception catching
try {
System.out.println("Ans = " + factorial(input));
}
catch (IllegalArgumentException expObj) {
System.out.println(expObj.getMessage());
}
We choose to print out the error message in this
}
case. There are other ways to handle this error.
}