It is very close to getting to work, but I am still unable to get the right output...Anyone can help me please?
Here are the codes:
Date class:
/*------------------------------------------------------------------------------------
Name: Mahesh Panta
File Name: Date.java
Course Section: 1337.001
Purpose: To write a class for a Date Object
--------------------------------------------------------------------------------------*/
public class Date
{
private int month; //value of month as an integer
private int day; //value of day as an integer
private int year; //value of year as an integer
public Date ( int dateMonth, int dateDay, int dateYear ) //the constructor
{ //with the parameter
month = dateMonth;
day = dateDay;
year = dateYear;
} //end the constructor with the parameter
public Date() // default constructor
{
month = 0;
day = 0;
year = 0;
}
public void setMonth( int dateMonth ) //method setMonth
{
month = dateMonth;
}
public void setDay (int dateDay ) //method setDay
{
day = dateDay;
}
public void setYear( int dateYear ) //method setYear
{
year = dateYear;
}
public int getMonth() //method getMonth
{
return month;
}
public int getDay() //method getDay
{
return day;
}
public int getYear() //method getYear
{
return year;
}
public void addMonth(int m)
{
month = month + m;
if (month>12)
{
month-=12;
addYear(1);
}
}
public void addDay(int d)
{
day = d + 1; //add 1 to the day and assign it for day
if (day>28)
{
if (month==2)
{
day-=28;
addMonth(1);
}
else
if ((month==4||month==6||month==9||month==11)&&day>30)
{
day-=30;
addMonth(1);
}
else
if (day>31)
{
day-=31;
addMonth(1);
}
} //add 1 to the day and assign it for day
}
public void addYear(int y)
{
//year = y + 10;
//year = year + y;
year+=y;
}
/*public String toString()
{
return month + "/"+ day + "/" + year; //return the value of
}*/
//concatenated month, day, and year
public String toString()
{
String d=""+day,m=""+month,y=""+year;
if (d.length()<2)
d="0"+d;
if (m.length()<2)
m="0"+m;
while (y.length()<4)
y="0"+y;
return m + "/"+ d + "/" + y; //return the value of
} //concatenated month, day, and year
}
TestDate:
/*----------------------------------------------------------------
Name: Mahesh Panta
File Name: Date.java
Course Section: 1337.01
Purpose: To write a Class TestDate that will test the Date Class
???Assume the following Test Data for Phase I and II
o Birthday is 9/30/1986
o Default date was set to 6/26/1960
o One day is added to birthday Date Object
o Six months is added to birthday Date Object
o 10 years is added to birthday Date Object
-----------------------------------------------------------------*/
public class TestDate
{
public static void main(String args[])
{
Date date1 = new Date(); //construct a date1 object
Date date2 = new Date(9,30,1986); //construct a date2 object of date of birth
System.out.println(date1.toString()); //print date1, toString called
System.out.println(date2.toString()); //print date2, toString called
date1.setMonth(6); //set the default month 6
date1.setDay(26); //set the default day 26
date1.setYear(1960); //set the default year 1960
System.out.println(date1.toString()); //print the default date1
date2.addMonth(7); //addMonth = month of birth(7) + 6
System.out.println(date2.toString()); //print
date2.addDay(7); //addDay = day of birth (7) + 1
System.out.println(date2.toString()); //print
date2.addYear(1980); //addYear = year of birth (1980) + 10
System.out.println(date2.toString()); //print
}
}
/*date1.setMonth(6); //set the default month 6
date1.setDay(26); //set the default day 26
date1.setYear(1960); //set the default year 1960
System.out.println(date1.toString()); //print the default date1
date2.addDay(1);
System.out.println(date2.toString()); //print
//date2.addYear(1);
date2.addDay(7); //addDay = day of birth (7) + 1
System.out.println(date2.toString()); *///print