Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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)); }

2 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

3 Methods 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)); } Methods constructor

4 TestPoint Class public class TestPoint { public static void main(String[] args) { Point p = new Point(1, 2); Point q = new Point(3, 4); double d = p.distance(q); System.out.println(d); p.x = 4; p.y = 5; System.out.println(q.distance(p)); } 2 objects of Point class x,y distance() p x,y distance() q

5 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) { Point p = new Point(1, 2); Point q = new Point(3, 4); double d = p.distance(q); System.out.println(d); } Main Method in a Class Test method in Point class java Point => 2

6 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; } 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)); } getters and setters

7 Data Member Initialization public class Point { private double x = 0.0; private 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)); } Set the default values

8 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); }

9 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); } public void toString() { return “(“ + x + “, “ + y + “)”; } Point p = new Point(1,2); System.out.println(p); The output is (1,2)

10 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; } public double area() { return width * height; } center width height

11 Comparison of Two Approaches Object Oriented Approach: public class Point { data members methods: (distance, add, toString…) } … Point p = new Point(1,2); Point q = new Point(2,3); System.out.println(p + q); Double d = p.distance(q); Procedure Approach: public class Point { // define static functions public static double distance (double x1, double y1, double x2, double y2) { … } public static toString(double x, double y) { … } } … double d = Point.distance(1, 2, 2, 3); …

12 An Example: Bouncing Balls 1 4 3 2 Ball ======== State: x, y, speed, size, color Methods: move … Ball[] ball = new Ball[N]; ball[0] = new Ball(x, y, vx, vy, r, color); Ball[0].move(); …


Download ppt "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."

Similar presentations


Ads by Google