25.8k views
4 votes
JAVA

Write a program into which we could enter Lily's Longitude and Latitude data. Each time a new longitude and latitude is entered it should ask if you
want to continue - the program should continue to ask for input if the user enters 1, and stop when the user enters 0. If an invalid pair of coordinates
entered by the user (i.e. with latitude not between - 90 and 90 inclusive or longitude not between -180 and 180 inclusive) then the program should
print "Incorrect Latitude or Longitude".
Once the user has finished inputting data, the program should display the farthest distance traveled by Lily in each direction (you may assume the user
has entered at least one valid longitude/latitude pair). However any invalid pairs of coordinates should be ignored when calculating these values - this
includes ignoring a valid latitude if it is entered with an invalid longitude and vice-versa.
The farthest points are given by:
• Farthest North - maximum latitude
• Farthest South- minimum latitude
• Farthest East - maximum longitude
• Farthest West - minimum longitude
Please note - you are not expected to enter all of Lily's data into your program: you can simply make up some sample data points if you wish.
the sample runs are in the picture.

JAVA Write a program into which we could enter Lily's Longitude and Latitude data-example-1

1 Answer

3 votes

import java.util.Scanner;

public class JavaApplication59 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

double north = -180, south = 180, east = -90, west = 90;

while (true){

System.out.println("Please enter the longitude:");

double lon = scan.nextDouble();

System.out.println("Please enter the latitude:");

double lat = scan.nextDouble();

if (lon > 90 || lon < -90 || lat >180 || lat < -180){

System.out.println("Incorrect Latitude or Longitude");

}

else{

if (lat > north){

north = lat;

}

if (lat < south){

south = lat;

}

if (lon > east){

east = lon;

}

if (lon < west){

west = lon;

}

}

System.out.println("Would you like to enter another location (1 for yes, 0 for no)?");

int choice = scan.nextInt();

if (choice == 0){

break;

}

}

System.out.println("Farthest North: "+north);

System.out.println("Farthest South: "+south);

System.out.println("Farthest East: "+east);

System.out.println("Farthest west: "+west);

}

}

I hope this helps!

User Emilles
by
4.5k points