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

Write a server for multiple clients. The client sends loan information (annual i

ID: 3766310 • Letter: W

Question

Write a server for multiple clients. The client sends loan information (annual interest rate, number of years, and loan amount) to the server (see Figure 30.21(b)). The server computes monthly payment and total payment and sends them back to the client (see Figure 30.21(a)). Name the client Exercise30_1 Client and the server Exercise30_1 Server. The client in (b) 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 (a).

Explanation / Answer

//Server.java: The server accepts data from the client, processes it
//and returns the result back to the client
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);

     }
}
}

__________________________________

//Client.java: The client sends the input to the server and receives
//result back from the server

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);
           }
     }
}
}

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