CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with...

22
CSE 341 -- S. Tanimoto 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented vs object-oriented. get- name(student-number) student.getName()
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with...

Page 1: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

1

Object-Oriented Design

Object: An encapulation of data with the methods that operate on that data.

Compare: function-oriented vs object-oriented. get-name(student-number) student.getName()

Page 2: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

2

Budd’s Example: Ordering Flowers

Budd orders flowers for Sally by sending a message to Flora.

Budd and Flora are members of a community of agents.

Budd

Flora

Sally

Delivery person

Sally’s florist

Flower arranger

Wholesaler

Grower

Gardener

Page 3: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

3

Characteristics of an ObjectEncapulates data and methods. Acts like an agent.

Has “responsibility” delegated to it by the designer

Is an instance of a class.

Its public members (data and/or methods) are accessible outside of its class.

Its private members are accessible only within methods of its class, permitting information hiding)

Goes into action when “sent” a “message” (i.e., when one of its member functions is called or “invoked”).

Page 4: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

4

Designing a System

Identify the component parts of the system

Associate data and functionality with each part

Identify the communication links among the components

Page 5: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

5

Designing an Object

Identify its responsibility. (“What” not “how”)

Identify the data it needs.

Separate out those services it needs and can delegate to other objects.

Page 6: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

6

Implementing an Object

Name its class.

Create prototypes for its public methods (to be compatible with the calls from other objects).

Design the internal data structures and create private variables for them.

Implement the public and any private methods making calls to other objects as needed.

Page 7: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

7

Example: A Program for Displaying Random Line

Segments and Their Intersections

Specs:

1. The program should randomly create a set of 10 line segments.

2. The segments should be displayed on a canvas.

3. Each intersection should be shown with a bold dot.

Page 8: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

8

Sample Desired Result

Page 9: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

9

Our Key Objects: Line SegmentsResponsibilities:

1. Display themselves.

2. Find their intersections with others.

3. Create themselves.

Delegated services:

1. Create random point

2. Get a list of all the other line segments

Page 10: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

10

Other ObjectsPoints:

1. Create point, create random point.

Set of line segments:

1. Create 10 line segments

2. Make a list of all the line segments other than a given one.

3. Display the set.

Page 11: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

11

Data Needed by LineSegmentPoints p1, p2 (endpoints of the segment)

Integer id

We declare that it is the responsibility of the higher-numbered line segment to determine any intersection between it and another segment.

Vector intersections

This is a list of intersections for which it is responsible.

Page 12: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

12

LineSegment.javaimport java.awt.*;import java.util.*;public class LineSegment { LSPoint p1, p2; int id; LineSegmentCollection collection; Vector intersections;

public LSPoint getP1() { return p1; } public LSPoint getP2() { return p2; } public LineSegment(int anID, LineSegmentCollection theCollection){...} public void paint(Graphics g) {...} private void computeIntersections() {...} private LSPoint segIntersection(LineSegmentls){...} private void plotIntersections(Graphics g) {...}}

Page 13: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

13

In LineSegment.java -- Constructor

public LineSegment(int anID, LineSegmentCollection theCollection){

id = anID; collection = theCollection; p1 = new LSPoint(); p2 = new LSPoint(); computeIntersections();}

Page 14: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

14

In LineSegment.java -- paint

public void paint(Graphics g) { g.drawLine(p1.getX(), p1.getY(), p2.getX(), p2.getY()); plotIntersections(g);}

Page 15: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

15

LineSegment: computeIntersections

private void computeIntersections() { intersections = new Vector(); for (Enumeration segments = (collection.otherSegments(this)).elements(); segments.hasMoreElements();) { LineSegment aLineSeg = (LineSegment) segments.nextElement(); LSPoint p = segIntersection(aLineSeg); if (p != null) intersections.addElement(p); }}

Page 16: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

16

LineSegment: segIntersectionprivate LSPoint segIntersection(LineSegment ls) { LSPoint p3 = ls.getP1(); LSPoint p4 = ls.getP2(); int x1 = p1.getX(); int y1 = p1.getY(); int x2 = p2.getX(); int y2 = p2.getY(); int x3 = p3.getX(); int y3 = p3.getY(); int x4 = p4.getX(); int y4 = p4.getY(); double d1 = (x1 * y2) - (x2 * y1); double d2 = (x3 * y4) - (x4 * y3); double d3 = ((x1 - x2) * (y3 - y4)) - ((x3 - x4) * (y1 - y2)); int x = (int) (((d1 * (x3 - x4)) - (d2 * (x1 - x2))) / d3); int y = (int) (((d1 * (y3 - y4)) - (d2 * (y1 - y2))) / d3);

Page 17: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

17

LineSegment: segIntersection (cont)

int minx1 = Math.min(x1, x2); if (x < minx1) return null; int maxx1 = Math.max(x1, x2); if (x > maxx1) return null; int miny1 = Math.min(y1, y2); if (y < miny1) return null; int maxy1 = Math.max(y1, y2); if (y > maxy1) return null; int minx2 = Math.min(x3, x4); if (x < minx2) return null; int maxx2 = Math.max(x3, x4); if (x > maxx2) return null; int miny2 = Math.min(y3, y4); if (y < miny2) return null; int maxy2 = Math.max(y3, y4); if (y > maxy2) return null; return new LSPoint(x, y);}

Page 18: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

18

LineSegment: plotIntersections

private void plotIntersections(Graphics g) { for (Enumeration intPoints = intersections.elements(); intPoints.hasMoreElements();) { LSPoint p = (LSPoint) intPoints.nextElement(); int x = p.getX(); int y = p.getY(); int radius = 5; g.fillOval(x-radius, y-radius, 2*radius, 2*radius); }}

Page 19: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

19

The Class LSPointimport java.util.Random;

public class LSPoint { int x, y; static Random rg = new Random();

public LSPoint() {x = Math.abs(rg.nextInt()) % 200;y = Math.abs(rg.nextInt()) % 200;

} public LSPoint(int xVal, int yVal) {

x = xVal;y = yVal;

} public int getX() { return x; } public int getY() { return y; }}

Page 20: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

20

The Class LineSegmentCollection

import java.applet.*;import java.awt.*;import java.util.*;

public class LineSegmentCollection extends Applet { Vector theSegments;

public void init() { theSegments = new Vector();

for (int i = 0; i < 10; i++) { theSegments.addElement(new LineSegment(i, this));

} }

Page 21: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

21

LineSegmentCollection (cont)

public void paint(Graphics g) { setBackground(Color.red); g.setColor(Color.black); for (Enumeration theEnum = theSegments.elements(); theEnum.hasMoreElements();) { ((LineSegment)theEnum.nextElement()).paint(g);

} }

public Vector otherSegments(LineSegment oneSegment) { Vector copyOfList = (Vector)(theSegments.clone()); copyOfList.removeElement((Object)oneSegment); return copyOfList; }}

Page 22: CSE 341 -- S. Tanimoto Java Objects 1 Object-Oriented Design Object: An encapulation of data with the methods that operate on that data. Compare: function-oriented.

CSE 341 -- S. Tanimoto Java Objects

22

Summary of Object-Oriented Design

Express the system as a collection of interacting agents (“objects”)

Identify data and functionality for each kind of object

Identify communication links among types of objects

Specify the interfaces for each type of object

Implement object behavior in classes and methods