Results 1 to 8 of 8
  1. #1
    YiBoog is offline Member
    Join Date
    Nov 2009
    Location
    City of Angels
    Posts
    7
    Rep Power
    0

    Default constructor check

    Greetings~ Below you will see my completed code for my date program. However, when I enter the dates it does not produce the same dates entered but returns 00/00/0. Can someone please assist me on where I may be wrong? I am thinking its the constructor but it looks fine the way its set up...thoughts???

    Java Code:
    public class Date
    {
    	private int month; // 12 months in a year
    	private int day; // different days in month pending leap year
    	private int year; // users year
    		
    	public Date(int myDay, int myMonth, int myYear)
    	{
    		if (myDay < 1 || myDay > monthDays(myMonth))
    		{
    			myDay = 0;
    			day = myDay;
    		}
    		if (myMonth < 1 || myMonth > 12)
    		{
    			myMonth = 0;
    			month = myMonth;			
    		}
    		if (myYear < 1900 || myYear > 2200)
    		{
    			myYear = 0;
    			year = myYear;
    		}		
    	} // end Date constructor
    	
    	//Confirm the months in a year and day output
    	private int monthDays(int myMonth)
    	{
    		if (myMonth == 2)
    			return 28; //February has only 28 days out of the year excluding leap year
    		if (myMonth == 4 || myMonth == 6 || myMonth == 9 || myMonth == 11)
    			return 30; //These 4 months have only 30 days out of the year
    		return 31; //return 31 for the remainder of the months
    				
    	} // end monthDays 
    	
    	public int getDay()
    	{
    		return day;
    	}
    	
    	public int getMonth()
    	{
    		return month;
    	}
    	
    	public int getYear()
    	{
    		return year;
    	}
    	
    	public void nextDay()
    	{
    		if(day == monthDays(month))
    		{
    			day = 1;
    			month++;
    		}
    		else day++;
    		
    		if(month == 13)
    		{
    			month = 1;
    			year++;
    		}
    	}//end nextDay()
    	
    	// return a String of the form month/day/year
    	public String toString()
    	{
    		String theDate = "";
    		
    		if(day < 10)
    			theDate = theDate + "0";
    		theDate = theDate + day;
    		theDate = theDate + "/";
    		
    		if(month < 10)
    			theDate = theDate + "0";
    		theDate = theDate + "/";
    		theDate = theDate + year;
    		
    		return theDate;
    	} // end toString
    	
    	public static void main(String[] args)
    	{
    		Date myDate = new Date(14,11,1999);
    		System.out.println(myDate);
    		
    		myDate.nextDay();
    		System.out.println(myDate);
    	}
    
    
    }
    respectfully,

    Boog

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You need to set the values of those instance variables in your constructor. Right now they never have anything but the default initialisation value which is 0.

  3. #3
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    Hi YiBoog,

    You are correct in thinking the problem is in the constructor.
    The assignments should not be inside of the conditonal statements.
    They should come after them...

    Java Code:
    	public Date(int myDay, int myMonth, int myYear)
    	{
    		if (myDay < 1 || myDay > monthDays(myMonth))
    		{
    			myDay = 0;
    			[B]day = myDay;[/B]
    		}
    	...

    should be
    Java Code:
    	public Date(int myDay, int myMonth, int myYear)
    	{
    		if (myDay < 1 || myDay > monthDays(myMonth))
    		{
    			myDay = 0;
    		}
    		[B]day = myDay;[/B]
    	...

    As you can see, in your orginal code the assignment is only made if myDay is less than 1 or greater than the number of days in a month.

    You may wonder: "if the values are not assigned, then why do they have the value of zero?"
    This is because instance-variables are initialized to default values when they are not assigned in a constructor.
    The default value for a instance-variable of type int is zero.
    For boolean it is false, and object references are given the default value of null.
    Last edited by Turtle; 11-19-2009 at 01:23 AM.

  4. #4
    YiBoog is offline Member
    Join Date
    Nov 2009
    Location
    City of Angels
    Posts
    7
    Rep Power
    0

    Default constructor check

    Thanks turtle~ that took care of the print part for my main...however, another problem arose after running the program, the month did not print...any thoughts??? I made some changes to the code to print in a mm/dd/yyyy format...It doesn't take my month from the main and convert it, or does it???

    Here is my new code:

    Java Code:
    public class Date
    {
    	private int month; // 12 months in a year
    	private int day; // different days in month pending leap year
    	private int year; // users year
    		
    	public Date(int myMonth, int myDay, int myYear)
    	{
    		if (myDay < 1 || myDay > monthDays(myMonth))
    		{
    			myDay = 0;
    		}
    		else
    			day = myDay;
    		
    		if (myMonth < 1 || myMonth > 12)
    		{
    			myMonth = 0;
    		}
    		else
    			month = myMonth;			
    		
    		if (myYear < 1900 || myYear > 2200)
    		{
    			myYear = 0;
    		}
    		else
    			year = myYear;
    				
    	} // end Date constructor
    	
    	//Confirm the months in a year and day output
    	private int monthDays(int myMonth)
    	{
    		if (myMonth == 2)
    			return 28; //February has only 28 days out of the year excluding leap year
    		if (myMonth == 4 || myMonth == 6 || myMonth == 9 || myMonth == 11)
    			return 30; //These 4 months have only 30 days out of the year
    		return 31; //return 31 for the remainder of the months
    				
    	} // end monthDays 
    	
    	public int getDay()
    	{
    		return day;
    	}
    	
    	public int getMonth()
    	{
    		return month;
    	}
    	
    	public int getYear()
    	{
    		return year;
    	}
    	
    	public String toString()
    	{
    		String theDate = "";
    		
    		if(month < 10)
    			theDate = theDate + "0";
    		theDate = theDate + "/";
    		
    		if(day < 10)
    			theDate = theDate + "0";
    		theDate = theDate + day;
    		theDate = theDate + "/";
    		theDate = theDate + year;
    		
    		return theDate;
    	} // end toString
    	
    	public void nextDay()
    	{
    		if(day == monthDays(month))
    		{
    			day = 1;
    			month++;
    		}
    		else day++;
    		
    		if(month == 13)
    		{
    			month = 1;
    			year++;
    		}
    	}//end nextDay()
    	
    	// return a String of the form month/day/year
    	
    	
    	public static void main(String[] args)
    	{
    		Date myDate = new Date(11,28,1988);
    		System.out.println(myDate);
    		
    		//myDate.nextDay();
    		//System.out.println(myDate);
    	}
    
    
    }
    AND here is what it prints~

    Java Code:
    /28/1988
    respectfully,

    Boog
    Last edited by YiBoog; 11-19-2009 at 07:38 AM.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    In your toString method you never actually add the month to the date string.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    If you follow that toString() method step by step you can see where that String result comes from. btw, your constructor still isn't correct; hint: remove all those 'else' keywords because you want to do this:

    Java Code:
    if (<parameter is not correct>)
       parameter= <some dummy but correct value>;
    member= parameter; // it always contains a correct value
    kind regards,

    Jos

  7. #7
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    Use GregorianCalendar and you don't have to do any of that

  8. #8
    YiBoog is offline Member
    Join Date
    Nov 2009
    Location
    City of Angels
    Posts
    7
    Rep Power
    0

    Default constructor check

    Ahhhhh yes, the toString()!!! Thank you for that!!! Now it prints like it should~:):)

    Respectfully,

    Boog:D

Similar Threads

  1. Don't know what to check for
    By Chasingxsuns in forum New To Java
    Replies: 8
    Last Post: 08-26-2009, 05:59 AM
  2. pls check it
    By aRTx in forum New To Java
    Replies: 0
    Last Post: 03-24-2009, 02:35 PM
  3. Check box tag
    By elizaabru in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-26-2008, 02:37 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •