Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Distance between points  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wrote program to calculate the distance between two points using java, it worked, but i need a program that will run continuously for any points i state until i exit the loop.
here is my code:
 
Bartender
Posts: 2155
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For loops you can read this tutorial: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
Also, you may find this useful: http://www.coderanch.com/how-to/java/UserInput

Usually it is better to write x * x than Math.pow(x, 2).
Also, take a look at Math.hypot() method
 
lowercase baba
Bartender
Posts: 12613
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally, you don't want much code in your main method. what you want to do is put most of your code into logical methods. You do this by analyzing what needs to be done:

1) get data
2) compute distance
3) print distance


You may then need to refine each of these. For example, "get data" has several parts - prompt user, read value in from keyboard (or wherever), validate data...etc. and these would be callled 4 times (once for each coordinate).

You could then have a method called "get_and_print_data". if you had that method, then it's pretty simple to write a loop that keeps calling it over and over...until your exit condition is met.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!