a) Output the date in multiple formats, such as
i) MM/DD/YYYY
ii) February 25, 2011
iii) DDD YYYY

I'm supposed to do this: b) Use overloaded constructors to create Date3 objects initialized with dates of the formats in part (a). In the first case, the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day number in the year. [Hint: to convert the String representation of the month to a numeric value, compare Strings using the equals method. For example, if s1 and s2 are Strings, the method call s1.equal(s2) returns true if the Strings are identical and otherwise returns false.

// Date3.java 
// Date3 class declaration.

public class Date3 
{
   private int month; // 1-12
   private int day; // 1-31 based on month
   private int year; // any year
   private String[] months = new String[]{ "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }; 
   

   // constructor: call checkMonth to confirm proper value for month; 
   // call checkDay to confirm proper value for day
   public Date3( int theMonth, int theDay, int theYear )
   {
      month = checkMonth( theMonth ); // validate month
      year = theYear; // could validate year
      day = checkDay( theDay ); // validate day

      System.out.printf( 
         "Date3 object constructor for date %s\n", this );
   } // end Date3 constructor
   
   public Date3( String m, int d, int y){
	   this(m, d, y);
   }
   
   public Date3( int m, int y){
	   this(m,0, y);
   }
   

   // utility method to confirm proper month value
   private int checkMonth( int testMonth )
   {
      if ( testMonth > 0 && testMonth <= 12 ) // validate month
         return testMonth;
      else // month is invalid 
      { 
         System.out.printf( 
            "Invalid month (%d) set to 1.", testMonth );
         return 1; // maintain object in consistent state
      } // end else
   } // end method checkMonth

   // utility method to confirm proper day value based on month and year
   private 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
   } // end method checkDay
   
   public String getMonthString(int month){
	   	return months[month];
	  	}
   
   
  /* public String monthAsString()
   {
       //returns month as a string rather than an integer
       switch (month)
       {
           case 1: return "January";
           case 2: return "February";
           case 3: return "March";
           case 4: return "April";
           case 5: return "May";
           case 6: return "June";
           case 7: return "July";
           case 8: return "August";
           case 9: return "September";
           case 10: return "October";
           case 11: return "November";
           case 12: return "December";
           default: return "";          
       }         
   }*/



   
   // return a String of the form month/day/year
   public String toString()
   { 
      return String.format( "%d/%d/%d", month, day, year ); 
   } // end method toString
} // end class Date3
public class Date3Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	    Date3 myDate = new Date3(9, 16, 2011);
	    Date3 myDate2 = new Date3("June", 24, 2010);
	    Date3 myDate3 = new Date3(259, 2005);

	    // all three dates above are equal and will therefore print
	    // the same dates

	    System.out.println(myDate);
	    System.out.println(myDate2);
	    System.out.println(myDate3); 

	}

}

can you tell me what problem you are facing in this code.So i can concentrate on that problem only instead of reading whole code

can you tell me what problem you are facing in this code.So i can concentrate on that problem only instead of reading whole code

I'm having a problem with this: "In the third case it should receive two integer values, the first of which represents the day number in the year. [Hint: to convert the String representation of the month to a numeric value, compare Strings using the equals method. For example, if s1 and s2 are Strings, the method call s1.equal(s2) returns true if the Strings are identical and otherwise returns false." and I'm not sure i did line 24 and 28 the right way.

can you help with Can you help on this:

iii) DDD YYYY
I have no idea on what do with III

SimpleDateFormat

I assume you are supposed to code this yourself, and you can't use SimpleDateFormat?

DDD is the number of days into the year. If it's <=31 then its a day in January.
If its >31 and <=28 then (DDD-31) is a day in February. Etc.
You already have a daysInMonth array, so you can use that to find out which month DDD is in, and which day in the month that is.

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.