Reputation: 1
I have an assignment for class in which I have to create a class (called FunWithCalendars) that takes in 3 int values (month, day, and year) and uses 1 boolean method (isValid) to check if the overall date is valid, which uses 3 other helper methods (isLeapYear, isValidMonth, isValidDay) to determine whether the day and month given are valid. I now want to test my file in Eclipse. So far I've learned to test classes/methods with this code ( Im a newbie, so this is the only way I know):
public class driverFunWithCalendars {
public static void main(String [] args ) {
FunWithCalendars test = new FunWithCalendars () ;
System.out.println( test.FunWithCalendars () );
However, Im not understanding how I'm supposed to test the boolean method for a true or false when I have to enter int values that aren't defined in the method. The error in the code above is at "new FunWithCalendars () ;" and "(test.funwithcalendars () );". Here's my main code:
public class FunWithCalendars
{
private int month, day, year;
public FunWithCalendars( int m, int d, int y )
{
month = m;
day = d;
year = y;
}
public boolean isValid ( boolean isValidMonth, boolean isValidDay )
{
if ( isValidMonth && isValidDay )
{
return true;
}
else
{
return false;
}
}
public boolean isLeapYear (int year)
{
if ( (year / 400) == 0 )
{
return true;
}
else if ( ((year / 4) == 0) && ((year/100) !=0))
{
return true;
}
else
{
return false;
}
}
public boolean isValidMonth ( int month )
{
if ( month >= 1 && month <= 12 )
{
return true;
}
else
{
return false;
}
}
public boolean isValidDay ( int month, int day, int year, boolean isLeapYear )
{
if ( (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && ((day<=31) && (day>=1)) )
{
return true;
}
else if ( (month == 4 || month == 6 || month == 9 || month == 11) && ((day<=30) && (day>=1)) )
{
return true;
}
else if ( (month == 2) && ( isLeapYear = true) && ((day<=29) && (day>=1)) )
{
return true;
}
else if ( (month == 2) && ( isLeapYear = false) && ((day<=28) && (day>=1)) )
{
return true;
}
else
{
return false;
}
}
}
}
}
Basically, I just want to know how I can test my code with 3 int values ( a date, or month, day year) to see if the date is valid or not using my isValid method. I'm really sorry if my question is unclear, or the assignment in the way I explained is confusing but like I said Im a newbie to Java and to Stackoverflow questions and I have yet to understand Java to the point that I can ask more efficient and concise questions, but I really hope some of you can help me. Ill also attach a transript of my original assignment, which may be less confusing and helpful to you if you're trying to help me:
/* The calendar we use today is the Gregorian Calendar, devised by Italian astronomer Aloysius Lilius and decreed by Pope Gregory XIII on February 24th, 1582. In the Gregorian Calendar, the average length of a year is exactly 365.2425 days. We can make that happen with some rather complex "leap year" rules:
If a year is divisible by 4, it's a leap year… unless it's divisible by 100, in which case it's not a leap year… unless it's divisible by 400, in which case it's a leap year.
Okay, that's confusing. Let's try again:
A year is a leap year if:
a. It's divisible by 400
b. It's divisible by 4 and it's not divisible by 100.
Based on those rules, 1512, 1600, and 2000 were leap years. 1514, 1700, and 1900 were not.
(And you thought leap year was just every four years! That was the case with Julius Caesar's calendar—which was wrong, because adding a 366th day every four years would give us an average year length of 365.25 days instead of the far more accurate 365.2425 days. 0.0075 days per year doesn't sound like a lot, but in the 1500s astronomers noticed that predictable events, such as the solstice and equinox, were "off" by about 10 days. Hence the new, improved calendar.)
In the Gregorian Calendar, the number of days per month are as follows:
Month Name Days
1 January 31
2 February 28 or 29
3 March 31
4 April 30
5 May 31
6 June 30
7 July 31
8 August 31
9 September 30
10 October 31
11 November 30
12 December 31
Based on the Gregorian Calendar's leap year rules, and the usual number of days per month, complete the class FunWithCalendars. You will need:
Three int field variables to keep track of the month, day, and year.
The constructor of FunWithCalendars, which will take in values for the month, day, and year, and assign them to field variables. (This is written for you, you may use the starter file FunWithCalendars.java ).
The method:
boolean isValid()
which will return a true or false. A true result means that the month and day are valid values for that year.
You will need three helper methods:
boolean isLeapYear()
which will return a true only if the year represents a leap year.
You also need:
boolean isValidMonth()
which will return a true only if the month field is between 1 and 12. And finally:
boolean isValidDay()
which will return a true only if that day exists in that month in the given year. Remember to check for a leap year when checking to see if 29 is valid in February!
Again, we expect a true from isValid() if and only if we get a true from isValidMonth() and from isValidDay(). (We will assume that all years are valid.)
Tests:
7/20/2010 is valid.
13/1/2009 is not valid. (13 is not a valid month.)
11/31/2009 is not valid. (That day does not exist in that month.)
2/29/2007 is not valid. (2007 was not a leap year.)
2/29/2000 is valid. (2000 was a leap year.)
*/
Thanks to anyone who can help!
Upvotes: 0
Views: 1664
Reputation: 86276
Let’s take the first test as an example:
Tests:
7/20/2010 is valid.
So in your driver class/test class construct a FunWithCalendars
object denoting July 20 2010. The constructor takes three arguments for this purpose. Next call its isValid
method. I believe that the idea was that you shouldn’t need to pass the same arguments again. Your isValid
method takes two boolean
arguments. Instead I believe that it should take no arguments and itself call the two helper methods passing the values that are already inside the FunWithCalendars
object. So before you can get your driver class to work, I believe you have to fix your design on this point.
Once you get the call to isValid()
to work, store the return value into a variable. Compare it to the expected value (true
in this case). If they are equal, print a statement that the test passed. If they are not equal, print a statement containing both the expected and the observed value.
Do similarly for the other tests. Don’t copy-paste the code, though. Instead wrap it in a method and call the method for each test case, passing as arguments the data needed for that particular test. Remember to include the expected result as an argument so the method can compare.
Edit:
… My confusion is in how to construct an object (in general, and also specifically FunWithCalendars), how to call the isValid method and have it not take any arguments, how to have the isValid method call the two helper methods which pass the values that are in the FunWIthCalendars object.
It’s basic stuff, and I don’t think Stack Overflow is a good place to teach basic stuff. Let’s give it a try, only please set your expectations low.
How to construct an object: You’re already doing this in your driver class using the new
operator:
FunWithCalendars test = new FunWithCalendars () ;
Only you need to pass the correct arguments to the constructor. Your constructor takes three int
arguments, so it needs to be something like:
FunWithCalendars test = new FunWithCalendars(7, 20, 2020);
How to call the isValid
method and have it take no arguments, after the above line:
boolean calculatedValidity = test.isValid();
This stores the value returned from isValid()
(false
or true
) into a newly created boolean
variable that I have named calculatedValidity
. From there we may check whether it has the expected value, act depending on it and/or print it. The simplest thing is to print it, for example:
System.out.println("Is 7/20/2020 valid? " + calculatedValidity);
Calling with no arguments requires that the method hasn’t got any parameters:
public boolean isValid ()
{
How to have isValid()
call the two helper methods: You may simple write the method calls en lieu of mentioning the parameters that were there before. Again remember to pass the right arguments:
if (isValidMonth(month) && isValidDay(month, day, year, isLeapYear(year)) )
In the method calls here I am using the instance variables (fields) of the FunWithCalendars
object as arguments. This causes the method to use the numbers that we entered through the constructor and to use the three helper methods.
I have run your code with the above changes. My print statement printed the expected:
Is 7/20/2020 valid? true
PS I am on purpose not saying anything about possible bugs in your code. It’s a lot better for you to have your tests tell you whether there are any.
Upvotes: 0