Advertisement
luliu

Quadratic Equation Calculator Client

Apr 29th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 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 Client
  11.  */
  12.  
  13. import java.io.*;
  14. import java.net.*;
  15. import javafx.application.Application;
  16. import javafx.geometry.Insets;
  17. import javafx.scene.Scene;
  18. import javafx.scene.control.Button;
  19. import javafx.scene.control.Label;
  20. import javafx.scene.control.ScrollPane;
  21. import javafx.scene.control.TextArea;
  22. import javafx.scene.control.TextField;
  23. import javafx.scene.layout.BorderPane;
  24. import javafx.scene.layout.VBox;
  25. import javafx.stage.Stage;
  26.  
  27. public class CalculatorClient extends Application {
  28.     // IO streams
  29.     DataOutputStream toServer = null;
  30.     DataInputStream fromServer = null;
  31.  
  32.     @Override // Override the start method in the Application class
  33.     public void start(Stage primaryStage) {
  34.         // Create pane to hold the label and text field
  35.         BorderPane paneForTextField = new BorderPane();
  36.         paneForTextField.setPadding(new Insets(5, 5, 5, 5));
  37.         paneForTextField.setStyle("-fx-border-color: green");
  38.         TextField tf1 = new TextField();
  39.         TextField tf2 = new TextField();
  40.         TextField tf3 = new TextField();
  41.         Button start = new Button("Start");
  42.         VBox vBox = new VBox(10);
  43.         vBox.setPadding(new Insets(5, 5, 5, 5));
  44.         vBox.getChildren().addAll(new Label("Enter coefficient a: "), tf1, new Label("Enter coefficient b: "), tf2,
  45.                 new Label("Enter coefficient c: "), tf3, start);
  46.         paneForTextField.setCenter(vBox);
  47.  
  48.         // Create pane to hold the paneforTextField and TextArea
  49.         BorderPane mainPane = new BorderPane();
  50.         // Text area to display contents
  51.         TextArea ta = new TextArea();
  52.         mainPane.setCenter(new ScrollPane(ta));
  53.         mainPane.setBottom(paneForTextField);
  54.  
  55.         // Create a scene and place it in the stage
  56.         Scene scene = new Scene(mainPane, 450, 500);
  57.         primaryStage.setTitle("Client"); // Set the stage title
  58.         primaryStage.setScene(scene); // Place the scene in the stage
  59.         primaryStage.show(); // Display the stage
  60.  
  61.         start.setOnAction(e -> {
  62.             try {
  63.                 // Get the coefficients from the text field
  64.                 double coA = Double.parseDouble(tf1.getText().trim());
  65.                 double coB = Double.parseDouble(tf2.getText().trim());
  66.                 double coC = Double.parseDouble(tf3.getText().trim());
  67.                 // Send the coefficients to the server
  68.                 toServer.writeDouble(coA);
  69.                 toServer.writeDouble(coB);
  70.                 toServer.writeDouble(coC);
  71.                 toServer.flush();
  72.  
  73.                 // Get results from the server
  74.                 // Display to the text area
  75.                 ta.appendText("Coefficient A is " + coA + "\n");
  76.                 ta.appendText("Coefficient B is " + coB + "\n");
  77.                 ta.appendText("Coefficient C is " + coC + "\n");
  78.                 int status = fromServer.readInt();
  79.                 double root1, root2;
  80.  
  81.                 if (status == 2) {
  82.                     root1 = fromServer.readDouble();
  83.                     root2 = fromServer.readDouble();
  84.                     ta.appendText("Root1 received from the server is " + root1 + '\n');
  85.                     ta.appendText("Root2 received from the server is " + root2 + '\n');
  86.                 } else if (status == 1) {
  87.                     root1 = fromServer.readDouble();
  88.                     ta.appendText("Root1 received from the server is " + root1 + '\n');
  89.                 } else if (status == 0) {
  90.                     ta.appendText("There is no real root return");
  91.                 }
  92.  
  93.             } catch (IOException ex) {
  94.                 System.err.println(ex);
  95.             }
  96.         });
  97.  
  98.         try {
  99.             // Create a socket to connect to the server
  100.             Socket socket = new Socket("cs.stcc.edu", 5007);
  101.             // Socket socket = new Socket("134.241.96.49", 5007);
  102.  
  103.             // Create an input stream to receive data from the server
  104.             fromServer = new DataInputStream(socket.getInputStream());
  105.  
  106.             // Create an output stream to send data to the server
  107.             toServer = new DataOutputStream(socket.getOutputStream());
  108.         } catch (IOException ex) {
  109.             ta.appendText(ex.toString() + '\n');
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * The main method is only needed for the IDE with limited JavaFX support.
  115.      * Not needed for running from the command line.
  116.      */
  117.     public static void main(String[] args) {
  118.         launch(args);
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement