Question: Code existing from 31.1 is listed below. --------------------------------------------------------------------------------------------------------------------------------------------------- import java.io.*; import java.net.*; import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextArea; import

Code existing from 31.1 is listed below.
---------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Client extends Application {
// Datastreams
DataOutputStream toServer = null;
DataInputStream fromServer = null;
//txtfield loan
private TextField tfAnnualInterestRate = new TextField();
private TextField tfNumberOfYears = new TextField();
private TextField tfLoanAmount = new TextField();
// Button for sending info
private Button btSubmit = new Button("Submit");
@Override
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
// Alignment txtfields
tfAnnualInterestRate.setAlignment(Pos.BASELINE_RIGHT);
tfNumberOfYears.setAlignment(Pos.BASELINE_RIGHT);
tfLoanAmount.setAlignment(Pos.BASELINE_RIGHT);
// Information pane,grid
GridPane paneForLoanInfo = new GridPane();
paneForLoanInfo.add(new Label("Annual Interest Rate"), 0, 0);
paneForLoanInfo.add(tfAnnualInterestRate, 1, 0);
paneForLoanInfo.add(new Label("Number Of Years"), 0, 1);
paneForLoanInfo.add(tfNumberOfYears, 1, 1);
paneForLoanInfo.add(btSubmit, 2, 1);
paneForLoanInfo.add(new Label("Loan Amount"), 0, 2);
paneForLoanInfo.add(tfLoanAmount, 1, 2);
// Textarea
TextArea ta = new TextArea();
pane.setTop(paneForLoanInfo);
pane.setCenter(new ScrollPane(ta));
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 500, 200);
primaryStage.setTitle("Loan Calculator, Client"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
btSubmit.setOnAction(e -> {
try {
// Annual interest from field
double annualInterestRate = Double.parseDouble(
tfAnnualInterestRate.getText().trim());
int numberOfYears = Integer.parseInt(
tfNumberOfYears.getText().trim());
double loanAmount = Double.parseDouble(
tfLoanAmount.getText().trim());
// Transmit to server
toServer.writeDouble(annualInterestRate);
toServer.writeInt(numberOfYears);
toServer.writeDouble(loanAmount);
toServer.flush();
// Get payment from server
Double monthlyPayment = fromServer.readDouble();
Double totalPayment = fromServer.readDouble();
// Display to teat area
ta.appendText("Annual Interest Rate: " + annualInterestRate + ' ');
ta.appendText("Number Of Years: " + numberOfYears + ' ');
ta.appendText("Loan Amount: " + loanAmount + ' ');
ta.appendText("monthlyPayment: " + monthlyPayment + ' ');
ta.appendText("totalPayment: " + totalPayment + ' ');
}
catch (IOException ex) {
System.err.println(ex);
}
});
try {
// Socket connection
Socket clientSocket = new Socket("localhost", 8000);
// Input stream
fromServer = new DataInputStream(clientSocket.getInputStream());
// Output stream
toServer = new DataOutputStream(clientSocket.getOutputStream());
}
catch (IOException ex) {
ta.appendText(ex.toString() + ' ');
}
}
}
-----------------------------------------------------------------------------------------------------------
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.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class Server extends Application {
@Override
public void start(Stage primaryStage) {
// Txt area
TextArea tas = new TextArea();
// Create a scene and place it in the stage
Scene scene = new Scene(new ScrollPane(tas), 500, 200);
primaryStage.setTitle("Loan Calculator, Server"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
new Thread(() -> {
try {
// Serversocket
ServerSocket serverSocket = new ServerSocket(8000);
Platform.runLater(() ->
tas.appendText("Server started at " + new Date() + ' '));
// Connection request
Socket socket = serverSocket.accept();
// Input stream
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
// Outputstream
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
while
(true) {
Date dateClientConnected = new Date();
// Get loan info form client
double annualInterestRate = inputFromClient.readDouble();
int numberOfYears = inputFromClient.readInt();
double loanAmount = inputFromClient.readDouble();
// Compute payments
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
// Transmit to client
outputToClient.writeDouble(monthlyPayment);
outputToClient.writeDouble(totalPayment);
Platform.runLater(() -> {
tas.appendText("Connected to a client at " + dateClientConnected + ' ');
tas.appendText("Annual Interest Rate: " + annualInterestRate + ' ');
tas.appendText("Number Of Years: " + numberOfYears + ' ');
tas.appendText("Loan Amount: " + loanAmount + ' ');
tas.appendText("monthlyPayment: " + monthlyPayment + ' ');
tas.appendText("totalPayment: " + totalPayment + ' ');
});
}}
catch(IOException ex) {
ex.printStackTrace();
}
}).start();
}
}
Sections 31.3 and 31.4 *31.3 (Loan server for multiple clients) Revise Programming Exercise 31.1 to write a server for multiple clients. Sections 31.3 and 31.4 *31.3 (Loan server for multiple clients) Revise Programming Exercise 31.1 to write a server for multiple clients
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
