0
   

Is following class immutable and how can it be changed into immutable class?

 
 
Reply Tue 8 May, 2012 02:15 pm
Is following class immutable and how can it be changed into immutable class? Is it true that if you remove last getLoanDate-method, then you have immutable class and why is that so?

public class Loan {
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;
}

/** Return numberOfYears */
public int getNumberOfYears() {
return numberOfYears;
}

/** Return loanAmount */
public double getLoanAmount() {
return 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;
}

/** kuukausimaksu K * q^n * ( (1-q)/(1-q^n)
missä q = 1 + vuosikorko/1200
*/
public double kuukausiEra() {
double q = 1 + annualInterestRate / 1200;
int n = numberOfYears * 12;
double qn = Math.pow(q, n);
double era = loanAmount * qn * ((1 - q) / (1 - qn));
return era;
}

/** Find total payment */
public double getTotalPayment() {
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}

/** Return loan date */
public java.util.Date getLoanDate() {
return loanDate;
}
}
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 1,046 • Replies: 2
No top replies

 
maxdancona
 
  1  
Reply Tue 8 May, 2012 08:32 pm
@alvoutila,
I don't do homework for people, but I can help you think this through. You already have a good clue that the getLoanDate method is a problem. Can you figure out why?

What is the definition of immutable and how does this violate it?
alvoutila
 
  1  
Reply Wed 9 May, 2012 03:57 am
@maxdancona,
All attributes are private attributes
There are not setX-methods in the class
No method returns an object, which can be changed with methods of the object.
But now I don't get why getLoanDate - method makes class mutable. Maybe because it changes value of loanDate - attribute? Am I right?
0 Replies
 
 

Related Topics

 
  1. Forums
  2. » Is following class immutable and how can it be changed into immutable class?
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 04/25/2024 at 02:41:33