Advertisement
rjsantiago0001

Quadratic Client

Apr 30th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.59 KB | None | 0 0
  1. // Ricardo Santiago
  2. // 4/29/16
  3. // CSCI-112
  4. // Quadratic Equation Solver Client
  5.  
  6. import java.io.*;
  7. import java.net.*;
  8. import java.util.*;
  9. import javafx.application.Application;
  10. import javafx.geometry.Insets;
  11. import javafx.geometry.Pos;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Button;
  14. import javafx.scene.control.Label;
  15. import javafx.scene.control.TextArea;
  16. import javafx.scene.control.TextField;
  17. import javafx.scene.layout.BorderPane;
  18. import javafx.scene.layout.HBox;
  19. import javafx.scene.layout.VBox;
  20. import javafx.scene.text.Font;
  21. import javafx.scene.text.Text;
  22. import javafx.stage.Stage;
  23.  
  24. public class Client extends Application {
  25.  
  26.     static String strA, strB, strC;
  27.     private static String ansStr;
  28.     private static double root1;
  29.     private static double root2;
  30.     private static int statusNum;
  31.     private static String statusString;
  32.  
  33.     public void start(Stage primaryStage) {
  34.  
  35.         BorderPane root = new BorderPane();
  36.         Button btEnter = new Button("Enter");
  37.         Button btClear = new Button("Clear");
  38.         HBox buttonPanel = new HBox(5);
  39.  
  40.         buttonPanel.setAlignment(Pos.CENTER);
  41.         buttonPanel.getChildren().addAll(btEnter, btClear);
  42.         buttonPanel.setPadding(new Insets(5));
  43.  
  44.         TextField tfA = new TextField("C");
  45.         tfA.setMaxSize(50, 10);
  46.         TextField tfB = new TextField("B");
  47.         tfB.setMaxSize(50, 10);
  48.         TextField tfC = new TextField("A");
  49.         tfC.setMaxSize(50, 10);
  50.  
  51.         TextArea tA = new TextArea();
  52.         tA.setMaxSize(250, 80);
  53.         tA.setEditable(false);
  54.  
  55.         Text text = new Text("Enter values for a, b, and c");
  56.         text.setFont(new Font(20));
  57.  
  58.         VBox textFieldPanel = new VBox(5);
  59.         textFieldPanel.setPadding(new Insets(5));
  60.         textFieldPanel.setAlignment(Pos.CENTER_LEFT);
  61.         textFieldPanel.getChildren().addAll(tfC, tfB, tfA);
  62.  
  63.         HBox hB = new HBox();
  64.         hB.getChildren().addAll(text);
  65.         hB.setAlignment(Pos.CENTER);
  66.  
  67.         root.setBottom(buttonPanel);
  68.         root.setCenter(tA);
  69.         root.setLeft(textFieldPanel);
  70.         root.setTop(hB);
  71.  
  72.         Scene scene = new Scene(root, 400, 200);
  73.         primaryStage.setTitle("Quadratic Equation Solver");
  74.         primaryStage.setScene(scene);
  75.         primaryStage.setResizable(false);
  76.         primaryStage.show();
  77.  
  78.         btEnter.setOnAction(e -> {
  79.             setStrA(tfA.getText());
  80.             setStrB(tfB.getText());
  81.             setStrC(tfC.getText());
  82.             Client.runClient();
  83.  
  84.             if (getStatusNum() == 0) {
  85.                 tA.setText(getStatusString());
  86.             } else {
  87.                 tA.setText("root 1 = " + getRoot1() + "\n root 2 = " + getRoot2() + "\n status = " + getStatusNum());
  88.             }
  89.         });
  90.  
  91.         btClear.setOnAction(e -> {
  92.             tfA.setText(null);
  93.             tfB.setText(null);
  94.             tfC.setText(null);
  95.             tA.setText(null);
  96.         });
  97.     }
  98.  
  99.     static void runClient() {
  100.  
  101.         Socket client;
  102.         DataOutputStream output;
  103.         DataInputStream input;
  104.  
  105.         try {
  106.             pStr("Creating Client Socket ");
  107.             client = new Socket("cs.stcc.edu", 5013);
  108.             pStr("SUCCESS!!!");
  109.  
  110.             input = new DataInputStream(client.getInputStream());
  111.             output = new DataOutputStream(client.getOutputStream());
  112.  
  113.             double argA = Double.parseDouble(strA);
  114.             output.writeDouble(argA);
  115.             output.flush();
  116.  
  117.             double argB = Double.parseDouble(strB);
  118.             output.writeDouble(argB);
  119.             output.flush();
  120.  
  121.             double argC = Double.parseDouble(strC);
  122.             output.writeDouble(argC);
  123.             output.flush();
  124.  
  125.             setRoot1(input.readDouble());
  126.             setRoot2(input.readDouble());
  127.             setStatusNum(input.readInt());
  128.  
  129.             input.close();
  130.             output.close();
  131.             client.close();
  132.  
  133.         } catch (IOException e) {
  134.             e.printStackTrace();
  135.         }
  136.     }
  137.  
  138.     public static String getStatusString() {
  139.         return statusString;
  140.     }
  141.  
  142.     public static void setStatusString(String statusString) {
  143.         Client.statusString = statusString;
  144.     }
  145.  
  146.     public String getAnsStr() {
  147.         return ansStr;
  148.     }
  149.  
  150.     public static void setAnsStr(String str) {
  151.         ansStr = str;
  152.     }
  153.  
  154.     public static String getStrA() {
  155.         return strA;
  156.     }
  157.  
  158.     public static void setStrA(String strA) {
  159.         Client.strA = strA;
  160.     }
  161.  
  162.     public static String getStrB() {
  163.         return strB;
  164.     }
  165.  
  166.     public static void setStrB(String strB) {
  167.         Client.strB = strB;
  168.     }
  169.  
  170.     public static String getStrC() {
  171.         return strC;
  172.     }
  173.  
  174.     public static void setStrC(String strC) {
  175.         Client.strC = strC;
  176.     }
  177.  
  178.     public static double getRoot1() {
  179.         return root1;
  180.     }
  181.  
  182.     public static void setRoot1(double root1) {
  183.         Client.root1 = root1;
  184.     }
  185.  
  186.     public static double getRoot2() {
  187.         return root2;
  188.     }
  189.  
  190.     public static void setRoot2(double root2) {
  191.         Client.root2 = root2;
  192.     }
  193.  
  194.     public static int getStatusNum() {
  195.         return statusNum;
  196.     }
  197.  
  198.     public static void setStatusNum(int statusVal) {
  199.         Client.statusNum = statusVal;
  200.     }
  201.  
  202.     static void pStr(String p) {
  203.         System.out.println(p);
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement