Advertisement
luliu

Quadratic Equation Calculator Web Server

Apr 29th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. /*
  2.  * Name: Lu Liu
  3.  * Date: 4/29/2016
  4.  * Course Number: CSC-112
  5.  * Course Name: Intermediate Topics in Java Programming
  6.  * Email: lliu0001@student.stcc.edu
  7.  *
  8.  * Assignment: HW # 16
  9.  * Programe Description:
  10.  * Quadratic Equation Calculator Web Server
  11.  */
  12.  
  13. import java.io.*;
  14. import java.net.*;
  15. import java.util.Date;
  16. import javafx.application.Application;
  17. import javafx.application.Platform;
  18. import javafx.scene.Scene;
  19. import javafx.scene.control.ScrollPane;
  20. import javafx.scene.control.TextArea;
  21. import javafx.stage.Stage;
  22.  
  23. public class CalculatorServer extends Application {
  24.     @Override // Override the start method in the Application class
  25.     public void start(Stage primaryStage) {
  26.         // Text area for displaying contents
  27.         TextArea ta = new TextArea();
  28.  
  29.         // Create a scene and place it in the stage
  30.         Scene scene = new Scene(new ScrollPane(ta), 450, 200);
  31.         primaryStage.setTitle("Server"); // Set the stage title
  32.         primaryStage.setScene(scene); // Place the scene in the stage
  33.         primaryStage.show(); // Display the stage
  34.  
  35.         new Thread(() -> {
  36.             try {
  37.                 // Create a server socket
  38.                 ServerSocket serverSocket = new ServerSocket(5007);
  39.                 Platform.runLater(() -> ta.appendText("Server started at " + new Date() + '\n'));
  40.  
  41.                 // Listen for a connection request
  42.                 Socket socket = serverSocket.accept();
  43.  
  44.                 // Create data input and output streams
  45.                 DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
  46.                 DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
  47.  
  48.                 while (true) {
  49.                     // Receive data from the client
  50.                     try {
  51.                         double a = inputFromClient.readDouble();
  52.                         double b = inputFromClient.readDouble();
  53.                         double c = inputFromClient.readDouble();
  54.                         double d = b * b - 4 * a * c;
  55.                         int status = 0;
  56.                         double root1, root2;
  57.                        
  58.                         // Compute the root
  59.                         if (d == 0) {
  60.                             status = 1;
  61.                         } else if (d > 0) {
  62.                             status = 2;
  63.                         } else if (d < 0) {
  64.                             status = 0;
  65.                         }
  66.                         // Send data back to the client
  67.                         outputToClient.writeInt(status);
  68.                         switch (status) {
  69.                         case 1:
  70.                             outputToClient.writeDouble(-b / (2 * a));
  71.                             break;
  72.                         case 2:
  73.                             outputToClient.writeDouble((-b + Math.sqrt(d)) / (2 * a));
  74.                             outputToClient.writeDouble((-b - Math.sqrt(d)) / (2 * a));
  75.                             break;
  76.                         }
  77.  
  78.                     } catch (Exception e) {
  79.                         outputToClient.writeInt(0);
  80.                     }
  81.                 }
  82.  
  83.             } catch (IOException ex) {
  84.                 ex.printStackTrace();
  85.             }
  86.         }).start();
  87.     }
  88.  
  89.     /**
  90.      * The main method is only needed for the IDE with limited JavaFX support.
  91.      * Not needed for running from the command line.
  92.      */
  93.     public static void main(String[] args) {
  94.         launch(args);
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement