This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Data Abstraction - Data Abstraction - Interfaces and Interfaces and ImplementationsImplementations
Cmput 115 - Lecture 1Department of Computing Science
• some thing that has: - identity - state - set of defined behaviours * operations (methods) the object does * requests operations (methods) other objects do
• a set of things that shareshare (have in common): - an identification technique - state variables - set of defined behaviours * operations (methods) the object does * requests operations (methods) other objects do
Example Class:
student class (student id, name, year, program, address, email*, takes courses, activities*, special needs*)
An object has a public interface and a private state.
The interface is the set of all resources the class provides.
Objects that share the same interface are organized into a group called a class and each object in the class is called an instance of the class. (object = instance of a class)
The state of an object differentiates it from other objects in the same class.
– Creating instances of class B.– Sending messages to instances of class B.– Invoking static methods from class B.– Using variables that are declared in class B.– Using message arguments that are
Ratio r = new Ratio(1,1); // r == 1.0r = new Ratio(1,2); // r == 0.5r.add(new Ratio(1,3)); // r still 0.5r = r.add(new Ratio(1,4)); // r == 0.75System.out.println(r.value()); // 0.75 printed
19Implementation - Ratio Implementation - Ratio Class -1Class -1
import structure.*;
public class Ratio {/* an object for storing a fraction */protected int numerator; // numerator of ratioprotected int denominator; // denominator of ratio
public Ratio(int top, int bottom) {/* pre: bottom != 0 post: constructs a ratio equivalent to top/bottom */
Assert.pre(bottom != 0, "Denominator must not be 0");
21Implementation - Ratio Implementation - Ratio Class -3Class -3
public double value() {/* post: returns the real value equivalent to ratio */return (double)this.numerator/(double)this.denominator;}
public Ratio add(Ratio other) {/* pre: other is non-null post: return new fraction - the sum of this and other */Assert.pre(other != null, "Other must not be null");return new Ratio(this.numerator*other.denominator+this.denominator*other.numerator,this.denominator*other.denominator);}
public double getX();/* post: returns the x coordinate. */public double getY();/* post: returns the y coordinate. */public double getR();/* post: returns the distance from the origin. */public double getTheta();/* post: returns the angle in radians from the x axis. */
Class - CartesianPoint -5Class - CartesianPoint -5public void transform(double dr, double dTheta) {/* pre: if dr < 0 then |dr| < current r post: changes the r and theta coordinates by adding the given values to them. */
Class - CartesianPoint -7Class - CartesianPoint -7public PlanarPoint add(PlanarPoint aPoint) {/* pre: other is non-null post: return new PlanarPoint - the sum of this and other */
Assert.pre(other != null, "Other must not be null");return new CartesianPoint(this.x + aPoint.getX(),
this.y + aPoint.getY());}
/* Note that we cannot use aPoint.x or aPoint.y since aPoint might not be a Cartesian Point. It may be an instance of some other class that implements the PlanarPoint interface. */