Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //elab-source: LabThree.java
- import java.util.Scanner;
- public class LabThree {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String direction = "";
- double distance = 0;
- double x = 0;
- double y = 0;
- while(!direction.equals("END")) {
- System.out.print("Enter direction : ");
- direction = scanner.nextLine();
- if(direction.equals("END")) {
- direction = "END";
- break;
- }
- System.out.print("Enter distance : ");
- distance = Double.parseDouble(scanner.nextLine());
- if(direction.equals("N")) {
- y += distance;
- }
- else if(direction.equals("NE")) {
- x += distance*(Math.sqrt(2)/2);
- y += distance*(Math.sqrt(2)/2);
- }
- else if(direction.equals("E")) {
- x += distance;
- }
- else if(direction.equals("SE")) {
- x += distance*(Math.sqrt(2)/2);
- y -= distance*(Math.sqrt(2)/2);
- }
- else if(direction.equals("S")) {
- y -= distance;
- }
- else if(direction.equals("SW")) {
- x -= distance*(Math.sqrt(2)/2);
- y -= distance*(Math.sqrt(2)/2);
- }
- else if(direction.equals("W")) {
- x -= distance;
- }
- else if(direction.equals("NW")) {
- x -= distance*(Math.sqrt(2)/2);
- y += distance;
- }
- }
- System.out.println();
- System.out.printf("Location of treasure is [%.3f,%.3f]\n",x,y);
- System.out.printf("Distance from origin is %.3f",Math.sqrt((Math.pow(x,2))+(Math.pow(y,2))));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement