3-4 Java: Distance Between Two Points using Math Class

preview_player
Показать описание
Java Programming Challenge 3.4 - Distance Between Two Points using Math Class and java string format

Write an application that reads the (x,y) coordinates for two points. Compute the distance between the two points using a formula

Learning to code? Follow my Java tutorials for beginners. I do a lot of Java programming challenges and Java projects as part of my homework - every day!
If you are learning to program, nothing beats solving real programming exercises and coding challenges.
So don't forget to subscribe, as I release new programming videos every day!
Рекомендации по теме
Комментарии
Автор

This really helped me with my homework, thank you so much!!

catbatis
Автор

Nice. I'm learning java and have been looking for interesting projects. When I got to the formula I stopped the video and did it myself. Instead of calling the Math method I multipled both terms together instead of squaring them.
I see you used Double for the variables but wrote "nextInt" for the Scanner parts. How did that work?

ufotofu
Автор

Great stuff only problem is my OCD is going mental over that auto stub comment, still being there.

frdi
Автор

Here is how I put it together to make it more visually appealing.


import java.text.DecimalFormat;

public class DistanceFormula {

public static void main(String[] args)
{
//Variables needed to run program
DecimalFormat decForm = new DecimalFormat( "0.###" );
String strInput;
int x1 = 0;
int x2;
int y1;
int y2;
int repeat;
double distance;

//D0-While loop to allow for the program to restart on user command
do{

// User Input
strInput = JOptionPane.showInputDialog(null, "Enter X1 coordinate ");
x1 = Integer.parseInt(strInput);
strInput = JOptionPane.showInputDialog(null, "Enter Y1 coordinate ");
y1 = Integer.parseInt(strInput);
strInput = JOptionPane.showInputDialog(null, "Enter X2 coordinate ");
x2 = Integer.parseInt(strInput);
strInput = JOptionPane.showInputDialog(null, "Enter Y2 coordinate ");
y2 = Integer.parseInt(strInput);

//Calculation
distance = (Math.pow(x2-x1, 2) +(Math.pow(y2-y1, 2)));
distance = Math.sqrt(distance);

//Output
JOptionPane.showMessageDialog(null, "The distance is " + decForm.format( distance ));

// Does the user want to calculate another set of coordiates?
repeat = JOptionPane.showConfirmDialog(null, "Would you like to calculate another " +
"set of coordinates?", "Please Confirm.",
JOptionPane.YES_NO_OPTION);
}while (repeat == JOptionPane.YES_OPTION);

}

}

danielcarrilloiii
Автор

Why does it sound like you’re playing a xylophone at the same time? 🤔

julianlloyd
Автор

import java.util.Scanner;

public class Distance {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x1 = 0;
double x2;
double y1;
double y2;
double distance;


System.out.print("Enter X1 coordinate ");
x1 = scanner.nextDouble();
System.out.print("Enter Y1 coordinate ");
y1 = scanner.nextDouble();
System.out.print("Enter X2 coordinate ");
x2 = scanner.nextDouble();
System.out.print("Enter Y2 coordinate ");
y2 = scanner.nextDouble();
distance = (Math.pow(x2-x1, 2) +(Math.pow(y2-y1, 2)));
distance = Math.sqrt(distance);
System.out.print("The distance is " + distance);

}

}


I just did the process in 2 steps. I first add the coordinates and power then to 2. Then I square root the entire thing. I try to do all in one step but it just won't work. so anyone give me some feedback on how I can improve my code :)

randy
visit shbcf.ru