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.

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

:

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:

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Question 01: Write a Java program that prompts for and reads the latitude of a location...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming QUESTION 9 Write a program that prompts for and reads four integer input values,...

    C Programming QUESTION 9 Write a program that prompts for and reads four integer input values, then a single character command. Submit your program as a .c file named midterm_prog2.c. Note that, similarly to your programming assignments, some percentage of your grade will depend on proper programming style. Your program will calculate and print a new value based on the command that's entered, as follows: 'A' or 'a': Calculate the average of the four input values 'S' or 's': Calculate...

  • Wrote a program called ExceptionalDivide that asks the user for 2 integer values and displays the...

    Wrote a program called ExceptionalDivide that asks the user for 2 integer values and displays the quotient of the first value divided by the second value. Make sure the your program reads in the two input values as ints. Your program must catch either of two exceptions that might arise and crash your program java.util.InputMismatchException //wrong data type inputted java.lang.ArithmeticException //divide by zero error Once you catch these exceptions, your program must print out a message explaining what happened and...

  • C Programming Quesition (Structs in C): Write a C program that prompts the user for a...

    C Programming Quesition (Structs in C): Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date and use the formula on page 190 (see problem 2 in the textbook) to convert the date entered into a very large number representing a particular date. Here is the formula from Problem 2 in the textbook: A formula can be used to calculate the number of days between two dates. This is affected by...

  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts...

    JAVA: (15 marks) Write a program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. "Out of Bounds") and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle...

  • Java programming 1. Write a program that reads an unspecified number of integers, determines how many...

    Java programming 1. Write a program that reads an unspecified number of integers, determines how many positive and negative value have been read, and computes the total and average of the input values (not counting zeros. Your program ends with the input 0. Display the average as a floating point number Here are sample runs: (red indicates a user input) Enter an integer, the input ends if it is 0: 1 2 -1 3 0 The number of positives is...

  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

  • For this lab you will write a Java program that plays the dice game High-Low. In...

    For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------ ------ High 1 x Wager Low 1 x Wager Sevens 4 x...

  • Write a Java program that prompts the user for the page size used in a virtual...

    Write a Java program that prompts the user for the page size used in a virtual memory system; this will be a power of two between 512 (29) and 16384 (214), inclusive. Your program should check the user input for page size to make sure it is one of the allowable inputs (must be a power of 2 and cannot be smaller than 512 or larger than 16384), and should then prompt the user for a virtual address (assume 32-bit...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT