This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Why does my constructor always go to default date?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add some debug System.out.println() calls in there so you can see (and if needed post here) what route through the code a run is taking.
Include in those println's relevant variable values.
 
Marshal
Posts: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Thank you for using the code button(←link: please read) but you didn't quite get it right. Since you are new, I have corrected the code tags, and doesn't it look better

Put a print statement in the validate day method, and see what you get for the value of _year. See if you can work out what has gone wrong with your constructor before I let you out of your misery.

Don't say if (xxx) return true; else return false; or similar. Simply return the expression.Don't start variable names with _. Write this instead:-[edit]Lots of speillin' corrections.
 
Sheriff
Posts: 17316
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because your check for valid day will always return false. Understand when the _month variable is set to understand why you have a bug.

The convention of using an underscore (_) as a prefix to field names is not very good. I don’t know why students are still being taught this. Don’t do it. As Campbell advised, use this. instead to qualify field names vs. similar parameter and local variable names.
 
ran milly
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I updated my isValidD method but it's still making some problems for me.. will accept dates such as april 31st or feb 30th. where did i go wrong?
 as for the underscore variables,  in my assignment requirements it has to be private..


 private boolean isValidD(int day)
   {
       int year = this.getYear();
       int month = this.getMonth();
       if (day < MIN_DAY || day > MAX_LONG_MONTH_DAY)
           return false;
       switch (month)
       {
           case 1:return true;
           case 2: if ((leapYear(year)) )
               return (day <= MAX_LEAP_FEB );
           return (day <= MAX_NORMAL_FEB );  
           case 3:return true;
           case 4:return (day <= MAX_MEDIUM_MONTH_DAY);
           case 5:return true;
           case 6:return (day <= MAX_MEDIUM_MONTH_DAY);
           case 7:return true;
           case 8:return true;
           case 9:return (day <= MAX_MEDIUM_MONTH_DAY);
           case 10:return true;
           case 11: return (day <= MAX_MEDIUM_MONTH_DAY);
           case 12: return true;
           default: return true;
       }

   }
/** gets year
    * @return the year
    */
   public int getYear(){
       return _year;
   }

   /** gets month
    * @return the month
    */
   public int getMonth(){
       return _month;
   

   /**checks if leap year
    * @param year the year
    * @return true if leap year
    */
   private boolean leapYear (int year)
   {
       return (((year%4==0) && (year%100!=0)) || year%400==0);
   }
 
Junilu Lacar
Sheriff
Posts: 17316
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because your method will always return true now as long as day is between 1 and 31, inclusive. The month variable evaluated by the switch statement will always have a value of 0. You need to understand why this is so.
 
Junilu Lacar
Sheriff
Posts: 17316
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problems you are seeing originate in this constructor. The _month field will not be assigned a value until after the call to isValidD(day).
 
That feels good. Thanks. Here's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic