Question

In: Computer Science

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 year (e.g., 1 for Jan): 2
Enter a year: 2012
February 2012 has 29 days

Sample Run 2

Enter a month in the year (e.g., 1 for Jan): 4
Enter a year: 2005
April 2005 has 30 days

Sample Run 3

Enter a month in the year (e.g., 1 for Jan): 2
Enter a year: 2006
February 2006 has 28 days

Sample Run 4

Enter a month in the year (e.g., 1 for Jan): 2
Enter a year: 2000
February 2000 has 29 days

Class Name: Exercise03_11

If you get a logical or runtime error, please refer https://liveexample.pearsoncmg.com/faq.html.

Solutions

Expert Solution


import java.util.Scanner;

public class Pearson {
        public static void main(String[] args) throws Exception {
                Scanner sc = new Scanner(System.in);
                String arr[] = { " ", "January", "February", "March", "April", "May", "June", "July", "August", "September",
                                "October", "November", "December" };
                int days = 0;
                System.out.print("Enter a month in the year: ");
                int month = sc.nextInt();
                System.out.println("Enter a year) ");
                int year = sc.nextInt();
                days = getDays(month, year);
                System.out.println(arr[month] + " " + year + " has " + days + " days");

        }

        private static int getDays(int mm, int aYy) {
                int res = -1;
                if (mm == 1)
                        res = 31;

                if (mm == 2)
                        if (isLeapYear(aYy))
                                res = 29;
                        else
                                res = 28;

                if (mm == 3)
                        res = 31;

                if (mm == 4)
                        res = 30;

                if (mm == 5)
                        res = 31;

                if (mm == 6)
                        res = 30;

                if (mm == 7)
                        res = 31;

                if (mm == 8)
                        res = 31;

                if (mm == 9)
                        res = 30;

                if (mm == 10)
                        res = 31;

                if (mm == 11)
                        res = 30;

                if (mm == 12)
                        res = 31;

                return res;
        }

        private static boolean isLeapYear(int userYear) {
                boolean leap = false;
                // if any year is divisable by 4 than there are many chances for leap
                // year except few
                if (userYear % 4 == 0) {
                        // if it is divisable by 100 than it shoud also divisable by 400
                        // like 2000 etc
                        if (userYear % 100 == 0) {
                                // year is divisible by 400, so the year is a leap year
                                if (userYear % 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 YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

Write a JAVA program that prompts the user for the number of names they’d like to...
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write a Java program that prompts for and reads the number N of cities or locations...
Write a Java program that prompts for and reads the number N of cities or locations to be processed. It then loops N times to prompt for and read, for each location, the decimal latitude, decimal longitude, and decimal magnetic declination. It then computes and displays, for each location, the Qibla direction (or bearing) from Magnetic North. The true bearing from a point A to a point B is the angle measured in degrees, in a clockwise direction, from the...
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter...
Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5.  For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle. (Hint: A point is in the rectangle if its horizontal distance to (0, 0) is less than or equal to10 / 2 and its vertical...
Write a program in JAVA that prompts the user for a lower bound and an upper...
Write a program in JAVA that prompts the user for a lower bound and an upper bound. Use a loop to output all of the even integers within the range inputted by the user on a single line.
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
Use Selections, DO NOT USE ARRAYS AND METHODS. (Find future dates) Write a program that prompts...
Use Selections, DO NOT USE ARRAYS AND METHODS. (Find future dates) Write a program that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1, ..., and Saturday is 6). Also prompt the user to enter the number of days after today for a future day and display the future day of the week. ** Can not use java.time.DayOfWeek; SAMPLE RUN #1: java FindFutureDates Enter today's day: 4↵ Enter the number...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT