In: Computer Science
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:
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
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();
}
}