1 / 12

The Point Class

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) {

efrem
Télécharger la présentation

The Point Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Data Members 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)); } } constructor Methods

  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() x,y distance() p q

  5. 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) { Point p = new Point(1, 2); Point q = new Point(3, 4); double d = p.distance(q); System.out.println(d); } } 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; } } width center height

  11. Comparison of Two Approaches 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); … 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);

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

More Related