The Loan class, does not implement Serializable. Rewrite the Loan class to imple
ID: 3659899 • Letter: T
Question
The Loan class, does not implement Serializable. Rewrite the Loan class to implement Serializable. Write a program that creates five Loan objects and stores them in a file named Exercise19_6.dat. 1 public { 2 private double annualInterestRate; 3 private int numberOfYears; 4 private double loanAmount; 5 private java.util.Date loanDate; 6 7 /** Default constructor */ 8 public Loan() { 9 this(2.5, 1, 1000); 10 } 11 12 /** Construct a loan with specified annual interest rate, 13 number of years, and loan amount 14 */ 15 16 { 17 this.annualInterestRate = annualInterestRate; 18 this.numberOfYears = numberOfYears; 19 this.loanAmount = loanAmount; 20 loanDate = new java.util.Date(); 21 } 22 23 /** Return annualInterestRate */ 24 public double getAnnualInterestRate() { 25 return annualInterestRate; 26 } double loanAmount) public Loan(double annualInterestRate, int numberOfYears, class Loan no-arg constructor constructor 27 28 /** Set a new annualInterestRate */ 29 public void setAnnualInterestRate(double annualInterestRate) { 30 this.annualInterestRate = annualInterestRate; 31 } 32 33 /** Return numberOfYears */ 34 public int getNumberOfYears() { 35 return numberOfYears; 36 } 37 38 /** Set a new numberOfYears */ 39 public void setNumberOfYears(int numberOfYears) { 40 this.numberOfYears = numberOfYears; 41 } 42 43 /** Return loanAmount */ 44 public double getLoanAmount() { 45 return loanAmount; 46 } 47 48 /** Set a newloanAmount */ 49 public void setLoanAmount(double loanAmount) { 50 this.loanAmount = loanAmount; 51 } 52 53 /** Find monthly payment */ 54 public double getMonthlyPayment() { 55 double monthlyInterestRate = annualInterestRate / 1200; 56 double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 57 (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12))); 58 return monthlyPayment; 59 } 60 61 /** Find total payment */ 62 public double getTotalPayment() { 63 double totalPayment = getMonthlyPayment() * numberOfYears * 12; 64 return totalPayment; 65 } 66 67 /** Return loan date */ 68 public java.util.Date getLoanDate() { 69 return loanDate; 70 } 71 }Explanation / Answer
Loan.java
import java.io.Serializable;
public class Loan implements Serializable{
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private java.util.Date loanDate;
/** Default constructor */
public Loan() {
this(2.5, 1, 1000);
}
/** Construct a loan with specified annual interest rate,
number of years, and loan amount 14 */
/** Return annualInterestRate */
public double getAnnualInterestRate() {
return annualInterestRate;
}
public Loan(double annualInterestRate, int numberOfYears, double loanAmount){
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new java.util.Date();
}
/** Set a new annualInterestRate */
/** Return numberOfYears */
public int getNumberOfYears() {
return numberOfYears;
}
/** Set a new numberOfYears */
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
}
/** Return loanAmount */
public double getLoanAmount() {
return loanAmount;
}
/** Set a newloanAmount */
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
}
/** Find monthly payment */
public double getMonthlyPayment() {
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
return monthlyPayment; }
/** Find total payment */
public double getTotalPayment() {
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment; }
/** Return loan date */
public java.util.Date getLoanDate() {
return loanDate;
}
}
LoanTest.java
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class LoanTest {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
String fileName = "C:\Exercise19_6.dat";
Loan loanObject1 = new Loan(5, 22, 100000);
Loan loanObject2 = new Loan(8, 2, 500000);
Loan loanObject3 = new Loan(9, 12, 300000);
Loan loanObject4 = new Loan(7, 15, 500000);
Loan loanObject5 = new Loan(10, 8, 200000);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(loanObject1);
oos.writeObject(loanObject2);
oos.writeObject(loanObject3);
oos.writeObject(loanObject4);
oos.writeObject(loanObject5);
oos.close();
System.out.println("Successfully Written loan objects to : " + "C:\Exercise19_6.dat");
}
}
Output:
Successfully Written loan objects to : C:Exercise19_6.dat
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.