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

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.
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...
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...
1. Write a program that prompts the user for a filename, then reads that file in...
1. Write a program that prompts the user for a filename, then reads that file in and displays the contents backwards, line by line, and character-by character on each line. You can do this with scalars, but an array is much easier to work with. If the original file is: abcdef ghijkl the output will be: lkjihg fedcba Need Help with this be done in only PERL. Please use "reverse"
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a java program that asks the user for a number n and gives them the...
Write a java program that asks the user for a number n and gives them the possibility to choose between computing the sum and computing the product of 1,…,n. Example of running this program: Enter an integer number n: __7________ Enter Sum or Product: __Sum__________________________________ Program output: Sum of 1 ... 7 Sum or Product: Sum Sum = 28 Now second sample of second execution Enter an integer number n: __5__________________________________ Enter Sum or Product: __Product__________________________________ Program output:  Product of 1...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT