Advertisement
taweesoft

Treasure_Earth

Sep 4th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. //elab-source: LabThree.java
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class LabThree {
  6.  
  7.     public static void main(String[] args) {
  8.     Scanner scanner = new Scanner(System.in);
  9.         String direction = "";
  10.         double distance = 0;
  11.         double x = 0;
  12.         double y = 0;
  13.         while(!direction.equals("END")) {
  14.         System.out.print("Enter direction : ");
  15.         direction = scanner.nextLine();
  16.         if(direction.equals("END")) {
  17.             direction = "END";
  18.             break;
  19.         }
  20.         System.out.print("Enter distance : ");
  21.         distance = Double.parseDouble(scanner.nextLine());
  22.             if(direction.equals("N")) {
  23.                 y += distance;             
  24.             }
  25.             else if(direction.equals("NE")) {
  26.                 x += distance*(Math.sqrt(2)/2);
  27.                 y += distance*(Math.sqrt(2)/2);
  28.             }
  29.             else if(direction.equals("E")) {
  30.                 x += distance;         
  31.             }
  32.             else if(direction.equals("SE")) {
  33.                 x += distance*(Math.sqrt(2)/2);
  34.                 y -= distance*(Math.sqrt(2)/2);            
  35.             }
  36.             else if(direction.equals("S")) {
  37.                 y -= distance;             
  38.             }
  39.             else if(direction.equals("SW")) {
  40.                 x -= distance*(Math.sqrt(2)/2);
  41.                 y -= distance*(Math.sqrt(2)/2);            
  42.             }
  43.             else if(direction.equals("W")) {
  44.                 x -= distance;
  45.             }
  46.             else if(direction.equals("NW")) {
  47.                 x -= distance*(Math.sqrt(2)/2);
  48.                 y += distance;             
  49.             }
  50.         }
  51.         System.out.println();
  52.         System.out.printf("Location of treasure is [%.3f,%.3f]\n",x,y);
  53.         System.out.printf("Distance from origin is %.3f",Math.sqrt((Math.pow(x,2))+(Math.pow(y,2))));
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement