Q:

(BMI server) Write a server for a client. The client sends the weight and height for a person to the server (see Figure 31.18a)

0

 (BMI server) Write a server for a client. The client sends the weight and height for a person to the server (see Figure  31.18a). The server computes BMI (Body Mass Index) and sends back to the client a string that reports the BMI (see Figure 31.18b). See Section 3.8 for computing BMI. Name the client Exercise31_02Client and the server Exercise31_02Server.

FIGURE 31.18 The client in (a) sends the weight and height of a person to the server and receives the BMI from the server in (b).

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.io.*;
import java.net.*;
import java.util.Date;
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.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Exercise31_02Client extends Application {
	// IO streams
	DataOutputStream toServer = null;
	DataInputStream fromServer = null;

	// Text fields for BMI information
	private TextField tfWeight = new TextField();
	private TextField tfHeight = new TextField();

	@Override // Override the start method in the Application class
	public void start(Stage primaryStage) {
		// Main pane
		BorderPane pane = new BorderPane();

		// Set text field alignment right
		tfWeight.setAlignment(Pos.BASELINE_RIGHT);
		tfHeight.setAlignment(Pos.BASELINE_RIGHT);

		// Create button to send BMI info to server
		Button btSubmit = new Button("Submit");

		// Pane to hold BMI information and submit button
		GridPane paneForBmiInfo = new GridPane();
		paneForBmiInfo.add(new Label("Weight in pounds"), 0, 0);
		paneForBmiInfo.add(tfWeight, 1, 0);
		paneForBmiInfo.add(new Label("Height in inches"), 0, 1);
		paneForBmiInfo.add(tfHeight, 1, 1);
		paneForBmiInfo.add(btSubmit, 2, 1);

		// Text Area to display contents
		TextArea ta = new TextArea();
		pane.setTop(paneForBmiInfo);
		pane.setCenter(new ScrollPane(ta));

		// Create a scene and place it in the stage
		Scene scene = new Scene(pane, 400, 200);
		primaryStage.setTitle("Exercise31_01Client"); // Set the stage title
		primaryStage.setScene(scene); // Place the scene in the stage
		primaryStage.show(); // Display the stage

		btSubmit.setOnAction(e -> {
			try {
				// Get the weight and height from the text fields
				double weight = Double.parseDouble(tfWeight.getText().trim());
				double height = Double.parseDouble(tfHeight.getText().trim());

				// Send the BMI information to the server
				toServer.writeDouble(weight);
				toServer.writeDouble(height);
				toServer.flush();

				// Get string from the server
				String bmi = fromServer.readUTF();

				// Display to text area
				ta.appendText("Weight: " + weight + '\n');
				ta.appendText("Height: " + height + '\n');
				ta.appendText(bmi + '\n');
			}
			catch (IOException ex) {
				System.err.println(ex);
			}
		});

		try {
			// Create a socket to connect to the server
			Socket socket = new Socket("localhost", 8000);

			// Create an input stream to receive data from the server
			fromServer = new DataInputStream(socket.getInputStream());

			// Create an output stream to send data to the server
			toServer = new DataOutputStream(socket.getOutputStream());
		}
		catch (IOException ex) {
			ta.appendText(ex.toString() + '\n');
		}
	}
}

Exercise31_02Server.java

import java.io.*;
import java.net.*;
import java.util.Date;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.ScrollPane;
import javafx.stage.Stage;

public class Exercise31_02Server extends Application {
	final double KILOGRAMS_PER_POUND = 0.45359237; // Kilograms per pound
	final double METERS_PER_INCH = 0.0254; // Meters per Inch

	@Override // Override the start method in the Application class
	public void start(Stage primaryStage) {
		// Text area for displaying contents
		TextArea ta = new TextArea();

		// Create a scene and place it in the stage
		Scene scene = new Scene(new ScrollPane(ta), 450, 200);
		primaryStage.setTitle("Exercise31_02Server"); // Set the stage title
		primaryStage.setScene(scene); // Place the scene in the stage
		primaryStage.show(); // Display the stage

		new Thread(() -> {
			try {
				// Create a server socket
				ServerSocket serverSocket = new ServerSocket(8000);
				Platform.runLater(() ->
					ta.appendText("Exercise31_02Server started at " 
						+ new Date() + '\n'));

				// Listen for a connection request
				Socket socket = serverSocket.accept();

				// Create data input and output streams
				DataInputStream inputFromClient = new DataInputStream(
					socket.getInputStream());
				DataOutputStream outputToClient = new DataOutputStream(
					socket.getOutputStream());

				while (true) {
					Date date = new Date();

					// Receive the weight and height from the server
					double weight = inputFromClient.readDouble();
					double height = inputFromClient.readDouble();

					// Compute the BMI (Body Mass Index)
					double weightInKilograms = weight * KILOGRAMS_PER_POUND;
					double heightInMeters = height * METERS_PER_INCH;
					double bmi = weightInKilograms / Math.pow(heightInMeters, 2);

					// Create string with BMI information
					StringBuilder strBMI = new StringBuilder("BMI is " + 
						String.format("%.2f", bmi) + ". ");
					
					if (bmi < 18.5)
						strBMI.append("Underweight");
					else if (bmi < 25)
						strBMI.append("Normal");
					else if (bmi < 30)
						strBMI.append("Overweight");
					else
						strBMI.append("Obese");

					// Send string back to client
					outputToClient.writeUTF(strBMI.toString());

					Platform.runLater(() -> {
						ta.appendText("Connected to a client at " + date + '\n');
						ta.appendText("Weight: " + weight + '\n');
						ta.appendText("Height: " + height + '\n');
						ta.appendText(strBMI.toString() + '\n');
					});
				}
			}
			catch (IOException ex) {
				ex.printStackTrace();
			}
		}).start();
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now