Download presentation

Presentation is loading. Please wait.

Published byGia Longway Modified over 2 years ago

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(); …

Similar presentations

OK

Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.

Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Convert pdf ppt to ppt online ticket Ppt on air water and land pollution Ppt on mauryan Ppt on printed circuit board Download ppt on search engine Ppt on company law board Converter pdf to ppt online Ppt on cross cultural communication barriers Ppt on cash flow statement analysis Ppt on working of stock exchange in india