Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For some reason the javafx window wont compile or the server. Here is the code (BMI_Client.java) package Lesson_3_Program_1; import java.io.*; import java.net.*; import javafx.application.Application; import

image text in transcribed

For some reason the javafx window wont compile or the server.

Here is the code

(BMI_Client.java)

package Lesson_3_Program_1; import java.io.*; import java.net.*; import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class BMI_Client extends Application { // Text field for receiving radius private TextField tfWeight = new TextField(); private TextField tfHeight = new TextField(); private Button btSubmit = new Button("Submit"); // Text area to display contents private TextArea ta = new TextArea(); // IO streams DataOutputStream osToServer; DataInputStream isFromServer; @Override // Override the start method in the Application class public void start(Stage primaryStage) { ta.setWrapText(true); GridPane gridPane = new GridPane(); gridPane.add(new Label("Weight in pounds"), 0, 0); gridPane.add(new Label("Height in inches"), 0, 1); gridPane.add(tfWeight, 1, 0); gridPane.add(tfHeight, 1, 1); gridPane.add(btSubmit, 2, 1); tfWeight.setAlignment(Pos.BASELINE_RIGHT); tfHeight.setAlignment(Pos.BASELINE_RIGHT); tfHeight.setPrefColumnCount(5); BorderPane pane = new BorderPane(); pane.setCenter(new ScrollPane(ta)); pane.setTop(gridPane); // Create a scene and place it in the stage Scene scene = new Scene(pane, 350, 200); primaryStage.setTitle("BMI Client"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage btSubmit.setOnAction(e -> connectToServer()); try { // Create a socket to connect to the server Socket connectToServer = new Socket("localhost", 8000); //Socket connectToServer = new Socket("130.254.204.36", 8000); //Socket connectToServer = new Socket( // "drake.Armstrong.edu", 8000); // Create an input stream to receive data from the server isFromServer = new DataInputStream( connectToServer.getInputStream()); // Create an output stream to send data to the server osToServer = new DataOutputStream(connectToServer.getOutputStream()); } catch (IOException ex) { ta.appendText(ex.toString() + ' '); } } public void connectToServer() { try { // Get the annual interest rate from the text field double Weight = Double.parseDouble(tfWeight.getText().trim()); // Get the number of years from the text field int Height = Integer.parseInt(tfHeight.getText()); // Send the annual interest rate to the server osToServer.writeDouble(Weight); // Send the number of years to the server osToServer.writeInt(Height); osToServer.flush(); // Get monthly payment from the server double totalBMI = isFromServer.readDouble(); ta.appendText("Weight: " + Weight + " Height: " + Height + " "); ta.appendText("monthlyPayment: " + String.format("$%,.2f", totalBMI) + " " + ' '); } catch (IOException ex) { System.err.println(ex); } } /** * The main method is only needed for the IDE with limited JavaFX support. * Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }

(BMI.java)

package Lesson_3_Program_1; public class BMI implements java.io.Serializable { private double Weight; private int Height; private java.util.Date loanDate; /** * Default constructor */ public BMI() { this(2.5, 1); } /** * Construct a loan with specified annual interest rate, number of years and * loan amount */ public BMI(double Weight, int Height) { this.Weight = Weight; this.Height = Height; loanDate = new java.util.Date(); } /** * Return annualInterestRate */ public double getWeight() { return Weight; } /** * Set a new annualInterestRate */ public void setWeight(double annualInterestRate) { this.Weight = Weight; } /** * Return numberOfYears */ public int getHeight() { return Height; } /** * Set a new numberOfYears */ public void setHeight(int numberOfYears) { this.Height = Height; } /** * Find monthly payment */ public double getTotal() { double totalBMI = 703 * Weight / (Math.pow( Height, 2)); return totalBMI; } /** * Return loan date */ public java.util.Date getLoanDate() { return loanDate; } }

(BMI_Server.java)

package Lesson_3_Program_1; import java.io.*; import java.net.*; import java.util.*; //import Lesson_3_Program_1.BMI; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextArea; import javafx.stage.Stage; public class BMI_Server extends Application { // Text area for displaying contents private TextArea ta = new TextArea(); @Override // Override the start method in the Application class public void start(Stage primaryStage) { ta.setWrapText(true); // Create a scene and place it in the stage Scene scene = new Scene(new ScrollPane(ta), 400, 200); primaryStage.setTitle("BMI Server"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage new Thread(() -> connectToClient()).start(); } public void connectToClient() { try { // Create a server socket ServerSocket serverSocket = new ServerSocket(8000); Platform.runLater(() -> ta.appendText("Server started at " + new Date() + ' ')); Socket connectToClient = serverSocket.accept(); // Display the client number Platform.runLater(() -> { ta.appendText("Connected to a client " + " at " + new Date() + ' '); }); // Create data input and output streams DataInputStream isFromClient = new DataInputStream( connectToClient.getInputStream()); DataOutputStream osToClient = new DataOutputStream( connectToClient.getOutputStream()); // Continuously serve the client while (true) { // Receive weight from the client double Weight = isFromClient.readDouble(); // Receive height inches the client int Height = isFromClient.readInt(); // Compute monthly payment and total payment BMI total = new BMI( Weight, Height); double totalBMI = total.getTotal(); // Send results back to the client osToClient.writeDouble(totalBMI); Platform.runLater(() -> { ta.appendText("Weight: " + Weight + " Height: " + Height + " "); ta.appendText("Total BMI: " + totalBMI + " " + ' '); }); } } catch (IOException e) { System.err.println(e); } } /** * The main method is only needed for the IDE with limited JavaFX support. * Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } } 
(BMI Server) Write a server for a client. The client sends the weight ( in pounds) and height (in inches) for a person to the server (see Figure 33.18a). The server computes BMI (Body Mass Index), and sends back to the client a string that reports the BMI (see Figure 33.18b). See Section 3.8 for computing BMI. Name the client Exercise33_01Client and the server Exercise33_01Server

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions