Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Quadratic Equation Client
- // A.C. Silvestri
- // 4/29/17
- // CSC-112 Intermediate Java Programming
- // silvestri@stcc.edu
- // HW 10
- package client;
- import java.io.*;
- import java.util.*;
- import java.net.*;
- import javafx.application.Application;
- import javafx.geometry.HPos;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import javafx.scene.input.KeyCode;
- import javafx.scene.layout.FlowPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.VBox;
- import javafx.scene.text.Font;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- public class QuadraticEquationClient extends Application {
- private final static String SERVER = "cs.stcc.edu";
- private final static int PORT = 5009;
- private TextField aCoeff = new TextField();
- private TextField bCoeff = new TextField();
- private TextField cCoeff = new TextField();
- private TextField firstRoot = new TextField();
- private TextField secondRoot = new TextField();
- private TextField status = new TextField();
- private Button calcBtn = new Button();
- @Override
- public void start(Stage primaryStage) throws IOException {
- Scene scene = new Scene(buildStagePane(), 580, 150);
- primaryStage.setResizable(false);
- primaryStage.setTitle("Quadratic Equation");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- private VBox buildStagePane() {
- VBox vbox = new VBox();
- vbox.setSpacing(10);
- vbox.getChildren().addAll(buildInputBox(), buildOutputBox(), buildStatus());
- vbox.setAlignment(Pos.CENTER);
- return vbox;
- }
- private HBox buildInputBox() {
- HBox inputBox = new HBox();
- inputBox.setPadding(new Insets(10, 10, 0, 10));
- inputBox.setSpacing(10);
- aCoeff.setPromptText("Enter Coefficient A");
- bCoeff.setPromptText("Enter Coefficient B");
- cCoeff.setPromptText("Enter Coefficient C");
- inputBox.getChildren().addAll(new Label("a = "), aCoeff, new Label("b = "), bCoeff, new Label("c = "), cCoeff);
- return inputBox;
- }
- private HBox buildOutputBox() {
- HBox outputBox = new HBox();
- outputBox.setAlignment(Pos.TOP_CENTER);
- outputBox.setPadding(new Insets(10, 10, 0, 10));
- outputBox.setSpacing(10);
- firstRoot.setEditable(false);
- firstRoot.setDisable(true);
- secondRoot.setEditable(false);
- secondRoot.setDisable(true);
- outputBox.getChildren().addAll(new Label("x1 = "), firstRoot, new Label("x2 = "), secondRoot, buildButton());
- return outputBox;
- }
- private Button buildButton() {
- calcBtn.setText("Calculate");
- calcBtn.setOnAction(e -> calculate());
- return calcBtn;
- }
- private HBox buildStatus() {
- HBox statusBox = new HBox();
- statusBox.setAlignment(Pos.TOP_CENTER);
- statusBox.setPadding(new Insets(10, 10, 0, 10));
- statusBox.setSpacing(10);
- status.setText("");
- status.setPrefWidth(400);
- status.setFont(new Font(10));
- statusBox.getChildren().addAll(new Label("Status:"), status);
- return statusBox;
- }
- private void calculate() {
- Socket client;
- PrintWriter output;
- Scanner input;
- try {
- status.setText("Creating Client \nSocket ");
- client = new Socket(SERVER, PORT);
- status.setText("Connected");
- input = new Scanner(client.getInputStream());
- output = new PrintWriter(client.getOutputStream());
- int inputValue;
- output.println(aCoeff.getText());
- output.flush();
- output.println(bCoeff.getText());
- output.flush();
- output.println(cCoeff.getText());
- output.flush();
- inputValue = Integer.parseInt(input.nextLine());
- if (inputValue == 2) {
- firstRoot.setText(input.nextLine());
- secondRoot.setText(input.nextLine());
- } else if (inputValue == 1) {
- firstRoot.setText(input.nextLine());
- } else if (inputValue == 0) {
- firstRoot.setText("No real roots");
- secondRoot.clear();
- } else
- firstRoot.setText(input.nextLine());;
- } catch (Exception ex) {
- status.setText("Error: " + ex.getMessage());
- }
- }
- public static void main(String args[]) throws IOException {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement