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

Please code using Java and show an example that it will run successfully in an J

ID: 3679979 • Letter: P

Question

Please code using Java and show an example that it will run successfully in an JAVA IDE. Also please add comments:

31. (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 tot pay- ment, and sends them back to the client (see Figure 31.17b). Name the client Exercise31 01Client and the server Exercise1 01 Server. I Exerase310|Client Annual Interest Rate Number Of Years Loan Amount 3.5 Exercise31 01Server 3 Submit Exercise31 O!Server started at Wed Jul 24 23:39:33 EDT 2013 Connected to a client at Wed Jul 24 23:39:55 EDT 2013 Annual Interest Rate: 3.5 Number of Years: 3 Loan Amount: 5000.0 monthlyPayment: 146.51039863455347 total Payment: 5274.374350843926 5000 Annual Interest Rate: 3.5 Number of Years: 3 Loan Amount: 5000.0 monthlyPayment: 146.5103986345515 total Pa yment: 5274.374350843855 FIGURE 31.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

Hi below i have written the code for your reference,

//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