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

Marian G Java Exercise 30.4. Just 30, × Module Calendar-CIST2: ×/ 0133593517.pdf

ID: 3664282 • Letter: M

Question

Marian G Java Exercise 30.4. Just 30, × Module Calendar-CIST2: ×/ 0133593517.pdf -C Dwww.ebooksbucket.com/uploadsitprogramming/java/Introduction to Java%20Programming-Comprehensive Version·pdf 0133593517.pdf 1192 1345 PROGRAMMING EXERCISES MyProgrammingLab Section 31.2 31.1 (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. Exercise31 01Client Annual Interest Rate Number Of Years Loan Amount 3.5 Exercise31 01Server 3 Submit Exercise31_01Server 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 totalPayment: 5274.374350843926 5000 Annual Interest Rate: 3.5 Number of Years: 3 Loan Amount: 5000.0 monthlyPayment: 146.5103986345515 total Payment: 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) O e 1:59 AM 1/30/2016 Search the web and Windows

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