program to error-check own date class

Not sure if I have to methods right, or the test class

Page 1 of 1

10 Replies - 5899 Views - Last Post: 20 September 2009 - 08:17 AM Rate Topic: -----

#1 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

program to error-check own date class

Post icon  Posted 18 September 2009 - 05:59 PM

Hi, I think I've really messed up the assignment I'm working on. I was to create the Date.java program to perform error-checking on the initial values of month, day, and year. I'm supposed to provide a method nextDay() to increment the day by one. The Date object should always remain in a consistent state. Next, I am to write a program that tests the nextDay method in a loop that prints the date during each iteration of the loop to illustrate that the nextDay method works correctly. I'm supposed to test 1) incrementing into the next month, and 2) incrementing into the next year. I know I have something wrong somewhere (probably several things) because i have variables that are never read and my output is totally wrong.

Here is what I have for Date

public class Date 
{
	//Declare variables
	private int month;
	private int day;
	private int year;
	
	
	public Date(int m, int d, int y) 
	{
		int year = 0;
		int day = 0;
		int month = 2009;
		
	}

	
	public int checkDay( int testDay)
	{
	   int[]daysOfMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	   
	   if(testDay > 0 && testDay <= daysOfMonth[month])
		   return testDay;
	   else if (month == 2)
	   //else if(month  == testDay != 29 &&(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
	   
		   return testDay;
		   
	   else if(month ==2)
	   {
		   boolean isleap = true;
		   if(year % 4 != 0)
			   isleap = false;
		   else if((year % 100 == 0) && (year % 400 == 0))
			   isleap = true;
		   if((isleap == true) && (day <= 29))
			   return day;
		   else if((isleap == false) && (day <= 28))
			   return day;
		   
		   System.out.println("Day" + testDay + "invalid.Set to day 1.");
		   
		   return 1;}
	return day;
	   }
		
		 
		   public int nextDay()
		   {
			 int testDay = day + 1; 
			 
			 if( checkDay(testDay) == testDay)
				 day = testDay;
			 else 
			 {
				 day = 1;
				 nextMonth();
			 }
			return day;
	   
		   }
	
		   
		   public int nextMonth()
		   {
			   if (12 == month)
			   year++;
			   return month = 1;
		   
		   
		   }
	   

}
	


And for DateTest
public class DateTest {

	
	public static void main(String[] args)
	{
	  Date date = new Date(0, 0, 0);
	  Date date2 = new Date(11,27,1988);
	  
		  
	  
	  for (int counter = 0; counter < 40; counter++)
	  {
		  date2.nextDay();
		  
		  System.out.println("Incremented Date:" + date2.toString());
		  
	  }
	  
		
	}

}



Thsi is my output:
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9



And this is what my output is supposed to be:
Checking increment

Date object constructor for date 11/27/1988

Incremented Date: 11/28/1988
Incremented Date: 11/29/1988
Incremented Date: 11/30/1988
Day 31 invalid. Set to day 1.
Incremented Date: 12/1/1988
	...
	...
Incremented Date: 12/31/1988
Day 32 invalid. Set to day 1.
Incremented Date: 1/1/1989
	...
	...



I'm only required to loop through 40 times.

I'm sure that there is more than a couple problems with my code but I would appreciate help with any of it. Thanks!

This post has been edited by Vanessa6: 18 September 2009 - 06:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: program to error-check own date class

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10651
  • View blog
  • Posts: 39,562
  • Joined: 27-December 08

Re: program to error-check own date class

Posted 18 September 2009 - 06:27 PM

I don't see the toString() method in your Date class, and you invoke it in your DateTest class. By the way, you can also include your main method in the Date class for testing purposes.
Was This Post Helpful? 1
  • +
  • -

#3 krizjaz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 99
  • Joined: 07-October 07

Re: program to error-check own date class

Posted 18 September 2009 - 08:07 PM

View PostVanessa6, on 18 Sep, 2009 - 04:59 PM, said:

Hi, I think I've really messed up the assignment I'm working on. I was to create the Date.java program to perform error-checking on the initial values of month, day, and year. I'm supposed to provide a method nextDay() to increment the day by one. The Date object should always remain in a consistent state. Next, I am to write a program that tests the nextDay method in a loop that prints the date during each iteration of the loop to illustrate that the nextDay method works correctly. I'm supposed to test 1) incrementing into the next month, and 2) incrementing into the next year. I know I have something wrong somewhere (probably several things) because i have variables that are never read and my output is totally wrong.

Here is what I have for Date

public class Date 
{
	//Declare variables
	private int month;
	private int day;
	private int year;
	
	
	public Date(int m, int d, int y) 
	{
		int year = 0;
		int day = 0;
		int month = 2009;
		
	}

	
	public int checkDay( int testDay)
	{
	   int[]daysOfMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	   
	   if(testDay > 0 && testDay <= daysOfMonth[month])
		   return testDay;
	   else if (month == 2)
	   //else if(month  == testDay != 29 &&(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
	   
		   return testDay;
		   
	   else if(month ==2)
	   {
		   boolean isleap = true;
		   if(year % 4 != 0)
			   isleap = false;
		   else if((year % 100 == 0) && (year % 400 == 0))
			   isleap = true;
		   if((isleap == true) && (day <= 29))
			   return day;
		   else if((isleap == false) && (day <= 28))
			   return day;
		   
		   System.out.println("Day" + testDay + "invalid.Set to day 1.");
		   
		   return 1;}
	return day;
	   }
		
		 
		   public int nextDay()
		   {
			 int testDay = day + 1; 
			 
			 if( checkDay(testDay) == testDay)
				 day = testDay;
			 else 
			 {
				 day = 1;
				 nextMonth();
			 }
			return day;
	   
		   }
	
		   
		   public int nextMonth()
		   {
			   if (12 == month)
			   year++;
			   return month = 1;
		   
		   
		   }
	   

}
	


And for DateTest
public class DateTest {

	
	public static void main(String[] args)
	{
	  Date date = new Date(0, 0, 0);
	  Date date2 = new Date(11,27,1988);
	  
		  
	  
	  for (int counter = 0; counter < 40; counter++)
	  {
		  date2.nextDay();
		  
		  System.out.println("Incremented Date:" + date2.toString());
		  
	  }
	  
		
	}

}



Thsi is my output:
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9



And this is what my output is supposed to be:
Checking increment

Date object constructor for date 11/27/1988

Incremented Date: 11/28/1988
Incremented Date: 11/29/1988
Incremented Date: 11/30/1988
Day 31 invalid. Set to day 1.
Incremented Date: 12/1/1988
	...
	...
Incremented Date: 12/31/1988
Day 32 invalid. Set to day 1.
Incremented Date: 1/1/1989
	...
	...



I'm only required to loop through 40 times.

I'm sure that there is more than a couple problems with my code but I would appreciate help with any of it. Thanks!


HmMmMmMm. . I think, you need to make a method in your Date class that will display the month, day and year;
Like this one:
public String display(){
	return month + "/" + day + "/" + year;
}


and also, the day, year and month in your constructor is already defined. Your constructor should only be like this:
public Date1(int m, int d, int y)
	{
		year = y;
		day = d;
		month = m;

	}



Also, the value of m, d and y should be passed to month, day and year respectively so that the m, d and y which is your input will be used. It's your turn to change your code. There's still something wrong. It doesn't print the same as what you want to expect.

Happy coding! :)
Hope i can help
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: program to error-check own date class

Posted 18 September 2009 - 08:45 PM

You'll have to add a toString() method to your Date class

public String toString() {
   return month + "/" + year + "/" = day;
}



then when invoked for a String a representation of your Date object the toString() method will be invoked and you could do

Date x = new Date(10, 2000, 31);
System.out.println(x);

This post has been edited by pbl: 19 September 2009 - 12:36 PM

Was This Post Helpful? 1
  • +
  • -

#5 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: program to error-check own date class

Posted 18 September 2009 - 10:08 PM

View Postkrizjaz, on 18 Sep, 2009 - 07:07 PM, said:

View PostVanessa6, on 18 Sep, 2009 - 04:59 PM, said:

Hi, I think I've really messed up the assignment I'm working on. I was to create the Date.java program to perform error-checking on the initial values of month, day, and year. I'm supposed to provide a method nextDay() to increment the day by one. The Date object should always remain in a consistent state. Next, I am to write a program that tests the nextDay method in a loop that prints the date during each iteration of the loop to illustrate that the nextDay method works correctly. I'm supposed to test 1) incrementing into the next month, and 2) incrementing into the next year. I know I have something wrong somewhere (probably several things) because i have variables that are never read and my output is totally wrong.

Here is what I have for Date

public class Date 
{
	//Declare variables
	private int month;
	private int day;
	private int year;


	
	
	public Date(int m, int d, int y) 
	{
		int year = 0;
		int day = 0;
		int month = 2009;
		
	}

	
	public int checkDay( int testDay)
	{
	   int[]daysOfMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	   
	   if(testDay > 0 && testDay <= daysOfMonth[month])
		   return testDay;
	   else if (month == 2)
	   //else if(month  == testDay != 29 &&(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
	   
		   return testDay;
		   
	   else if(month ==2)
	   {
		   boolean isleap = true;
		   if(year % 4 != 0)
			   isleap = false;
		   else if((year % 100 == 0) && (year % 400 == 0))
			   isleap = true;
		   if((isleap == true) && (day <= 29))
			   return day;
		   else if((isleap == false) && (day <= 28))
			   return day;
		   
		   System.out.println("Day" + testDay + "invalid.Set to day 1.");
		   
		   return 1;}
	return day;
	   }
		
		 
		   public int nextDay()
		   {
			 int testDay = day + 1; 
			 
			 if( checkDay(testDay) == testDay)
				 day = testDay;
			 else 
			 {
				 day = 1;
				 nextMonth();
			 }
			return day;
	   
		   }
	
		   
		   public int nextMonth()
		   {
			   if (12 == month)
			   year++;
			   return month = 1;
		   
		   
		   }
	   

}
	


And for DateTest
public class DateTest {

	
	public static void main(String[] args)
	{
	  Date date = new Date(0, 0, 0);
	  Date date2 = new Date(11,27,1988);
	  
		  
	  
	  for (int counter = 0; counter < 40; counter++)
	  {
		  date2.nextDay();
		  
		  System.out.println("Incremented Date:" + date2.toString());
		  
	  }
	  
		
	}

}



Thsi is my output:
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9
Incremented Date:Date@10b62c9



And this is what my output is supposed to be:
Checking increment

Date object constructor for date 11/27/1988

Incremented Date: 11/28/1988
Incremented Date: 11/29/1988
Incremented Date: 11/30/1988
Day 31 invalid. Set to day 1.
Incremented Date: 12/1/1988
	...
	...
Incremented Date: 12/31/1988
Day 32 invalid. Set to day 1.
Incremented Date: 1/1/1989
	...
	...



I'm only required to loop through 40 times.

I'm sure that there is more than a couple problems with my code but I would appreciate help with any of it. Thanks!


HmMmMmMm. . I think, you need to make a method in your Date class that will display the month, day and year;
Like this one:
public String display(){
	return month + "/" + day + "/" + year;
}


and also, the day, year and month in your constructor is already defined. Your constructor should only be like this:
public Date1(int m, int d, int y)
	{
		year = y;
		day = d;
		month = m;

	}



Also, the value of m, d and y should be passed to month, day and year respectively so that the m, d and y which is your input will be used. It's your turn to change your code. There's still something wrong. It doesn't print the same as what you want to expect.

Happy coding! :)
Hope i can help


Thank you for the suggestions, I'll apply the changes you suggested and see if I can go from there :)
Was This Post Helpful? 0
  • +
  • -

#6 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: program to error-check own date class

Posted 18 September 2009 - 10:13 PM

View Postpbl, on 18 Sep, 2009 - 07:45 PM, said:

You'll have to add a toString() method to your Date class

[code]
public String toString() {
return month + "/" + year + "/" = day;
}
[code]

then when invoked for a String a representation of your Date object the toString() method will be invoked and you could do

Date x = new Date(10, 2000, 31);
System.out.println(x);


Thanks. I'll try your suggestion and hopefully I can figure out the rest.
Was This Post Helpful? 0
  • +
  • -

#7 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: program to error-check own date class

Posted 19 September 2009 - 01:24 PM

Using everyones suggestion and (unbelievable to me) I found a few things that needed changed on my own, i was able to get the program to almost work correctly. The only problem I have now is that the month isn't incrementing which is affecting the year incrementing. I've tried several different changes but nothing that I am trying works. Does anyone see what I have done wrong?

public int checkDay( int testDay)
	{
	   int[]daysOfMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	   
	   if(testDay > 0 && testDay <= daysOfMonth[month])
		   return testDay;
	  
		   
	   else if(month ==2)
	   {
		   boolean isleap = true;
		   if(year % 4 != 0)
			   isleap = false;
		   else if((year % 100 != 0) && (year % 400 == 0))
			   isleap = true;
		   if((isleap == true) && (day <= 29))
			   return day;
		   else if((isleap == false) && (day <= 28))
			   return day;
		   
		  
		   
		   return day;}
	return day; 
	   }
		
		 
		   public int nextDay()
		   {
			 int testDay = day + 1; 
			 
			 if( checkDay(testDay) == testDay)
				 day = testDay;
			 else 
			 {
				 day = 1;
				 nextMonth();
			 }
			return month;
	   
		   }
	
		   
		   public int nextMonth()
		   {
			   if (12 == month)
			   year++;
			   return month;
		   
		   }
		   
		   public int nextYear()
		   {
			   if (12 == year)
			   year++;
			   return year = 1;
			   
		   }
		   

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: program to error-check own date class

Posted 19 September 2009 - 02:05 PM

public int checkDay( int testDay)
	{
	   int[]daysOfMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	   
	   if(testDay > 0 && testDay <= daysOfMonth[month])
		   return testDay;
	  
		   
	   else if(month ==2)
	   {


What are you trying to do ? What is the logic behind that ?
Was This Post Helpful? 0
  • +
  • -

#9 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: program to error-check own date class

Posted 19 September 2009 - 04:44 PM

View Postpbl, on 19 Sep, 2009 - 01:05 PM, said:

public int checkDay( int testDay)
	{
	   int[]daysOfMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	   
	   if(testDay > 0 && testDay <= daysOfMonth[month])
		   return testDay;
	  
		   
	   else if(month ==2)
	   {


What are you trying to do ? What is the logic behind that ?


Honestly, I'm not sure. The java class I'm taking assumes an understanding of programming logic, which I don't have. So I'm tring to learn the programming locig as I learn the language. Most of my code I have pieced together from examples in my textbook and tutorials on the web. Please don't think I'm being lazy, I'm just trying my best to learn the syntax and understand the logic as I go along.

Anyway, I can atleast tell you what I think I am able to read in this part. I know that the array is used to reference how many days are in each month but for the second part I don't get where testDay could be less than zero unless this is a way to check for negative input. I'm pretty sure that the last part is just checking the year for leap year.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: program to error-check own date class

Posted 19 September 2009 - 06:29 PM

Just don't make sense

what you are doing:

if the day passed as parameter is between 1 and the number of day in the month you return that day

ELSE
so if day id let see -10 or 500
you check if it is a leap year... depending id it is a leap year or not you return that day anyhow
Was This Post Helpful? 1
  • +
  • -

#11 Vanessa6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 30-January 09

Re: program to error-check own date class

Posted 20 September 2009 - 08:17 AM

I still don't get it. I've made a couple more changes and took a couple of lines out that weren't needed. My logic is still messed up, The day changes but not the month or year.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1