Advertisement
Guest User

Quadratic Equation Client

a guest
Apr 29th, 2017
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. // Quadratic Equation Client
  2. // A.C. Silvestri
  3. // 4/29/17
  4. // CSC-112 Intermediate Java Programming
  5. // silvestri@stcc.edu
  6. // HW 10
  7.  
  8. package client;
  9.  
  10. import java.io.*;
  11. import java.util.*;
  12. import java.net.*;
  13. import javafx.application.Application;
  14. import javafx.geometry.HPos;
  15. import javafx.geometry.Insets;
  16. import javafx.geometry.Pos;
  17. import javafx.scene.Scene;
  18. import javafx.scene.control.Button;
  19. import javafx.scene.control.Label;
  20. import javafx.scene.control.TextField;
  21. import javafx.scene.input.KeyCode;
  22. import javafx.scene.layout.FlowPane;
  23. import javafx.scene.layout.HBox;
  24. import javafx.scene.layout.VBox;
  25. import javafx.scene.text.Font;
  26. import javafx.scene.text.Text;
  27. import javafx.stage.Stage;
  28.  
  29. public class QuadraticEquationClient extends Application {
  30.     private final static String SERVER = "cs.stcc.edu";
  31.     private final static int PORT = 5009;
  32.    
  33.     private TextField aCoeff = new TextField();
  34.     private TextField bCoeff = new TextField();
  35.     private TextField cCoeff = new TextField();
  36.     private TextField firstRoot = new TextField();
  37.     private TextField secondRoot = new TextField();
  38.     private TextField status = new TextField();
  39.     private Button calcBtn = new Button();
  40.  
  41.     @Override
  42.     public void start(Stage primaryStage) throws IOException {
  43.         Scene scene = new Scene(buildStagePane(), 580, 150);
  44.         primaryStage.setResizable(false);
  45.         primaryStage.setTitle("Quadratic Equation");
  46.         primaryStage.setScene(scene);
  47.         primaryStage.show();
  48.     }
  49.  
  50.     private VBox buildStagePane() {
  51.         VBox vbox = new VBox();
  52.         vbox.setSpacing(10);
  53.         vbox.getChildren().addAll(buildInputBox(), buildOutputBox(), buildStatus());
  54.         vbox.setAlignment(Pos.CENTER);
  55.         return vbox;
  56.     }
  57.    
  58.     private HBox buildInputBox() {
  59.         HBox inputBox = new HBox();
  60.         inputBox.setPadding(new Insets(10, 10, 0, 10));
  61.         inputBox.setSpacing(10);
  62.         aCoeff.setPromptText("Enter Coefficient A");
  63.         bCoeff.setPromptText("Enter Coefficient B");
  64.         cCoeff.setPromptText("Enter Coefficient C");
  65.         inputBox.getChildren().addAll(new Label("a =  "), aCoeff, new Label("b = "), bCoeff, new Label("c = "), cCoeff);
  66.         return inputBox;
  67.     }
  68.  
  69.     private HBox buildOutputBox() {
  70.         HBox outputBox = new HBox();
  71.         outputBox.setAlignment(Pos.TOP_CENTER);
  72.         outputBox.setPadding(new Insets(10, 10, 0, 10));
  73.         outputBox.setSpacing(10);
  74.         firstRoot.setEditable(false);
  75.         firstRoot.setDisable(true);
  76.         secondRoot.setEditable(false);
  77.         secondRoot.setDisable(true);
  78.         outputBox.getChildren().addAll(new Label("x1 = "), firstRoot, new Label("x2 = "), secondRoot, buildButton());
  79.         return outputBox;
  80.     }
  81.  
  82.     private Button buildButton() {
  83.         calcBtn.setText("Calculate");
  84.         calcBtn.setOnAction(e -> calculate());
  85.         return calcBtn;
  86.     }
  87.    
  88.     private HBox buildStatus() {
  89.         HBox statusBox = new HBox();
  90.         statusBox.setAlignment(Pos.TOP_CENTER);
  91.         statusBox.setPadding(new Insets(10, 10, 0, 10));
  92.         statusBox.setSpacing(10);
  93.         status.setText("");
  94.         status.setPrefWidth(400);
  95.         status.setFont(new Font(10));
  96.         statusBox.getChildren().addAll(new Label("Status:"), status);
  97.         return statusBox;
  98.     }
  99.    
  100.     private void calculate() {
  101.         Socket client;
  102.         PrintWriter output;
  103.         Scanner input;
  104.         try {
  105.             status.setText("Creating Client \nSocket ");
  106.             client = new Socket(SERVER, PORT);
  107.             status.setText("Connected");
  108.             input = new Scanner(client.getInputStream());
  109.             output = new PrintWriter(client.getOutputStream());
  110.             int inputValue;
  111.             output.println(aCoeff.getText());
  112.             output.flush();
  113.             output.println(bCoeff.getText());
  114.             output.flush();
  115.             output.println(cCoeff.getText());
  116.             output.flush();
  117.             inputValue = Integer.parseInt(input.nextLine());
  118.             if (inputValue == 2) {
  119.                 firstRoot.setText(input.nextLine());
  120.                 secondRoot.setText(input.nextLine());
  121.             } else if (inputValue == 1) {
  122.                 firstRoot.setText(input.nextLine());
  123.             } else if (inputValue == 0) {
  124.                 firstRoot.setText("No real roots");
  125.                 secondRoot.clear();
  126.             } else
  127.                 firstRoot.setText(input.nextLine());;
  128.         } catch (Exception ex) {
  129.             status.setText("Error: " + ex.getMessage());
  130.         }
  131.     }
  132.  
  133.     public static void main(String args[]) throws IOException {
  134.         launch(args);
  135.     }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement