The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
import java.util.Scanner;
public class task6
{
public static int user_integer(String message)
{
Scanner in = new Scanner(System.in);
int result;
while (true)
{
System.out.printf(message);
String s = in.next();
if (s.equals("q"))
{
System.out.printf("Exiting...n");
System.exit(0);
}
try
{
result = Integer.parseInt(s);
}
catch (Exception e)
{
System.out.printf("%s is not a valid number, try again.nn", s);
continue;
}
if (result <= 0)
{
System.out.printf("%s is <= 0, try again.nn", s);
continue;
}
return result;
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while (true)
{
int year = user_integer("Enter a year (must be > 0): ");
int month = user_integer("Enter a month (must be between 1 and 12): ");
if (month > 12)
{
System.out.printf("Invalid month.nn");
continue;
}
boolean leap = is_leap_year(year);
int result = year_days(year);
int result2 = month_days(year, month);
if (leap)
{
System.out.printf("Year %d is a leap year.n", year);
}
else
{
System.out.printf("Year %d is not a leap year.n", year);
}
System.out.printf("Year %d has %d days.n", year, result);
System.out.printf("Month %d, %d has %d days.nn", month, year, result2);
}
}
}
This is an incomplete program, that takes as input from the user a year and a month, and prints: whether the year is a leap year or not, the number of days in that year, and the number of days in that specific month of the year. Complete that program, by defining three functions.
The first function is called is_leap_year, and should satisfy the following specs:
The second function is calledyear_days, and should satisfy the following specs:
The third function is calledmonth_days, and should satisfy the following specs:
IMPORTANT: you are NOT allowed to modify in any way the main function.
Note: our LeapYear.java program has code that you may find useful, and you should feel free to reuse. Note that, as that program indicates, the rules for leap years are a bit more complicated than simply checking if a year is a multiple of four. More specifically, a year is a leap year if AT LEAST ONE of the following two conditions holds:
Hint: You may find it useful to call youris_leap_yearfunction from youryear_daysandmonth_daysfunctions. Otherwise you need to write significantly more lines of code.
This is an example run of the complete program:
Enter a year (must be > 0): 1900 Enter a month (must be between 1 and 12): 2 Year 1900 is not a leap year. Year 1900 has 365 days. Month 2, 1900 has 28 days. Enter a year (must be > 0): 2000 Enter a month (must be between 1 and 12): 2 Year 2000 is a leap year. Year 2000 has 366 days. Month 2, 2000 has 29 days. Enter a year (must be > 0): 2001 Enter a month (must be between 1 and 12): 2 Year 2001 is not a leap year. Year 2001 has 365 days. Month 2, 2001 has 28 days. Enter a year (must be > 0): 2005 Enter a month (must be between 1 and 12): 12 Year 2005 is not a leap year. Year 2005 has 365 days. Month 12, 2005 has 31 days. Enter a year (must be > 0): q Exiting...