LoanSerializablejava WriteLoanObjectsjava ReadLoadObjectsjav
Solution
Please find below the java programs asked in the above questions. I have added the comments for your better understanding. Let me know if any issue. :)
LoanSerializable.java:
public class LoanSerializable implements java.io.Serializable { // To Implement Serializable
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private java.util.Date loanDate;
public LoanSerializable() { /** The Default constructor */
this(2.5, 1, 1000);
}
/** Construct a loan with specified annual interest rate, no, of years, and loan amt */
public LoanSerializable(double annualInterestRate, int numberOfYears, double loanAmount) {
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new java.util.Date();
}
/** This will Return annualInterestRate */
public double getAnnualInterestRate() {
return annualInterestRate;
}
/** This will Set a new annualInterestRate */
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
/** This will Return numberOfYears */
public int getNumberOfYears() {
return numberOfYears;
}
/** This will Set numberOfYears */
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
}
/** This will Return loanAmount */
public double getLoanAmount() {
return loanAmount;
}
/** This will Set a new loanAmount */
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
}
/** This will Find monthly payment */
public double getMonthlyPayment() {
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 -
(1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)));
return monthlyPayment;
}
/** This will Find total payment */
public double getTotalPayment() {
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}
/** This will Return loan date */
public java.util.Date getLoanDate() {
return loanDate;
}
@Override /** This will Override the toString method in the Object class */
public String toString() {
return \"Date: \" + loanDate + \"\ Annual interest Rate: \" +
annualInterestRate + \"\ Years: \" + numberOfYears +
\"\ Loan amount: \" + loanAmount;
}
}
WriteLoanobjects.java:
import java.io.*;
public class LoanObjects {
public static void main(String[] args) throws IOException {
// The below will Create the 5 Loan objects
Loan loan1 = new Loan();
Loan loan2 = new Loan(8.25, 5, 120000.95);
Loan loan3 = new Loan(4.5, 4, 5000);
Loan loan4 = new Loan(5.0, 5, 10000);
Loan loan5 = new Loan(5.75, 5, 10000);
try ( // This will Create an output stream for the file LoanObjects.dat
ObjectOutputStream output = new ObjectOutputStream(new
BufferedOutputStream(new FileOutputStream(\"LoanObjects.dat\")));
) {
// The below will Write five Loan objects to the file
output.writeObject(loan1);
output.writeObject(loan2);
output.writeObject(loan3);
output.writeObject(loan4);
output.writeObject(loan5);
}
}
}
ReadLoanObjects.java:
import java.io.*;
public class LoanObjects {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
try ( // To Create an input stream for file LoanObjects.dat
ObjectInputStream input = new ObjectInputStream(new
BufferedInputStream(new FileInputStream(\"LoanObjects.dat\")))
) { // This will Read Loan objects from file and display the total loan amount
while (true) {
Loan loan = (Loan)input.readObject();
System.out.println(loan);
System.out.printf(\"Total loan amount: $%.2f\ \",
loan.getTotalPayment());
System.out.println();
}
}
catch (EOFException ex) {
// The Use of EOFExecption to end the loop
}
}
}


