Problem: 6.1


Question 6.1:Define a class Line that has four private instance variables: (x1, y1) and (x2, y2),

representing the value of the x-coordinate and y-coordinate of the first point and second point on the line,

respectively, in a two-dimensional coordinate system. The following figure shows a line with these two

points.

The class Line has the following instance methods.

• findSlope(): the method takes no parameter and returns the slope of the line. The formula for

calculating the slope of a line using two points is given below.

slope =

𝑦2−𝑦1

𝑥2−𝑥1

• toString(): the method returns a String containing the values of (x1, y1) and (x2, y2) on a line. As an

example, for a line that has two points (3, 4) and (9, 5), the method may return the following String.

Line has points (3, 4) and (9, 5)

Use appropriate datatype. Your class definition must also include constructors.

Write a Java program that creates an array of Line type objects. The size of the array is 4. User may

input the values of x and y-coordinates of two points on those lines using Scanner class.

Your program should also have the following static method.

isIntersecting(Line l1, Linel2)

The method takes two parameters – both are Line type objects. The method must return true if two lines

are intersecting each other at any point. Otherwise, the method returns false. You can determine whether

(x1, y1) (x2, y2)

a line intersects another line by comparing their slope. If slopes of both lines are the same, then lines are

parallel, not intersecting each other. Otherwise, they will intersect each other at some point. You must

use findSlope() instance method while defining this static method. Your main method must call

isIntersecting() method with appropriate parameters and print the result accordingly.


//Main Class

package com.company;

import java.util.Scanner;

public class MainClass {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Line line1 = new Line();
        System.out.println("Enter Line one value here....");
        for (int i = 0; i <= 3; i++) {
            if (i == 0) {
                System.out.print("Enter X1: ");
                double a = input.nextDouble();
                line1.setX1(a);
            }
            if (i == 1) {
                System.out.print("Enter Y1: ");
                double a = input.nextDouble();
                line1.setY1(a);
            }
            if (i == 2) {
                System.out.print("Enter X2: ");
                double a = input.nextDouble();
                line1.setX2(a);
            }
            if (i == 3) {
                System.out.print("Enter Y2: ");
                double a = input.nextDouble();
                line1.setY2(a);
            }
        }
        Line line2 = new Line();
        System.out.println("Enter Line two value here....");
        for (int i = 0; i <= 3; i++) {
            if (i == 0) {
                System.out.print("Enter X1: ");
                double a = input.nextDouble();
                line2.setX1(a);
            }
            if (i == 1) {
                System.out.print("Enter Y1: ");
                double a = input.nextDouble();
                line2.setY1(a);
            }
            if (i == 2) {
                System.out.print("Enter X2: ");
                double a = input.nextDouble();
                line2.setX2(a);
            }
            if (i == 3) {
                System.out.print("Enter Y2: ");
                double a = input.nextDouble();
                line2.setY2(a);
            }
        }
        System.out.println("Line 1: ");
        System.out.println(line1.toString());
        System.out.println("Line 2: ");
        System.out.println(line2.toString());
        System.out.print("Line 1 slope is: ");
        System.out.println(line1.findSlope());
        System.out.print("Line 2 slope is: ");
        System.out.println(line2.findSlope());
        System.out.println("If ine are intersecting print true else false.....");
        System.out.print("Result is: ");
        Line.isIntersecting(line1, line2);
    }
}


//Line  class

public class Line {
    private double[] arr = new double[4];
    public Line() {
        arr[0] = 0;
        arr[1] = 0;
        arr[2] = 0;
        arr[3] = 0;
    }
    public double getX1() {
        return arr[0];
    }
    public void setX1(double x1) {
        arr[0] = x1;
    }
    public double getY1() {
        return arr[1];
    }
    public void setY1(double y1) {
        arr[1] = y1;
    }
    public double getX2() {
        return arr[2];
    }
    public void setX2(double x2) {
        arr[2] = x2;
    }
    public double getY2() {
        return arr[3];
    }
    public void setY2(double y2) {
        arr[3] = y2;
    }
    double findSlope() {
        double slope = (arr[3] - arr[1]) / (arr[2] - arr[0]);
        return slope;
    }
    public String toString() {
        return "Line has points (" + arr[0] + ", " + arr[1] + ")" + " and " +
                "(" + arr[2] + ", " + arr[3] + ")";
    }
    public static void isIntersecting(Line a, Line b) {
        double slope1 = a.findSlope();
        double slope2 = b.findSlope();
        if (slope1 == slope2) {
            System.out.println("True");
        } else
            System.out.println("False");
    }
}






Previous Post Next Post