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...
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.
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...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT