Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dbd2164

Browse files
Merge pull request #72 from HarryDulaney/new-solutions
fix ex 31_02
2 parents f95e549 + b44b4ad commit dbd2164

File tree

3 files changed

+67
-68
lines changed

3 files changed

+67
-68
lines changed

‎ch_31/exercise31_02/BmiDto.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* This class is used to transfer data from the client to the server.
77
* For Exercise31_02, the client sends the weight and height for a person to the server.
88
*/
9-
public class BmiDto {
9+
public class BmiDto implements java.io.Serializable {
10+
private static final long serialVersionUID = 1L;
1011
private double weight;
1112
private double height;
1213

‎ch_31/exercise31_02/Exercise31_02Client.java

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,43 @@
1111
import java.io.DataInputStream;
1212
import java.io.DataOutputStream;
1313
import java.io.IOException;
14+
import java.io.ObjectOutputStream;
1415
import java.net.Socket;
16+
1517
public class Exercise31_02Client extends Application {
16-
DataOutputStream toServer = null;
18+
ObjectOutputStream toServer = null;
1719
DataInputStream fromServer = null;
20+
private TextField weightTextInput = new TextField();
21+
private TextField heightTextInput = new TextField();
1822

19-
// Text fields for BMI information
20-
private TextField tfWeight = new TextField();
21-
private TextField tfHeight = new TextField();
22-
23-
@Override // Override the start method in the Application class
23+
@Override
2424
public void start(Stage primaryStage) {
25-
// Main pane
2625
BorderPane pane = new BorderPane();
27-
28-
// Set text field alignment right
29-
tfWeight.setAlignment(Pos.BASELINE_RIGHT);
30-
tfHeight.setAlignment(Pos.BASELINE_RIGHT);
31-
32-
// Create button to send BMI info to server
26+
weightTextInput.setAlignment(Pos.BASELINE_RIGHT);
27+
heightTextInput.setAlignment(Pos.BASELINE_RIGHT);
3328
Button btSubmit = new Button("Submit");
34-
35-
// Pane to hold BMI information and submit button
3629
GridPane paneForBmiInfo = new GridPane();
3730
paneForBmiInfo.add(new Label("Weight in pounds"), 0, 0);
38-
paneForBmiInfo.add(tfWeight, 1, 0);
31+
paneForBmiInfo.add(weightTextInput, 1, 0);
3932
paneForBmiInfo.add(new Label("Height in inches"), 0, 1);
40-
paneForBmiInfo.add(tfHeight, 1, 1);
33+
paneForBmiInfo.add(heightTextInput, 1, 1);
4134
paneForBmiInfo.add(btSubmit, 2, 1);
42-
43-
// Text Area to display contents
4435
TextArea ta = new TextArea();
4536
pane.setTop(paneForBmiInfo);
4637
pane.setCenter(new ScrollPane(ta));
4738

48-
// Create a scene and place it in the stage
49-
Scene scene = new Scene(pane, 400, 200);
50-
primaryStage.setTitle("Exercise31_01Client"); // Set the stage title
51-
primaryStage.setScene(scene); // Place the scene in the stage
52-
primaryStage.show(); // Display the stage
39+
Scene scene = new Scene(pane, 560, 300);
40+
primaryStage.setTitle("Exercise31_02Client");
41+
primaryStage.setScene(scene);
42+
primaryStage.show();
5343

5444
btSubmit.setOnAction(e -> {
5545
try {
56-
// Get the weight and height from the text fields
57-
double weight = Double.parseDouble(tfWeight.getText().trim());
58-
double height = Double.parseDouble(tfHeight.getText().trim());
59-
46+
double weight = Double.parseDouble(weightTextInput.getText().trim());
47+
double height = Double.parseDouble(heightTextInput.getText().trim());
48+
BmiDto dto = new BmiDto(weight, height);
6049
// Send the BMI information to the server
61-
toServer.writeDouble(weight);
62-
toServer.writeDouble(height);
50+
toServer.writeObject(dto);
6351
toServer.flush();
6452

6553
// Get string from the server
@@ -69,8 +57,7 @@ public void start(Stage primaryStage) {
6957
ta.appendText("Weight: " + weight + '\n');
7058
ta.appendText("Height: " + height + '\n');
7159
ta.appendText(bmi + '\n');
72-
}
73-
catch (IOException ex) {
60+
} catch (IOException ex) {
7461
System.err.println(ex);
7562
}
7663
});
@@ -83,9 +70,8 @@ public void start(Stage primaryStage) {
8370
fromServer = new DataInputStream(socket.getInputStream());
8471

8572
// Create an output stream to send data to the server
86-
toServer = new DataOutputStream(socket.getOutputStream());
87-
}
88-
catch (IOException ex) {
73+
toServer = new ObjectOutputStream(socket.getOutputStream());
74+
} catch (IOException ex) {
8975
ta.appendText(ex.toString() + '\n');
9076
}
9177
}

‎ch_31/exercise31_02/Exercise31_02Server.java

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class Exercise31_02Server extends Application {
2121
public void start(Stage primaryStage) {
2222
TextArea displayLogTextArea = new TextArea();
2323
Scene scene = new Scene(new ScrollPane(displayLogTextArea), 450, 200);
24-
primaryStage.setTitle("TicTacToeServer");
24+
primaryStage.setTitle("Exercise31_02Server");
2525
primaryStage.setScene(scene);
2626
primaryStage.show();
2727
new Thread(() -> {
@@ -32,50 +32,62 @@ public void start(Stage primaryStage) {
3232
+ new Date() + '\n'));
3333

3434
Socket socket = serverSocket.accept();
35-
DataInputStream inputFromClient = new DataInputStream(
35+
ObjectInputStream inputFromClient = new ObjectInputStream(
3636
socket.getInputStream());
3737
DataOutputStream outputToClient = new DataOutputStream(
3838
socket.getOutputStream());
3939

4040
while (true) {
41-
Date date = new Date();
42-
double weight = inputFromClient.readDouble();
43-
double height = inputFromClient.readDouble();
44-
double weightInKilograms = weight * KILOGRAMS_PER_POUND;
45-
double heightInMeters = height * METERS_PER_INCH;
46-
double bmi = calculateBmi(weightInKilograms, heightInMeters);
47-
StringBuilder bmiString = new StringBuilder("BMI is " +
48-
String.format("%.2f", bmi) + ". ");
41+
Object object = inputFromClient.readObject();
42+
String result = handleObjectInput(object);
43+
outputToClient.writeUTF(result);
4944

50-
if (bmi < 18.5) {
51-
bmiString.append("Underweight");
52-
} else if (bmi < 25) {
53-
bmiString.append("Normal");
54-
} else if (bmi < 30) {
55-
bmiString.append("Overweight");
56-
} else {
57-
bmiString.append("Obese");
58-
}
59-
outputToClient.writeUTF(bmiString.toString());
60-
61-
Platform.runLater(() -> {
62-
displayLogTextArea.appendText("Connected to a client at " + date + '\n');
63-
displayLogTextArea.appendText("Weight: " + weight + '\n');
64-
displayLogTextArea.appendText("Height: " + height + '\n');
65-
displayLogTextArea.appendText(bmiString.toString() + '\n');
66-
});
45+
log(result, new Date(), displayLogTextArea);
6746
}
68-
} catch (IOException ex) {
69-
ex.printStackTrace();
47+
} catch (IOException | ClassNotFoundExceptionex) {
48+
System.out.println("Sever Error: " + ex.getMessage());
7049
}
7150
}).start();
7251
}
7352

74-
private double calculateBmi(double weight, double height) {
75-
double weightInKilograms = weight * KILOGRAMS_PER_POUND;
76-
double heightInMeters = height * METERS_PER_INCH;
53+
private String handleObjectInput(Object object) throws IOException, ClassNotFoundException {
54+
BmiDto dto = (BmiDto) object;
55+
double bmi = calculateBmi(dto);
56+
return "BMI is " +
57+
String.format("%.2f", bmi) +
58+
". " +
59+
getBmiString(bmi) +
60+
". ";
61+
}
62+
63+
private double calculateBmi(BmiDto dto) {
64+
double weightInKilograms = dto.getWeight() * KILOGRAMS_PER_POUND;
65+
double heightInMeters = dto.getHeight() * METERS_PER_INCH;
7766
return weightInKilograms /
7867
(heightInMeters * heightInMeters);
7968
}
8069

70+
private String getBmiString(double bmi) {
71+
if (bmi < 18.5) {
72+
return "Underweight";
73+
}
74+
75+
if (bmi < 25) {
76+
return "Normal";
77+
}
78+
79+
if (bmi < 30) {
80+
return "Overweight";
81+
}
82+
return "Obese";
83+
}
84+
85+
private void log(String result,
86+
Date date,
87+
TextArea displayLogTextArea) {
88+
Platform.runLater(() -> {
89+
displayLogTextArea.appendText("Connected to a client at " + date + '\n');
90+
displayLogTextArea.appendText(result + '\n');
91+
});
92+
}
8193
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /