Question
Using Java, under nextLeapYear in the MyCalendar class, can someone fill out /*** replace this line with an assignment statement ***/ and /*** replace
Using Java, under nextLeapYear in the MyCalendar class, can someone fill out "/*** replace this line with an assignment statement ***/ " and "/*** replace this line with an IF statement ***/ "?
This was our hint:
"year is an int variable that holds a positive value. Write one assignment statement to give newYear a value higher than year that is the next multiple of 4. For example, if year = 2001, assign 2004 to newYear. If year = 2100, assign 2104 to newYear.
Write one if statement so that if newYear is a multiple of 100 but not a multiple of 400, change newYear by adding 4 to it. "
MyCalendar class:
public class MyCalendar { /** * Check for leap year * * @param year int (> 1582) * @return true if leap year; false, otherwise */ public static boolean isLeapYear(int year) { boolean leapYear; leapYear = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; return leapYear; }
/** * Compute next leap year * * @param year int (> 1582) * @return if the given year is a leap year * simply return the same; * else * compute & return next leap year */ public static int nextLeapYear(int year) { int newYear = 0; // next leap year to be computed if (isLeapYear(year)) newYear = year; else { // assign newYear a value higher than year // that is the next multiple of 4 /*** replace this line with an assignment statement ***/
// if newYear is a multiple of 100 but not a multiple of 400, // change newYear by adding 4 to it /*** replace this line with an IF statement ***/ } return newYear; } }
LeapYearTester class:
import java.util.Scanner; public class LeapYearTester { public static void main (String[] args) { char response; // user response to continue or not int year; // input year int newYear; // next leap year computed Scanner input = new Scanner (System.in); // to read user input
// see if user wants to run the program System.out.print ("Want to run? (enter y to run): "); response = input.next().charAt(0); System.out.println (); // print blank line
// process input if user wishes to continue while (response == 'y' || response == 'Y') { // prompt and read a year System.out.print ("Enter a year > 1582: "); year = input.nextInt(); // process if year > 1582 if (year > 1582) { if (MyCalendar.isLeapYear(year)) System.out.println (year + " is a leap year."); else { System.out.println (year + " is not a leap year."); newYear = MyCalendar.nextLeapYear (year); System.out.println (newYear + " is the next leap year."); } } else { System.out.println ("Invalid year. The current rule for" + " computing leap years"); System.out.println ("was formulated by Pope Gregory XIII" + " only in 1582."); } System.out.println (); // print blank line
// see if user wants to tun the program System.out.print ("Want to run? (enter y to run): "); response = input.next().charAt(0); System.out.println (); // print blank line } // print closing remarks System.out.println ("Program has terminated."); System.out.println (); // print blank line } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started