Question

In: Computer Science

As you know, the number of days in each month of our calendar varies: • February...

As you know, the number of days in each month of our calendar varies: • February has 29 days in a leap year, or 28 days otherwise. • April, June, September, and November have 30 days. • All other months have 31 days. Usually, years that are divisible by 4 (e.g., 2008, 2012, 2016) are leap years. However, there’s an exception: years that are divisible by 100 (e.g., 2100, 2200) are not leap years. But there’s also an exception to that exception: years that are divisible by 400 (e.g., 1600, 2000) are leap years.
Within your Lab3HW folder, write a program named DaysInMonth.java that asks the user to enter a month (1-12) and year (1000-3000). Your program should then show the number of days in that month. If the user enters a month or year beyond the specified ranges, show an appropriate error message.
Here are some examples of what your completed program might look like when you run it. Underlined parts indicate what you type in as the program is running.
Example 1
Enter month (1-12): 0 Enter year (1000-3000): 2001 Error - month and/or year out of bounds.
1
Example 2
Enter month (1-12): 2 Enter year (1000-3000): 2016 2/2016 contains 29 days.
Example 3
Enter month (1-12): 2 Enter year (1000-3000): 2900 2/2900 contains 28 days.

Solutions

Expert Solution

import java.util.Scanner;

public class DaysInMonth {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter month(1-12): ");
       int month= sc.nextInt();
       System.out.println("Enter year(1000-3000)");
       int year = sc.nextInt();
       int days = getDays(month, year);
       if(month<1 || month>12 || year<1000 || month>3000){
           System.out.println("month and/or year out of bounds.");
       }
       System.out.println(month + "/" + year + " has " + days + " days");
   }
  
//using switch case it checks the days for given month
   private static int getDays(int mm, int aYy) {
       int res = -1;
       switch (mm) {
       case 1:
           res = 31;
           break;
       case 2:
           if (isLeap(aYy))
               res = 29;
           else
               res = 28;
           break;
       case 3:
           res = 31;
           break;
       case 4:
           res = 30;
           break;
       case 5:
           res = 31;
           break;
       case 6:
           res = 30;
           break;
       case 7:
           res = 31;
           break;
       case 8:
           res = 31;
           break;
       case 9:
           res = 30;
           break;
       case 10:
           res = 31;
           break;
       case 11:
           res = 30;
           break;
       case 12:
           res = 31;
           break;

       }
       return res;
   }

   private static boolean isLeap(int year) {
       boolean leap = false;
       // if any year is divisable by 4 than there are many chances for leap
       if (year % 4 == 0) {
           // if it is divisable by 100 than it shoud also divisable by 400
           if (year % 100 == 0) {
               // year is divisible by 400, so the year is a leap year
               if (year % 400 == 0)
                   leap = true;
               else
                   leap = false;
           } else
               leap = true;
       } else
           leap = false;
       return leap;
   }

}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

For the 12 month period February 2018 through January 2019, the number of degree days that...
For the 12 month period February 2018 through January 2019, the number of degree days that accrued for the Amherst, MA area was 5903. Many houses in New England heat with oil. Suppose a reasonable-size house required a total of 1400 gallons of oil for that recent entire 12- month period. Suppose also that heating oil is priced at $2.90 per gallon. A. What was the total cost to purchase the oil to heat the house for the entire year?...
The number of Ford Trucks sold in a month at a particular dealership varies from month...
The number of Ford Trucks sold in a month at a particular dealership varies from month to month. Suppose the probability distribution below describes monthly truck sales at this dealership. x f(x) 8 0.20 10 0.35 14 0.25 20 0.20 a. What is the Expected Value of monthly sales? b. Calculate the variance and standard deviation of monthly sales. c. Suppose this dealership makes $1500 profit on each truck sold. What is the expected monthly profit on sales of Ford...
Our class has 50 students. Assuming that no students are born on leap days (February 29),...
Our class has 50 students. Assuming that no students are born on leap days (February 29), what is the probability that no two students share the same birthday? What is the probability that at least one of the students has the same birthday as another student in the class? Please provide your answers in the form of a fraction.
What is the minimum number of bits needed to encode the days of the month in...
What is the minimum number of bits needed to encode the days of the month in a Canadian calendar. 1. 5 2. 3 3. 6 4. 4
Use Java (Find the number of days in a month) Write a program that prompts the...
Use Java (Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, If the user entered month 2 and year 2012, the program should display that February 2012 has 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 has 31 days. Sample Run 1 Enter a month in the...
You are researching a population of brittle stars that varies in the number of legs that...
You are researching a population of brittle stars that varies in the number of legs that they produce. You survey the local population, and come up with the numbers below. Assume that the phenotypes you encounter represent the full range of possible phenotypes in this population. Calculate the phenotypic variance in this trait. Do the traits in this population appear to be normally distributed? Give an explanation of why this pattern might be observed. Through a series of breeding experiments,...
3. The number of book-case units ordered in any one month varies between 100 and 1400...
3. The number of book-case units ordered in any one month varies between 100 and 1400 units, as per the probability model that is found in the associated RegINdust.csv file. Only the first 6 entries of this probability model are shown below (See associated file for full model). X PrX 100 0.3316 300 0.2558 650 0.0920 800 0.1620 850 0.1412 1050 0.0149 Table 1: First 6 entries of the probability model circumscribing the number of units of book-cases that will...
You know you will receive $1,450 each month in Social Security payments in retirement. Assuming you...
You know you will receive $1,450 each month in Social Security payments in retirement. Assuming you will get benefits for 30 years and a real interest rate of 3%, what are those payments worth to you today?
Plan production for a four-month period: February through May. For February and March, you should produce...
Plan production for a four-month period: February through May. For February and March, you should produce to exact demand forecast. For April and May, you should use overtime and inventory with a stable workforce; stable means that the number of workers needed for March will be held constant through May. However, government constraints put a maximum of 5,000 hours of overtime labor per month in April and May (zero overtime in February and March). If demand exceeds supply, then backorders...
Plan production for a four-month period: February through May. For February and March, you should produce...
Plan production for a four-month period: February through May. For February and March, you should produce to exact demand forecast. For April and May, you should use overtime and inventory with a stable workforce; stable means that the number of workers needed for March will be held constant through May. However, government constraints put a maximum of 5,000 hours of overtime labor per month in April and May (zero overtime in February and March). If demand exceeds supply, then backorders...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT