skip to Main Content

JAVA: THE MYPOINT CLASS

JAVA: THE MYPOINT CLASS

JAVA: THE MYPOINT CLASS

Java: The MyPoint class

package chapter10_10_4; /** * * @author Siavash Bakhshi */ public class Chapter10_10_4 { /** * @param args the command line arguments */ public static void main(String[] args) { //Create a point with x1 & y1 MyPoint MyPoint1 = new MyPoint(); System.out.println("Distance betweenn point (" + MyPoint1.getX() +","+ MyPoint1.getY() + ") and point (10,30.5) is: n" + MyPoint1.distance(10,30.5)); // if you want to use setX or setY functions to change values // MyPoint MyPoint2 = new MyPoint(10, 30.5); // System.out.println("Distance between point " + MyPoint2.getX() + " and point " + MyPoint2.getY() + " is " + MyPoint2.distance()); } } /** Construct a point with x & y */ class MyPoint { double x; double y; /** Construct a point with a default constructor */ MyPoint() { this.x = 0; this.y = 0; } /** Construct a point with a specified x & y */ MyPoint(double x, double y) { this.x = x; this.y = y; } /** Return x */ double getX() { return x; } /** Set a new x */ void setX(double x) { this.x = x; } /** Return y */ double getY() { return y; } /** Set a new y */ void setY(double y) { this.y = y; } /** Return the distance between this point & specified */ //double distance() { //return Math.sqrt((this.x-x2)*(this.x-x2) + (this.y-y2)*(this.y-y2)); //} /** Return the distance between this point & specified */ double distance(double x2, double y2) { return Math.sqrt((this.x-x2)*(this.x-x2) + (this.y-y2)*(this.y-y2)); } }