Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

31.3 (Loan server for multiple clients) Revise Programming Exercise 31.1 to writ

ID: 3810267 • Letter: 3

Question

31.3 (Loan server for multiple clients) Revise Programming Exercise 31.1 to write a
server for multiple clients.

Java PLEASE TYPE ANSWER

*3I.I (Loan server) Write a server for a client. The client sends loan informa- tion (annual interest rate, number of years, and loan amount) to the server (see Figure 31.17a). The server computes monthly payment and total pay- ment, and sends them back to the client (see Figure 31.17b). Name the client Exercise31 01Client and the server Exercise31 01Server. DR Exercise 31 01Clent Annual Interest Rate 3,5 Exercises Number of Years 3 Submit Exercise31-01Server started at Wedu 24 23:39:33 EDT 2013 Loan Amount Connected to a client at Wed Jul 24 23:39:55 EDT 2013 Annual nterest Rate: 3,5 Number of Years: 3 Number of Years: 3 Loan Amount: 50 00.0 Loan Amount: 5000.0 monthly Payment: 146 SiD398634ss347 monthly Payment: 146.5103966345515 total Payment: 5271.374350843855 total Payment: 5274.374350843926 (a) (b) FIGURE 3 l.17 The client in (a) sends the annual interest rate, number of years, and loan amount to the server and receives the monthly payment and total payment from the server in (b).

Explanation / Answer

Client.java // If you need please rename just replace Client with Excercise31_01Client file name as well as class name

import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Client extends JFrame implements ActionListener{

//Text field for receiving annual interest rate,number of years, loan amount
private JTextField jtfAnnualInterestRate = new JTextField();
private JTextField jtfNumOfYears = new JTextField();
private JTextField jtfLoanAmount = new JTextField();
private JButton jbtSubmit = new JButton("Submit");

//Text area for displaying contents
private JTextArea jta = new JTextArea();

//IO streams
DataOutputStream osToServer;
DataInputStream isFromServer;

public static void main(String[] args){
new Client();
}

public Client(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3,1));
p1.add(new JLabel("Annual Interest Rate"));
p1.add(new JLabel("Number Of Years"));
p1.add(new JLabel("Loan Amount"));

Panel p2 = new Panel();
p2.setLayout(new GridLayout(3,1));
p2.add(jtfAnnualInterestRate);
p2.add(jtfNumOfYears);
p2.add(jtfLoanAmount);

JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(p1, BorderLayout.WEST);

p.add(p2,BorderLayout.CENTER);
p.add(jbtSubmit,BorderLayout.EAST);

jtfAnnualInterestRate.setHorizontalAlignment(JTextField.RIGHT);
jtfNumOfYears.setHorizontalAlignment(JTextField.RIGHT);
jtfLoanAmount.setHorizontalAlignment(JTextField.RIGHT);

getContentPane().setLayout(new BorderLayout());
getContentPane().add(p,BorderLayout.NORTH);
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);

jbtSubmit.addActionListener(this); //Register listener

setTitle("Client");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

try{

//Create a socket to connect to the server
Socket connectToServer = new Socket("localhost",8000);

//Create an input stream to receive data from the server
isFromServer = new DataInputStream(connectToServer.getInputStream());

//Create an output stream to send data to the server
osToServer = new DataOutputStream(connectToServer.getOutputStream());

}

catch(IOException ex){
jta.append(ex.toString()+' ');
}
}

public void actionPerformed(ActionEvent e){
String actionCommand = e.getActionCommand();
if(e.getSource() instanceof JButton){
try{

//Get the annual interest rate from the text field
double annualInterestRate =
Double.parseDouble(jtfAnnualInterestRate.getText().trim());

//Get the number of years from the text field
int numOfYears =
Integer.parseInt(jtfNumOfYears.getText());

//Get the loan amount from the text field
double loanAmount =
Double.parseDouble(jtfLoanAmount.getText().trim());

//Send the annual interest rate to the server
osToServer.writeDouble(annualInterestRate);

//Send the number of years to the server
osToServer.writeInt(numOfYears);

//Send the loan amount to the server
osToServer.writeDouble(loanAmount);

osToServer.flush();

//Get monthly payment from the server
double monthlyPayment = isFromServer.readDouble();

//Get total payment from the server
double totalPayment = isFromServer.readDouble();

//Display to the text area
jta.append("Annual Interest Rate: " + annualInterestRate +" ");
jta.append("Number Of Years: " + numOfYears +" ");
jta.append("Loan Amount: " + loanAmount +" ");
jta.append("The Monthly Payment is: "+ monthlyPayment +" ");
jta.append("The Total Payment is: "+ totalPayment +" ");

}
catch(IOException ex){
System.err.println(ex);
}
}
}
}

Server.java // If you need please rename just replace Server with Excercise31_01Server file name as well as class name

import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame {

//Text area for displaying contents
private JTextArea jta = new JTextArea();

public static void main(String[] args){

new Server();

}

public Server() {

//Place text area on the frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);

setTitle("Server");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); //It is necessary to show the frame here!

try{

//Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
jta.append("Server started at" +new Date()+ ' ');

//Listen for a connection request
Socket connectToClient = serverSocket.accept();

//Create data input and output streams
DataInputStream isFromClient = new DataInputStream(
connectToClient.getInputStream());
DataOutputStream osToClient = new DataOutputStream(
connectToClient.getOutputStream());

while (true) {

//Receive annual interest rate from the client
double annualInterestRate = isFromClient.readDouble();

//Receive number of years from the client
int numOfYears = isFromClient.readInt();

//Receive loan amount from the client
double loanAmount = isFromClient.readDouble();


//Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;


//Compute total payment
double totalPayment=(loanAmount*annualInterestRate/100*numOfYears)+loanAmount;

//Compute monthly payment
double monthlyPayment = totalPayment/(numOfYears*12);

//Send monthly payment back to the client
osToClient.writeDouble(monthlyPayment);

//Send total payment back to the client
osToClient.writeDouble(totalPayment);


jta.append("The Annual Interest Rate received from client is "+ annualInterestRate+' ');
jta.append("The Number Of Years received from client is "+ numOfYears+' ');
jta.append("The Loan Amount received from client is "+ loanAmount+' ');
jta.append("The Monthly Payment is "+ monthlyPayment+' ') ;
jta.append("The Total Payment is"+ totalPayment+' ');
}

}

catch(IOException ex){
System.err.println(ex);

}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote