Omar_Natour

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

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