Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fmiur001.proj1;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.TextMessage;
- import shared.CourseClient;
- public class DistanceAnswer extends CourseClient {
- static private boolean debug = false;
- static boolean done = false;
- /**
- * @param args
- */
- public static void main(String[] args) {
- name = "fmiur001";
- try {
- if (debug) {
- BufferedReader in = new BufferedReader(new InputStreamReader(
- System.in));
- int counter = 1;
- // TODO: optional, but recommended
- // read data from standard in and test your central point finder
- while (done == false) {
- System.out.println("Enter three points: x0,y0;x1,y1;x3,y3");
- String input = in.readLine();
- int[] answer = findCloser(input);
- System.out.println(answer[0] + "," + answer[1]
- + " is closer to the first point");
- if (counter > 3){
- done = true;
- }else{
- counter++;
- }
- }
- } else {
- listenNetwork("triple_distance");
- while (done == false) {
- Message message = consumer.receive(10000);
- if (message != null) {
- onMessage(message);
- }
- }
- exitProgram();
- } // if
- } catch (Exception e) {
- e.printStackTrace();
- }
- } // main
- private static int[] findCloser(String input) {
- int[] answer = new int[2];
- String [] num_strings = input.split("\\D");
- int x0 = Integer.parseInt(num_strings[0]);
- int y0 = Integer.parseInt(num_strings[1]);
- int x1 = Integer.parseInt(num_strings[2]);
- int y1 = Integer.parseInt(num_strings[3]);
- int x2 = Integer.parseInt(num_strings[4]);
- int y2 = Integer.parseInt(num_strings[5]);
- // find distance between p0 and p1, p0 and p2.
- double dist0_1 = distance(x0,y0,x1,y1);
- double dist0_2 = distance(x0,y0,x2,y2);
- if (dist0_1 < dist0_2){
- answer[0] = x1;
- answer[1] = y1;
- }else{
- answer[0] = x2;
- answer[1] = y2;
- }
- return answer;
- }
- private static double distance(int x0, int y0, int x1, int y1) {
- return Math.sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));
- }
- public static void onMessage(Message message) {
- try {
- if (message instanceof TextMessage) {
- TextMessage txtMsg = (TextMessage) message;
- String msg = txtMsg.getText();
- String[] parts = msg.split(":");
- if (parts[0].equals("problem")) {
- // TODO: this is where your solution goes
- // parse out the problem
- // call your solution
- // then send back the answer
- System.out.println("problem received: " + parts[1]);
- int [] answer = findCloser(parts[1]);
- String response = "answer:" + answer[0] + ":" + answer[1];
- System.out.println("sending answer " + response);
- producer.send (session.createTextMessage(response));
- } else if (parts[0].equals("results")) {
- // TODO: this message tells you how many of the problems you
- // got right
- System.out.println ("results: " + parts[1] + " out of " + parts[2]);
- } else if (parts[0].equals("explain")) {
- // TODO: optional, this provides a history of the messages
- // passed back and forth, useful for debugging, most
- // debugging should be done though using the standard input
- // version
- System.out.println("explain: " + msg);
- }
- } else {
- System.out.println("[Received unknown: '" + message + "']");
- }
- } catch (JMSException e) {
- System.out.println("[ Caught: " + e);
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement