JAVA Exercises with classes and paramter passing: write a program that answers t
ID: 3758575 • Letter: J
Question
JAVA Exercises with classes and paramter passing:
write a program that answers these 4 questions:
1. given two points, compute the distance between the two points
2. given two points, compute the horizontal angle from the first point to the second
3. given the elevation angle and velocity, compute the (horizontal) distance an object travels
4. given a starting point, a distance, and a horizontal agle, compute the destination point
Give the user a menu including these 4 options plus an option to quit the program or repeat the menu until user wants to quit.
*Each point should be entered as x and y coordinates in feet, distance in feet, angles in degree and velocity on miles per hour.
*Distance and velocity must be greater than zero (positive)
*Horizontal angles must be between 0 and 360, inclusive
*Verticle angles must be between 0 and 90, exclusive (do not allow 0 or 90)
Explanation / Answer
package validate;
import java.util.Scanner;
public class Angle {
public static double distance(double x1,double y1,double x2,double y2){
return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
public static double angleBetween2Lines(double x1,double y1,double x2,double y2)
{
double angle1 = Math.atan2(y2 - y1,
x2 -x1);
double angle2 = Math.atan2(y1 - y1,
x2-x1);
return angle1-angle2;
}
public static double horizontalDistance(double velocity,double angle){
double range=(velocity*1609)*(velocity*1609)*Math.sin(2*Math.toRadians(angle));
range=range/(6.674*Math.pow(10,-11));
return range;
}
public static double[] point(double x1,double y1,double angle,double distance){
double []point2 = {0.0,0.0};
double x2=x1+distance*Math.cos(Math.toRadians(angle));
double y2=y1+distance*Math.sin(Math.toRadians(angle));
point2[0]=x2;
point2[1]=y2;
return point2;
}
public static void main(String []ar){
Scanner sc= new Scanner(System.in);
double x1=0.0;
double x2=0.0;
double y1=0.0;
double y2=0.0;
double angle=0.0;
double distance=0.0;
String ch="y";
while(ch.equalsIgnoreCase("y")){
System.out.println("what do you want 1. distance 2. angle 3. Horizontal Distance 4.Distanation Point 5. exit");
int choice =sc.nextInt();
switch(choice){
case 1:
System.out.println("enter points x1");
x1=sc.nextDouble();
System.out.println("enter points x2");
x2=sc.nextDouble();
System.out.println("enter points y1");
y1=sc.nextDouble();
System.out.println("enter points y2");
y2=sc.nextDouble();
System.out.println(distance(x1,y1,x2,y2));
break;
case 2:
System.out.println("enter points x1");
x1=sc.nextDouble();
System.out.println("enter points x2");
x2=sc.nextDouble();
System.out.println("enter points y1");
y1=sc.nextDouble();
System.out.println("enter points y2");
y2=sc.nextDouble();
System.out.println(Math.toDegrees(angleBetween2Lines(x1,y1,x2,y2)));
break;
case 3:
System.out.println("enter velocity");
double velocity=sc.nextDouble();
System.out.println("enter angle in degree");
angle=sc.nextDouble();
if(velocity<0||angle<0||angle>360)
System.out.println("sorry,data should be positive");
else
System.out.println("distance in miles "+horizontalDistance(velocity,angle)/1609.0);
break;
case 4:
System.out.println("enter point x1");
x1=sc.nextDouble();
System.out.println("enter point y1");
y1=sc.nextDouble();
System.out.println("enter angle");
angle=sc.nextDouble();
System.out.println("enter distance");
distance=sc.nextDouble();
double[]point2=new double[2];
if(distance<0||angle<0||angle>360)
{
System.out.println("sorry");
}
else{
point2=point(x1,y1,angle,distance);
System.out.println("x2="+point2[0]+"y2="+point2[1]);
}
break;
case 5:
System.exit(1);
}
System.out.println("do you want again press y ");
ch=sc.next();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.