Results 1 to 18 of 18
  1. #1
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default Help creating a Date class

    Ok. Im ignoring my other thread becuase its gotten too messy to follow. You said earlier i may be overthinking my program, but i feel like i need to do it that way. Basically, if you look in my advanceDate method, i need to check if they user enters the last day of a month, example 31 of january. If they do, i need to not only increment the day, but also the month, so i need it to say 3/1. so i think i need to be able to access the max value of a month, from an array of months. you know what im getting at, or im going to have sooo many ifs and elses. this is what i have so far. right now i only have february and leap years set up(or at least i think)

    Java Code:
    import java.text.*;  // needed for formatting
    import java.util.Scanner; // needed for input
    
    public class Lab4Days
    {
    	// class variable declarations
    	private int day;
    	private int month;
    	private int year;
    	
    	public Lab4Days() // constructor with default values
    	{
    		day = 0;
    		month = 0;
    		year = 0;
    	}
    	
    	public void setDate(int d, int m, int y) // sets date to specific values
    	{
    		day = d;
    		month = m;
    		year = y;
    	}
    	
    	public void inputDate()
    	{
    		Scanner scan = new Scanner (System.in);
    		System.out.print("Enter date as day month year: ");
    		day = scan.nextInt();
    		month = scan.nextInt();
    		year = scan.nextInt();		
    	}
    	
    	public void outputDate()
    	{
    		DecimalFormat dfOne = new DecimalFormat("00");
    		DecimalFormat dfTwo = new DecimalFormat("0000");
    		System.out.println("The date is: " + dfOne.format(day) + "/" + dfOne.format(month) + "/" + dfTwo.format(year)
    	}
    	
    	public void advanceDate()
    	{
    		if (month == 2)
    		{
    			if (day == 28)
    			{
    				if(year % 4 == 0)
    				{
    					if(year % 100 != 0 || year % 400 == 0)
    						{
    							day++;
    						}
    					else
    					{
    						month++;
    						day++;
    					}
    				else
    				{
    					month++;
    					day++;
    				}
    			else if( day > 28)
    			{
    				month++;
    				day++
    			}
    			else
    			{
    				day++
    			}
    		else
    		{
    			
    		}
    	}
    	
    	public static void main (String [] args)
    	{
    		
    	}
    }
    looks like what i have now would make the next month 3/30 or w/e instead of 3/1. should i make day++ day = 1
    Last edited by Fubarable; 04-29-2010 at 03:06 AM. Reason: Changed title: You really don't want just me answering this, believe me.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One way to solve this is to give your class a static int array that holds the number of days in each month and then use this array to help you decide whether to advance the day or to set the day to 1 and advance the month. The array could look something like

    Java Code:
    public static final int[] DAYS_IN_MONTH = {
      31, 28, 31, 30, //.... etc...
    };
    With obvious exceptions for the leap year (as you note), and the last day of the year. Note that this array is 0-based, meaning January is represented by the 0th item in the array, not the 1th item.

  3. #3
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    i put 0 as the 0th index. im confused on how i actually use it though in my advance date. so freaking confused :(. do i use a for loop or something. ergh so frustrating.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I'll just post up some pseudocode for you:
    Java Code:
    void incrementDate() {
      increment day by 1
      if day is greater than the value DAYS_IN_MONTH[month]
        set day to 1
        increment month by 1
        if month is greater than 12
         set month to 1
         increment year by 1
        endif
      endif 
    }
    This should be easy enough to implement.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    like i want to be able to do this:

    ask what the month is. they enter 1-12
    set month to the index of the array.
    if the value they put for day is equal to the value of that index, then add 1 to the month and set the day to 1.

    problem is. how do i do this. if month = 1 and day = value of index[1] then month++ and day = 1

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I think a better approach would be to check for mismatches in user input, rather than incrementing the date. Something aking to:
    Java Code:
    System.out.println("Enter month");
    Scanner sc = new Scanner(System.in);
    do {
      month = sc.nextInt();
    } while(month < 1 || month > 12);
    
    System.out.println("Enter day");
    do {
      day = sc.nextInt();
    } while(day < 1 || day > DAYS_IN_MONTH[month]);
    Just add in the leap year checking if user inputs 2 as the month and that should be it. Oh, and the code above doesn't check for InputMismatchException, and you should also inform the user if he inputs the wrong values.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    this is what i made. i havent run it yet, since i dont even have a main.

    Java Code:
    	public void advanceDate()
    	{
    		if (month != 2)
    		{
    			if (day == MONTH_ARRAY[month])
    			{
    				if (month == 12)
    				{
    					month == 1;
    					day == 1;
    					year++;
    				}
    				else
    				{
    					month++;
    					day++;
    				}
    			}
    			else
    			{
    				day++;
    			}
    		}
    		else
    		{
    			if (day == 28)
    			{
    				if(year % 4 == 0)
    				{
    					if(year % 100 != 0 || year % 400 == 0)
    						{
    							day++;
    						}
    					else
    					{
    						month++;
    						day == 1;
    					}
    				}
    				else
    				{
    					month++;
    					day == 1;
    				}
    			}
    			else if( day > 28)
    			{
    				month++;
    				day == 1;
    			}
    			else
    			{
    				day++
    			}	
    		}
    	}
    ill make sure in my input method they cant put more than 12 for month and more than the max days
    Last edited by Meta; 04-29-2010 at 04:00 AM.

  8. #8
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    Ok. I finished. And im pretty proud of what ive done, but its got some problems with it. Well, just one problem. If i enter 2 for month(february), and then try to enter 29 for days, it doesnt do anything, it sits until i enter a day less than 29. idk why. in my code, it says it increments the month by 1, and sets the day to 1.

    also. my setDate method isnt even used. whats the point of it? my teach told me to make a setDate method to set a date to specific values, but i do that with my constructor dont i?

    also can my advanceDate method be shorter, kinda long.

    Java Code:
    import java.text.*;  // needed for formatting
    import java.util.Scanner; // needed for input
    
    public class Lab4Date
    {
    	// class variable declarations
    	private int day;
    	private int month;
    	public static final int[] MONTH_ARRAY = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	private int year;
    	
    	public Lab4Date() // constructor with default values
    	{
    		month = 0;
    		day = 0;
    		year = 0;
    	}
    	
    	public Lab4Date(int m, int d, int y) // overloaded constructor
    	{
    		month = m;
    		day = d;
    		year = y;
    	}
    	
    	public void setDate(int m, int d, int y) // sets date to specific values
    	{
    		month = m;
    		day = d;
    		year = y;
    	}
    	
    	public void inputDate()
    	{
    		Scanner scan = new Scanner (System.in);
    		
    		System.out.println("Enter the month: ");
    		do
    		{
    			month = scan.nextInt();
    		} 
    		while(month < 1 || month > 12);
    		
    		System.out.println("Enter the day: ");
    		do 
    		{
      			day = scan.nextInt();
    		} 
    		while(day < 1 || day > MONTH_ARRAY[month]);
    		
    		System.out.println("Enter the year: ");
    		year = scan.nextInt();		
    	}
    	
    	public void outputDate()
    	{
    		DecimalFormat dfOne = new DecimalFormat("00");
    		DecimalFormat dfTwo = new DecimalFormat("0000");
    		System.out.println("The date is: " + dfOne.format(month) + "/" + dfOne.format(day) + "/" + dfTwo.format(year));
    	}
    	
    	public void advanceDate()
    	{
    		if (month != 2)
    		{
    			if (day == MONTH_ARRAY[month])
    			{
    				if (month == 12)
    				{
    					month = 1;
    					day = 1;
    					year++;
    				}
    				else
    				{
    					month++;
    					day = 1;
    				}
    			}
    			else
    			{
    				day++;
    			}
    		}
    		else
    		{
    			if (day == 28)
    			{
    				if(year % 4 == 0)
    				{
    					if(year % 100 != 0 || year % 400 == 0)
    						{
    							day++;
    						}
    					else
    					{
    						month++;
    						day = 1;
    					}
    				}
    				else
    				{
    					month++;
    					day = 1;
    				}
    			}
    			else if( day > 28)
    			{
    				month++;
    				day = 1;
    			}
    			else
    			{
    				day++;
    			}	
    		}
    	}
    			
    	public static void main (String [] args)
    	{
    		Lab4Date a = new Lab4Date();		//call constructor with default values
    		Lab4Date b = new Lab4Date(2,23,1991);	//call constructor with argument list
    		
    		int ans;
    		Scanner scan = new Scanner (System.in);
    		
    		System.out.println("Testing constructor with default values (0,0,0): ");
    		a.outputDate();
    		
    		System.out.println("Testing the constructor with initialized values of (2,23,1991): ");
    		b.outputDate();
    		
    		do
    		{
    			a.inputDate();
    			a.outputDate();
    			a.advanceDate();
    			System.out.println("One day later");
    			a.outputDate();
    			System.out.println("Continue? Enter 1 to accept. Enter any other number to quit.");
    			ans = scan.nextInt();
    		} 
    		while (ans == 1);
    	}
    }

  9. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The input method just checks if the values the user typed in are in the correct bounds. Your advanceDate() method has the day = 1 if day > DAYS_IN_MONTH[month] checking.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    ok. i fixed it. it works now. only thing is. wat the heck was i supposed to use the setdate method for.

  11. #11
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Setter and getter methods are always usefull. Suppose you want to increment the date a week, or a month, or a set number of days. Instead of calling the advanceDate() method a bajillion times, you can just set the date directly. Also, you could have a method that checks if the date is regular, for instance:
    Java Code:
    void makeRegular() {
      month = month%12;
      day = day%DAYS_IN_MONTH[month];
    }
    The use it in your setDate method, and you don't have to worry about accidentally inputting a wrong date:
    Java Code:
    void setDate(int d, int m, int y) {
      day = d;
      month = m;
      year = y;
      makeRegular();
    }
    Also, I'd suggest you break up your longer methods into helper methods, so instead of writting up the whole logic of leap year checking into your advanceDate() method, make a isLeapYear() method:
    Java Code:
    boolean isLeapYear() {
      return year%4 == 0;
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by m00nchile View Post
    make a isLeapYear() method
    I second that. You can use it like

    Java Code:
    if (day == MONTH_ARRAY[month] + (isLeapYear() ? 1 : 0))
    {
    which eliminates more than half the code in advanceDate().

    Java Code:
    public static final int[] MONTH_ARRAY = {0, 31, // etc
    If this is homework and you want to impress whoever is marking it, start the array at 31. (See the remarks made earlier about zero based arrays and ask if you are unsure. It's the index that's supposed to start at zero). Writing arrays the way you have resembles Pascal so your code ends up like it was written in a funny "Peter Sellers" accent. Zero based arrays are more professional.

    Why the constructor that sets the field values to zero? Is this date (which doesn't actually correspond to an actual date...) so significant that it deserves its own special constructor? If not, then it would be better to remove it: having a Date instance with bogus field values that haven't been set properly is just an accident waiting to happen. (Having said that this seems a common homework "requirement", although a silly one imho)

  13. #13
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by pbrockway2
    ...so your code ends up like it was written in a funny "Peter Sellers" accent...
    Ack, now I can't get the image of Inspector Clouseau sitting at the computer going "Non statique? *rumble tumble* KATO, NOT NOW".
    Ever seen a dog chase its tail? Now that's an infinite loop.

  14. #14
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    my teacher says my setDate method should do the same thing as my overloaded constructor, but use different values. i set it up the way she explained it to me. but it doesnt like it.

    Java Code:
    import java.text.*;  // needed for formatting
    import java.util.Scanner; // needed for input
    
    public class Lab4Date
    {
    	// class variable declarations
    	private int day;
    	private int month;
    	public static final int[] MONTH_ARRAY = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	private int year;
    	
    	public Lab4Date() // constructor with default values
    	{
    		month = 0;
    		day = 0;
    		year = 0;
    	}
    	
    	public Lab4Date(int m, int d, int y) // overloaded constructor
    	{
    		month = m;
    		day = d;
    		year = y;
    	}
    	
    	public void setDate(int m, int d, int y) // sets date to specific values
    	{
    		month = m;
    		day = d;
    		year = y;
    	}
    	
    	public void inputDate() // allows the user to enter a valid date
    	{
    		Scanner scan = new Scanner (System.in);
    		
    		System.out.println("Enter the month: ");
    		do
    		{
    			month = scan.nextInt();
    		} 
    		while(month < 1 || month > 12);
    		
    		System.out.println("Enter the day: ");
    		if (month != 2)
    		{
    			do 
    			{
      				day = scan.nextInt();
    			} 
    			while(day < 1 || day > MONTH_ARRAY[month]);	
    		}
    		else
    		{
    			do
    			{
    				day = scan.nextInt();
    			}
    			while(day < 1 || day > 29);
    		}
    		
    		System.out.println("Enter the year: ");
    		year = scan.nextInt();		
    	}
    	
    	public void outputDate() //outputs the date in the correct format
    	{
    		DecimalFormat dfOne = new DecimalFormat("00");
    		DecimalFormat dfTwo = new DecimalFormat("0000");
    		System.out.println("The date is: " + dfOne.format(month) + "/" + dfOne.format(day) + "/" + dfTwo.format(year));
    	}
    	
    	public void advanceDate() // increments the date by one. Does tests to determine for leap years, end of months, and end of years.
    	{
    		if (month != 2)
    		{
    			if (day == MONTH_ARRAY[month])
    			{
    				if (month == 12)
    				{
    					month = 1;
    					day = 1;
    					year++;
    				}
    				else
    				{
    					month++;
    					day = 1;
    				}
    			}
    			else
    			{
    				day++;
    			}
    		}
    		else
    		{
    			if (day == 28)
    			{
    				if(year % 4 == 0)
    				{
    					if(year % 100 != 0 || year % 400 == 0)
    						{
    							day++;
    						}
    					else
    					{
    						month++;
    						day = 1;
    					}
    				}
    				else
    				{
    					month++;
    					day = 1;
    				}
    			}
    			else if( day > 28)
    			{
    				month++;
    				day = 1;
    			}
    			else
    			{
    				day++;
    			}	
    		}
    	}
    			
    	public static void main (String [] args)
    	{
    		Lab4Date a = new Lab4Date();		// call constructor with default values
    		Lab4Date b = new Lab4Date(2,23,1991);	// call constructor with argument list
    		Lab4Date c = new setDate(4,29,2010); // call setDate method with specific date
    		
    		int ans;
    		Scanner scan = new Scanner (System.in);
    		
    		System.out.println("Testing constructor with default values (0,0,0): ");
    		a.outputDate(); // call outputDate method to show the default values
    		
    		System.out.println("Testing the constructor with initialized values of (2,23,1991): ");
    		b.outputDate(); // call the outputDate method to show initialized values 
    		
    		System.out.println("Testing the method with specific valyes of (4,29,2010");
    		c.outputDate(); // call the outputDate method to show specific values
    		
    		do
    		{
    			a.inputDate(); // calls the inputDate method to allow the user to enter their date
    			a.outputDate(); // calls the outputDate method to show the first date the user entered
    			a.advanceDate(); // calls the advanceDate method to increment the date by one day
    			System.out.println("One day later");
    			a.outputDate(); // calls the outputDate method to show the new date
    			System.out.println("Continue? Enter 1 to accept. Enter any other number to quit.");
    			ans = scan.nextInt(); 
    		} 
    		while (ans == 1); // continue the program until the user wants to quit
    	}
    }
    I:\Documents\Spring 2010\Computer Science II\Lab4Date.java:138: non-static method setDate(int,int,int) cannot be referenced from a static context
    Lab4Date c = setDate(4,29,2010); // call setDate method with specific date
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab4Date.java:138: incompatible types
    found : void
    required: Lab4Date
    Lab4Date c = setDate(4,29,2010); // call setDate method with specific date
    ^
    2 errors


    i tried just calling it without lab4date c =. i still get some errors.

  15. #15
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You use a constructor to create an instance of Lab4Date, then you can use setDate() method on that object.
    Java Code:
    Lab4Date date = new Lab4Date();
    date.setDate(1,1,2000);
    And also, if you've written up a setDate() method, don't copy and paste the code into the constructor, just use the setDate() method again:
    Java Code:
    public class Lab4Date {
      public Lab4Date(int m, int d, int y) {
        setDate(m,d,y);
      }
    }
    The most prominent reason for making a procedure into a separate method is if you need to call it multiple times. Instead of writing the same code multiple times, just call the method.
    Last edited by m00nchile; 04-29-2010 at 08:56 PM.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  16. #16
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    if i have this already

    Java Code:
    Lab4Date a = new Lab4Date();		// call constructor with default values
    Lab4Date b = new Lab4Date(2,23,1991);	// call constructor with argument list
    im able to do a.setDate(4,29,2010) and also able to do b.setDate(4,29,2010). why do both work?

    even tho this may be repetitive stuff or w/e this is part of the assignment, but its confusing

  17. #17
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    anyone know why a.setDate(4,29,2010) and b.setDate(4,29,2010) both do the same thing? i can use either one?

    my main:
    Java Code:
    public static void main (String [] args)
    	{
    		Lab4Date a = new Lab4Date();		// call constructor with default values
    		Lab4Date b = new Lab4Date(2,23,1991);	// call constructor with argument list
    		
    		int ans;
    		Scanner scan = new Scanner (System.in);
    		
    		System.out.println("Testing constructor with default values (0,0,0): ");
    		a.outputDate(); // call outputDate method to show the default values
    		
    		System.out.println("Testing the constructor with initialized values of (2,23,1991): ");
    		b.outputDate(); // call the outputDate method to show initialized values 
    		
    		System.out.println("Testing the method with specific values of (4,29,2010): ");
    		a.setDate(4,29,2010); // call setDate method with specific date
    		
    		do
    		{
    			a.inputDate(); // calls the inputDate method to allow the user to enter their date
    			a.outputDate(); // calls the outputDate method to show the first date the user entered
    			a.advanceDate(); // calls the advanceDate method to increment the date by one day
    			System.out.println("One day later");
    			a.outputDate(); // calls the outputDate method to show the new date
    			System.out.println("Continue? Enter 1 to accept. Enter any other number to quit.");
    			ans = scan.nextInt(); 
    		} 
    		while (ans == 1); // continue the program until the user wants to quit
    	}
    }
    my constructors and setDate method:

    Java Code:
    public Lab4Date() // constructor with default values
    	{
    		month = 0;
    		day = 0;
    		year = 0;
    	}
    	
    	public Lab4Date(int m, int d, int y) // overloaded constructor
    	{
    		month = m;
    		day = d;
    		year = y;
    	}
    	
    	public void setDate(int m, int d, int y) // sets date to specific values
    	{
    		month = m;
    		day = d;
    		year = y;
    		outputDate(); // calls the outputDate method to show the specified date
    	}

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Both 'a' and 'b' are instances of your Lab4Date.
    The definition (ie class) of Lab4Date says you can create an instance of it either with no parameters (the empty constructor) or by providing the date as three integers (the constructor with d, m and y).

    So far so good.

    But it also says that you can set the date of a Lab4Date instance, using the setDate(d,m,y) method. So you can setDate on any instance of Lab4Date.

    Simples, as those meerkats say.

Similar Threads

  1. Dissertation idea
    By prof.deedee in forum Java Certification
    Replies: 4
    Last Post: 02-24-2010, 02:11 PM
  2. need an Idea ...
    By Mekonom in forum AWT / Swing
    Replies: 43
    Last Post: 11-29-2009, 03:26 PM
  3. MP3 Player idea
    By vinoth in forum New To Java
    Replies: 3
    Last Post: 08-18-2009, 01:10 AM
  4. I have an idea !!
    By HosHos in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 06:38 AM
  5. i couid not get the idea:
    By sivasayanth in forum New To Java
    Replies: 2
    Last Post: 01-18-2008, 05:52 PM

Posting Permissions

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