Write a program that has the user input two points in the xy
Write a program that has the user input two points in the xy-plane, and then displays the equation of the line through those points in y = mx + b form. This should involve some very minor algebra, so it should be easy, but there’s one catch:
I want you to design your program so that the user will enter each point in the following format: (2, 3). That is, an opening (, a number, a comma, a number, a closing ).
So, your interface should look like:
Enter the first point: (1.1,2)
Enter the second point: (2.6,8)
The equation is: y = 4x - 2.4
(Here, the (1.1,2) and (2.6,8) are values chosen and input by the user; they should NOT be part of your code. The rest of the above is output shown.)
Specifications: your program must
• clearly ask the user to enter two points, and then accept any two points in the xy-plane (so long as they have different x-coordinates) from the user, who will enter the points in the form shown – e.g., (3,4).
• clearly display the equation of the line through those two points in y = mx + b form as shown above. “Ugly” output, e.g. y = 0x + -5 instead of y = -5 is acceptable.
Solution
import java.util.Scanner;
public class StraightLine {
/**method to calculate line constant (b) value
*
* @param m slope of the line
* @param x x-coordinate of point
* @param y y-coordinate of the same point
* @return a double value
*/
private static double calculateConstantValue(double m,float x,float y){
double b=0;
b=y-m*x; //calculating constant (b) value by passing x,y and m value
return b;
}
/**method to print the straight line equation
*
* @param m slope of a line
* @param b a constant value
*/
private static void printLineEquation(double m,double b){
if(m==0){
System.out.format(\"The equation is: y=%+.1f\",b); //remove x coordinate if slope value is zero.
}else{
System.out.format(\"The equation is: y=%.1fx%+.1f\",m,b); // using System.out.format to format straight line output
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in); //creating scanner object for taking input from user via console
System.out.println(\"Enter the first point:\");
String firstPoint=sc.next(); //first point coordinate should be in format of (a number a comma(,), a number a comma(,)).like \"(1.1,2)\"
System.out.println(\"Enter the second point:\");
String secondPoint=sc.next(); //second point coordinate should be in format of (a number a comma(,), a number a comma(,)).like \"(2.6,8)\"
firstPoint=firstPoint.replace(\'(\',\' \'); // removing unnecessary brackets\"( \" from firstPoint coordinate which is entered by the user
firstPoint=firstPoint.replace(\')\',\' \'); // removing unnecessary brackets\" )\" from firstPoint coordinate which is entered by the user
secondPoint=secondPoint.replace(\'(\',\' \'); // removing unnecessary brackets\"(\" from secondPoint coordinate which is entered by the user
secondPoint=secondPoint.replace(\')\',\' \'); // removing unnecessary brackets\" )\" from secondPoint coordinate which is entered by the user
String firstPointXcordinate=(firstPoint.split(\",\")[0]); //splitting coordinate based on \",\" character and taking first element from array as x1 coordinate
float x1Cordinate=Float.parseFloat(firstPointXcordinate); // converting x1Cordinate from string to float
String firstPointYcordinate=(firstPoint.split(\",\")[1]); //splitting coordinate based on \",\" character and taking second element from array as y1 coordinate
float y1Cordinate=Float.parseFloat(firstPointYcordinate); // converting y1Cordinate from string to float
String secondPointXcordinate=(secondPoint.split(\",\")[0]); //splitting coordinate based on \",\" character and taking first element from array as x2 coordinate
float x2Cordinate=Float.parseFloat(secondPointXcordinate); // converting x2Cordinate from string to float
String secondPointYcordinate=(secondPoint.split(\",\")[1]); //splitting coordinate based on \",\" character and taking second element from array as y2 coordinate
float y2Cordinate=Float.parseFloat(secondPointYcordinate); // converting y2Cordinate from string to float
double m=(double)(y2Cordinate-y1Cordinate)/(x2Cordinate-x1Cordinate); // using mathematical formula for calculating slope m=(y2-y1)/(x2-x1)
m=(double)Math.round(m*100)/100; // storing slope value in variable m upto 2 decimal places.
double b=calculateConstantValue(m, x1Cordinate, y1Cordinate); // calculating straight line constant(b) value by passing slope, x1Cordinate and y1Cordinate and same can be achieve by passing slope, x2Cordinate and y2Cordinate both will lead to the same result.
b=(double)Math.round(b*100)/100; // storing constant (b) value in variable b upto 2 decimal places.
printLineEquation(m, b); // print straight line equation by passing slope and constant (b) value.
}
}

