In: Computer Science
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.
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; } }
===================================================================