01 Adt
01 Adt
Algorithms
2
Policies for students
These contents are only used for students
PERSONALLY.
Students are NOT allowed to modify or
deliver these contents to anywhere or anyone
for any purpose.
3
Recording of modifications
Course codes cs1010, cs1020, cs2010 are
placed by 501042, 501043, 502043
respectively.
4
Objectives
Motivation
1. Software Engineering Issues
(1/5)
Program Design Principles
o Abstraction
Concentrate on what it can do and not how it does it
Eg: Use of Java Interface
o Coupling
Restrict interdependent relationship among classes to the
minimum
o Cohesion
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
Data: water
Operations: chill, crush, cube,
and isEmpty
Data structure: the internal
structure of the dispenser
Walls: made of steel
The only slits in the walls:
Input: water
Output: chilled water, crushed
ice, or ice cubes.
Crushed ice can be made in many ways.
We don’t care how it was made
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
[501043 Lecture 7: ADT]
22
Eg: Complex Number as ADT (2/6)
ADT
2.
add(c)
Complex(r,i)
minus(c)
imagpart()
class Complex {
private ... // data members
public Complex(double r, double i) { ... } // create a new object
public void add(Complex c) { ... } // this = this + c
public void minus(Complex c) { ... } // this = this - c
public void times(Complex c) { ... } // this = this * c
public double realpart() { ... } // returns this.real
public double imagpart() { ... } // returns this.imag
}
class Complex {
private double real;
private double imag;
// CONSTRUCTOR
public Complex(double r, double i) { real = r; imag = i; }
// ACCESSORS
public double realpart() { return real; }
public double imagpart() { return imag; }
// MUTATORS
public void add (Complex c) { // this = this + c (a + bi) + (c + di)
real += c.realpart(); = (a + c) + (b + d)i
imag += c.imagpart();
}
public void minus(Complex c) { // this = this - c (a + bi) – (c + di)
real -= c.realpart();
= (a – c) + (b – d)i
imag -= c.imagpart();
}
public void times(Complex c) { // this = this * c
real = real*c.realpart() - imag*c.imagpart(); (a + bi) (c + di)
imag = real*c.imagpart() + imag*c.realpart(); = (ac – bd) + (ad + bc)i
}
}
[501043 Lecture 7: ADT]
25
Eg: Complex Number as ADT (5/6)
ADT
Another possible implementation: Polar
2.
class Complex {
private double ang; // the angle of the vector
private double mag; // the magnitude of the vector
:
:
public times(Complex c) { // this = this * c
ang += c.angle();
mag *= c.mag();
}
:
:
}
representations
From Polar to Cartesian: real = mag * cos(ang);
imag = mag * sin(ang);
From Cartesian to Polar: ang = tan-1(imag/real);
mag = real / cos(ang);
or mag = sqrt(real2 + imag2);
y-axis
2
E.g.: Complex number 2 + i
(real, imag) mag = 2/cos(ang) or sqrt(22 + 12) =
2.236
1 (2, 1) real = 2
ag imag = 1
m
Complex.java
public interface Complex {
public double realpart(); // returns this.real
public double imagpart(); // returns this.imag
public double angle(); // returns this.ang
public double mag(); // returns this.mag
public void add(Complex c); // this = this + c
public void minus(Complex c); // this = this - c
public void times(Complex c); // this = this * c
}
In Java 7 and earlier, methods in an interface only have
signatures (headers) but no implementation
However, Java 8 introduces “default methods” to interfaces.
They provide default implementations which can be overridden
by the implementing class.
[501043 Lecture 7: ADT]
31
Example #2: ComplexCart (1/2)
Cartesian Implementation (Part 1 of 2)
Interface
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. Fraction as ADT (1/3)
We are going to view Fraction as an ADT, before we
proceed to provide two implementations of Fraction
Ex.
Data Behaviors
members Add
Numerator Minus
Denominator Times We will leave out divide
for the moment
Simplify
if (f1.equals(f2))
System.out.println("The fractions are the same.");
else
System.out.println("The fractions are not the same.");
// Data members
private int numer;
private int denom;
// Constructors
public Fraction() { this(1,1); }
public Fraction(int numer, int denom) {
setNumer(numer);
setDenom(denom);
}
// Accessors
public int getNumer() { // fill in the code }
public int getDenom() { // fill in the code }
// Mutators
public void setNumer(int numer) { // fill in the code }
public void setDenom(int denom) { // fill in the code }
while (b > 0) {
rem = a%b;
a = b;
b = rem;
}
return a;
}
// Fill in the code for all the methods below
public FractionI simplify() { // fill in the code }
public FractionI add(FractionI f) { // fill in the code }
public FractionI minus(FractionI f) { // fill in the code }
public FractionI times(FractionI f) { // fill in the code }
51
End of file