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 ff55044

Browse files
bug fix ex 31 02
1 parent b96297e commit ff55044

File tree

7 files changed

+242
-61
lines changed

7 files changed

+242
-61
lines changed

‎README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
<img style="width:25%; height:25%;" src="./resources/coverpicture.png" alt="header-img-book-cover">
44

5-
### This repo contains my solutions to the end-of-chapter exercise’s from <a href="https://www.amazon.com/Intro-Java-Programming-Comprehensive-Version/dp/0133761312">Y. Daniel Liang’s Intro to Java Programming (10th Edition)</a>
5+
### This repo contains <ahref="#exercise-solutions-shortcut">my solutions to the end-of-chapter exercise’s</a> from <a href="https://www.amazon.com/Intro-Java-Programming-Comprehensive-Version/dp/0133761312">Y. Daniel Liang’s Intro to Java Programming (10th Edition)</a>
66

7-
### I've included links below to all the freely accessible compainion material and quick links to navigate through my solutions by chapter.
7+
### I've included links below to all the freely accessible companion material and quick links to navigate through my solutions by chapter.
88

99
### If you would like to contribute, please see: <a href="#contribute">Ways to Contribute</a>
1010

@@ -63,7 +63,7 @@ ____
6363

6464
____
6565

66-
<h1>Exercise Solutions:</h1>
66+
<h1id="exercise-solutions-shortcut">Exercise Solutions:</h1>
6767
<h5>Quick Links to navigate these solutions by chapter</h5>
6868
<h6>
6969
<img src="./resources/images/icons8-complete-26.png" alt="complete-check-img"/>

‎ch_18/exercise18_31/Exercise18_31.class

3.48 KB
Binary file not shown.

‎ch_31/Exercise31_02.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

‎ch_31/exercise31_02/BmiDto.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ch_31.exercise31_02;
2+
3+
/**
4+
* Data Transfer Object for BMI data
5+
* <p>
6+
* This class is used to transfer data from the client to the server.
7+
* For Exercise31_02, the client sends the weight and height for a person to the server.
8+
*/
9+
public class BmiDto {
10+
private double weight;
11+
private double height;
12+
13+
public BmiDto() {
14+
}
15+
16+
public BmiDto(double weight, double height) {
17+
this.weight = weight;
18+
this.height = height;
19+
}
20+
21+
public double getWeight() {
22+
return weight;
23+
}
24+
25+
public double getHeight() {
26+
return height;
27+
}
28+
}

‎ch_31/exercise31_02/Exercise31_02.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ch_31.exercise31_02;
2+
3+
import javafx.application.Application;
4+
import javafx.application.Platform;
5+
import javafx.stage.Stage;
6+
7+
/**
8+
* *31.2 (BMI server) Write a server for a client. The client sends the weight and
9+
* height for a person to the server (see Figure 31.18a). The server computes
10+
* BMI (Body Mass Index) and sends back to the client a string that reports the
11+
* BMI (see Figure 31.18b). See Section 3.8 for computing BMI. Name the client
12+
* Exercise31_02Client and the server Exercise31_02Server.
13+
*/
14+
public class Exercise31_02 extends Application {
15+
@Override
16+
public void start(Stage primaryStage) throws Exception {
17+
System.out.println("##-_-_--_--_--__-_-- Starting: Exercise31_02 --_-__--__--__--_--_--##");
18+
Platform.runLater(() -> new Exercise31_02Server().start(new Stage()));
19+
Platform.runLater(() -> new Exercise31_02Client().start(new Stage()));
20+
}
21+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package ch_31.exercise31_02;
2+
3+
import javafx.application.Application;
4+
import javafx.event.ActionEvent;
5+
import javafx.event.EventHandler;
6+
import javafx.geometry.HPos;
7+
import javafx.geometry.Pos;
8+
import javafx.geometry.VPos;
9+
import javafx.scene.Scene;
10+
import javafx.scene.control.Button;
11+
import javafx.scene.control.Label;
12+
import javafx.scene.control.TextArea;
13+
import javafx.scene.control.TextField;
14+
import javafx.scene.layout.GridPane;
15+
import javafx.stage.Stage;
16+
17+
import java.io.*;
18+
import java.net.Socket;
19+
20+
public class Exercise31_02Client extends Application {
21+
private TextField weightTextInput = new TextField();
22+
private TextField heightTextInput = new TextField();
23+
private TextArea displayResultTextArea = new TextArea();
24+
25+
private Button submitButton = new Button("Submit");
26+
27+
// Host name or ip
28+
String host = "localhost";
29+
30+
private ObjectOutputStream toServer;
31+
private DataInputStream fromServer;
32+
33+
public Exercise31_02Client() {
34+
System.out.println("##-_-_--_--_--__-_-- Starting: Exercise31_02Client --_-__--__--__--_--_--##");
35+
}
36+
37+
@Override
38+
public void start(Stage primaryStage) {
39+
Stage stage = new Stage();
40+
GridPane pane = new GridPane();
41+
pane.add(new Label("Weight in pounds"), 0, 0);
42+
pane.add(weightTextInput, 1, 0);
43+
pane.add(new Label("Height in inches"), 0, 1);
44+
pane.add(heightTextInput, 1, 1);
45+
pane.add(submitButton, 1, 3);
46+
pane.add(displayResultTextArea, 1, 4);
47+
GridPane.setHalignment(submitButton, HPos.RIGHT);
48+
GridPane.setValignment(displayResultTextArea, VPos.BOTTOM);
49+
pane.setAlignment(Pos.CENTER);
50+
weightTextInput.setPrefColumnCount(15);
51+
heightTextInput.setPrefColumnCount(15);
52+
submitButton.setOnAction(new ButtonListener());
53+
54+
Scene scene = new Scene(pane, 450, 200);
55+
stage.setTitle("BMI Client");
56+
stage.setScene(scene);
57+
stage.show();
58+
59+
try (Socket socket = new Socket(host, 8000)) {
60+
// Create an output stream to the server
61+
toServer = new ObjectOutputStream(socket.getOutputStream());
62+
// Create an input stream from the server
63+
fromServer = new DataInputStream(socket.getInputStream());
64+
} catch (IOException ex) {
65+
System.out.println("##----------- Client Error: IOException: " + ex.getMessage() + " -----------##");
66+
}
67+
}
68+
69+
/**
70+
* Custom event handler for the submit button
71+
*/
72+
private class ButtonListener implements EventHandler<ActionEvent> {
73+
@Override
74+
public void handle(ActionEvent e) {
75+
try {
76+
// Get weight and height from the text fields
77+
double weight = Double.parseDouble(weightTextInput.getText().trim());
78+
double height = Double.parseDouble(heightTextInput.getText().trim());
79+
// Create a BmiDto and send to the server
80+
BmiDto s = new BmiDto(weight, height);
81+
toServer.writeObject(s);
82+
toServer.flush();
83+
// Get resulting BMI from the server
84+
double bmi = fromServer.readDouble();
85+
// Display to the text area
86+
displayResultTextArea.setText("Weight: " + weight + "\nHeight: " + height + "\nBMI is: " + bmi
87+
+ "\n" + getCategory(bmi));
88+
} catch (IOException ex) {
89+
System.out.println("##----------- Client Error: IOException: " + ex.getMessage() + " -----------##");
90+
}
91+
}
92+
}
93+
94+
private String getCategory(double bmi) {
95+
if (bmi < 18.5) {
96+
return "Underweight";
97+
} else if (bmi < 25) {
98+
return "Normal";
99+
} else if (bmi < 30) {
100+
return "Overweight";
101+
} else {
102+
return "Obese";
103+
}
104+
}
105+
106+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package ch_31.exercise31_02;
2+
3+
import javafx.application.Application;
4+
import javafx.application.Platform;
5+
import javafx.scene.Scene;
6+
import javafx.scene.control.ScrollPane;
7+
import javafx.scene.control.TextArea;
8+
import javafx.stage.Stage;
9+
10+
import java.io.*;
11+
import java.net.ServerSocket;
12+
import java.net.Socket;
13+
import java.util.Date;
14+
import java.util.Objects;
15+
import java.util.concurrent.atomic.AtomicBoolean;
16+
17+
public class Exercise31_02Server extends Application {
18+
private static final double KILOGRAMS_PER_POUND = 0.45359237;
19+
private static final double METERS_PER_INCH = 0.0254;
20+
21+
private AtomicBoolean isRunning = new AtomicBoolean(true);
22+
23+
public Exercise31_02Server() {
24+
System.out.println("##-_-_--_--_--__-_-- Starting: Exercise31_02Server --_-__--__--__--_--_--##");
25+
}
26+
27+
28+
@Override
29+
public void start(Stage primaryStage) {
30+
TextArea displayLogTextArea = new TextArea();
31+
Scene scene = new Scene(new ScrollPane(displayLogTextArea), 450, 200);
32+
primaryStage.setTitle("TicTacToeServer");
33+
primaryStage.setScene(scene);
34+
primaryStage.show();
35+
new Thread(() -> runServer(displayLogTextArea)).start();
36+
}
37+
38+
private void runServer(TextArea displayLogTextArea) {
39+
/* Create a server socket. Use try with resources to close the socket automatically */
40+
try (ServerSocket serverSocket = new ServerSocket(8000)) {
41+
// Listen for a connection request
42+
Socket socket = socket = serverSocket.accept();
43+
// Create data input and output streams
44+
try (ObjectInputStream inputFromClient = new ObjectInputStream(
45+
socket.getInputStream())) {
46+
try (DataOutputStream outputToClient = new DataOutputStream(
47+
socket.getOutputStream())) {
48+
49+
Platform.runLater(() -> displayLogTextArea.appendText(new Date() +
50+
": Server started at socket 8000\n"));
51+
while (true) {
52+
if (!isRunning.get()) {
53+
break;
54+
}
55+
if (inputFromClient.available() > 0) {
56+
// Receive Object from the client
57+
Object object = inputFromClient.readObject();
58+
if (Objects.nonNull(object) && object instanceof BmiDto) {
59+
BmiDto bmiDto = (BmiDto) object;
60+
double weight = bmiDto.getWeight();
61+
double height = bmiDto.getHeight();
62+
double bmi = calculateBmi(weight, height);
63+
// Send area back to the client
64+
outputToClient.writeDouble(bmi);
65+
}
66+
}
67+
68+
}
69+
}
70+
}
71+
72+
} catch (ClassNotFoundException | IOException ex) {
73+
System.out.println("##----------- Server Error: Exception: " + ex.getMessage() + " -----------##");
74+
}
75+
}
76+
77+
private double calculateBmi(double weight, double height) {
78+
double weightInKilograms = weight * KILOGRAMS_PER_POUND;
79+
double heightInMeters = height * METERS_PER_INCH;
80+
return weightInKilograms /
81+
(heightInMeters * heightInMeters);
82+
}
83+
84+
}

0 commit comments

Comments
(0)

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