9 Replies - 19947 Views - Last Post: 08 March 2011 - 09:45 PM Rate Topic: -----

#1 Tsukuyomi   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 22-February 11

Java Date class help

Posted 08 March 2011 - 02:37 AM

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

	}

}


This post has been edited by Tsukuyomi: 08 March 2011 - 02:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Date class help

#2 TFoSSDQ   User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Java Date class help

Posted 08 March 2011 - 06:54 AM

On line 25, your constructor which takes the String will call the constructor which takes 3 ints so it wouldn't work.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Java Date class help

Posted 08 March 2011 - 07:08 AM

Also, your array for months is the same for all instances of the class. This is a good example of where you would want to use a global (and perhaps a constant). No reason for each instance to have a String[], when you can associate it with the class and save memory.
Was This Post Helpful? 1
  • +
  • -

#4 Tsukuyomi   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 22-February 11

Re: Java Date class help

Posted 08 March 2011 - 02:56 PM

am i doing line 24 and 28 right? and how do I convert the String representation of the month to a numeric value, compare Strings using the equals method?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Java Date class help

Posted 08 March 2011 - 06:28 PM

Why would you invoke this in your constructor with the three params? It just recursively invokes the constructor. In that constructor, you want to initialize your instance variables.
this(m, d, y);    



Quote

and how do I convert the String representation of the month to a numeric value

Iterate through the array until you find a match. The numeric equivelant is the index of the element in the array (assuming you want January to start at 0. Otherwise, just add 1 to the index).

Quote

and how do I convert the String representation of the month to a numeric value

if(myString.equals("this is another String")){
    //code
}


Was This Post Helpful? 1
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Date class help

Posted 08 March 2011 - 08:41 PM

Wash !!!!

   private String[] months = new String[]{ "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }; 

   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 "";          
       }         
   }


public String monthAsString()
   {
      if(month < 1 || month > 12)
         return null;
      return months[month-1];
   }


Was This Post Helpful? 1
  • +
  • -

#7 Tsukuyomi   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 22-February 11

Re: Java Date class help

Posted 08 March 2011 - 08:48 PM

View Postpbl, on 08 March 2011 - 08:41 PM, said:

Wash !!!!

   private String[] months = new String[]{ "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }; 

   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 "";          
       }         
   }


public String monthAsString()
   {
      if(month < 1 || month > 12)
         return null;
      return months[month-1];
   }




the methods have the same name
Was This Post Helpful? -1
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Date class help

Posted 08 March 2011 - 08:55 PM

View PostTsukuyomi, on 08 March 2011 - 10:48 PM, said:

the methods have the same name

Yes the second one should replace the first one
same result is a lot less lines and more efficient
Was This Post Helpful? 0
  • +
  • -

#9 Tsukuyomi   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 114
  • Joined: 22-February 11

Re: Java Date class help

Posted 08 March 2011 - 09:10 PM

View Postpbl, on 08 March 2011 - 08:55 PM, said:

View PostTsukuyomi, on 08 March 2011 - 10:48 PM, said:

the methods have the same name

Yes the second one should replace the first one
same result is a lot less lines and more efficient

Can you help on this:
ii) February 25, 2011
iii) DDD YYYY

I can only get the numbers to print for II and I have no idea on what do with III
Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Date class help

Posted 08 March 2011 - 09:45 PM

   public Date3( String m, int d, int y){
	   this(m, d, y);
   }


Your Date3 class does not have a constructor that accepts as parameter (String, int, int)
but the actual one so you are kind as in an infinite loop, calling back youself
but I think the compiler should diagnose that it probably shows you an error on that line
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1