Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3998050
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:28:38+00:00 2026-05-20T07:28:38+00:00

I wrote an if statement that should write different output depending on the data.

  • 0

I wrote an if statement that should write different output depending on the data. It works if int y = 2000, m = 5, d = 06;, however it doesn’t output the correct value when int y = 2889, m = 44, d = 16;.

This is my code. Could someone please help me to understand what is wrong.

public class Date1 {

    private int year = 1; // any year
    private int month = 1; // 1-12
    private int day = 1; // 1-31 based on month

    //method to set the year
    public void setYear(int y) {
        if (y <= 0) {
            System.out.println("That is too early");
            year = 1;
        }

        if (y > 2011) {
            System.out.println("That year hasn't happened yet!");
            y = 2011;
        } else {
            year = y;
        }
    }

    public int setMonth(int theMonth) {
        if ( theMonth > 0 && theMonth <= 12 ) { // validate month
            return theMonth;
        } else { // month is invalid 
            System.out.printf("Invalid month (%d) set to 1.", theMonth);
            return 1; // maintain object in consistent state
        } // end else   
    }

    public int setDay( int theDay) {
        int[] daysPerMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        // check if day in range for month
        if ( theDay > 0 && theDay <= daysPerMonth[ month ] ) {
            return theDay;
        }

        // check for leap year
        if ( month == 2 && theDay == 29 && ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ) ) {
            return theDay;
        }

        System.out.printf( "Invalid day (%d) set to 1.", theDay );
        return 1;  // maintain object in consistent state 
    }

    //method to return the year
    public int getYear() {
        return year; 
    }

    //method to return the month
    public int getMonth(){
        return month; 
    }

    //method to return the day 
    public int getDay(){
        return day; 
    }

    // return a String of the form year/month/day
    public String toUniversalStringTime() { 
        return String.format( "The date using a default constructor %d/%d/%d \n", getYear(), getMonth(), getDay() ); 
    } // end toUniversalStringTime
}

public class Date1Test {

    public static void main(String[] args) {        
        int y = 2000, m = 5, d = 06;        

        Date1 d1 = new Date1(); //create a new object

        System.out.println(d1.toUniversalStringTime()); //call toUniversalStringTime()

        System.out.printf("The date I created is %d/%d/%d \n", y , m , d);  
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:28:39+00:00Added an answer on May 20, 2026 at 7:28 am

    I don’t see anywhere in your code where you are calling the setDay, setMonth or setYear methods, so I would expect the call to toUniversalStringTime to always print

    "The date using a default constructor 111 \n"
    

    Then after that call you print it again manually using the values for y, m and d

    "The date I created is 200056 \n"
    

    You need to call the set methods on the d1 object after creation, or pass in the parameters to a constructor to set them, e.g.

    d1.setYear(y);
    d1.setMonth(m);
    d1.setDay(d);
    

    but please pay attention to some of the other comments that have been made with regards to refactoring your code, because as has been mentioned, each of your setter methods have fundamental flaws in them that need to be fixed first.

    Other general notes to your code:

    In your setYear method you are using the value of y to update the year variable of the object, but in the second if:

    if (y > 2011) {
        System.out.println("That year hasn't happened yet!");
        y = 2011;
    }
    

    you are actually setting y to 2011 rather than year, so this will have no effect.

    For some reason in your setMonth method you are not actually setting the month, but you are just validating the value that is passed in, i.e. if the value is not between 1 and 12 you return 1. So the code doesn’t match the name of the method and you should change one or the other.

    Your setDay method is the same as setMonth in that it doesn’t actually set the day, just validates it. But what’s even worse here is that the call to setDay depends heavily on the month and year already having been set, since you use the month and year variables to determine whether or not the day is valid. This means that setDay must only be called after setMonth and setYear, otherwise you will always default to checking against January 0001 (since month and year are set to 1 by default).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need write an update statement that used multiple tables to determine which rows
I wrote a windows service using VB that read some legacy data from Visual
I have a problem with going over an if statement that the code should
I know that with mysql you can write SQL statements into a .sql file
I want to re-write a method that has way too many nested if statements.
I just wrote an if statement in the lines of if (value == value1
How can I write an insert statement which includes the & character? For example,
I'm trying to write an iterator for results from a PDO statement but I
I've a python script which works just as it should, but I need to
In my Python application, I need to write a regular expression that matches a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.