I'm supposed to write this program using a default constructor but when I compile I got this errors:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Date1(int, int, int) is undefined
The constructor Date1(int, int, int) is undefined
at Date1Test.main(Date1Test.java:23)
/**
*
*/
/**
* @author Animator
*
*/
public class Date1 {
private int year = 1; // any year
private int month = 1; // 1-12
private int day = 1; // 1-31 based on month
/*public Date1(){
this(1, 1, 1);
}
public Date1( int yyyy, int mm, int d){
year = yyyy ;
month = mm ;
day= d;
}*/
public int CheckDay ( int testDay){
int[] daysPerMonth =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// check if day in range for month
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
// check for leap year
if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
System.out.printf( "Invalid day (%d) set to 1.", testDay );
return 1; // maintain object in consistent state
}
public void setYear(int y){
if (y < 0)
{
System.out.println("That is too early");
year = 1;
}
if (y > 2011)
{
System.out.println("That year hasn't happened yet!");
y = 2011;
}
year = y;
}
public void setMonth(int m){
month = ((m >= 1 && m < 13)? m : 1);
}
public void setDay( int d){
day = (( d >=1 && d > 31)? d : 1);
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
// return a String of the form month/day/year
public String toUniversalStringTime()
{
return String.format( "The date is %d/%d/%d", getYear(), getMonth(), getDay() );
} // end method toString
}
import java.util.Scanner;
public class Date1Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int year, month, day;
Scanner input = new Scanner(System.in);
System.out.print("Enter the year (yyyy) ");
year = input.nextInt();
System.out.print("Enter the month(mm) ");
month = input.nextInt();
System.out.print("Enter the day(dd) ");
day = input.nextInt();
Date1 d3 = new Date1(year, month, day);
Date1 d1 = new Date1();
Date1 d2 = new Date1( 12, 5, 7);
System.out.print("The date for d1 is:" );
System.out.println(d1.toUniversalStringTime());
//System.out.println(d1.toUniversalStringTime());
System.out.printf("The date for d2 is %d/%d/%d \n", d2.getYear(), d2.getMonth(), d2.getDay());
System.out.printf("The date for d3(the one you just enter) is %d/%d/%d ", d3.getYear(), d3.getMonth(), d3.getDay());
}
}