5 Replies - 998 Views - Last Post: 23 February 2011 - 06:04 PM Rate Topic: -----

#1 Tsukuyomi   User is offline

  • D.I.C Head

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

Date code not working correctly.

Posted 22 February 2011 - 08:08 PM

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

}



Is This A Good Question/Topic? 0
  • +

Replies To: Date code not working correctly.

#2 pbl   User is offline

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

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

Re: Date code not working correctly.

Posted 22 February 2011 - 08:14 PM

int your setMonth() method you never set the value of your instance variable month to the value of the parameter theMonth.

And please do not use stupid useless font in your topic text. Had to fetch my sunglasses before reading your post
Was This Post Helpful? 0
  • +
  • -

#3 Tsukuyomi   User is offline

  • D.I.C Head

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

Re: Date code not working correctly.

Posted 23 February 2011 - 03:56 AM

View Postpbl, on 22 February 2011 - 08:14 PM, said:

int your setMonth() method you never set the value of your instance variable month to the value of the parameter theMonth.

And please do not use stupid useless font in your topic text. Had to fetch my sunglasses before reading your post

what do you mean? I said return 1.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Date code not working correctly.

Posted 23 February 2011 - 03:59 AM

** Useless title renamed **

Please use descriptive titles when posting. Saying HELP ME PLEASE is going to attract the wrong type of attention to your post.
Was This Post Helpful? 0
  • +
  • -

#5 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: Date code not working correctly.

Posted 23 February 2011 - 06:47 AM

Set methods are mean't to be void which means you shouldn't return anything. Simply set the value you are passing like this.

 public void setDay( int theDay){
  day=theDay;
  }



Have a look at macosxnerd101's OOP- A Look at Encapsulation.
Was This Post Helpful? 1
  • +
  • -

#6 pbl   User is offline

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

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

Re: Date code not working correctly.

Posted 23 February 2011 - 06:04 PM

View PostTsukuyomi, on 23 February 2011 - 05:56 AM, said:


what do you mean? I said return 1.
[/quote]
Yes but your toIniversalTime() method calls your getMonth() method which returns your month instance variable that yoy have never set

if ( theMonth > 0 && theMonth <= 12 ) // validate month
return theMonth;

you have a month = theMonth; missing
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1