ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 26 May 2017 My Price 9.00

public class task6

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:

  • is_leap_yeartakes one argument, calledyear, which is an integer greater than 0 (we will only test your code with such values).
  • Ifyearis a leap year, then the function returns boolean valuetrue.
  • Otherwise,year_days(year)returns false.

The second function is calledyear_days, and should satisfy the following specs:

  • year_daystakes one argument, calledyear, which is an integer greater than 0 (we will only test your code with such values).
  • Ifyearis a leap year, thenyear_days(year)returns 366.
  • Otherwise,year_days(year)returns 365.

The third function is calledmonth_days, and should satisfy the following specs:

  • month_daystakes two arguments, calledyear, month. Argumentyearis an integer greater than 0 (we will only test your code with such values). Themonthargument is an integer between 1 and 12 (we will only test your code with such values).
  • The function returns the number of days that the month lasts for. Unless the month is 2 (for February), the result does not depend on the year. If the month is 2, then obviously the result should be 28 or 29, depending on whether the year is a leap year.

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:

  1. The year is a multiple of 4 and the year is NOT a multiple of 100.
  2. The year is a multiple of 400.

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...

Answers

(11)
Status NEW Posted 26 May 2017 03:05 AM My Price 9.00

-----------

Not Rated(0)