|
-
March 22nd, 2011, 06:26 PM
#1
Need help with code to test dates
I'm having some trouble with this code that asks for input and then displays if it is a real date or not. We were given the first code and we had to write the second code based on a provided UML diagram. I finished writing the second one and got it to compile but it displays the wrong results such as Invalid Date: No such month every single time. I've reviewed the code several times and the logic makes sense to me, what am I missing? Thanks
/*
Test program for Date class
*/
import java.util.Scanner;
public class Assignment5
{
/* Test driver for Date class
Prompt for the number of Date objects to test, then create Date objects and determine
if they are valid dates.
*/
public static void main (String [] args)
{
Scanner keyboard = new Scanner(System.in);
int monthVal;
int dayVal;
int yearVal;
int maxLoop;
System.out.println ("Enter number of dates to test");
maxLoop = keyboard.nextInt();
for (int time = 0; time < maxLoop; time++)
{
System.out.println ("Enter the month, day and year separated by a space");
monthVal = keyboard.nextInt();
dayVal = keyboard.nextInt();
yearVal = keyboard.nextInt();
Date aDate = new Date (monthVal, dayVal, yearVal);
if (aDate.isValid())
System.out.println ("Valid Date: " + aDate);
else
System.out.println ("Invalid Date: " + aDate);
System.out.println();
}
}
}
public class Date //Sets up the new class
{
private int month; // creates separte private variables
private int day;
private int year;
public Date (int month, int day, int year) //Constructs the Date class
{
int newMonth = month;
int newDay = day;
int newYear = year;
}
public boolean isValid() //Sets up the boolean expression and the restrictions to make it true
{
if (year >= 1582)
if(month >= 1 && month <=12)
if (month == 4 || month == 6 || month == 9 || month== 11)
if (day >=1 && day <= 30)
return true;
else
return false;
else
if (month == 2)
if (isLeapYear())
if (day >=1 && day <= 29)
return true;
else
return false;
else
if (day >= 1 && day <=28)
return true;
else
return false;
else
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
if (day >=1 && day <= 31)
return true;
else
return false;
else
return false;
else
return false;
else
return false;
}
public String toString() //Creates the toString
{
switch (month) //Creates the switch statement that outputs the correct month
{
case 1:
return "January" + "," + day + "," + year;
case 2:
return "February" + "," + day + "," + year;
case 3:
return "March" + "," + day + "," + year;
case 4:
return "April" + "," + day + "," + year;
case 5:
return "May" + "," + day + "," + year;
case 6:
return "June" + "," + day + "," + year;
case 7:
return "July" + "," + day + "," + year;
case 8:
return "August" + "," + day + "," + year;
case 9:
return "September" + "," + day + "," + year;
case 10:
return "October" + "," + day + "," + year;
case 11:
return "November" + "," + day + "," + year;
case 12:
return "December" + "," + day + "," + year;
default:
return "No such month";
}
}
private boolean isLeapYear() //private isLeapYear determines if it is a leap year or not through boolean
{
if (year % 4 == 0)
if (year % 100 == 0 && year % 400 !=0)
return true;
else
return false;
else
return false;
}
}
-
March 22nd, 2011, 08:14 PM
#2
Re: Need help with code to test dates
In your toString method you are testing the value of the "month" variable but you never assigned a value to month.
-
March 23rd, 2011, 12:07 PM
#3
Re: Need help with code to test dates
Your Date class constructor throws away its arguments by assigning them to local variables (which aren't used).
We are what we repeatedly do. Excellence, then, is not an act, but a habit...
Aristotle
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|