Hi Im writing my first program using methods and objects and constructors. I am supposed to validate a date using a daterec driver program that has the constructor with a default date of today, a constructor that accepts the user inputs for month day and year and my validate method which checks to see if date is valid and a tostring method to convert the date into a string and print which i have done for the most part. the problem is the program I need to write to test this daterec class. I have to create three objects one named today and one anyday created w no arguments and display aa valid date, then also a noday object which displays invalid dates. I have an example but I am confused on passing the information as well as the code to do it
I would like some examples of how for example to print the dateREC and print the 1, 1 2008 with the object today I dont know what I need to get it over there please help. I also want to take the inputs from th user validate or invalidate the date and print with the anyDay or noDay objects.
Edit by mw: Added Code Tags and removed SHOUTING from subject line. [ February 05, 2008: Message edited by: marc weber ]
Please use code tags round your posts; it makes them easier to read. You have all sorts of things going awry.
You are creating the same date because you are passing no arguments to the constructor. Look at the several calls to the DateRec constructor; they all go the the no-args version which gives 1st January. You might do better to put the bit about calling the Scanner back into DateTest, or even call it like this If you print out "good" you get "false" so your validate() method is doing something strange. Look closely at what you have written about if(day . . . Also why have you written < 2008? And don't use if-else blocks in such a method. You can write
Lose the "myToString" method and override the standard toString method, where you can say There are alternatives available. Then you can print the object directly like thisLose the return value from the validate() method; let it set "good" then rather than sayingyou sayYou haven't got the method signature for the main method in DateTest right. Look at it very carefully for tiny errors.
I hope I have helped you a bit. Good luck
Joined: Feb 04, 2008
Thanks for the help however I cannot seem to write the code needed to get the user inputs of month day and year into the constructor that accepts the values through the parameter list does anyone have any examples how I could take inputs in one class and assign them to a constructor. This should be that hard but I am lost any help would be appreciated
Joined: Oct 13, 2005
Pardon? You can try this in the main methodor you can put that sort of code inside the constructor, but you can't put it inside the constructor with arguments. Otherwise you get this sort of thingwhere you pass 8th February 2008 to the constructor then use totally different values to set the object up. I think you want the version with the Scanner in the main method.