Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.81 KB | None | 0 0
  1.  
  2. package fmiur001.proj1;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. import javax.jms.JMSException;
  8. import javax.jms.Message;
  9. import javax.jms.TextMessage;
  10.  
  11. import shared.CourseClient;
  12.  
  13. public class DistanceAnswer extends CourseClient {
  14.  
  15.         static private boolean debug = false;
  16.         static boolean done = false;
  17.  
  18.         /**
  19.          * @param args
  20.          */
  21.         public static void main(String[] args) {
  22.  
  23.                 name = "fmiur001";
  24.                 try {
  25.                         if (debug) {
  26.  
  27.                                 BufferedReader in = new BufferedReader(new InputStreamReader(
  28.                                                 System.in));
  29.                                 int counter = 1;
  30.  
  31.                                 // TODO: optional, but recommended
  32.                                 // read data from standard in and test your central point finder
  33.                                 while (done == false) {
  34.                                         System.out.println("Enter three points: x0,y0;x1,y1;x3,y3");
  35.                                         String input = in.readLine();
  36.                                         int[] answer = findCloser(input);
  37.                                         System.out.println(answer[0] + "," + answer[1]
  38.                                                       + " is closer to the first point");
  39.                                         if (counter > 3){
  40.                                                 done = true;
  41.                                         }else{
  42.                                                 counter++;
  43.                                         }
  44.                                 }
  45.  
  46.                         } else {
  47.                                 listenNetwork("triple_distance");
  48.  
  49.                                 while (done == false) {
  50.  
  51.                                         Message message = consumer.receive(10000);
  52.                                         if (message != null) {
  53.                                                 onMessage(message);
  54.                                         }
  55.                                 }
  56.                                 exitProgram();
  57.                         } // if
  58.                 } catch (Exception e) {
  59.                         e.printStackTrace();
  60.                 }
  61.  
  62.         } // main
  63.  
  64.         private static int[] findCloser(String input) {
  65.                 int[] answer = new int[2];
  66.                 String [] num_strings = input.split("\\D");
  67.                 int x0 = Integer.parseInt(num_strings[0]);
  68.                 int y0 = Integer.parseInt(num_strings[1]);
  69.                 int x1 = Integer.parseInt(num_strings[2]);
  70.                 int y1 = Integer.parseInt(num_strings[3]);
  71.                 int x2 = Integer.parseInt(num_strings[4]);
  72.                 int y2 = Integer.parseInt(num_strings[5]);
  73.                 // find distance between p0 and p1, p0 and p2.
  74.  
  75.                 double dist0_1 = distance(x0,y0,x1,y1);
  76.                 double dist0_2 = distance(x0,y0,x2,y2);
  77.  
  78.                 if (dist0_1 < dist0_2){
  79.                         answer[0] = x1;
  80.                         answer[1] = y1;
  81.                 }else{
  82.                         answer[0] = x2;
  83.                         answer[1] = y2;
  84.                 }
  85.                 return answer;
  86.         }
  87.  
  88.         private static double distance(int x0, int y0, int x1, int y1) {
  89.                 return Math.sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));
  90.         }
  91.  
  92.         public static void onMessage(Message message) {
  93.  
  94.                 try {
  95.  
  96.                         if (message instanceof TextMessage) {
  97.                                 TextMessage txtMsg = (TextMessage) message;
  98.  
  99.                                 String msg = txtMsg.getText();
  100.                                 String[] parts = msg.split(":");
  101.                                 if (parts[0].equals("problem")) {
  102.                                         // TODO: this is where your solution goes
  103.                                         // parse out the problem
  104.                                         // call your solution
  105.                                         // then send back the answer
  106.                                         System.out.println("problem received: " + parts[1]);
  107.                                         int [] answer = findCloser(parts[1]);
  108.                                         String response = "answer:" + answer[0] + ":" + answer[1];
  109.                                         System.out.println("sending answer " + response);
  110.  
  111.                                         producer.send (session.createTextMessage(response));
  112.  
  113.  
  114.                                 } else if (parts[0].equals("results")) {
  115.  
  116.                                         // TODO: this message tells you how many of the problems you
  117.                                         // got right
  118.                                         System.out.println ("results: " + parts[1] + " out of " + parts[2]);
  119.  
  120.  
  121.                                 } else if (parts[0].equals("explain")) {
  122.                                         // TODO: optional, this provides a history of the messages
  123.                                         // passed back and forth, useful for debugging, most
  124.                                         // debugging should be done though using the standard input
  125.                                         // version
  126.                                         System.out.println("explain: " + msg);
  127.                                 }
  128.  
  129.                         } else {
  130.                                 System.out.println("[Received unknown: '" + message + "']");
  131.                         }
  132.  
  133.                 } catch (JMSException e) {
  134.                         System.out.println("[ Caught: " + e);
  135.                         e.printStackTrace();
  136.                 }
  137.         }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement