120 likes | 253 Views
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) {
E N D
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; 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
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
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
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
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
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
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); } public void toString() { return “(“ + x + “, “ + y + “)”; } } Point p = new Point(1,2); System.out.println(p); 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; } public double area() { return width * height; } } width center height
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);
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(); …