Java Number and Eligibility Checkers
Java Number and Eligibility Checkers
If the number is zero, the NumberSignChecker would produce the output 'Number is zero.' as it directly checks if the number equals zero in its conditional statements .
The LargestNumber program uses logical AND operators (&&) and the greater than or equal to (>=) relational operators to compare the three numbers. It checks each number against the others to determine which one is the largest. For example, num1 is declared the largest if num1 >= num2 and num1 >= num3 .
The VotingEligibility program determines voting eligibility by checking if the age variable is greater than or equal to 18. If the age is at least 18, the person is eligible to vote; otherwise, they are not eligible .
The LeapYearChecker program first checks if the year is divisible by 4, which is the basic requirement for a leap year. It then checks if the year is divisible by 100; if so, it must also be divisible by 400 to be considered a leap year. This accounts for the exception in the Gregorian calendar where years divisible by 100 are not leap years unless they are also divisible by 400 .
The LeapYearChecker handles the exception for centuries by using a nested conditional check. After determining that a year is divisible by 4, it further checks if the year is divisible by 100. If it is, an additional check verifies if the year is divisible by 400. If not divisible by 400, the year is set as not a leap year, overriding the initial condition .
The EvenOddChecker can handle negative numbers because the modulo operation used for determining evenness or oddness works the same way with negative numbers as with positive ones. A negative number modulo 2 will still determine even if the result is zero .
To adapt the VotingEligibility program for different countries, you could replace the hardcoded age threshold with a variable or a constant that can be easily updated without modifying the core logic. This would make the program flexible and applicable to any voting age requirement by changing just one value .
The EvenOddChecker program uses the modulo operator (%) to determine if a number is even or odd. Specifically, it checks if the remainder of the division of the number by 2 is zero. If the remainder is zero, the number is even; otherwise, it is odd .
The NumberSignChecker uses conditional statements to compare the given number to zero. If the number is greater than zero, it categorizes the number as positive. If the number is less than zero, it categorizes the number as negative. Finally, if the number is exactly zero, it states that the number is zero .
The LargestNumber program uses 'else' for the final comparison because after checking the first two conditions (num1 is the largest or num2 is the largest), if neither of those is true, logically num3 must be the largest. Consequently, an additional check is unnecessary, and an 'else' suffices for clean and efficient code .