LoanSerializable.java, WriteLoanObjects.java, ReadLoadObjects.java, Date.java Wr
ID: 3776648 • Letter: L
Question
LoanSerializable.java, WriteLoanObjects.java, ReadLoadObjects.java, Date.java Write the LoanSerializable.java class to implement Serializable. Write a program WriteLoanObjects.java that creates five Loan objects and stores them in a file named LoanObjects.dat. (Restore objects from a file). Write a program ReadLoanObjects.java 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. Methods and attributes of Loan class Loan -annualInterestRate: double -numberOfYears: int -loanAmount: double -loanDate: java.util.Date The annual interest rate of the loan (default: 2.5). The number of years for the loan (default: 1). The loan amount (default: 1000). The date this loan was created. +Loan() +Loan(annualInterestRate: double, numberOfYears: int,loanAmount: double) +getAnnualInterestRate(): double +getNumberOfYears(): int +getLoanAmount(): double +getLoanDate(): java.util.Date +setAnnualInterestRate( annualInterestRate: double): void +setNumberOfYears( numberOfYears: int): void +setLoanAmount( loanAmount: double): void +getMonthlyPayment(): double +getTotalPayment(): double +toString(): String Constructs a default Loan object. Constructs a loan with specified interest rate, years, and loan amount. Returns the annual interest rate of this loan. Returns the number of the years of this loan. Returns the amount of this loan. Returns the date of the creation of this loan. Sets a new annual interest rate for this loan. Sets a new number of years for this loan. Sets a new amount for this loan. Returns the monthly payment for this loan. Returns the total payment for this loan. Returns string representation of the object General instructions 1. Use try…catch blocks
Explanation / Answer
//FIleName Loan.java
import java.io.Serializable;
import java.util.Date;
public class Loan implements Serializable{
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private Date loanDate;
public Loan() {
this.annualInterestRate = 2.5;
this.loanAmount=1000;
this.numberOfYears=1;
this.loanDate = new Date();
}
public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
this.loanDate = new Date();
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public int getNumberOfYears() {
return numberOfYears;
}
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
}
public double getLoanAmount() {
return loanAmount;
}
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
}
public Date getLoanDate() {
return loanDate;
}
public void setLoanDate(Date loanDate) {
this.loanDate = loanDate;
}
public void getMonthlyPayment(){
double monthlyPayment = (this.loanAmount*(1+(this.annualInterestRate/100)*this.numberOfYears))/(this.numberOfYears*12);
System.out.println("Monthly Payment:"+Math.round(monthlyPayment*100.0)/100.0);
}
public double getTotalPayment(){
double totalPayment = this.loanAmount*(1+(this.annualInterestRate/100)*this.numberOfYears);
return Math.round(totalPayment*100.0)/100.0;
}
@Override
public String toString(){
return "Annual Interest Rate:"+this.annualInterestRate+
" Loan Amount:"+this.loanAmount+
"Number Of Years:"+this.numberOfYears+
"Laon Date:"+this.loanDate;
}
public static void main(String[] args) {
Loan loan = new Loan(8.5, 2, 2000000);
System.out.println(loan.toString());
loan.setAnnualInterestRate(12);
loan.setNumberOfYears(1);
loan.setLoanAmount(1200);
loan.getMonthlyPayment();
System.out.println("Total Payment:"+loan.getTotalPayment());
}
}
//FileName:WriteLoanObjects.java
public class WriteLoanObjects {
public void serializeLoan(){
Loan loan1 = new Loan(9.5,3,100000);
Loan loan2 = new Loan(8.5,3,200000);
Loan loan3 = new Loan(7.5,3,300000);
Loan loan4 = new Loan(6.5,3,400000);
Loan loan5 = new Loan(5.5,3,500000);
try{
FileOutputStream fout = new FileOutputStream("LoanObjects.dat");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(loan1);
oos.writeObject(loan2);
oos.writeObject(loan3);
oos.writeObject(loan4);
oos.writeObject(loan5);
oos.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args) {
WriteLoanObjects writeLoanObjects = new WriteLoanObjects();
writeLoanObjects.serializeLoan();
}
}
//FileName: ReadLoadObjects.java
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ReadLoadObjects {
public void readLoanObjects() throws IOException, ClassNotFoundException {
Loan loan;
ObjectInputStream input = new ObjectInputStream(new FileInputStream("LoanObjects.dat"));
while (true) {
try {
loan = (Loan) (input.readObject());
System.out.println(loan.toString());
} catch (EOFException ex) {
break;
}
}
input.close();
}
public static void main(String[] args) {
try {
ReadLoadObjects readLoadObjects =new ReadLoadObjects();
readLoadObjects.readLoanObjects();
} catch (IOException ex) {
Logger.getLogger(ReadLoadObjects.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ReadLoadObjects.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.