Question

In: Computer Science

Write a Java program that prompts for and reads the number N of cities or locations...

Write a Java program that prompts for and reads the number N of cities or locations to be processed. It then loops N times to prompt for and read, for each location, the decimal latitude, decimal longitude, and decimal magnetic declination. It then computes and displays, for each location, the Qibla direction (or bearing) from Magnetic North.

The true bearing from a point A to a point B is the angle measured in degrees, in a clockwise direction, from the line joining the true north and point A to the line joining point A and point B:

The magnetic bearing from a point A to a point B is the angle measured in degrees, in a clockwise direction, from the line joining the magnetic north and point A to the line joining point A and point B.

The magnetic declination of a point A is the angle between the line joining point A to the true north and the line joining point A and the magnetic north.

Note: Magnetic declination is positive for easterly declinations, and negative for westerly declinations.

The formulas to find the true bearing of a point B from point A are:

  • x = sin(Math.toRadians(longitudeB – longitudeA))
  • y = cos(latitudeARadians)*tan(latitudeBRadians) - sin(latitudeARadians)*cos((longitudeB – longitudeA) Radians)
  • radians = tan-1(x/y);
  • angleInDegrees = Math.toDegrees(radians);

Depending on the signs of x and y, the true bearing angle   angleInDegrees computed above is normalized as:

If x = 0 and y > 0 and magneticDeclination > 0: angleInDegrees = angleInDegrees + 360

If x = 0 and y < 0:     angleInDegrees = angleInDegrees + 180

If x > 0 and y < 0:     angleInDegrees = angleInDegrees + 180

If x < 0 and y < 0:     angleInDegrees = angleInDegrees + 180

If x < 0 and y > 0:     angleInDegrees = angleInDegrees + 360

To find the magnetic bearing (or direction) of a point B from a point A, use the following formula:

          magneticBearing = normalizedTrueBearing - magneticDeclination

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

import java.util.Scanner;

public class QiblaDirection {

   public static void main(String[] args) {
       final int N=3;
       double[] longi=new double[N];
       double[] lati=new double[N];
       double[] magneticDeclination=new double[N];
       String[] name=new String[N];
       int i;
       double longiQibla=-39.75;
       double latiQibla=21.45;
       double x,y,radians,angleInDegrees;
       double magneticBearing;
       Scanner reader=new Scanner(System.in);
       for(i=0;i<N;i++){
           System.out.print((i+1)+" :Please give city name :");
           name[i]=reader.nextLine();
       }
       System.out.println("\n\n");
       for(i=0;i<N;i++){
           System.out.print("Please give latitude for "+name[i]+ ":");
           lati[i]=Double.parseDouble(reader.nextLine());
           System.out.print("Please give longitude for "+name[i]+ ":");
           longi[i]=Double.parseDouble(reader.nextLine());
           System.out.print("Please give magnetic declination for "+name[i]+ ":");
           magneticDeclination[i]=Double.parseDouble(reader.nextLine());
       }
      
       for(i=0;i<N;i++){
           x = Math.sin(longiQibla - longi[i]);
           y = Math.cos(Math.toRadians(lati[i]))*Math.tan(Math.toRadians(latiQibla)) - Math.sin(Math.toRadians(lati[i]))*Math.cos(Math.toRadians(longiQibla-longi[i]));
           //x = Math.sin(longi[i]-longiQibla);
           //y = Math.cos(Math.toRadians(latiQibla))*Math.tan(Math.toRadians(lati[i])) - Math.sin(Math.toRadians(latiQibla))*Math.cos(Math.toRadians(longi[i]-longiQibla));
           radians = Math.atan(x/y);
           angleInDegrees = Math.toDegrees(radians);
           if(x == 0 && y > 0 && magneticDeclination[i] > 0){
               angleInDegrees = angleInDegrees + 360;
           }else if(x == 0 && y < 0)
               angleInDegrees = angleInDegrees + 180;
           else if(x > 0 && y < 0)
               angleInDegrees = angleInDegrees + 180;
           else if(x < 0 && y < 0)
               angleInDegrees = angleInDegrees + 180;
           else if(x < 0 && y > 0)
               angleInDegrees = angleInDegrees + 360;
           magneticBearing = angleInDegrees - magneticDeclination[i];
           System.out.println("For "+name[i]+" magnetic bearing is :"+magneticBearing);
       }
       reader.close();

   }

}


Related Solutions

PYTHON WHILE Write a program that prompts for and reads the number ? of spheres to...
PYTHON WHILE Write a program that prompts for and reads the number ? of spheres to be processed. If ?≤0 your program must display an error message and terminate; otherwise it does the following for ? times: Write a program that prompts for and reads the number ?n of spheres to be processed. If ?≤0n≤0 your program must display an error message and terminate; otherwise it does the following for ?n times: Prompts for and reads the volume of a...
Please write a JAVA program which reads a number n from the user and check whether...
Please write a JAVA program which reads a number n from the user and check whether n is a perfect number or not. For example, when n = 7, the print out should be 7 is not a perfect number. If the input n is 6, then the program prints out 6 = 1 * 2 * 3
Write a JAVA program that prompts the user for the number of names they’d like to...
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Use Java (Find the number of days in a month) Write a program that prompts the...
Use Java (Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, If the user entered month 2 and year 2012, the program should display that February 2012 has 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 has 31 days. Sample Run 1 Enter a month in the...
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
JAVA Write a program that prompts the user to enter a matrix number of rows and...
JAVA Write a program that prompts the user to enter a matrix number of rows and number of columns. In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it. Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use global variables. Here...
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
Write a Java program that reads a whole number on the keyboard and obtains and displays...
Write a Java program that reads a whole number on the keyboard and obtains and displays twice and triple that number on the screen
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program that prompts for and reads in the two side lengths of a right...
Write a program that prompts for and reads in the two side lengths of a right triangle (floating point numbers) and then calls a float-valued function, hypot1, that calculates and returns the length of the hypotenuse of the triangle. The program then displays the two side lengths and the hypotenuse length. Note: The hypot1 function returns the hypotenuse length – it does not display it; the function should not contain any cout nor cin statements. Math review: Recall that for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT