Embed
Email

class

Document Sample

Shared by: hedongchenchen
Categories
Tags
Stats
views:
3
posted:
11/28/2011
language:
English
pages:
12
The Point Class

public class Point {

public double x;

public double y;



public Point(double x0, double y0)

{

x = x0; y = y0;

}



public double distance(Point p)

{

return Math.sqrt((x-p.x)*(x-p.x) – (y-p.y)*(y-p.y));

}

}

Data Members

public class Point {

public double x;

Data Members

public double y;



public Point(double x0, double y0)

{

x = x0; y = y0;

}



public double distance(Point p)

{

return Math.sqrt((x-p.x)*(x-p.x) – (y-p.y)*(y-p.y));

}

}

Methods

public class Point {

public double x;

public double y;



public Point(double x0, double y0)

{

constructor

x = x0; y = y0;

}



public double distance(Point p)

{

return Math.sqrt((x-p.x)*(x-p.x) – (y-p.y)*(y-p.y));

}

}

Methods

TestPoint Class



public class TestPoint {

public static void main(String[] args)

{

Point p = new Point(1, 2); 2 objects of Point

Point q = new Point(3, 4); class

double d = p.distance(q);

System.out.println(d);



p.x = 4; p.y = 5;

System.out.println(q.distance(p));

}

} p q

x,y x,y

distance() distance()

Main Method in a Class

public class Point {

public double x;

public double y;



public Point(double x0, double y0)

{

x = x0; y = y0;

}



public double distance(Point p)

{

return Math.sqrt((x-p.x)*(x-p.x) – (y-p.y)*(y-p.y));

}



public static void main(String[] args)

{

Test method in Point

Point p = new Point(1, 2); class

Point q = new Point(3, 4);

double d = p.distance(q); java Point => 2

System.out.println(d);

}

}

Getters and Setters

public class Point {

private double x;

private double y;



public Point(double x0, double y0)

{

x = x0; y = y0;

}



public double getX() { return x; }

public double getY() { return y; }

public void setX(double x) { this.x = x; } getters and setters

public void setY(double y) { this.y = y; }



public double distance(Point p)

{

return Math.sqrt((x-p.x)*(x-p.x) – (y-p.y)*(y-p.y));

}



}

Data Member Initialization

public class Point {

private double x = 0.0;

private double y = 0.0; Set the default values

public Point(double x0, double y0)

{

x = x0; y = y0;

}



public double getX() { return x; }

public double getY() { return y; }

public void setX(double x) { this.x = x; }

public void setY(double y) { this.y = y; }



public double distance(Point p)

{

return Math.sqrt((x-p.x)*(x-p.x) – (y-p.y)*(y-p.y));

}



}

Final Key Word

public class Point {

private final double x;

private final double y;



public Point(double x0, double y0)

{

x = x0; y = y0;

}



public double getX() { return x; }

public double getY() { return y; }

public void setX(double x) { this.x = x; }

public void setY(double y) { this.y = y; }



public double distance(Point p)

{

return Math.sqrt((x-p.x)*(x-p.x) – (y-p.y)*(y-p.y));

}



public Point add(Point p)

{

return new Point(x + p.x, y + p.y);

}

}

toString Method

public class Point {

private final double x = 0.0;

private final double y = 0.0;

public Point(double x0, double y0) {

x = x0; y = y0;

}



public double getX() { return x; }

public double getY() { return y; }

public void setX(double x) { this.x = x; }

public void setY(double y) { this.y = y; }



public double distance(Point p) {

return Math.sqrt((x-p.x)*(x-p.x) – (y-p.y)*(y-p.y));

}



public Point add(Point p) {

return new Point(x + p.x, y + p.y); Point p = new Point(1,2);

}

System.out.println(p);

public void toString() {

return “(“ + x + “, “ + y + “)”; The output is (1,2)

}

}

Objects in Another Class



public class Rectangle {

private Point center = new Point(0,0);

private double width = 0;

private double height = 0;



public Rectangle(Point c, double w, double h) {

center = c; width = w; height = h;

} width

public double area()

{ center height

return width * height;

}



}

Comparison of Two Approaches



Object Oriented Approach: Procedure Approach:



public class Point { public class Point {

data members // define static functions

methods: public static double distance (double x1,

(distance, add, toString…) double y1, double x2, double y2)

} {…}

public static toString(double x, double y)

… {…}

Point p = new Point(1,2); }

Point q = new Point(2,3);

System.out.println(p + q); …

Double d = p.distance(q); double d = Point.distance(1, 2, 2, 3);



An Example: Bouncing Balls

Ball

========

3

State: x, y, speed, size,

color



1 Methods:

2

move

4







Ball[] ball = new Ball[N];

ball[0] = new Ball(x, y, vx, vy, r, color);

Ball[0].move();





Related docs
Other docs by hedongchenchen
spec_2_
Views: 0  |  Downloads: 0
Life Expectancy Table
Views: 0  |  Downloads: 0
sbda tender document
Views: 0  |  Downloads: 0
Momentum010111
Views: 0  |  Downloads: 0
PVK06_DesignAndCoding
Views: 0  |  Downloads: 0
80R4852 TAD-D
Views: 0  |  Downloads: 0
spring_06
Views: 0  |  Downloads: 0
The 451 Group
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!