In this chapter, the class Date was designed to implement the date in a program,
ID: 3584296 • Letter: I
Question
In this chapter, the class Date was designed to implement the date in a program, but the method setDate and the constructor with parameters do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the method setDate and the constructor with parameters so that the values of month, day, and year are checked before storing the date into the data members. Add a method is LeapYear to check whether a year is a leapyear. Then, write and test program to test your class. Please help need this in Java languageExplanation / Answer
import java.lang.Math; public class Date { /* Let's do this thing properly and set up a look-up table * which will be used in to return month as string bit at * the end. We'll store the months in an array of type * String and as computers count from zero, our first entry * will be monthToString[0], returning January. */ public static String [] monthToString= { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; /* I'm going to use a look-up table for the number of days * in each month as follows: */ public static int [] noOfDays= { 31,28,31,30,31,30,31,31,30,31,30,31 }; /* I'm going to attempt to calculate the day entered based * on the numeric date entered (oh no! Maths :-( !!!) so, I * need a look-up table for that... Extra day added for * leap-years (ie, anything after a legal leap-year has an * off-set), this first table is for the 20th Century, ie, * a 19 perfix on the year: */ public static String [] daysOfWeek20th= { "Mon","Tue","Wed","Thu", "Fri","Sat","Sun","Mon" }; /* And this is the corrosponding look-up table for the 21st * century boy! */ public static String [] daysOfWeek21st= { "Sun","Mon","Tue","Wed","Thu", "Fri","Sat","Sun" }; /* Here are the month codes! */ public static int [] monthCodes= { 6,2,2,5,0,3,5,1,4,6,2,4 }; /* And we need to declare some variables for working out the day: */ public static float a, x, y, z; public static int v, c; /* Here is a variable of type char that'll force a line-feed * when using System.out.print ("text"+ln+"more text"); */ public static char ln=13; /* The following variable of type String is also used in * the sub-routine that returns the month as string */ public static String mon=""; /* Okay, here's some variables of type integer (whole numbers) * which will be used in the constructor thingy */ private int day, month, year; /* Error catching for type date in DD/MM/YY format to * check for legal dates, assuming a 19xx or 20xx prefix*/ public Date (int dd, int mm, int yy) { day=dd; month=mm; year=yy; if (month12) { System.out.println("?Month not valid error"+ln+"Reset to default value."); month=01; } if (day31) { System.out.println("?Day not valid error"+ln+"Reset to default value."); day=01; } /* As there is no easy way to store a leap-year in a look-up table * I'm going to check for the 29th February first and validate it * later. Of course, this is advanced programming, isn't it? */ if (day>29 && month==2) { System.out.println("?Day not valid error"+ln+"Except for leap-years, Feburary has only 28 days."); day=01; } /* Now, let's check to see if a legal leap-year has been entered * by the user, shall we? Oh, we need to do some maths. */ if (day==29 && month==2 && ((year-2000)%4!=0)) { System.out.println("?Date not valid error"+ln+"Not a leap-year."); day=28; } /* So, we've tested for a valid leap-year, the only thing that could * trip up our array above for the numbers of days in each month. */ if (daynoOfDays[month-1] && month!=2) { System.out.println("?Date not valid error"+ln+"Not a legal date."); day=01; } /* That's a whole lot more efficient and advanced than having a * 'switch case' or a load of 'if' statements checking for each * condition, isn't it? Okay, I'd use a switch case if I thought * that the person maintaining the Java code wasn't very good and needed * it to be readable, but if you use comments correctly then you * don't necessarily need code to be more readable, should you? */ } /* This will initialise the date and then we'll do some stuff wid it */ public Date() { /* Because I've gone slightly over-board on the error checking, * I need to initialise the date to 1,1,0 (Day, Month, Year) otherwise * ?Day not valid error and ?Month not valid error is thrown back at * the user, at least on the command line interface (ie, DOS). * Still, you can't be too diligent! */ this(1,1,0); } /* This takes a copy of the date. */ public Date (Date other) { this (other.day, other.month, other.year); } /* This method will return the monthAsString, as suggested by its' name */ public String monthAsString() { /* This is very efficient because it has in-built error checking * actually without testing for loads of conditions */ if (month>0 && monthRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.