java - If statement not working correctly for dates -


i wrote if statement should write different output depending on data. works if int y = 2000, m = 5, d = 06;, doesn't output correct value when int y = 2889, m = 44, d = 16;.

this code. please me understand wrong.

public class date1 {      private int year = 1; // year     private int month = 1; // 1-12     private int day = 1; // 1-31 based on month      //method set year     public void setyear(int y) {         if (y <= 0) {             system.out.println("that 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 invalid              system.out.printf("invalid month (%d) set 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 month         if ( theday > 0 && theday <= dayspermonth[ month ] ) {             return theday;         }          // check 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 1.", theday );         return 1;  // maintain object in consistent state      }      //method return year     public int getyear() {         return year;      }      //method return month     public int getmonth(){         return month;      }      //method return day      public int getday(){         return day;      }      // return string of form year/month/day     public string touniversalstringtime() {          return string.format( "the date using 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 new object          system.out.println(d1.touniversalstringtime()); //call touniversalstringtime()          system.out.printf("the date created %d/%d/%d \n", y , m , d);       } } 

i don't see anywhere in code calling setday, setmonth or setyear methods, expect call touniversalstringtime print

"the date using default constructor 111 \n" 

then after call print again manually using values y, m , d

"the date created 200056 \n" 

you need call set methods on d1 object after creation, or pass in parameters constructor set them, e.g.

d1.setyear(y); d1.setmonth(m); d1.setday(d); 

but please pay attention of other comments have been made regards refactoring code, because has been mentioned, each of setter methods have fundamental flaws in them need fixed first.

other general notes code:

in setyear method using value of y update year variable of object, in second if:

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

you setting y 2011 rather year, have no effect.

for reason in setmonth method not setting month, validating value passed in, i.e. if value not between 1 , 12 return 1. code doesn't match name of method , should change 1 or other.

your setday method same setmonth in doesn't set day, validates it. what's worse here call setday depends heavily on month , year having been set, since use month , year variables determine whether or not day valid. means setday must called after setmonth , setyear, otherwise default checking against january 0001 (since month , year set 1 default).


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -