Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. /*
  2.  * Name: Phat Tu
  3.  * Date: 04/28/17
  4.  * Course Number: CSC-112-D01
  5.  * Course Name: Intermediate Java
  6.  * Problem Number: HW #10
  7.  * Email: pgtu0001@student.stcc.edu
  8.  * Create a server-client that calculates the quadratic formula.
  9.  */
  10.  
  11. import java.net.*;
  12. import java.text.DateFormat;
  13. import java.text.SimpleDateFormat;
  14. import java.io.*;
  15. import java.util.Date;
  16. import java.util.Scanner;
  17.  
  18. public class QuadraticServer implements Runnable {
  19.     private int portNum;
  20.  
  21.     public QuadraticServer(int port) {
  22.         this.portNum = port;
  23.     }
  24.  
  25.     public class ThreadConnect extends Thread {
  26.         private Socket connection;
  27.  
  28.         ThreadConnect(Socket x) {
  29.             connection = x;
  30.         }
  31.  
  32.         public void run() {
  33.             PrintWriter pw = null;
  34.             Scanner sc = null;
  35.             try {
  36.                 pw = new PrintWriter(connection.getOutputStream());
  37.                 sc = new Scanner(connection.getInputStream());
  38.                 double a, b, c, discriminant, root1, root2;
  39.                 try {
  40.                     a = Double.parseDouble(sc.nextLine());
  41.                     b = Double.parseDouble(sc.nextLine());
  42.                     c = Double.parseDouble(sc.nextLine());
  43.                     discriminant = b * b - 4 * a * c;
  44.                     root1 = ((-1 * b) + Math.sqrt(discriminant)) / (2 * a);
  45.                     root2 = ((-1 * b) - Math.sqrt(discriminant)) / (2 * a);
  46.                     pw.println(status(discriminant));
  47.                     switch (status(discriminant)) {
  48.                     case 0:
  49.                         break;
  50.                     case 1:
  51.                         pw.println(root1);
  52.                         break;
  53.                     case 2: {
  54.                         pw.println(root1);
  55.                         pw.println(root2);
  56.                     }
  57.                         break;
  58.                     default:
  59.                         throw new RuntimeException("Operation has failed.");
  60.                     }
  61.                     pw.flush();
  62.                 } catch (Exception e) {
  63.                 }
  64.             } catch (Exception e) {
  65.             } finally {
  66.                 try {
  67.                     sc.close();
  68.                 } catch (Exception e) {
  69.                 }
  70.                 try {
  71.                     pw.close();
  72.                 } catch (Exception e) {
  73.                 }
  74.                 try {
  75.                     connection.close();
  76.                 } catch (Exception e) {
  77.                 }
  78.             }
  79.         }
  80.  
  81.         private int status(double discriminant) {
  82.             if (discriminant < 0)
  83.                 return 0;
  84.             else if (discriminant == 0)
  85.                 return 1;
  86.             else if (discriminant > 0)
  87.                 return 2;
  88.             return -1;
  89.         }
  90.     }
  91.  
  92.     @Override
  93.     public void run() {
  94.         ServerSocket server;
  95.  
  96.         try {
  97.             server = new ServerSocket(this.portNum);
  98.             while (true) {
  99.                 message("Waiting for connection.");
  100.                 Socket connection = server.accept();
  101.                 logging(connection);
  102.                 new Thread(new ThreadConnect(connection)).start();
  103.                 message("Done.\n");
  104.             }
  105.         } catch (Exception e) {
  106.             e.printStackTrace();
  107.         }
  108.  
  109.     }
  110.  
  111.     private void logging(Socket connection) {
  112.         message("");
  113.         message("Communicating with " + connection.getInetAddress() + " " + connection.getPort());
  114.         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  115.         Date date = new Date();
  116.         message("Time: " + dateFormat.format(date));
  117.     }
  118.  
  119.     private static void message(String string) {
  120.         System.out.println(string);
  121.     }
  122.  
  123.     public static void main(String[] args) {
  124.         QuadraticServer q = new QuadraticServer(5012);
  125.         new Thread(q).start();
  126.     }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement