Question about Java constructor.

Asked 2 years ago, Updated 2 years ago, 22 views

Hello! The code below is the code that outputs a valid date or not via isValid() if the constructor does not initialize to a date that meets the conditions.

For example, if you created an instance as of February 30, 2018, running the isValid() method results in a wrong output.

What I want to ask is that if you do setYear setMonth setDay in the constructor, you get the desired value In the order setDay setMonth setYear, an invalid value is printed.

Can you tell me why the order of use of the setter method in the constructor affects the results?

public class MyDate {
    private int day;
    private int month;
    private int year;
    boolean isValid=true;

    public MyDate(int day, int month, int year) {
        setYear(year);
        setMonth(month);
        setDay(day);
    }
    public int getDay() {
        return day;
    }


    public void setDay(int day) {
        switch(month) {
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                if (day <0 || day >31) {
                    isValid = false;
                }
                else {
                    this.day = day;
                }
                break;
            case 4: case 6: case 9: case 11:
                if (day <0 || day >30) {
                    isValid = false;
                }
                else {
                    this.day = day;
                }
                break;
            case 2:

                    if (day <0 || day >28) {
                        isValid = false;
                    } 
                    else {
                        this.day = day;
                    }

                break;
            default:
                isValid = false;
        }
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        if(month>=1 && month<=12)
            this.month = month;
        else
            isValid=false;
    }



    public int getYear() {
        return year;
    }

    public void setYear(int year) {

        this.year = year;
    }




    public String isValid() {
        if(isValid==true)
            return "Right";
        else
            return "Wrong";
    }
}

I'll edit it and upload it! Below is the code that creates an instance for the above class.

public class MyDateTest {

    public static void main(String[] args) {
        MyDate d1=new MyDate(30,2,2018);
        MyDate d2=new MyDate(30,3,2018);

        System.out.println ("must be 30 February 2018");
        System.out.println(d1.getYear());
        System.out.println(d1.getMonth());
        System.out.println(d1.getDay());
        System.out.println(d1.isValid());

        System.out.println ("must be 30 March 2018");
        System.out.println(d2.getYear());
        System.out.println(d2.getMonth());
        System.out.println(d2.getDay());
        System.out.println(d2.isValid());
    }

java

2022-09-21 14:34

2 Answers

This is because the setDay() method is checking the private variable month.

If setMonth() does not run before setDay(), the month value, which is still empty in setDay() will switch... It doesn't go through the case door properly.


2022-09-21 14:34

Didn't you test it by changing the name of the setter while leaving the variables that will go into each setter? There seems to be no problem with the implementation.


2022-09-21 14:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.