Skip to content

Commit

Permalink
Merge pull request #72 from HarryDulaney/new-solutions
Browse files Browse the repository at this point in the history
fix ex 31_02
  • Loading branch information
HarryDulaney authored Nov 20, 2023
2 parents f95e549 + b44b4ad commit dbd2164
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 68 deletions.
3 changes: 2 additions & 1 deletion ch_31/exercise31_02/BmiDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* This class is used to transfer data from the client to the server.
* For Exercise31_02, the client sends the weight and height for a person to the server.
*/
public class BmiDto {
public class BmiDto implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private double weight;
private double height;

Expand Down
56 changes: 21 additions & 35 deletions ch_31/exercise31_02/Exercise31_02Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,43 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Exercise31_02Client extends Application {
DataOutputStream toServer = null;
ObjectOutputStream toServer = null;
DataInputStream fromServer = null;
private TextField weightTextInput = new TextField();
private TextField heightTextInput = new TextField();

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

@Override // Override the start method in the Application class
@Override
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
weightTextInput.setAlignment(Pos.BASELINE_RIGHT);
heightTextInput.setAlignment(Pos.BASELINE_RIGHT);
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(weightTextInput, 1, 0);
paneForBmiInfo.add(new Label("Height in inches"), 0, 1);
paneForBmiInfo.add(tfHeight, 1, 1);
paneForBmiInfo.add(heightTextInput, 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
Scene scene = new Scene(pane, 560, 300);
primaryStage.setTitle("Exercise31_02Client");
primaryStage.setScene(scene);
primaryStage.show();

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());

double weight = Double.parseDouble(weightTextInput.getText().trim());
double height = Double.parseDouble(heightTextInput.getText().trim());
BmiDto dto = new BmiDto(weight, height);
// Send the BMI information to the server
toServer.writeDouble(weight);
toServer.writeDouble(height);
toServer.writeObject(dto);
toServer.flush();

// Get string from the server
Expand All @@ -69,8 +57,7 @@ public void start(Stage primaryStage) {
ta.appendText("Weight: " + weight + '\n');
ta.appendText("Height: " + height + '\n');
ta.appendText(bmi + '\n');
}
catch (IOException ex) {
} catch (IOException ex) {
System.err.println(ex);
}
});
Expand All @@ -83,9 +70,8 @@ public void start(Stage primaryStage) {
fromServer = new DataInputStream(socket.getInputStream());

// Create an output stream to send data to the server
toServer = new DataOutputStream(socket.getOutputStream());
}
catch (IOException ex) {
toServer = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException ex) {
ta.appendText(ex.toString() + '\n');
}
}
Expand Down
76 changes: 44 additions & 32 deletions ch_31/exercise31_02/Exercise31_02Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Exercise31_02Server extends Application {
public void start(Stage primaryStage) {
TextArea displayLogTextArea = new TextArea();
Scene scene = new Scene(new ScrollPane(displayLogTextArea), 450, 200);
primaryStage.setTitle("TicTacToeServer");
primaryStage.setTitle("Exercise31_02Server");
primaryStage.setScene(scene);
primaryStage.show();
new Thread(() -> {
Expand All @@ -32,50 +32,62 @@ public void start(Stage primaryStage) {
+ new Date() + '\n'));

Socket socket = serverSocket.accept();
DataInputStream inputFromClient = new DataInputStream(
ObjectInputStream inputFromClient = new ObjectInputStream(
socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(
socket.getOutputStream());

while (true) {
Date date = new Date();
double weight = inputFromClient.readDouble();
double height = inputFromClient.readDouble();
double weightInKilograms = weight * KILOGRAMS_PER_POUND;
double heightInMeters = height * METERS_PER_INCH;
double bmi = calculateBmi(weightInKilograms, heightInMeters);
StringBuilder bmiString = new StringBuilder("BMI is " +
String.format("%.2f", bmi) + ". ");
Object object = inputFromClient.readObject();
String result = handleObjectInput(object);
outputToClient.writeUTF(result);

if (bmi < 18.5) {
bmiString.append("Underweight");
} else if (bmi < 25) {
bmiString.append("Normal");
} else if (bmi < 30) {
bmiString.append("Overweight");
} else {
bmiString.append("Obese");
}
outputToClient.writeUTF(bmiString.toString());

Platform.runLater(() -> {
displayLogTextArea.appendText("Connected to a client at " + date + '\n');
displayLogTextArea.appendText("Weight: " + weight + '\n');
displayLogTextArea.appendText("Height: " + height + '\n');
displayLogTextArea.appendText(bmiString.toString() + '\n');
});
log(result, new Date(), displayLogTextArea);
}
} catch (IOException ex) {
ex.printStackTrace();
} catch (IOException | ClassNotFoundException ex) {
System.out.println("Sever Error: " + ex.getMessage());
}
}).start();
}

private double calculateBmi(double weight, double height) {
double weightInKilograms = weight * KILOGRAMS_PER_POUND;
double heightInMeters = height * METERS_PER_INCH;
private String handleObjectInput(Object object) throws IOException, ClassNotFoundException {
BmiDto dto = (BmiDto) object;
double bmi = calculateBmi(dto);
return "BMI is " +
String.format("%.2f", bmi) +
". " +
getBmiString(bmi) +
". ";
}

private double calculateBmi(BmiDto dto) {
double weightInKilograms = dto.getWeight() * KILOGRAMS_PER_POUND;
double heightInMeters = dto.getHeight() * METERS_PER_INCH;
return weightInKilograms /
(heightInMeters * heightInMeters);
}

private String getBmiString(double bmi) {
if (bmi < 18.5) {
return "Underweight";
}

if (bmi < 25) {
return "Normal";
}

if (bmi < 30) {
return "Overweight";
}
return "Obese";
}

private void log(String result,
Date date,
TextArea displayLogTextArea) {
Platform.runLater(() -> {
displayLogTextArea.appendText("Connected to a client at " + date + '\n');
displayLogTextArea.appendText(result + '\n');
});
}
}

0 comments on commit dbd2164

Please sign in to comment.