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()); 

	}

}

have u tried after uncommenting ur constructors?

have u tried after uncommenting ur constructors?

I'm supposed to use a default constructor.

I did the ode but it's not working the way I want it too. For example if I change

int y = 2000, m = 5, d = 06; to int y = 2889, m = 44, d = 16; it prints it instead of what is in the if statement in class Date1.

public class Date1 {
	
	   private int year = 1; // any year
	   private int month = 1; // 1-12
	   private int day = 1; // 1-31 based on month
	   
	
	   //method to set the year
	   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;
		   }
		   else 
			   
		   year = y;
	   }
	   
	   public int setMonth(int theMonth){
		   if ( theMonth > 0 && theMonth <= 12 ) // validate month
		         return theMonth;
		      else // month is invalid 
		      { 
		         System.out.printf( 
		            "Invalid month (%d) set to 1.", theMonth);
		         return 1; // maintain object in consistent state
		      } // end else
		   
	   }
	   
	   public int setDay( int theDay){

		   int[] daysPerMonth = 
	         { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	   
	      // check if day in range for month
	      if ( theDay > 0 && theDay <= daysPerMonth[ month ] )
	         return theDay;
	   
	      // check for leap year
	      if ( month == 2 && theDay == 29 && ( year % 400 == 0 || 
	           ( year % 4 == 0 && year % 100 != 0 ) ) )
	         return theDay;
	   
	      System.out.printf( "Invalid day (%d) set to 1.", theDay );
	      return 1;  // maintain object in consistent state
		   

		 
	   }
	   
	   //method to return the year
	   public int getYear(){
		   
		   
		   return year; 
	   }
	   
	 //method to return the month
	   public int getMonth(){
		   
		   return month; 
	   }
	   
	 //method to return the day 
	   public int getDay(){
		   return day; 
	   }
	   
	   // return a String of the form year/month/day
	   public String toUniversalStringTime() 
	   { 
	      return String.format( "The date using a default constructor %d/%d/%d \n", getYear(), getMonth(), getDay() ); 
	   } // end toUniversalStringTime

}
public class Date1Test {

	public static void main(String[] args) {
	
		
		int y = 2000, m = 5, d = 06; 
		
	    
	  	Date1 d1 = new Date1(); //create a new object
	
	
		System.out.println(d1.toUniversalStringTime()); //call toUniversalStringTime()
		
		System.out.printf("The date I created is %d/%d/%d \n", y , m , d); 
	
	}

}

set values of day, month and year using the setter methods u've created. if u r to use the default constructor, then u cannot create objects of Date1 using the constructors u created (and there will be errors when uve called then after commenting).

so set the values using the setters u created. eg:

Date1 d1 = new Date1();

System.out.print("Enter the year (yyyy) ");
year = input.nextInt();
d1.setYear(year);

and in the setDay function in Date1 class i think u meant to say

public void setDay( int d){
    day = (( d >=1 && d < 31)? d : 1);
//d < 31 instead of d>31
}

hope this help.

I did the ode but it's not working the way I want it too. For example if I change

int y = 2000, m = 5, d = 06; to int y = 2889, m = 44, d = 16; it prints it instead of what is in the if statement in class Date1.

public class Date1 {
	
	   private int year = 1; // any year
	   private int month = 1; // 1-12
	   private int day = 1; // 1-31 based on month
	   
	
	   //method to set the year
	   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;
		   }
		   else 
			   
		   year = y;
	   }
	   
	   public int setMonth(int theMonth){
		   if ( theMonth > 0 && theMonth <= 12 ) // validate month
		         return theMonth;
		      else // month is invalid 
		      { 
		         System.out.printf( 
		            "Invalid month (%d) set to 1.", theMonth);
		         return 1; // maintain object in consistent state
		      } // end else
		   
	   }
	   
	   public int setDay( int theDay){

		   int[] daysPerMonth = 
	         { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	   
	      // check if day in range for month
	      if ( theDay > 0 && theDay <= daysPerMonth[ month ] )
	         return theDay;
	   
	      // check for leap year
	      if ( month == 2 && theDay == 29 && ( year % 400 == 0 || 
	           ( year % 4 == 0 && year % 100 != 0 ) ) )
	         return theDay;
	   
	      System.out.printf( "Invalid day (%d) set to 1.", theDay );
	      return 1;  // maintain object in consistent state
		   

		 
	   }
	   
	   //method to return the year
	   public int getYear(){
		   
		   
		   return year; 
	   }
	   
	 //method to return the month
	   public int getMonth(){
		   
		   return month; 
	   }
	   
	 //method to return the day 
	   public int getDay(){
		   return day; 
	   }
	   
	   // return a String of the form year/month/day
	   public String toUniversalStringTime() 
	   { 
	      return String.format( "The date using a default constructor %d/%d/%d \n", getYear(), getMonth(), getDay() ); 
	   } // end toUniversalStringTime

}
public class Date1Test {

	public static void main(String[] args) {
	
		
		int y = 2000, m = 5, d = 06; 
		
	    
	  	Date1 d1 = new Date1(); //create a new object
	
	
		System.out.println(d1.toUniversalStringTime()); //call toUniversalStringTime()
		
		System.out.printf("The date I created is %d/%d/%d \n", y , m , d); 
	
	}

}

i'd suggest you to use ur previous method with minor changes.

you could have separate functions to validate year, month and day. in the setter just set the value.

maybe something like this.

//in the Date1 class. 

   public boolean isValidYear(int y) {
        if (y > 0 && y <=2011) {
            return true;
        }
        return false;
   }

//and to validate during runtime

System.out.print("Enter the year (yyyy) ");
  year = input.nextInt();
  while(d1.isValidYear(year)==false){
    System.out.println("Invalid year, year must be 2011 or earlier.");
    System.out.print("Enter the year (yyyy) ");
    year = input.nextInt();
  }

//now set year
d1.setYear(year);

try doing the same for day and month.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.