Constructor has different parameter type with datafield












2















I have a class called Monster, every Monster has a name, dateofbirth, and a weapon. and The Monster constructor
should have the following signature:



Monster(String name, int day, int month, int year, String weaponName)


To create a monster the following code should work:



new Monster("Godzilla",11,10,2000,"VenomThrower")


So I create the datafield in Monster:



public String name;
public MyDate dateOfBirth;
public int day;
public int month;
public int year;
public Weapon weapon;


However, I have to use the MyDate class defined below to create a MyDate object for dateofbirth.
And use the Weapon class defined below to create a Weapon object for the monster’s weapon.



If I have to use the Object MyDate and Weapon, their type would be different from the constructor(int day, int month, int year) and (String weaponName), I try to cast the parameters, but it compiles error. Except casting, is there another way to achieve this?? Thank you.



public class MyDate {

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


MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public int getMonth() {
return month;
}

public void setMonth(int month) {
this.month = month;
}

public int getDay() {
return day;
}

public void setDay(int day) {
this.day = day;
}
}

public class Weapon {

private String name;
Weapon(String n) {
this.name = n;
}
public String getName() {
return name;
}
}









share|improve this question

























  • One more thing about dates: I think the MyDaye class is part of the assignment, so you have to use it this time, but if you ever have a choice – use the date and time classes from the java.time package. For instance, LocalDate would be a perfect fit in the place of MyDate in this example.

    – MC Emperor
    Nov 21 '18 at 7:32













  • Yes, MyDate was already given. Thank you! I learn a new thing :)

    – Shin Yu Wu
    Nov 21 '18 at 7:36
















2















I have a class called Monster, every Monster has a name, dateofbirth, and a weapon. and The Monster constructor
should have the following signature:



Monster(String name, int day, int month, int year, String weaponName)


To create a monster the following code should work:



new Monster("Godzilla",11,10,2000,"VenomThrower")


So I create the datafield in Monster:



public String name;
public MyDate dateOfBirth;
public int day;
public int month;
public int year;
public Weapon weapon;


However, I have to use the MyDate class defined below to create a MyDate object for dateofbirth.
And use the Weapon class defined below to create a Weapon object for the monster’s weapon.



If I have to use the Object MyDate and Weapon, their type would be different from the constructor(int day, int month, int year) and (String weaponName), I try to cast the parameters, but it compiles error. Except casting, is there another way to achieve this?? Thank you.



public class MyDate {

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


MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public int getMonth() {
return month;
}

public void setMonth(int month) {
this.month = month;
}

public int getDay() {
return day;
}

public void setDay(int day) {
this.day = day;
}
}

public class Weapon {

private String name;
Weapon(String n) {
this.name = n;
}
public String getName() {
return name;
}
}









share|improve this question

























  • One more thing about dates: I think the MyDaye class is part of the assignment, so you have to use it this time, but if you ever have a choice – use the date and time classes from the java.time package. For instance, LocalDate would be a perfect fit in the place of MyDate in this example.

    – MC Emperor
    Nov 21 '18 at 7:32













  • Yes, MyDate was already given. Thank you! I learn a new thing :)

    – Shin Yu Wu
    Nov 21 '18 at 7:36














2












2








2








I have a class called Monster, every Monster has a name, dateofbirth, and a weapon. and The Monster constructor
should have the following signature:



Monster(String name, int day, int month, int year, String weaponName)


To create a monster the following code should work:



new Monster("Godzilla",11,10,2000,"VenomThrower")


So I create the datafield in Monster:



public String name;
public MyDate dateOfBirth;
public int day;
public int month;
public int year;
public Weapon weapon;


However, I have to use the MyDate class defined below to create a MyDate object for dateofbirth.
And use the Weapon class defined below to create a Weapon object for the monster’s weapon.



If I have to use the Object MyDate and Weapon, their type would be different from the constructor(int day, int month, int year) and (String weaponName), I try to cast the parameters, but it compiles error. Except casting, is there another way to achieve this?? Thank you.



public class MyDate {

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


MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public int getMonth() {
return month;
}

public void setMonth(int month) {
this.month = month;
}

public int getDay() {
return day;
}

public void setDay(int day) {
this.day = day;
}
}

public class Weapon {

private String name;
Weapon(String n) {
this.name = n;
}
public String getName() {
return name;
}
}









share|improve this question
















I have a class called Monster, every Monster has a name, dateofbirth, and a weapon. and The Monster constructor
should have the following signature:



Monster(String name, int day, int month, int year, String weaponName)


To create a monster the following code should work:



new Monster("Godzilla",11,10,2000,"VenomThrower")


So I create the datafield in Monster:



public String name;
public MyDate dateOfBirth;
public int day;
public int month;
public int year;
public Weapon weapon;


However, I have to use the MyDate class defined below to create a MyDate object for dateofbirth.
And use the Weapon class defined below to create a Weapon object for the monster’s weapon.



If I have to use the Object MyDate and Weapon, their type would be different from the constructor(int day, int month, int year) and (String weaponName), I try to cast the parameters, but it compiles error. Except casting, is there another way to achieve this?? Thank you.



public class MyDate {

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


MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public int getMonth() {
return month;
}

public void setMonth(int month) {
this.month = month;
}

public int getDay() {
return day;
}

public void setDay(int day) {
this.day = day;
}
}

public class Weapon {

private String name;
Weapon(String n) {
this.name = n;
}
public String getName() {
return name;
}
}






java constructor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 23:00









Mureinik

184k22136202




184k22136202










asked Nov 20 '18 at 22:51









Shin Yu WuShin Yu Wu

968




968













  • One more thing about dates: I think the MyDaye class is part of the assignment, so you have to use it this time, but if you ever have a choice – use the date and time classes from the java.time package. For instance, LocalDate would be a perfect fit in the place of MyDate in this example.

    – MC Emperor
    Nov 21 '18 at 7:32













  • Yes, MyDate was already given. Thank you! I learn a new thing :)

    – Shin Yu Wu
    Nov 21 '18 at 7:36



















  • One more thing about dates: I think the MyDaye class is part of the assignment, so you have to use it this time, but if you ever have a choice – use the date and time classes from the java.time package. For instance, LocalDate would be a perfect fit in the place of MyDate in this example.

    – MC Emperor
    Nov 21 '18 at 7:32













  • Yes, MyDate was already given. Thank you! I learn a new thing :)

    – Shin Yu Wu
    Nov 21 '18 at 7:36

















One more thing about dates: I think the MyDaye class is part of the assignment, so you have to use it this time, but if you ever have a choice – use the date and time classes from the java.time package. For instance, LocalDate would be a perfect fit in the place of MyDate in this example.

– MC Emperor
Nov 21 '18 at 7:32







One more thing about dates: I think the MyDaye class is part of the assignment, so you have to use it this time, but if you ever have a choice – use the date and time classes from the java.time package. For instance, LocalDate would be a perfect fit in the place of MyDate in this example.

– MC Emperor
Nov 21 '18 at 7:32















Yes, MyDate was already given. Thank you! I learn a new thing :)

– Shin Yu Wu
Nov 21 '18 at 7:36





Yes, MyDate was already given. Thank you! I learn a new thing :)

– Shin Yu Wu
Nov 21 '18 at 7:36












4 Answers
4






active

oldest

votes


















2














As you've seen, you can't just go and cast arbitrary variables to other classes. You could, however, call the respective constructors of MyDate and Weapon in Monster's constructor:



public class Monster {
private String name;
private MyDate dateOfBirth;
private Weapon weapon;

public Monster(String name, int day, int month, int year, String weaponName) {
this.name = name;
this.dateOfBirth = new MyDate(year, month, day);
this.weapon = new Weapon(weaponName);
}
}





share|improve this answer
























  • a monster shouldn't care about int day, int month, int year, I think it's better to pass a prepared MyDate object instead

    – Andrew Tobilko
    Nov 20 '18 at 22:58











  • @AndrewTobilko As far as I understood the question, the signature of the constructor is given. I don't think OP is free to change it.

    – Mureinik
    Nov 20 '18 at 22:58











  • @AndrewTobilko (although I do agree in principal)

    – Mureinik
    Nov 20 '18 at 22:59











  • Thank you!!! And yes, I can't change the original constructor, because it's defined by the question.

    – Shin Yu Wu
    Nov 21 '18 at 1:20



















2














I would go at it like this:



public class Monster{

public String name;
public MyDate dateOfBirth;
public Weapon weapon;

public Monster(String name, int day, int month, int year, String weaponName){
this.name = name;
this.dateOfBirth = new DateOfBirth(day, month, year);
this.weapon = new Weapon(weaponName);
}

//second constructor for good measure
public Monster(String name, DateOfBirth day, Weapon weapon){
this.name = name;
this.dateOfBirth = day;
this.weapon = weapon;
}

//also relay all getters and setters to the underlying objects:
public int getDay(){
return dateOfBirth.getDay()
}
//...
}


You should definitely drop the redundant day, month and year, because they are stored in dayOfMonth.






share|improve this answer

































    1














    You could either create another constructor to accept MyDate and Weapon objects:



    public Monster(String name, MyDate birthDay, Weapon weapon) {
    //...
    }


    or in your constructor you could do:



    public Monster(String name, int day, int month, int year, String weaponName){ 
    this.weapon = new Weapon(weaponName);
    this.dateOfBirth = new MyDate(day, month, year);
    //...
    }


    To create the appropriate objects from the given parameters






    share|improve this answer































      0














      I'm not exactly clear on what you're asking. If you are indicating that you must retain the Monster constructor as you have defined it, but store MyDate and Weapon objects in your Monster class, then the answer is simple.



      Monster(String name, int day, int month, int year, String weaponName) {
      this.name = name;
      this.dateOfBirth = new MyDate(year, month, day);
      this.weapon = new Weapon(weaponName);
      }


      I don't think this kind of coupling is a very good idea. Better to define your constructor like so:



      Monster(String name, MyDate dateOfBirth, Weapon weapon) {
      this.name = name;
      this.dateOfBirth = dateOfBirth;
      this.weapon = weapon;
      }


      and call the constructor like this:



      Monster m = new Monster("Godzilla", new MyDate(2000,11,10), new Weapon("VenomThrower"));


      Also, You might want to have a look at using java.time.LocalDate to replace your MyDate class.



      If you have a fixed set of Weapons. You could redefine Weapon as an enum rather than a class



      public enum Weapon {
      VenomThrower, . . . ;
      }


      Then Weapon references will look like,



      Weapon.VenomThrower





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402787%2fconstructor-has-different-parameter-type-with-datafield%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        As you've seen, you can't just go and cast arbitrary variables to other classes. You could, however, call the respective constructors of MyDate and Weapon in Monster's constructor:



        public class Monster {
        private String name;
        private MyDate dateOfBirth;
        private Weapon weapon;

        public Monster(String name, int day, int month, int year, String weaponName) {
        this.name = name;
        this.dateOfBirth = new MyDate(year, month, day);
        this.weapon = new Weapon(weaponName);
        }
        }





        share|improve this answer
























        • a monster shouldn't care about int day, int month, int year, I think it's better to pass a prepared MyDate object instead

          – Andrew Tobilko
          Nov 20 '18 at 22:58











        • @AndrewTobilko As far as I understood the question, the signature of the constructor is given. I don't think OP is free to change it.

          – Mureinik
          Nov 20 '18 at 22:58











        • @AndrewTobilko (although I do agree in principal)

          – Mureinik
          Nov 20 '18 at 22:59











        • Thank you!!! And yes, I can't change the original constructor, because it's defined by the question.

          – Shin Yu Wu
          Nov 21 '18 at 1:20
















        2














        As you've seen, you can't just go and cast arbitrary variables to other classes. You could, however, call the respective constructors of MyDate and Weapon in Monster's constructor:



        public class Monster {
        private String name;
        private MyDate dateOfBirth;
        private Weapon weapon;

        public Monster(String name, int day, int month, int year, String weaponName) {
        this.name = name;
        this.dateOfBirth = new MyDate(year, month, day);
        this.weapon = new Weapon(weaponName);
        }
        }





        share|improve this answer
























        • a monster shouldn't care about int day, int month, int year, I think it's better to pass a prepared MyDate object instead

          – Andrew Tobilko
          Nov 20 '18 at 22:58











        • @AndrewTobilko As far as I understood the question, the signature of the constructor is given. I don't think OP is free to change it.

          – Mureinik
          Nov 20 '18 at 22:58











        • @AndrewTobilko (although I do agree in principal)

          – Mureinik
          Nov 20 '18 at 22:59











        • Thank you!!! And yes, I can't change the original constructor, because it's defined by the question.

          – Shin Yu Wu
          Nov 21 '18 at 1:20














        2












        2








        2







        As you've seen, you can't just go and cast arbitrary variables to other classes. You could, however, call the respective constructors of MyDate and Weapon in Monster's constructor:



        public class Monster {
        private String name;
        private MyDate dateOfBirth;
        private Weapon weapon;

        public Monster(String name, int day, int month, int year, String weaponName) {
        this.name = name;
        this.dateOfBirth = new MyDate(year, month, day);
        this.weapon = new Weapon(weaponName);
        }
        }





        share|improve this answer













        As you've seen, you can't just go and cast arbitrary variables to other classes. You could, however, call the respective constructors of MyDate and Weapon in Monster's constructor:



        public class Monster {
        private String name;
        private MyDate dateOfBirth;
        private Weapon weapon;

        public Monster(String name, int day, int month, int year, String weaponName) {
        this.name = name;
        this.dateOfBirth = new MyDate(year, month, day);
        this.weapon = new Weapon(weaponName);
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 22:56









        MureinikMureinik

        184k22136202




        184k22136202













        • a monster shouldn't care about int day, int month, int year, I think it's better to pass a prepared MyDate object instead

          – Andrew Tobilko
          Nov 20 '18 at 22:58











        • @AndrewTobilko As far as I understood the question, the signature of the constructor is given. I don't think OP is free to change it.

          – Mureinik
          Nov 20 '18 at 22:58











        • @AndrewTobilko (although I do agree in principal)

          – Mureinik
          Nov 20 '18 at 22:59











        • Thank you!!! And yes, I can't change the original constructor, because it's defined by the question.

          – Shin Yu Wu
          Nov 21 '18 at 1:20



















        • a monster shouldn't care about int day, int month, int year, I think it's better to pass a prepared MyDate object instead

          – Andrew Tobilko
          Nov 20 '18 at 22:58











        • @AndrewTobilko As far as I understood the question, the signature of the constructor is given. I don't think OP is free to change it.

          – Mureinik
          Nov 20 '18 at 22:58











        • @AndrewTobilko (although I do agree in principal)

          – Mureinik
          Nov 20 '18 at 22:59











        • Thank you!!! And yes, I can't change the original constructor, because it's defined by the question.

          – Shin Yu Wu
          Nov 21 '18 at 1:20

















        a monster shouldn't care about int day, int month, int year, I think it's better to pass a prepared MyDate object instead

        – Andrew Tobilko
        Nov 20 '18 at 22:58





        a monster shouldn't care about int day, int month, int year, I think it's better to pass a prepared MyDate object instead

        – Andrew Tobilko
        Nov 20 '18 at 22:58













        @AndrewTobilko As far as I understood the question, the signature of the constructor is given. I don't think OP is free to change it.

        – Mureinik
        Nov 20 '18 at 22:58





        @AndrewTobilko As far as I understood the question, the signature of the constructor is given. I don't think OP is free to change it.

        – Mureinik
        Nov 20 '18 at 22:58













        @AndrewTobilko (although I do agree in principal)

        – Mureinik
        Nov 20 '18 at 22:59





        @AndrewTobilko (although I do agree in principal)

        – Mureinik
        Nov 20 '18 at 22:59













        Thank you!!! And yes, I can't change the original constructor, because it's defined by the question.

        – Shin Yu Wu
        Nov 21 '18 at 1:20





        Thank you!!! And yes, I can't change the original constructor, because it's defined by the question.

        – Shin Yu Wu
        Nov 21 '18 at 1:20













        2














        I would go at it like this:



        public class Monster{

        public String name;
        public MyDate dateOfBirth;
        public Weapon weapon;

        public Monster(String name, int day, int month, int year, String weaponName){
        this.name = name;
        this.dateOfBirth = new DateOfBirth(day, month, year);
        this.weapon = new Weapon(weaponName);
        }

        //second constructor for good measure
        public Monster(String name, DateOfBirth day, Weapon weapon){
        this.name = name;
        this.dateOfBirth = day;
        this.weapon = weapon;
        }

        //also relay all getters and setters to the underlying objects:
        public int getDay(){
        return dateOfBirth.getDay()
        }
        //...
        }


        You should definitely drop the redundant day, month and year, because they are stored in dayOfMonth.






        share|improve this answer






























          2














          I would go at it like this:



          public class Monster{

          public String name;
          public MyDate dateOfBirth;
          public Weapon weapon;

          public Monster(String name, int day, int month, int year, String weaponName){
          this.name = name;
          this.dateOfBirth = new DateOfBirth(day, month, year);
          this.weapon = new Weapon(weaponName);
          }

          //second constructor for good measure
          public Monster(String name, DateOfBirth day, Weapon weapon){
          this.name = name;
          this.dateOfBirth = day;
          this.weapon = weapon;
          }

          //also relay all getters and setters to the underlying objects:
          public int getDay(){
          return dateOfBirth.getDay()
          }
          //...
          }


          You should definitely drop the redundant day, month and year, because they are stored in dayOfMonth.






          share|improve this answer




























            2












            2








            2







            I would go at it like this:



            public class Monster{

            public String name;
            public MyDate dateOfBirth;
            public Weapon weapon;

            public Monster(String name, int day, int month, int year, String weaponName){
            this.name = name;
            this.dateOfBirth = new DateOfBirth(day, month, year);
            this.weapon = new Weapon(weaponName);
            }

            //second constructor for good measure
            public Monster(String name, DateOfBirth day, Weapon weapon){
            this.name = name;
            this.dateOfBirth = day;
            this.weapon = weapon;
            }

            //also relay all getters and setters to the underlying objects:
            public int getDay(){
            return dateOfBirth.getDay()
            }
            //...
            }


            You should definitely drop the redundant day, month and year, because they are stored in dayOfMonth.






            share|improve this answer















            I would go at it like this:



            public class Monster{

            public String name;
            public MyDate dateOfBirth;
            public Weapon weapon;

            public Monster(String name, int day, int month, int year, String weaponName){
            this.name = name;
            this.dateOfBirth = new DateOfBirth(day, month, year);
            this.weapon = new Weapon(weaponName);
            }

            //second constructor for good measure
            public Monster(String name, DateOfBirth day, Weapon weapon){
            this.name = name;
            this.dateOfBirth = day;
            this.weapon = weapon;
            }

            //also relay all getters and setters to the underlying objects:
            public int getDay(){
            return dateOfBirth.getDay()
            }
            //...
            }


            You should definitely drop the redundant day, month and year, because they are stored in dayOfMonth.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '18 at 23:03

























            answered Nov 20 '18 at 22:57









            leonardkraemerleonardkraemer

            3,45111532




            3,45111532























                1














                You could either create another constructor to accept MyDate and Weapon objects:



                public Monster(String name, MyDate birthDay, Weapon weapon) {
                //...
                }


                or in your constructor you could do:



                public Monster(String name, int day, int month, int year, String weaponName){ 
                this.weapon = new Weapon(weaponName);
                this.dateOfBirth = new MyDate(day, month, year);
                //...
                }


                To create the appropriate objects from the given parameters






                share|improve this answer




























                  1














                  You could either create another constructor to accept MyDate and Weapon objects:



                  public Monster(String name, MyDate birthDay, Weapon weapon) {
                  //...
                  }


                  or in your constructor you could do:



                  public Monster(String name, int day, int month, int year, String weaponName){ 
                  this.weapon = new Weapon(weaponName);
                  this.dateOfBirth = new MyDate(day, month, year);
                  //...
                  }


                  To create the appropriate objects from the given parameters






                  share|improve this answer


























                    1












                    1








                    1







                    You could either create another constructor to accept MyDate and Weapon objects:



                    public Monster(String name, MyDate birthDay, Weapon weapon) {
                    //...
                    }


                    or in your constructor you could do:



                    public Monster(String name, int day, int month, int year, String weaponName){ 
                    this.weapon = new Weapon(weaponName);
                    this.dateOfBirth = new MyDate(day, month, year);
                    //...
                    }


                    To create the appropriate objects from the given parameters






                    share|improve this answer













                    You could either create another constructor to accept MyDate and Weapon objects:



                    public Monster(String name, MyDate birthDay, Weapon weapon) {
                    //...
                    }


                    or in your constructor you could do:



                    public Monster(String name, int day, int month, int year, String weaponName){ 
                    this.weapon = new Weapon(weaponName);
                    this.dateOfBirth = new MyDate(day, month, year);
                    //...
                    }


                    To create the appropriate objects from the given parameters







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 20 '18 at 22:57









                    GBlodgettGBlodgett

                    10.3k42035




                    10.3k42035























                        0














                        I'm not exactly clear on what you're asking. If you are indicating that you must retain the Monster constructor as you have defined it, but store MyDate and Weapon objects in your Monster class, then the answer is simple.



                        Monster(String name, int day, int month, int year, String weaponName) {
                        this.name = name;
                        this.dateOfBirth = new MyDate(year, month, day);
                        this.weapon = new Weapon(weaponName);
                        }


                        I don't think this kind of coupling is a very good idea. Better to define your constructor like so:



                        Monster(String name, MyDate dateOfBirth, Weapon weapon) {
                        this.name = name;
                        this.dateOfBirth = dateOfBirth;
                        this.weapon = weapon;
                        }


                        and call the constructor like this:



                        Monster m = new Monster("Godzilla", new MyDate(2000,11,10), new Weapon("VenomThrower"));


                        Also, You might want to have a look at using java.time.LocalDate to replace your MyDate class.



                        If you have a fixed set of Weapons. You could redefine Weapon as an enum rather than a class



                        public enum Weapon {
                        VenomThrower, . . . ;
                        }


                        Then Weapon references will look like,



                        Weapon.VenomThrower





                        share|improve this answer




























                          0














                          I'm not exactly clear on what you're asking. If you are indicating that you must retain the Monster constructor as you have defined it, but store MyDate and Weapon objects in your Monster class, then the answer is simple.



                          Monster(String name, int day, int month, int year, String weaponName) {
                          this.name = name;
                          this.dateOfBirth = new MyDate(year, month, day);
                          this.weapon = new Weapon(weaponName);
                          }


                          I don't think this kind of coupling is a very good idea. Better to define your constructor like so:



                          Monster(String name, MyDate dateOfBirth, Weapon weapon) {
                          this.name = name;
                          this.dateOfBirth = dateOfBirth;
                          this.weapon = weapon;
                          }


                          and call the constructor like this:



                          Monster m = new Monster("Godzilla", new MyDate(2000,11,10), new Weapon("VenomThrower"));


                          Also, You might want to have a look at using java.time.LocalDate to replace your MyDate class.



                          If you have a fixed set of Weapons. You could redefine Weapon as an enum rather than a class



                          public enum Weapon {
                          VenomThrower, . . . ;
                          }


                          Then Weapon references will look like,



                          Weapon.VenomThrower





                          share|improve this answer


























                            0












                            0








                            0







                            I'm not exactly clear on what you're asking. If you are indicating that you must retain the Monster constructor as you have defined it, but store MyDate and Weapon objects in your Monster class, then the answer is simple.



                            Monster(String name, int day, int month, int year, String weaponName) {
                            this.name = name;
                            this.dateOfBirth = new MyDate(year, month, day);
                            this.weapon = new Weapon(weaponName);
                            }


                            I don't think this kind of coupling is a very good idea. Better to define your constructor like so:



                            Monster(String name, MyDate dateOfBirth, Weapon weapon) {
                            this.name = name;
                            this.dateOfBirth = dateOfBirth;
                            this.weapon = weapon;
                            }


                            and call the constructor like this:



                            Monster m = new Monster("Godzilla", new MyDate(2000,11,10), new Weapon("VenomThrower"));


                            Also, You might want to have a look at using java.time.LocalDate to replace your MyDate class.



                            If you have a fixed set of Weapons. You could redefine Weapon as an enum rather than a class



                            public enum Weapon {
                            VenomThrower, . . . ;
                            }


                            Then Weapon references will look like,



                            Weapon.VenomThrower





                            share|improve this answer













                            I'm not exactly clear on what you're asking. If you are indicating that you must retain the Monster constructor as you have defined it, but store MyDate and Weapon objects in your Monster class, then the answer is simple.



                            Monster(String name, int day, int month, int year, String weaponName) {
                            this.name = name;
                            this.dateOfBirth = new MyDate(year, month, day);
                            this.weapon = new Weapon(weaponName);
                            }


                            I don't think this kind of coupling is a very good idea. Better to define your constructor like so:



                            Monster(String name, MyDate dateOfBirth, Weapon weapon) {
                            this.name = name;
                            this.dateOfBirth = dateOfBirth;
                            this.weapon = weapon;
                            }


                            and call the constructor like this:



                            Monster m = new Monster("Godzilla", new MyDate(2000,11,10), new Weapon("VenomThrower"));


                            Also, You might want to have a look at using java.time.LocalDate to replace your MyDate class.



                            If you have a fixed set of Weapons. You could redefine Weapon as an enum rather than a class



                            public enum Weapon {
                            VenomThrower, . . . ;
                            }


                            Then Weapon references will look like,



                            Weapon.VenomThrower






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 0:09









                            Tom DrakeTom Drake

                            43738




                            43738






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402787%2fconstructor-has-different-parameter-type-with-datafield%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to change which sound is reproduced for terminal bell?

                                Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents