views:

44

answers:

3

I'm having some trouble with a hw assignment. In one assignment, we had to create a Person class. Mine was:

public class Person
{
    String firstName;
    String lastName;
    String telephone;
    String email;

    public Person()
    {
       firstName = "";
       lastName = "";
       telephone = "";
       email = "";
    }

    public Person(String firstName)
    {
        this.firstName = firstName;
    }

    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;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public String getTelephone()
    {
        return telephone;
    }

    public void setTelephone(String telephone)
    {
        this.telephone = telephone;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public boolean equals(Object otherObject)
    {
        // a quick test to see if the objects are identical
        if (this == otherObject) {
            return true;
        }

        // must return false if the explicit parameter is null
        if (otherObject == null) {
            return false;
        }

        if (!(otherObject instanceof Person)) {
            return false;
        }

        Person other = (Person) otherObject;
        return firstName.equals(other.firstName) && lastName.equals(other.lastName) &&
            telephone.equals(other.telephone) && email.equals(other.email);
    }

    public int hashCode() 
    {
        return 7 * firstName.hashCode() +
            11 * lastName.hashCode() + 
            13 * telephone.hashCode() + 
            15 * email.hashCode();
    }

    public String toString()
    {
        return getClass().getName() + "[firstName = " + firstName + '\n'
                                    + "lastName = " + lastName + '\n'
                                    + "telephone = " + telephone + '\n'
                                    + "email = " + email + "]";
    }
}

Now we have to create a Loan class that uses Person as an attribute, and then extend that Loan class. My Loan class is:

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

    public void printPayments()
    {
        double monthlyInterest;
        double monthlyPrincipalPaid;
        double newPrincipal;
        int paymentNumber = 1;
        double monthlyInterestRate = interestRate / 1200;
        double monthlyPayment = loanAmount * (monthlyInterestRate) / 
                                (1 - Math.pow((1 + monthlyInterestRate),( -1 * loanLength)));

        // amortization table
        while (loanAmount != 0) {
            monthlyInterest = loanAmount * monthlyInterestRate;
            monthlyPrincipalPaid = monthlyPayment - monthlyInterest;
            newPrincipal = loanAmount - monthlyPrincipalPaid;
            loanAmount = newPrincipal;

            System.out.println("Payment Number | Interest | Principal | Loan Balance");
            System.out.printf("%d, %.2f, %f, %f", paymentNumber++, monthlyInterest, newPrincipal, loanAmount);
        }
    }
    /*
    //method to print first payment
    public double getFirstPayment()
    {
    }

    method to print last payment
    public double getLastPayment()
    {
    }*/

    private Person client;
    private int loanId;
    private double interestRate;
    private int loanLength;
    private double loanAmount;
    private static int nextId = 1;

}

And then extended the Loan class with CarLoan class, there is a function prototype of:

public CarLoan(Person client, double vehiclePrice, double downPayment, double salesTax,
                    double interestRate, CAR_LOAN_TERMS length)

I'm confused on how I use the Person constructor from the superclass. I cannot necessarily do

super(client);

in my constructor which is what the book did with some primitive types in their example. Not sure what the correct thing to do is... Any thoughts? Thanks!

A: 

If CarLoan is to extend Person, then Person becomes the superclass of CarLoan.

From within the constructor of CarLoan you must always call one of the Person constructors via the super keyword before any other processing takes place.

However, it seems to me very much like you must be confused, as your prototype method passes an instance of Person to CarLoan. Further, I cannot see why a class called CarLoan would extend a person.

Finbarr
+3  A: 

CarLoan should not extend Person. That makes no sense since a CarLoan can't be a Person.

But Person can be a class variable in the CarLoan class.

public class CarLoan {

  private Person client;
  private double vehiclePrice;

  public CarLoan(Person client, double vehiclePrice, double downPayment, double salesTax, double interestRate, CAR_LOAN_TERMS length) {

    this.client = client;
    this.vehiclePrice = vehiclePrice;
    ..
  }
}
Espen
I think client should be a protected member of Loan.
NomeN
@NomeN: If the client was declared in Loan, then it should have been protected. But it doesn't add any value in this case when no class extends from CarLoan.
Espen
@Espen The OP mentions in his post that Person should be an attribute of Loan, i.o.w. there should be a field of type Person in Loan. Because you need access to this field from CarLoan to set it, you'll need either get/set methods for this field or make it protected.
NomeN
+1  A: 

It looks like you want to be using composition in stead of inheritance.

In plain english, a CarLoan has a client (of type Person). A CarLoan itself is not a Person (which inheritance would suggest).

So you should do what Espen suggests (composition), in stead of CarLoan extends Person (inheritance).

A possibly legit use of inheritance would be:

class Waiter extends Person {
    String employeeId;

    // A waiter is a person with some extra information
    public Waiter(String firstName, String lastName, String telephone, 
                  String email, String employeeId) {
      super(firstName, lastName, telephone, email); // must be first
      this.employeeId = employeeId;
    }

}
extraneon