Given the pre-defined Date class below, add an instance method named addWeeks to
ID: 3580434 • Letter: G
Question
Given the pre-defined Date class below, add an instance method named addWeeks to the Date class. The addweeks method accepts an integer as a parameter and shifts the data represented by the Date object by that many weeks. A week is considered to be exactly 7 days. You may assume the value passed is nonnegative. For example, if the following Date (9/19) is declared in client code: Date d = new Date (9, 19); The following calls to the addWeeks method would modify the Date object's state indicated in the comments. d.addWeeks(1); // adds 1 week (7 days) to d, becoming 9/26 d. addWeeks (5); // adds 5 weeks (35 days) to d, becoming 10/31 public class Date { private int day; private int month; public Date() { } public Date(int month, int day) { } public int getMonth() { } public int getDay() { } public void setDate(int month, int day) {} public String toString() { } public boolean equals(Date d) { } public int daysInMonth() {}Explanation / Answer
public void addWeek(int num)
{
int days = num*7;
for(int i=0;i<days;i++)
{
day = day+1;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(day==31)
{
day=1;
month++;
if(month==12)
{
month=1;
}
}
}
else if(month==4||month==6||month==9||month==11)
{
if(day==30)
{
day=1;
month++;
}
}
else if(month==2) {
if(day==28) {
day=1;
month++:
}
}
}
}
Related 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.