0

Hi and hope someone can help. I doing some Java lessons (sadly they don't give us model answers until we actually get the whole thing working!) and am currently trying to get multiple constructors to work. I already have a working Person object as follows:-

public class Person {

private String name;
private MyDate birthday; // This is a date based on a Person's birthday 
private MyDate today; // A date based on today's date
private int day; // day of birth
private int month; // month of birth
private int year; // year of birth
// private int numDays;

public Person(String name, int pp, int kk, int vv) {
    this.name = name;
    this.day = pp;
    this.month = kk;
    this.year = vv;
    this.birthday = new MyDate(pp, kk, vv); 
}

and now they want us to create shortened versions of these such as these but both are showing errors in NetBeans:-

    public Person(String name) {
    MyDate today = today(); // Creates a date based on today's date
    this.Person(name, today);
}

public Person(String name, MyDate birthday) {
    this(name, birthday);
}

Ande here's the MyDate method

public class MyDate {

private int day;
private int month;
private int year;

public MyDate(int pv, int kk, int vv) {
    this.day = pv;
    this.month = kk;
    this.year = vv;
}

Any ideas on how I can get these working? Thanks for your interest.

4 Answers 4

2

You should give acceptable names to your parameters in your constructor. Do your really have to store day, month and year ?

public class Person {

    private String name;
    private MyDate birthday; // This is a date based on a Person's birthday 
    private MyDate today; // A date based on today's date

    public Person(String name, int day, int month, int year) {
        this(name, new MyDate(day, month, year)); 
    }

    public Person(String name) {
        this(name, today());
    }

    public Person(String name, MyDate birthday) {
        this.name = name;
        this.birthday = birthday;
    }

}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Thomas but they get used elsewhere for things like finding someone's age, checking who is older etc.
OK but I think they must be stored in the MyDate object and not in this object. Then retrieve these values with today.getDay() today.getMonth(), today.getYear() for example !
Well done Thomas, I think you've got to the heart of my struggle. I'm off to experiment now filled with optimism rather than the despair of 10 mins ago. Thanks!
:) good to hear. accept the answer if it fits for you. good luck for next lesson !
2

You should not leave

public Person(String name, MyDate birthday)
{
  this(name, birthday);
} 

because it will call itself again and again: this() will call the constructor matching the params you passed, but you pass the same params to this() than you've got with the constructor. You're creating an infinite loop.

6 Comments

Thanks Brendan but how do I deconstruct birthday so I can send it off to the original Person object/method i.e. the first Person needs a set of 3 ints to build a Person
Presumably the opposite of the way you build it in the original constructor
Oh! You're problem is that you want to get info out of "MyDate"? If so, your should call methods declared in MyDate to get the months, the year and so on. If MyDate is java.util.Date that is pretty obvious. Could you give more information about MyDate?
Thanks both they get constructed with 'code'public class MyDate { private int day; private int month; private int year; public MyDate(int pv, int kk, int vv) { this.day = pv; this.month = kk; this.year = vv; }'code'
Great Brendan, got it now, as you say I just need to call up MyDate to deconstruct it. My thanks
|
1

All you need to do is:

public Person(String name, MyDate birthday) {
this.name = name;
this.birthday = birthday;
}

If your class MyDate has getters and setters for day, month and year, then your constructor can also do as following:

public Person(String name, MyDate birthday) {
this(name, birthday.getDay(), birthday.getMonth(), birthday.getYear());
}

For this you will need to update your MyDate class as follows:

public class MyDate {

private int day;
private int month;
private int year;

public MyDate(int pv, int kk, int vv) {
    this.day = pv;
    this.month = kk;
    this.year = vv;
}

public void setDay(int day) {
this.day = day;
}
public void setMonth(int month) {
this.day = month;
}
public void setYear(int year) {
this.day = year;
}
public int getDay() {
return this.day;
}
public int getMonth() {
return this.month;
}
public int getYear() {
return this.year;
}

}

1 Comment

Well done user2881767 and Thomas you've both sent me off in the right direction whereby I can deconstruct stuff that I've already created. Really pleased I asked as, although I'd come across the terms getters and setters before, I never understood their significance. Brilliant, thanks again.
0

You can define any number of constructors with each having different signature. Constructor can call only one constructor(should not call itself as that will create infinite loop).

public class Person {

    private String name;
    private MyDate birthday; // This is a date based on a Person's birthday 
    private MyDate today; // A date based on today's date

    public Person(String name, int pp, int kk, int vv) {
        this(name, new MyDate(pp, kk, vv)); 
    }

    public Person(String name, MyDate birthday) {
        this(name); //Third constructor
        this.birthday = birthday; 
    }

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

}

class MyDate{
    private int day;
    private int month;
    private int year;

    public MyDate(int pv, int kk, int vv) {
        this.day = pv;
        this.month = kk;
        this.year = vv;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.