Problem2: In this problem you ask the user for a month and day. You will then ca
ID: 3710956 • Letter: P
Question
Problem2: In this problem you ask the user for a month and day. You will then calculate the season based on the month and day.
a.) Create a class named Problem2
b.) Create two integer instance variables, month and day.
c.) Create a constructor to initialize the variable. The constructor should have 2 arguments, m and d. Initialize month and day with these two arguments.
d.) Create a method called calcSeason. It should not have any arguments and should return a string.
e.) The method should implement the following algorithm:
If month is 1,2, or 3, season = “Winter”
Else if month is 4,5, or 6, season = “Spring”
Else if month is 7,8, or 9, season = “Summer”
Else season = “Fall”
If month is divisible by 3 and day >=21
If season is “Winter”, season = “Spring”
Else if season is “Spring”, season = “Summer”
Else if season is “Summer” season = “Fall”
Else season = “Winter
f.) Return Season
Problem 3: Create a class named Problem2Tester (reminder: all tester classes have a main method)
a.) Import Scanner
b.) Create a Scanner object
c.) Prompt the use to enter a month as a number
d.) Get the number from the user and store it as a variable
e.) Prompt the use to enter a day as a number
f.) Get the number from the user and store it as a variable
g.) Create a Problem2 object
h.) Print the result from calcSeason (this can be done directly in the print statement or you can create a new variable and print it)
Explanation / Answer
import java.util.Scanner;
class Problem2 {
int month;
int day;
/**
* @param month
* @param day
*/
public Problem2(int month, int day) {
super();
this.month = month;
this.day = day;
}
String calcSeason(){
if(month>=1 && month <=3){
if(month % 3 == 0 && day >=21)
return "Spring";
else
return "Winter";
}else if(month>=4 && month <=6){
if(month % 3 == 0 && day >=21)
return "Summer";
else
return "Spring";
}else if(month>=7 && month <=9){
if(month % 3 == 0 && day >=21)
return "Fall";
else
return "Summer";
}else{
if(month % 3 == 0 && day >=21)
return "Winter";
else
return "Fall";
}
}
}
public class Problem2Tester {
public static void main(String args[]) {
Scanner console = new Scanner(System.in);
int month = -99;
int day = -99;
do {
System.out.print("Enter month as number: ");
month = console.nextInt();
System.out.print("Enter day as number: ");
day = console.nextInt();
} while (!((month <= 12 && month >= 1) && (day >= 1 && day <= 31)));
Problem2 prob2 = new Problem2(month, day);
System.out.println(prob2.calcSeason());
}
}
Output
Enter month as number: 4
Enter day as number: 21
Spring
Enter month as number: 9
Enter day as number: 22
Fall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.