Omar_Natour

Natour, O. 4/28/16 Csc-112 Quad Server

Apr 28th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. /*
  2.  * Omar Natour
  3.  * 4/28/16
  4.  * Csc-112 Java 2
  5.  * Hw# 16
  6.  * Create a server to go on cs.stcc.edu that calculates quadratic roots
  7.  * Ojnatour0001@student.stcc.edu
  8.  */
  9.  
  10. import java.net.*;
  11. import java.io.*;
  12.  
  13. public class QuadServer {
  14.     public static void main(String[] args) {
  15.         QuadServer q = new QuadServer();
  16.         q.runServer();
  17.     }
  18.  
  19.     void runServer() {
  20.         ServerSocket server;
  21.         Socket connection;
  22.  
  23.         try {
  24.             System.out.println("Creating server socket 5009 .....");
  25.             server = new ServerSocket(5009);
  26.             System.out.println("It's Alive!!");
  27.  
  28.             while (true) {
  29.                 System.out.println("Wating for connection");
  30.                 connection = server.accept();
  31.                 System.out.println("Connected");
  32.  
  33.                 ThreadConnect t = new ThreadConnect(connection);
  34.                 t.start();
  35.             }
  36.         } catch (IOException e) {
  37.             e.printStackTrace();
  38.         }
  39.     }
  40.  
  41.     public class ThreadConnect extends Thread {
  42.         Socket connection;
  43.  
  44.         ThreadConnect(Socket x) {
  45.             connection = x;
  46.         }
  47.  
  48.         public void run() {
  49.  
  50.             DataOutputStream output = null;
  51.             DataInputStream input = null;
  52.  
  53.             try {
  54.                 input = new DataInputStream(connection.getInputStream());
  55.                 output = new DataOutputStream(connection.getOutputStream());
  56.  
  57.                 double a = 0, b = 0, c = 0;
  58.                 int status;
  59.                
  60.                 a = input.readDouble();
  61.                 b = input.readDouble();
  62.                 c = input.readDouble();
  63.                
  64.                 status = getStatus(a, b, c);
  65.  
  66.                 output.writeInt(status);
  67.  
  68.                 if (status != -1) {
  69.                     if (status == 1) {
  70.                         double[] roots = QuadraticFormula(a, b, c, status);
  71.                         output.writeDouble(roots[0]);
  72.                     } else if (status == 2) {
  73.                         double[] roots = QuadraticFormula(a, b, c, status);
  74.                         output.writeDouble(roots[0]);
  75.                         output.writeDouble(roots[1]);
  76.                     }
  77.                     System.out.println("Output given");
  78.                 }
  79.             }
  80.             catch (Exception e) {
  81.                 //System.out.println(e.getMessage());
  82.             } finally {
  83.                 try {
  84.                     input.close();
  85.                 } catch (Exception e) {
  86.                 }
  87.                 try {
  88.                     output.close();
  89.                 } catch (Exception e) {
  90.                 }
  91.                 try {
  92.                     connection.close();
  93.                 } catch (Exception e) {
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99.     private int getStatus(double a, double b, double c) {
  100.  
  101.         int status = -1;
  102.         double radicand = (Math.pow(b, 2)) - (4 * (a) * (c));
  103.  
  104.         if (radicand < 0)
  105.             status = 0;
  106.         else if (radicand == 0)
  107.             status = 1;
  108.         else if (radicand > 0)
  109.             status = 2;
  110.         if(a == 0)
  111.             status =-1;
  112.  
  113.         return status;
  114.     }
  115.  
  116.     private double[] QuadraticFormula(double a, double b, double c, int status) {
  117.  
  118.         double[] root = new double[2];
  119.         double radicand = (Math.pow(b, 2)) - (4 * (a) * (c));
  120.  
  121.         if (status == 1) {
  122.             root[0] = ((-1 * b) + (Math.sqrt(radicand))) / (2 * a);
  123.             // root[1] = 0;
  124.         } else if (status == 2) {
  125.             root[0] = ((-1 * b) + (Math.sqrt(radicand))) / (2 * a);
  126.             root[1] = ((-1 * b) - (Math.sqrt(radicand))) / (2 * a);
  127.         }
  128.         return root;
  129.     }
  130. }
Add Comment
Please, Sign In to add comment