Question

In: Computer Science

1.Write a Java program that prompts the user for a month and day and then prints...

1.Write a Java program that prompts the user for a month and day and then prints the season determined by the following rules.

If an invalid value of month (<1 or >12) or invalid day is input, the program should display an error message and stop. Notice that whether the day is invalid depends on the month! You may assume that the user will never enter anything other than integers (no random strings or floats will be tested.)

Tips: Break this problem down into smaller pieces. This is always good practice when tackling a larger problem. Break it up into pieces that you can test individually and work on one piece at a time. You might try writing the pseudo-code for each piece.

First, see if you can get a month and ensure that it is valid. Test out only this piece of functionality before continuing. Make sure you test not only the good and bad cases, but also for boundary cases. For example, try entering -5, 0, 1, 4, 12, 13, and 56.

Next, see if you can get a day and ensure that it is valid. Test this piece too.

Finally, use the now-valid month and day to determine which season it is in. If you tested the earlier pieces, you will now know that any bugs are due to a problem here.

Solutions

Expert Solution

Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !

===========================================================================

import java.util.Scanner;

public class Season {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        int month = getMonth();
        int day = getDay(month);

        if (month == 3 || month == 4 || month == 5) {
            System.out.println(month + "/" + day + " falls in Spring");
        }

        if (month == 6 || month == 7 || month == 8) {
            System.out.println(month + "/" + day + " falls in Summer");
        }

        if (month == 9 || month == 10 || month == 11) {
            System.out.println(month + "/" + day + " falls in Fall");
        }

        if (month == 12 || month == 1 || month == 2) {
            System.out.println(month + "/" + day + " falls in Winter");
        }


    }


    public static int getMonth() {

        int month = 0;
        do {
            System.out.print("Enter month [1-12]: ");
            month = scanner.nextInt();
            if (month < 1 || month > 12) {
                System.out.print("Error: Month should be between 1 and 12 (both inclusive)\n");
            }
        } while (month < 1 || month > 12);

        return month;
    }


    public static int getDay(int month) {

        int day = 0;

        int monthDays[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int daysInMonth = monthDays[month];
        do {
            System.out.print("Enter days [1-" + daysInMonth + "]: ");
            day = scanner.nextInt();
            if (day < 1 || day > daysInMonth) {
                System.out.print("Error: Day should be between 1 and " + daysInMonth + " (both inclusive)\n");
            }


        } while (day < 1 || day > daysInMonth);


        return day;
    }
}

===================================================================


Related Solutions

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 program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> For this project, you are to: 1) You should validate any data coming from the...
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> c++ project. need help.
Write a program named CheckMonth2 that prompts a user to enter a birth month and day....
Write a program named CheckMonth2 that prompts a user to enter a birth month and day. Display an error message that says Invalid date if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February). If the month and day are valid, display them with a message. For example, if the month entered is 2, and the day...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT