Java slope between two points: In the previous article we have discussed Java Program to Find Volume of Prism
In this article we will see how to find the slope of a line, Python Slope Between Two Points, Python Find Slope Between Two Points, The Slope Of A Line Is Double, How To Find Slope Between Two Points.
Java Program to Find Slope of a Line
Before jumping into the program directly, let’s see first how we get the slope of a line.
Slope of a Line: m=(y2 - y1) / (x2 - x1)
Where,
- (x1,y1) is one point of line
- (x2,y2) is another point of line.
- m represents the slope of line.
Let’s see different ways to find slope of a line.
Approach:
- Declare/Take input value for two points of line means four input values for x1,y1,x2,y2.
- Now, find the slope of line using formula
(y2 - y1) / (x2 - x1) - Then display the result.
Method-1: Java Program to Find Slope of a Line By Using Static Value
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Scanner class object craeted
Scanner sc=new Scanner(System.in);
//points value are declared
double x1 =4;
System.out.println("value of x1:"+x1);
double y1 =3;
System.out.println("value of y1:"+y1);
double x2 =2;
System.out.println("value of x2:"+x2);
double y2 =6;
System.out.println("value of y2:"+y2);
//Finding slope of line
double slopeOfLine = (y2 - y1) / (x2 - x1);
//printing the result
System.out.println("Slope of Line is : " +slopeOfLine );
}
}
Output: value of x1:4.0 value of y1:3.0 value of x2:2.0 value of y2:6.0 Slope of Line is : -1.5
Method-2: Java Program to Find Slope of a Line By User Input Value
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Scanner class object craeted
Scanner sc=new Scanner(System.in);
//points value are taken as input from user
System.out.println("Enter value of x1:");
double x1 =sc.nextDouble();
System.out.println("Enter value of y1:");
double y1 =sc.nextDouble();
System.out.println("Enter value of x2:");
double x2 =sc.nextDouble();
System.out.println("Enter value of y2:");
double y2 =sc.nextDouble();
//Finding slope of line
double slopeOfLine = (y2 - y1) / (x2 - x1);
//printing the result
System.out.println("Slope of Line is : " +slopeOfLine );
}
}
Output: Enter value of x1: Enter value of y1: Enter value of x2: Enter value of y2: Slope of Line is : -1.5
Method-3: Java Program to Find Slope of a Line By User Defined Method
import java.io.*;
public class Main
{
public static void main(String[] args)
{
//points value are assigned
double x1 = 4, y1 = 3;
double x2 = 2, y2 = 6;
//calling the findSlope() user defined method
//and stroing the result in variable 'slopeOfLine'
double slopeOfLine=findSlope(x1, y1, x2, y2);
System.out.println("Slope of Line is : " +slopeOfLine );
}
//findSlope() m,method to find slope of line
static double findSlope(double x1, double y1, double x2, double y2)
{
//Finding slope of line
// and returning the result
return (y2 - y1) / (x2 - x1);
}
}
Output: Slope of Line is : -1.5
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Try this: How To Find Slope Between Two Points
Read more: Java Program to Find Line Angle from Two Point
Related Java Articles: