Java | UML Design - 2 | Line and Point

Perahin
0

>> Point and Line Uml Design

Objective:

       To learn about class and objects

       To learn to implement a class using UML

Task:

Implement the following classes, each on a separate file:


Now create a Line object and invoke the length() method.

SOLUTION:

#Point.java:

package lab_tasks;

public class Point {
    private int x,y;
    Point(int x, int y){
        this.x = x;
        this.y = y;
    }
    Point(){}
    int getX(){
        return x;
    }
    int getY(){
        return y;
    }
    void setX(int x){
        this.x = x;
    }
    void setY(int y){
        this.y = y;
    }
    public String toString() {
        return "("+x+","+y+")";
    }
    double distance(Point point){
        return Math.sqrt(Math.pow((point.x - this.x),2)+Math.pow((point.y - this.y),2));
        // calculating distance between two points
        // initially one point is user defined and other is (0,0)
        // (0,0) auto generated when an object under Point class is being created

        // @but when "start_point.distance(this.end_point)" line calls distance like this
        // point.x or point.y indicates to the end_points
        // and this.x or this.y indicates to the start_points
    }
}

#Main.java:

package lab_tasks;

public class Lab8_Main {
    public static void main(String[] args) {
    Point p1 = new Point(2,2);
    Point p2 = new Point(4,4);

    Line l1 = new Line(p1,p2);
    System.out.print("Length of "+p1+" & "+p2+" = "+l1.length());
    }
}

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !