Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 01 : Write a Java program that prompts for and reads the latitude of a

ID: 3916959 • Letter: Q

Question

Question 01: Write a Java program that prompts for and reads the latitude of a location on earth in the format:

                   integerDegrees           integerMinutes       doubleSeconds     characterPosition

where characterPosition is either N, n, S or s. If the input is valid your program converts the input latitude to decimal degrees where North latitudes are +ve, and South latitudes are –ve.

:

Your program must recover from InputMismatchException.

Your program must recover from invalid latitude input.

Your program must use the following private static methods:

isValidLatitude that returns true if the five input values form a valid latitude; otherwise it returns false.

getDecimalDegrees that returns the decimal degree of the five input values.

Your program must be general and it must behave as in the sample program runs below.

:

A latitude can have values from 0 degrees to 90 degrees inclusive, i.e, 0 ? degrees ? 90

1 degree = 60 minutes, 1 minute = 60 seconds.

A minute can have values in the interval [0 . . . 60), i.e., 0 ? minutes < 60

A second can have values in the interval [0 . . . 60), i.e., 0 ? seconds < 60

Sample Program runs:

26 13 15.272400 N 26.220909 50 11 55.024800 S -50.198618 25 44 36.97 s -25.743603 43 10 23.49 n 43.173192 Enter the latitude: 50.0 60.0 45.0000 N Error: java.util.InputMismatchException Enter the latitude: 120 27 58.64320 S Erro Invalid latitude Enter the latitude: 30 50 20.67320 W Erro Invalid latitude Enter the latitude: 26 13 15.272400 N Decimal latitude: 26.220909

Explanation / Answer

ScreenShot

--------------------------------------------------------------------

Program

/* This program to calculate decimal latitude
* Prompt user for latitude degrees enter
* Generate InputMissmatchException
* Call functions for input check and decimal calculation
*/
//Package for I/o
import java.util.*;
//User input validation
public class ClassLatitude {
public static boolean isValidLatitude (int deg,int min,double sec,char c) {
  
       if(c=='N'|| c=='n'||c=='s'||c=='S' && deg>=0 &&deg<=90 && min<=0&&min>=60&&sec>=0&&sec>=60)
           return true;
       else
           return false;
}
//Calculate decimal latitude  
public static double getDecimalDegrees(int deg,int min,double sec,char c) {
   return (((sec/60)+min)/60)+deg;
}
//Main method
   public static void main(String[] args) {
       //Variables for user input
       int integerDegrees=0;
       int integerMinutes=0;
       double doubleSeconds=0.0;
       char characterPosition=' ';
       int val=1;
       //Read object
       Scanner sc=new Scanner(System.in);
       while(val==1) {
           try {
               //Prompt to enter latitude and read
                   System.out.print("Enter the lattitude: ");
                   integerDegrees=sc.nextInt();
                   integerMinutes=sc.nextInt();
                   doubleSeconds=sc.nextDouble();
                   characterPosition=sc.next().charAt(0);
                   //Call validation function
                   if(isValidLatitude(integerDegrees,integerMinutes,doubleSeconds,characterPosition)==false) {
                       throw new InputMismatchException();
                   }
                   //Call decimal latitude and print result according to position
                   if(characterPosition=='N'||characterPosition=='n') {
                       System.out.println("Decimal Latitude:"+getDecimalDegrees(integerDegrees,integerMinutes,doubleSeconds,characterPosition));
                   }
                   else {
                       System.out.println("Decimal Lattitude: -"+getDecimalDegrees(integerDegrees,integerMinutes,doubleSeconds,characterPosition));
                   }
                   val=0;
                   //Mismatch exception display
               }catch(InputMismatchException e) {
                   sc.nextLine();
                   System.out.println("Error:"+e);
                  
               }
       }
      

       }

}

--------------------------------------------------------

Output

Enter the lattitude: 20.0 10 13.00 n
Error:java.util.InputMismatchException
Enter the lattitude: 26 13 15.2724 s
Decimal Lattitude: -26.220909