Getter/Setter (composition, Java, HW)
Posted
by Crystal
on Stack Overflow
See other posts from Stack Overflow
or by Crystal
Published on 2010-04-25T22:41:02Z
Indexed on
2010/04/25
22:43 UTC
Read the original article
Hit count: 232
I have one class called Person that basically looks like:
public class Person
{
String firstName;
String lastName;
String telephone;
String email;
public Person()
{
firstName = "";
lastName = "";
telephone = "";
email = "";
}
public Person(String firstName, String lastName, String telephone, String email)
{
this.firstName = firstName;
this.lastName = lastName;
this.telephone = telephone;
this.email = email;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
....
Using that class, I setup an abstract class called Loan that looks like:
public abstract class Loan
{
public void setClient(Person client)
{
this.client = client;
}
public Person getClient()
{
return client;
}
public void setLoanId(int nextId)
{
loanId = nextId;
nextId++;
}
public int getLoanId()
{
return loanId;
}
public void setInterestRate(double interestRate)
{
this.interestRate = interestRate;
}
public double getInterestRate()
{
return interestRate;
}
public void setLoanLength(int loanLength)
{
this.loanLength = loanLength;
}
public int getLoanLength()
{
return loanLength;
}
public void setLoanAmount(double loanAmount)
{
this.loanAmount = loanAmount;
}
public double getLoanAmount(double loanAmount)
{
return loanAmount;
}
private Person client;
private int loanId;
private double interestRate;
private int loanLength;
private double loanAmount;
private static int nextId = 1;
}
I have to extend the Loan class with CarLoan and it looks like:
public class CarLoan extends Loan
{
public CarLoan(Person client, double vehiclePrice, double downPayment, double salesTax,
double interestRate, CAR_LOAN_TERMS length)
{
super.setClient(client);
super.setInterestRate(interestRate);
this.client = client;
this.vehiclePrice = vehiclePrice;
this.downPayment = downPayment;
this.salesTax = salesTax;
this.length = length;
}
public void setVehiclePrice(double vehiclePrice)
{
this.vehiclePrice = vehiclePrice;
}
public double getVehiclePrice()
{
return vehiclePrice;
}
public void setDownPayment(double downPayment)
{
this.downPayment = downPayment;
}
public double getDownPayment()
{
return downPayment;
}
public void setSalesTax(double salesTax)
{
this.salesTax = salesTax;
}
public double getSalesTax()
{
return salesTax;
}
public String toString()
{
return getClass().getName() + "[vehiclePrice = " + vehiclePrice + '\n'
+ "downPayment = " + downPayment + '\n'
+ "salesTax = " + salesTax
+ "]";
}
public enum CAR_LOAN_TERMS {TWO_YEAR, THREE_YEAR, SIX_YEAR};
private double vehiclePrice;
private double downPayment;
private double salesTax;
Few questions.
(a) Is what I did in the Loan class to setClient correct given what I have in the Person class? (e.g.this.client = client)
(b) Can I call super twice in a method? I have to set two attributes from the Loan class from the constructor in the CarLoan class and I thought that would be a way to do it.
(c) Do you have to set attributes for enumeration types differently in a constructor or getter/setter methods? I get an error for (this.length = length) in my CarLoan class and I was unsure of how enumeration values should be set. Thanks!
© Stack Overflow or respective owner