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

D 0133593517.pdf Java Exercise 30.4. Just 30 X Bb Module Calendar CIST23 X C www

ID: 3664212 • Letter: D

Question

D 0133593517.pdf Java Exercise 30.4. Just 30 X Bb Module Calendar CIST23 X C www.ebooksbucket.com to Java%20Programming Comprehensive Version.pdf /uploads/itprogramming/java/Introduction 0133593517.pdf 1192 1345 PROGRAMMING EXERCISES My Programming Lab Section 31.2 *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. Exercise 1 01Client Annual Interest Rate 3.5 Exercise3 1 01Server Number of Years Submit Exercise 31 01Server started at Wed Jul 24 23:39:33 EDT 2013 Loan Amount 5000 Connected to a client at Wed Jul 24 23:39:55 EDT 2013 Annual Interest Rate: 3.5 Annual Interest Rate: 3.5 Number of Years: 3 Number of Years: 3 Loan Amount: 5000.0 Loan Amount: 5000.0 monthly Payment: 146.51039863455347 monthly Payment: 146.5103986345515 total Payment: 5274.374350843926 total Payment: 5274.374350843855 (a) (b) 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) Search the web and Windows 59 AM 1/30/2016

Explanation / Answer

Lets assume that the sever is running on port 5020

import java.io.*;
import java.net.*;

public class Server {
   public static void main(String[] args) {
       int serverPort = 5020;
       ServerSocket serverSocket = null;
       ObjectOutputStream toClient = null;
       ObjectInputStream fromClient = null;
       try {
           serverSocket = new ServerSocket(serverPort);
           while(true) {
               Socket socket = serverSocket.accept();
               System.out.println("Connected to client " +
                   socket.getRemoteSocketAddress());
                  
               toClient = new ObjectOutputStream(
                   new BufferedOutputStream(socket.getOutputStream()));
               fromClient = new ObjectInputStream(
                   new BufferedInputStream(socket.getInputStream()));
                  
               Message annualInterest = (Message) fromClient.readObject();
               Message numberofYears = (Message) fromClient.readObject();
               Message loanAmount = (Message) fromClient.readObject();
              
               System.out.println("Annual Interest Rate " +
                   annualInterest.number);
               System.out.println("Number of Years " +
                   numberofYears.number);
               System.out.println("Loan Amount " +
                   loanAmount.number);
              
               // here you can use interest calculation formula to get monthly payment as well as annual payment then send it to client
               // for sending back to client refer following given syntax

               toClient.writeObject(new Message(monthlyPayment.number));
               toClient.writeObject(new Message(annualPayment.number));
               toClient.flush();
           }
       }
       catch(IOException e) {
           e.printStackTrace();
           System.exit(1);
       }
       catch(ClassNotFoundException e) {
           e.printStackTrace();
           System.exit(1);
       }
   }