Advertisement
Guest User

Justin Slocum - HW10 - QuadraticServer

a guest
Apr 28th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package quadraticcalculation;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.text.DateFormat;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.Scanner;
  9.  
  10. public class QuadraticServer {
  11.     static final int PORTNUMBER = 5010;
  12.  
  13.     static void pStr(String p) {
  14.         System.out.println(p);
  15.     }
  16.  
  17.     void runServer() {
  18.         ServerSocket server;
  19.         Socket connection;
  20.  
  21.         try {
  22.             pStr("Creating Server Socket " + PORTNUMBER + " . . . ");
  23.             server = new ServerSocket(PORTNUMBER);
  24.             pStr("SUCCESS!!!");
  25.  
  26.             while (true) {
  27.                 pStr("Waiting for connection.");
  28.                 connection = server.accept();
  29.                 pStr("Done");
  30.                 QuadraticCalculationThread t = new QuadraticCalculationThread(connection);
  31.                 t.start();
  32.                 pStr("");
  33.                 pStr("Communicating with " + connection.getInetAddress() + " " + connection.getPort());
  34.                 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  35.                 Date date = new Date();
  36.                 pStr("Time: " + dateFormat.format(date));
  37.             }
  38.         } catch (Exception e) {
  39.             e.printStackTrace();
  40.         }
  41.     }
  42.  
  43.     public class QuadraticCalculationThread extends Thread {
  44.         private Socket connection;
  45.  
  46.         QuadraticCalculationThread(Socket x) {
  47.             connection = x;
  48.         }
  49.  
  50.         public void run() {
  51.  
  52.             try (Scanner input = new Scanner(connection.getInputStream());
  53.                     PrintWriter output = new PrintWriter(connection.getOutputStream());
  54.             ) {
  55.                 double a = 0, b = 0, c = 0, r1 = 0, r2 = 0;
  56.                 int status = 0;
  57.                 String statmsg = null;
  58.                 try {
  59.                 a = Double.parseDouble(input.nextLine());
  60.                 b = Double.parseDouble(input.nextLine());
  61.                 c = Double.parseDouble(input.nextLine());
  62.                 if (a == 0) {
  63.                     status = -1;
  64.                     statmsg = "Not a quadratic equation.";
  65.                 } else {
  66.                     double discrim = b * b - 4 * a * c;
  67.                     if (discrim < 0) {
  68.                         statmsg = "No real roots.";
  69.                     } else if (discrim == 0) {
  70.                         status = 1;
  71.                         statmsg = "One real root.";
  72.                         r1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
  73.                     } else if (discrim > 0) {
  74.                         status = 2;
  75.                         statmsg = "Two real roots.";
  76.                         r1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
  77.                         r2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
  78.                     }
  79.                 }
  80.                 } catch (Exception e){
  81.                     status = -1;
  82.                     statmsg = "ERROR: Please enter an actual number.";
  83.                 }
  84.                 output.println(status);
  85.                 output.flush();
  86.                 output.println(statmsg);
  87.                 output.flush();
  88.                 if (status == 1) { //If only one root.
  89.                     output.println(r1);
  90.                     output.flush();
  91.                 } else if (status == 2) { //If two roots.
  92.                     output.println(r1);
  93.                     output.flush();
  94.                     output.println(r2);
  95.                     output.flush();
  96.                 }
  97.             } catch (Exception e) {
  98.                 pStr("ERROR!");
  99.             } finally {
  100.                 try {
  101.                     connection.close();
  102.                 } catch (Exception e) {
  103.                 }
  104.             }
  105.         }
  106.     }
  107.  
  108.     public static void main(String args[]) {
  109.         QuadraticServer s = new QuadraticServer();
  110.         s.runServer();
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement