Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Omar Natour
- * 4/28/16
- * Csc-112 Java 2
- * Hw# 16
- * Create a client that connects to the server on cs.stcc.edu that calculates quadratic roots
- * Ojnatour0001@student.stcc.edu
- */
- import java.io.*;
- import java.net.*;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Pos;
- import javafx.scene.Node;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextField;
- import javafx.stage.Stage;
- import javafx.scene.layout.*;
- import javafx.scene.text.Text;
- public class QuadClient extends Application{
- public static void main(String[] args) {
- launch(args);
- }
- public void start(Stage primaryStage) {
- BorderPane bPane = new BorderPane();
- bPane.setTop(getTop());
- bPane.setCenter(getCenter());
- bPane.setBottom(getBottom());
- Scene sce = new Scene(bPane,300,120);
- primaryStage.setTitle("Quadratics");
- primaryStage.setScene(sce);
- primaryStage.show();
- primaryStage.setResizable(false);
- }
- //global text fields
- TextField tfA = new TextField();
- TextField tfB = new TextField();
- TextField tfC = new TextField();
- TextField tfX = new TextField();
- TextField tfY = new TextField();
- //Setting the top part of the window(The a b and c inputs)
- private Node getTop() {
- HBox hbTop = new HBox(15);
- Text tA = new Text("A");
- Text tB = new Text("B");
- Text tC = new Text("C");
- tfA.setPrefColumnCount(3);
- tfB.setPrefColumnCount(3);
- tfC.setPrefColumnCount(3);
- hbTop.getChildren().addAll(tA, tfA, tB, tfB, tC, tfC);
- hbTop.setAlignment(Pos.CENTER);
- return hbTop;
- }
- //Setting the button in the middle of the window
- private Node getCenter(){
- StackPane center = new StackPane();
- Button bCalc = new Button("Calculate");
- bCalc.setPrefHeight(15);
- bCalc.setPrefWidth(85);
- EventHandler<ActionEvent> clicked = e ->{
- status = 0;
- try{
- setA();
- setB();
- setC();
- }catch(Exception ex){
- status = -1;
- }
- runClient();
- };
- bCalc.setOnAction(clicked);
- center.getChildren().add(bCalc);
- return center;
- }
- //Setting the output text fields
- private Node getBottom(){
- HBox results = new HBox(20);
- tfX.setEditable(false);
- tfY.setEditable(false);
- results.getChildren().addAll(tfX, tfY);
- results.setPrefHeight(29);
- results.setAlignment(Pos.CENTER);
- return results;
- }
- //Global variables
- int status;
- double a;
- double b;
- double c;
- //getters and setters for above variables
- private void setA() {
- this.a = Double.parseDouble(tfA.getText());
- }
- private void setB(){
- this.b = Double.parseDouble(tfB.getText());
- }
- private void setC(){
- this.c = Double.parseDouble(tfC.getText());
- }
- private double getA(){
- return a;
- }
- private double getB(){
- return b;
- }
- private double getC(){
- return c;
- }
- //Connecting to and giving the server the numbers to calculate
- void runClient() {
- Socket client;
- DataOutputStream output;
- DataInputStream input;
- try {
- client = new Socket("cs.stcc.edu", 5009);
- input = new DataInputStream(client.getInputStream());
- output = new DataOutputStream(client.getOutputStream());
- output.writeDouble(getA());
- output.flush();
- output.writeDouble(getB());
- output.flush();
- output.writeDouble(getC());
- output.flush();
- if(status != -1){
- status = input.readInt();
- if (status == 0){
- tfX.setText("No real roots");
- tfY.setText("No real roots");
- }
- else if (status == 1) {
- double x1 = input.readDouble();
- tfY.setText(String.valueOf(x1));
- tfX.setText("One real root");
- }
- else if (status == 2) {
- double x1 = input.readDouble();
- tfX.setText(String.valueOf(x1));
- double x2 = input.readDouble();
- tfY.setText(String.valueOf(x2));
- }
- else if(status == -1){
- tfX.setText("A cannot be zero");
- tfY.setText("Can't divide by zero");
- }
- }
- else{
- tfX.setText("Bad Input");
- tfY.setText("Bad Input");
- }
- try{
- input.close();
- }catch(Exception e){}
- try{
- output.close();
- }catch(Exception e){}
- try{
- client.close();
- }catch(Exception e){}
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment