lundi 19 octobre 2020

Class to test if date is valid using Int input with boolean output, and how to test (java)

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!

Aucun commentaire:

Enregistrer un commentaire