Write a program that helps the user calculate both the distance between two poin
ID: 3850765 • Letter: W
Question
Write a program that helps the user calculate both the distance between two points in the 2D Cartesian plane and the midpoint of the line segment those two points define. Try to make your results as readable as possible.
Oh, btw, the exact level of the user may be a bit variable. The client said they want to use this program anywhere in their K-12 school system. So, you'll need to account for the possibility that the user may use appropriate notation during entry or might not. They may leave off one or both parentheses or leave out the comma between the two coordinates. If they do, don't worry about it, but print a little reminder message to tell them what notation they messed up on.
So, to enter the point (3,-4), the user should be able to enter any of: (3,-4), 3,-4), (3,-4, (3 -4), 3,-4, 3 -4), (3 -4, or even 3 -4. Only the first would be quiet, however, the others would generate 1, 2, or even 3 warning/reminder messages about missing notation.
As an example, you mighthave the program interaction look something like (the parts in this color are typed by the user). -/pointcalcinp. out Welcone to the 2D Point Program! Where is your first point? 3.4 12.2) You were missing the open parenthesis before the x coordinate! You were missing the comma to separate coordinates Please use proper notation... Where is your second point? (13.4, 12.2 You were missing the close parenthesis after the y coordinate! Please use proper notation... Thank you! Calculating. Done. (3.4, 12.2) is 10 units away from (13.4, 12.2). The midpoint of the line segment fron (3.4, 12.2) to (13.4, 12.2) is (8.4, 12.2). Thank you for using the 2PP!! Endeavor to have a day.Explanation / Answer
Hi,
Since you didnt specify which language i went ahead and implemented this in java, have added comments let me know if you have any additional doubts
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
import org.apache.poi.util.SystemOutLogger;
public class Point {
static List<Float> point1= new ArrayList<>();// for storing points
static List<Float> point2= new ArrayList<>();
public static void main(String[] args) {
System.out.println("Welcome to 2D point program!!");
input(); // function to take input
calculate(point1,point2); // calculate and print
System.out.println("Thank you for using the program");
}
private static void calculate(List<Float> point1, List<Float> point2) {
// distance is the sqrt root of the sum of squares of x and y coordinates of 2 points
double distance= Math.sqrt(Math.pow((point1.get(0)-point2.get(0)),2)+Math.pow((point1.get(1)-point2.get(1)),2));
System.out.println("("+point1.get(0)+","+point1.get(1)+") is "+distance+" units away from ("+point2.get(0)+","+point2.get(1)+")");
// mid point is the average of x and y coordinates of the point
System.out.println("The midpoint of line segement from ("+point1.get(0)+","+point1.get(1)+") "
+ "to ("+point2.get(0)+","+point2.get(1)+") is ("+(point1.get(0)+point2.get(0))/2+","+(point1.get(1)+point2.get(1))/2+")");
}
private static void input() {
Scanner kb = new Scanner(System.in); // scanner for taking input
System.out.println("where is your 1st point?");
String exp = kb.nextLine();
while(!valid(exp))// loop till a valid point is entered
{
System.out.println("please follow proper notation");
System.out.println("where is your 1st point?");
exp = kb.nextLine(); // takes the input as string form
}
String[] splited = exp.split(","); // splits the string into 2 with the delimeter as comma
point1.add(Float.valueOf(splited[0].replace("(","")));// first remove the brackets and store it as float value
point1.add(Float.valueOf(splited[1].replace(")","")));
System.out.println("where is your 2nd point?");
exp = kb.nextLine();
while(!valid(exp))
{
System.out.println("please follow proper notation");
System.out.println("where is your 2nd point?");
exp = kb.nextLine();
}
splited = exp.split(",");
point2.add(Float.valueOf(splited[0].replace("(","")));
point2.add(Float.valueOf(splited[1].replace(")","")));
}
private static boolean valid(String exp) { // function to check valid input or not
int n=exp.length();
if(exp.charAt(0)!='(') // if first bracket is missing
System.out.println("missing paranthesis before x coordinate");
if(exp.charAt(n-1)!=')') // if last bracket is missing
System.out.println("missing paranthesis after y coordinate");
if(!exp.contains(","))
System.out.println("missing comma to seperate coordinates");
String regex = "\(-?([0-9]*[.])?[0-9]+,-?([0-9]*[.])?[0-9]+\)";// this is the regex to check if the expression is of the form (float,float)
Pattern pattern = Pattern.compile(regex);
return pattern.matcher(exp).find();
}
}
Thumbs up if this was helpful, otherwise let me know in comments.
Good day
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.