Results 1 to 18 of 18
Thread: Help creating a Date class
- 04-29-2010, 03:01 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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)
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 = 1Java 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) { } }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.
-
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
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.Java Code:public static final int[] DAYS_IN_MONTH = { 31, 28, 31, 30, //.... etc... };
- 04-29-2010, 03:25 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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.
- 04-29-2010, 03:38 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
I'll just post up some pseudocode for you:
This should be easy enough to implement.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 }Ever seen a dog chase its tail? Now that's an infinite loop.
- 04-29-2010, 03:39 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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
- 04-29-2010, 03:53 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
I think a better approach would be to check for mismatches in user input, rather than incrementing the date. Something aking to:
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.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]);Ever seen a dog chase its tail? Now that's an infinite loop.
- 04-29-2010, 03:57 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
this is what i made. i havent run it yet, since i dont even have a main.
ill make sure in my input method they cant put more than 12 for month and more than the max daysJava 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++ } } }Last edited by Meta; 04-29-2010 at 04:00 AM.
- 04-29-2010, 04:35 AM #8
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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); } }
- 04-29-2010, 04:39 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 04-29-2010, 04:49 AM #10
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
ok. i fixed it. it works now. only thing is. wat the heck was i supposed to use the setdate method for.
- 04-29-2010, 05:01 AM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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:
The use it in your setDate method, and you don't have to worry about accidentally inputting a wrong date:Java Code:void makeRegular() { month = month%12; day = day%DAYS_IN_MONTH[month]; }
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:void setDate(int d, int m, int y) { day = d; month = m; year = y; makeRegular(); }
Java Code:boolean isLeapYear() { return year%4 == 0; }Ever seen a dog chase its tail? Now that's an infinite loop.
- 04-29-2010, 06:05 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
I second that. You can use it like
which eliminates more than half the code in advanceDate().Java Code:if (day == MONTH_ARRAY[month] + (isLeapYear() ? 1 : 0)) {
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.Java Code:public static final int[] MONTH_ARRAY = {0, 31, // etc
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)
- 04-29-2010, 06:41 AM #13
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Ack, now I can't get the image of Inspector Clouseau sitting at the computer going "Non statique? *rumble tumble* KATO, NOT NOW".
Originally Posted by pbrockway2 Ever seen a dog chase its tail? Now that's an infinite loop.
- 04-29-2010, 07:56 PM #14
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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.
I:\Documents\Spring 2010\Computer Science II\Lab4Date.java:138: non-static method setDate(int,int,int) cannot be referenced from a static contextJava 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 } }
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.
- 04-29-2010, 08:53 PM #15
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
You use a constructor to create an instance of Lab4Date, then you can use setDate() method on that object.
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:Lab4Date date = new Lab4Date(); date.setDate(1,1,2000);
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.Java Code:public class Lab4Date { public Lab4Date(int m, int d, int y) { setDate(m,d,y); } }Last edited by m00nchile; 04-29-2010 at 08:56 PM.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 04-29-2010, 09:22 PM #16
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
if i have this already
im able to do a.setDate(4,29,2010) and also able to do b.setDate(4,29,2010). why do both work?Java Code:Lab4Date a = new Lab4Date(); // call constructor with default values Lab4Date b = new Lab4Date(2,23,1991); // call constructor with argument list
even tho this may be repetitive stuff or w/e this is part of the assignment, but its confusing
- 04-29-2010, 09:52 PM #17
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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:
my constructors and setDate method: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 } }
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 }
- 04-30-2010, 09:28 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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
-
Dissertation idea
By prof.deedee in forum Java CertificationReplies: 4Last Post: 02-24-2010, 02:11 PM -
need an Idea ...
By Mekonom in forum AWT / SwingReplies: 43Last Post: 11-29-2009, 03:26 PM -
MP3 Player idea
By vinoth in forum New To JavaReplies: 3Last Post: 08-18-2009, 01:10 AM -
I have an idea !!
By HosHos in forum New To JavaReplies: 1Last Post: 08-12-2009, 06:38 AM -
i couid not get the idea:
By sivasayanth in forum New To JavaReplies: 2Last Post: 01-18-2008, 05:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks