Q:

(Store Loan objects) The Loan class in Listing 10.2 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 Exercise17_06.dat

0

(Store Loan objects) The Loan class in Listing 10.2 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 Exercise17_06.dat.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
*    (Store Loan objects) The Loan class in Listing 10.2 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      *
*    Exercise17_06.dat.                                                          *
*********************************************************************************/
import java.io.*;

public class Exercise_17_06 {
	public static void main(String[] args) throws IOException {
		// Create five 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 ( // Create an output stream for the file Exercise17_06.dat
			ObjectOutputStream output = new ObjectOutputStream(new 
				BufferedOutputStream(new FileOutputStream("Exercise17_06.dat")));
		) {
			// Write five Loan objects to the file
			output.writeObject(loan1);
			output.writeObject(loan2);
			output.writeObject(loan3);
			output.writeObject(loan4);
			output.writeObject(loan5);
		}
	}
}

Loan.java

/**************************************
*            Loan                     *
*-----------------------------------  *
* -annualInterestRate: double         *
* -numberOfYears: int                 *
* -loanAmount: double                 *
* -loanDate: java.util.Date           *
* +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          *
**************************************/

// Implement Serializable
public class Loan implements java.io.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
	 */
	public Loan(double annualInterestRate, int numberOfYears,
		double loanAmount) {
		this.annualInterestRate = annualInterestRate;
		this.numberOfYears = numberOfYears;
		this.loanAmount = loanAmount;
		loanDate = new java.util.Date();
	}

	/** Return annualInterestRate */
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}

	/** Set an new annualInterestRate */
	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = 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 new loanAmount */
	public void setLoanAmount(double loanAmount) {
		this.loanAmount = loanAmount;
	} 

	/** Find monthly payment */
	public double getMonthlyPayment() {
		double monthlyInterestRate = annualInterestRate / 1200;
		double monthlyPayment = loanAmount * monthlyInterestRate / (1 -
			(1 / Math.pow(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;
	}

	@Override /** Override the toString method in the Object class */
	public String toString() {
		return "Date: " + loanDate + "\nAnnual interest Rate: " + 
			annualInterestRate + "\nYears: " + numberOfYears +
			"\nLoan amount: " + loanAmount;
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now