Movatterモバイル変換


[0]ホーム

URL:


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

Commitce30a1a

Browse files
author
jsquared21
committed
Add Ex31_02
1 parent26aa084 commitce30a1a

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed
Binary file not shown.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
importjava.io.*;
2+
importjava.net.*;
3+
importjava.util.Date;
4+
importjavafx.application.Application;
5+
importjavafx.geometry.Pos;
6+
importjavafx.scene.Scene;
7+
importjavafx.scene.control.Button;
8+
importjavafx.scene.control.Label;
9+
importjavafx.scene.control.TextArea;
10+
importjavafx.scene.control.TextField;
11+
importjavafx.scene.control.Label;
12+
importjavafx.scene.control.ScrollPane;
13+
importjavafx.scene.layout.GridPane;
14+
importjavafx.scene.layout.BorderPane;
15+
importjavafx.stage.Stage;
16+
17+
publicclassExercise31_02ClientextendsApplication {
18+
// IO streams
19+
DataOutputStreamtoServer =null;
20+
DataInputStreamfromServer =null;
21+
22+
// Text fields for BMI information
23+
privateTextFieldtfWeight =newTextField();
24+
privateTextFieldtfHeight =newTextField();
25+
26+
@Override// Override the start method in the Application class
27+
publicvoidstart(StageprimaryStage) {
28+
// Main pane
29+
BorderPanepane =newBorderPane();
30+
31+
// Set text field alignment right
32+
tfWeight.setAlignment(Pos.BASELINE_RIGHT);
33+
tfHeight.setAlignment(Pos.BASELINE_RIGHT);
34+
35+
// Create button to send BMI info to server
36+
ButtonbtSubmit =newButton("Submit");
37+
38+
// Pane to hold BMI information and submit button
39+
GridPanepaneForBmiInfo =newGridPane();
40+
paneForBmiInfo.add(newLabel("Weight in pounds"),0,0);
41+
paneForBmiInfo.add(tfWeight,1,0);
42+
paneForBmiInfo.add(newLabel("Height in inches"),0,1);
43+
paneForBmiInfo.add(tfHeight,1,1);
44+
paneForBmiInfo.add(btSubmit,2,1);
45+
46+
// Text Area to display contents
47+
TextAreata =newTextArea();
48+
pane.setTop(paneForBmiInfo);
49+
pane.setCenter(newScrollPane(ta));
50+
51+
// Create a scene and place it in the stage
52+
Scenescene =newScene(pane,400,200);
53+
primaryStage.setTitle("Exercise31_01Client");// Set the stage title
54+
primaryStage.setScene(scene);// Place the scene in the stage
55+
primaryStage.show();// Display the stage
56+
57+
btSubmit.setOnAction(e -> {
58+
try {
59+
// Get the weight and height from the text fields
60+
doubleweight =Double.parseDouble(tfWeight.getText().trim());
61+
doubleheight =Double.parseDouble(tfHeight.getText().trim());
62+
63+
// Send the BMI information to the server
64+
toServer.writeDouble(weight);
65+
toServer.writeDouble(height);
66+
toServer.flush();
67+
68+
// Get string from the server
69+
Stringbmi =fromServer.readUTF();
70+
71+
// Display to text area
72+
ta.appendText("Weight: " +weight +'\n');
73+
ta.appendText("Height: " +height +'\n');
74+
ta.appendText(bmi +'\n');
75+
}
76+
catch (IOExceptionex) {
77+
System.err.println(ex);
78+
}
79+
});
80+
81+
try {
82+
// Create a socket to connect to the server
83+
Socketsocket =newSocket("localhost",8000);
84+
85+
// Create an input stream to receive data from the server
86+
fromServer =newDataInputStream(socket.getInputStream());
87+
88+
// Create an output stream to send data to the server
89+
toServer =newDataOutputStream(socket.getOutputStream());
90+
}
91+
catch (IOExceptionex) {
92+
ta.appendText(ex.toString() +'\n');
93+
}
94+
}
95+
}
Binary file not shown.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
importjava.io.*;
2+
importjava.net.*;
3+
importjava.util.Date;
4+
importjavafx.application.Application;
5+
importjavafx.application.Platform;
6+
importjavafx.scene.Scene;
7+
importjavafx.scene.control.TextArea;
8+
importjavafx.scene.control.ScrollPane;
9+
importjavafx.stage.Stage;
10+
11+
publicclassExercise31_02ServerextendsApplication {
12+
finaldoubleKILOGRAMS_PER_POUND =0.45359237;// Kilograms per pound
13+
finaldoubleMETERS_PER_INCH =0.0254;// Meters per Inch
14+
15+
@Override// Override the start method in the Application class
16+
publicvoidstart(StageprimaryStage) {
17+
// Text area for displaying contents
18+
TextAreata =newTextArea();
19+
20+
// Create a scene and place it in the stage
21+
Scenescene =newScene(newScrollPane(ta),450,200);
22+
primaryStage.setTitle("Exercise31_02Server");// Set the stage title
23+
primaryStage.setScene(scene);// Place the scene in the stage
24+
primaryStage.show();// Display the stage
25+
26+
newThread(() -> {
27+
try {
28+
// Create a server socket
29+
ServerSocketserverSocket =newServerSocket(8000);
30+
Platform.runLater(() ->
31+
ta.appendText("Exercise31_02Server started at "
32+
+newDate() +'\n'));
33+
34+
// Listen for a connection request
35+
Socketsocket =serverSocket.accept();
36+
37+
// Create data input and output streams
38+
DataInputStreaminputFromClient =newDataInputStream(
39+
socket.getInputStream());
40+
DataOutputStreamoutputToClient =newDataOutputStream(
41+
socket.getOutputStream());
42+
43+
while (true) {
44+
Datedate =newDate();
45+
46+
// Receive the weight and height from the server
47+
doubleweight =inputFromClient.readDouble();
48+
doubleheight =inputFromClient.readDouble();
49+
50+
// Compute the BMI (Body Mass Index)
51+
doubleweightInKilograms =weight *KILOGRAMS_PER_POUND;
52+
doubleheightInMeters =height *METERS_PER_INCH;
53+
doublebmi =weightInKilograms /Math.pow(heightInMeters,2);
54+
55+
// Create string with BMI information
56+
StringBuilderstrBMI =newStringBuilder("BMI is " +
57+
String.format("%.2f",bmi) +". ");
58+
59+
if (bmi <18.5)
60+
strBMI.append("Underweight");
61+
elseif (bmi <25)
62+
strBMI.append("Normal");
63+
elseif (bmi <30)
64+
strBMI.append("Overweight");
65+
else
66+
strBMI.append("Obese");
67+
68+
// Send string back to client
69+
outputToClient.writeUTF(strBMI.toString());
70+
71+
Platform.runLater(() -> {
72+
ta.appendText("Connected to a client at " +date +'\n');
73+
ta.appendText("Weight: " +weight +'\n');
74+
ta.appendText("Height: " +height +'\n');
75+
ta.appendText(strBMI.toString() +'\n');
76+
});
77+
}
78+
}
79+
catch (IOExceptionex) {
80+
ex.printStackTrace();
81+
}
82+
}).start();
83+
}
84+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp