Question: Suppose a file named Exercise17_07.dat has been created using the ObjectOutputStream. The file contains Loan objects. The Loan class in Listing 10.2 does not implement

Suppose a file named Exercise17_07.dat has been created using the ObjectOutputStream. The file contains Loan objects. The Loan class in Listing 10.2 does not implement Serializable. Rewrite the Loan class to implement Serializable. Write a program that reads the Loan objects from the file and displays the total loan amount. Suppose you don?t know how many Loan objects are there in the file, use EOFException to end the loop.

Listing

public class Loan { private double annualInterestRate; private int numberofYears; private double

loanAmount; private java.util.Date loanDate; 5 /** Default constructor */ public Loan() {

public class Loan { private double annualInterestRate; private int numberofYears; private double loanAmount; private java.util.Date loanDate; 5 /** Default constructor */ public Loan() { this(2.5, 1, 1000); 10 11 12 /** Construct a loan with specified annual interest rate, 13 number of years, and loan amount */ public Loan(double annualInterestRate, int numberOfYears, double loanAmount) { this. annualInterestRate = annualInterestRate; this.number0fYears = number0fYears; this. loanAmount = loanAmount; loanDate - new java.util.Date(); 14 15 16 17 18 19 20 21 22 23 /** Return annualInterestRate */ public double getAnnualInterestRate() { return annualInterestRate; 24 25 26 27 28 29 30 /** Set a new annualInterestRate */ public void setAnnualInterestRate(double annualInterestRate) { this. annualInterestRate = annualInterestRate; 31 32 33 /** Return numberofYears */ public int getNumberofYears () { return number0fYears; 34 35 36 37 38 /** Set a new numberOfYears */ public void setNumberofYears(int numberofYears) { this.numberofYears = number0fYears; 39 40 41 42 43 /** Return loanAmount */ public double getLoanAmount () { return loanAmount; 44 45 46 47 48 /** Set a new 1loanAmount */ public void setLoanAmount (double loanAmount) { this. loanAmount = loanAmount; 49 50 51 52 /** Find monthly payment */ public double getMonthlyPayment() { double monthlyInterestRate = annualInterestRate / 1200; double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (1 / Math.pow(1i + monthlyInterestRate, number0fYears * 12))); return monthlyPayment; 53 54 55 56 57 58 59 60 61 /** Find total payment */ public double getTotal Payment () { double totalPayment = getMonthlyPayment () * numberofYears * 12; return totalPayment; 62 63 64 65 66 67 /** Return loan date */ public java.util.Date getloanDate() { return loanDate; 68 69 70 71 }

Step by Step Solution

3.47 Rating (176 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Program Plan Define the Loan class with Serializable Define the file with using Obj ectOnput St ream ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!