Unless you need to edit, it\'s safer to stay in Protected View Enable Editing We
ID: 2247865 • Letter: U
Question
Unless you need to edit, it's safer to stay in Protected View Enable Editing We need to calculate how far a robot moved in the x and y directions. We prompt the lowly undergraduate research assistant to input the total distance the robot moved and at what angle relative to the x axis. We then want the program to calculate and display how far the robot moved in the x and y directions. Write an error-free Java program to do the following things. Prompt the user to input a number. The number (maybe a decimal) corresponds to the angle that the robot moved relative to the x-axis. The user will enter the angle in degrees. I. Prompt the user to input the total distance that the robot moved from the origin. (Assume that the distance is in cm.) 2. The angle is not very accurate, so the angle value entered by the user should be rounded to the nearest integer. Also, if the angle is less than 4 degrees, use the unary increment operator (++) to add 1 to the (rounded) angle. 3. Display the angle in degrees. If applicable, you should use the value of the angle after it has been rounded and incremented. 4. Calculate and display how far the robot moved along the x and y-axes. You need to save the results from the x and y values in a variable. (You may assume that the angle is between 0 degrees and 90 degrees.) When you display the x and y results, only display the total distance moved in that direction - you do not need to display whether it is in the positive or negative direction. 5. The robot can fly! Assume the robot takes off and flies straight up (z direction) a distance of 12.5 cm. Calculate and display the distance from the origin for the new location. 6· The output should look like the following: Enter the angle in degrees: 25 Enter the distance moved (cm): 56.5 Angle 25.0 Distance moved in x direction = 51.20638996757072Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author Sam
*/
public class RobotMove {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the angle in degrees:");
int angle = Math.round(Float.parseFloat(br.readLine()));
if (angle < 4)
angle++;
System.out.println("Enter the distance moved (cm):");
double distance = Double.parseDouble(br.readLine());
System.out.println("Angle = " + angle);
double x = Math.cos(angle * Math.PI / 180) * distance;
double y = Math.sin(angle * Math.PI / 180) * distance;
System.out.println("Distance moved in x direction = " + x);
System.out.println("Distance moved in y direction = " + y);
double z = 12.5;
distance = Math.sqrt(x*x + y*y + z*z);
System.out.println("After moving 12.5 cm in z direction, distance = " + distance);
}
}
NOTE:
3D Distance formula is (x2 + y2 + z2)0.5 ,so accordingly, the answer given in the question is wrong. The correct answer is 57.86622503671723 and not 62.658...
**Correct me if am wrong
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.