Omar_Natour

Natour, O. 4/28/16 Csc-112 Quad Client

Apr 28th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. /*
  2.  * Omar Natour
  3.  * 4/28/16
  4.  * Csc-112 Java 2
  5.  * Hw# 16
  6.  * Create a client that connects to the server on cs.stcc.edu that calculates quadratic roots
  7.  */
  8.  
  9. import java.io.*;
  10. import java.net.*;
  11. import javafx.application.Application;
  12. import javafx.event.ActionEvent;
  13. import javafx.event.EventHandler;
  14. import javafx.geometry.Pos;
  15. import javafx.scene.Node;
  16. import javafx.scene.Scene;
  17. import javafx.scene.control.Button;
  18. import javafx.scene.control.TextField;
  19. import javafx.stage.Stage;
  20. import javafx.scene.layout.*;
  21. import javafx.scene.text.Text;
  22.  
  23. public class QuadClient extends Application{
  24.     public static void main(String[] args) {
  25.         launch(args);
  26.     }
  27.    
  28.     public void start(Stage primaryStage) {
  29.         BorderPane bPane = new BorderPane();
  30.         bPane.setTop(getTop());
  31.         bPane.setCenter(getCenter());
  32.         bPane.setBottom(getBottom());
  33.        
  34.         Scene sce = new Scene(bPane,300,120);
  35.        
  36.         primaryStage.setTitle("Quadratics");
  37.         primaryStage.setScene(sce);
  38.         primaryStage.show();
  39.         primaryStage.setResizable(false);
  40.        
  41.     }
  42.     //global text fields
  43.     TextField tfA = new TextField();
  44.     TextField tfB = new TextField();
  45.     TextField tfC = new TextField();
  46.    
  47.     TextField tfX = new TextField();
  48.     TextField tfY = new TextField();
  49.    
  50.  
  51.     //Setting the top part of the window(The a b and c inputs)
  52.     private Node getTop() {
  53.         HBox hbTop = new HBox(15);
  54.        
  55.         Text tA = new Text("A");
  56.         Text tB = new Text("B");
  57.         Text tC = new Text("C");
  58.        
  59.         tfA.setPrefColumnCount(3);
  60.         tfB.setPrefColumnCount(3);
  61.         tfC.setPrefColumnCount(3);
  62.        
  63.         hbTop.getChildren().addAll(tA, tfA, tB, tfB, tC, tfC);
  64.         hbTop.setAlignment(Pos.CENTER);
  65.        
  66.         return hbTop;
  67.     }
  68.    
  69.     //Setting the button in the middle of the window
  70.     private Node getCenter(){
  71.         StackPane center = new StackPane();
  72.        
  73.         Button bCalc = new Button("Calculate");
  74.         bCalc.setPrefHeight(15);
  75.         bCalc.setPrefWidth(85);
  76.        
  77.         EventHandler<ActionEvent> clicked = e ->{
  78.             status = 0;
  79.             try{
  80.             setA();
  81.             setB();
  82.             setC();
  83.             }catch(Exception ex){
  84.                 status = -1;
  85.             }
  86.             runClient();
  87.         };
  88.        
  89.         bCalc.setOnAction(clicked);
  90.         center.getChildren().add(bCalc);
  91.        
  92.         return center;
  93.     }
  94.    
  95.     //Setting the output text fields
  96.     private Node getBottom(){
  97.         HBox results = new HBox(20);
  98.        
  99.         tfX.setEditable(false);
  100.         tfY.setEditable(false);
  101.    
  102.         results.getChildren().addAll(tfX, tfY);
  103.        
  104.         results.setPrefHeight(29);
  105.         results.setAlignment(Pos.CENTER);
  106.        
  107.         return results;
  108.     }
  109.    
  110.     //Global variables
  111.     int status;
  112.     double a;
  113.     double b;
  114.     double c;
  115.    
  116.     //getters and setters for above variables
  117.     private void setA() {
  118.         this.a = Double.parseDouble(tfA.getText());
  119.     }
  120.     private void setB(){
  121.         this.b = Double.parseDouble(tfB.getText());
  122.     }
  123.     private void setC(){
  124.         this.c = Double.parseDouble(tfC.getText());
  125.     }
  126.     private double getA(){
  127.         return a;
  128.     }
  129.     private double getB(){
  130.         return b;
  131.     }
  132.     private double getC(){
  133.         return c;
  134.     }
  135.        
  136.     //Connecting to and giving the server the numbers to calculate
  137.      void runClient()  {
  138.         Socket client;
  139.         DataOutputStream output;
  140.         DataInputStream input;
  141.  
  142.         try {
  143.             client = new Socket("cs.stcc.edu", 5009);
  144.  
  145.             input = new DataInputStream(client.getInputStream());
  146.             output = new DataOutputStream(client.getOutputStream());
  147.  
  148.             output.writeDouble(getA());
  149.             output.flush();
  150.             output.writeDouble(getB());
  151.             output.flush();
  152.             output.writeDouble(getC());
  153.             output.flush();
  154.            
  155.             if(status != -1){
  156.                 status = input.readInt();
  157.                 if (status == 0){
  158.                     tfX.setText("No real roots");
  159.                     tfY.setText("No real roots");
  160.                 }
  161.                 else if (status == 1) {
  162.                     double x1 = input.readDouble();
  163.                     tfY.setText(String.valueOf(x1));
  164.                     tfX.setText("One real root");
  165.                 }
  166.                 else if (status == 2) {
  167.                     double x1 = input.readDouble();
  168.                     tfX.setText(String.valueOf(x1));
  169.                     double x2 = input.readDouble();
  170.                     tfY.setText(String.valueOf(x2));
  171.                 }
  172.                 else if(status == -1){
  173.                     tfX.setText("A cannot be zero");
  174.                     tfY.setText("Can't divide by zero");
  175.                 }
  176.             }
  177.             else{
  178.                 tfX.setText("Bad Input");
  179.                 tfY.setText("Bad Input");  
  180.             }
  181.        
  182.             try{
  183.                 input.close();
  184.             }catch(Exception e){}
  185.             try{
  186.                 output.close();
  187.             }catch(Exception e){}
  188.             try{
  189.                 client.close();
  190.             }catch(Exception e){}
  191.            
  192.         }catch (Exception e) {
  193.             e.printStackTrace();
  194.         }
  195.     }
  196. }
Add Comment
Please, Sign In to add comment