Home
Answers
Viewqa
Java-Beginners
loop to input multiple points to calculate final distance and speed

QuestionAsk Questions?

Posted in : Java Beginners

import java.util.Scanner;

public class Travel
{
private double time;

private double distance;

public Travel() { time=0; distance=0; } public void setTime(double t) { time=t; } public void setDistance(double d) { distace=d; } public void getTime() { return time; } public void getDistance() { return distance; } public static void main (String[] args) { Scanner input= new Scanner(System.in); System.out.print(" Enter two doubles for points and an integer for speed"); double x1= input.nextDouble(); double y1= input.nextDouble(); int s1= input.nextInt(); int n=2; do{ System.out.print(" Way point", +n); Scanner input= new Scanner(System.in); double x2 =input.nextDouble(); double y2=input.nextDouble); int s2=input.nextInt(); n=n+1; double d= Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1, 2); distance=distance+d; double t= distance/s1; time=time+t; x1=x2; y1=y2; s1=s2; } while(s1!=0)

System.out.println(" The total distance is", +distance); System.out.println("The total time is ", time/60, "hours ", time%60, "minutes");

}

View Answers

Related Tutorials/Questions & Answers:

Advertisements