Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Java program compute the total distance traveled and the average speed in miles per hour Use the map scaling factor GPS

Many Global Positioning Satellite (GPS) units can record waypoints. The waypoint
marks the coordinates of a location on a map along with a timestamp. Consider a
GPS unit that stores waypoints in terms of an (X,Y) coordinate on a map together
with a timestamp t that records the number of seconds that have elapsed since the
unit was turned on. Write a program that allows the user to enter as many waypoints
as desired, storing each waypoint in an ArrayList, where each waypoint
is represented by a class that you design. Each waypoint represents a successive sample
point during a hike along some route. The coordinates should be input as doubles,
and the timestamp as an integer. Have your program compute the total distance
traveled and the average speed in miles per hour. Use the map scaling factor of
1 = 0.1 miles. For example, if the only two waypoints are (X=1,Y=1,T=0) and
(X=2,Y=1,T=3600), then the hiker traveled a distance of 0.1 miles in 3,600 seconds,
or 0.1 miles per hour.

Answer:


/**
 * QuestionGPS.java
 * This program computes the distance and average speed based
 * on a set of waypoints.  It uses an ArrayList of type Waypoint
 * to store and calculate the distances.
 */

import java.util.Scanner;
import java.util.ArrayList;

/**
* The Waypoint class stores the details for a single waypoint.
*/
class Waypoint
{
	private double x, y;		// Coordinates for the waypoint
	private int time;		// Time in seconds the waypoint was recorded

	/**
	* Constructors
	*/
	public Waypoint()
	{
		time =0;
		x=0;
		y=0;
	}

	public Waypoint(double x, double y, int time)
	{
		this.x = x;
		this.y = y;
		this.time = time;
	}

	/**
	* Accessor and mutator methods
	*/
	public void setX(double x)
	{
		this.x = x;
	}
	public void setY(double y)
	{
		this.y = y;
	}
	public void setTime(int t)
	{
		this.time = t;
	}
	public double getX()
	{
		return x;
	}
	public double getY()
	{
		return y;
	}
	public int getTime()
	{
		return time;
	}
}

/**
* The GPS class keeps an ArrayList of Waypoints and had
* methods to operate on this ArrayList.
*/
class GPS
{
	private ArrayList<Waypoint> waypoints;
	public static final double MAPSCALE = 0.1;		// 1 unit = 0.1 miles

	GPS()
	{
		waypoints = new ArrayList<Waypoint>();
	}

	/**
	* This static method calculates the distance between two points.
	* @param x1 X coordinate of point 1
	* @param y1 Y coordinate of point 1
	* @param x2 X coordinate of point 2
	* @param y2 Y coordinate of point 2
	* @return double Distance between (x1,y1) and (x2,y2)\
	*/
	public static double calcDistance(double x1, double y1, double x2, double y2)
	{
		return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
	}

	/**
	* This method inputs data from the keyboard into the ArrayList.
	*/
	public void inputData()
	{
		String s;
		Scanner scan = new Scanner(System.in);
		int t;
		double x,y;

		System.out.println("Enter waypoints, one per line, in the format:  x y time");
		System.out.println("Enter a blank line when finished.");

		do
		{
			s = scan.nextLine();			// Read in a line
			if (s.length() > 1)
			{
				// Extract the coordinates and time from the String using a scanner
				Scanner waypointScan = new Scanner(s);
				x = waypointScan.nextDouble();
				y = waypointScan.nextDouble();
				t = waypointScan.nextInt();
				// Create a new waypoint
				Waypoint w = new Waypoint(x,y,t);
				// Add it to the ArrayList
				waypoints.add(w);
			}
		} while (s.length() > 1);
		System.out.println();
	}

	/**
	* totalDistance iterates through the Waypoint arraylist and calculates
	* the distance between successive points.  These distances are summed
	* and returned.
	* @return double total Euclidean distance between all points in the Waypoint arraylist.
	*/
	public double totalDistance()
	{
		double d = 0;
		Waypoint lastPoint = null, currentPoint = null;

		if (waypoints.size() < 2) return 0;
		lastPoint = waypoints.get(0);
		for (int i=1; i<waypoints.size(); i++)
		{
			currentPoint = waypoints.get(i);
			d += calcDistance(lastPoint.getX(), lastPoint.getY(),
					currentPoint.getX(), currentPoint.getY());
			lastPoint = currentPoint;
		}
		return d;
	}


	/**
	* totalTime iterates through the Waypoint arraylist and calculates
	* the time between successive points.  The difference in time is summed
	* and returned.  This assumes that time is increasing with each successive waypoint.
	*
	* @return int total time in seconds for the course recorded in the waypoints.
	*/
	public int totalTime()
	{
		int t = 0;
		Waypoint lastPoint = null, currentPoint = null;

		if (waypoints.size() < 2) return 0;
		lastPoint = waypoints.get(0);
		for (int i=1; i<waypoints.size(); i++)
		{
			currentPoint = waypoints.get(i);
			t += (currentPoint.getTime() - lastPoint.getTime());
			lastPoint = currentPoint;
		}
		return t;
	}
}

public class QuestionGPS
{
	/**
	* Main method
	* In main we create a GPS object, input data from the keyboard,
	* then calculate and output the total distance, time, and average speed.
	*/
	public static void main(String[] args)
	{
		GPS g = new GPS();
		double totalDistance;
		int totalTime;

		g.inputData();
		totalDistance = g.totalDistance();
		totalTime = g.totalTime();
		System.out.printf("The total distance = %3.4f miles.\n",
				totalDistance * g.MAPSCALE);
		System.out.printf("The total time = %d seconds, or %3.4f hours.\n",
				totalTime, totalTime/3600.0);
		System.out.printf("The average speed is %3.4f miles per hour.",
				totalDistance*3600*g.MAPSCALE/totalTime);
		System.out.println();
	}
} 


About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!