In: Computer Science
This is in java, thank you!
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.
Below is the source code for the two classes:
1. Problem2 class:
/*
* class for calculating the season
*/
public class Problem2 {
//month and day variables
private int month;
private int day;
//constructor
public Problem2(int m, int d) {
month = m;
day = d;
}
/*
* Method to calculate the season
*/
public String calcSeason() {
String season = "";
if ((month == 1) || (month == 2) ||
(month == 3)) {
season =
"Winter";
}
else if ((month == 4) || (month ==
5) || (month == 6)) {
season =
"Spring";
}
else if ((month == 7) || (month ==
8) || (month == 9)) {
season =
"Summer";
}
else {
season =
"Fall";
}
//if month is divisible by 3 and
day is greater than or equal to 21
if ((month%3 == 0) && (day
>= 21)) {
if (season ==
"Winter") {
season = "Spring";
}
else if (season
== "Spring") {
season = "Summer";
}
else if (season
== "Summer") {
season = "Fall";
}
else {
season = "Winter";
}
}
return season;
}
}
2. Problem2Tester Class:
import java.util.Scanner;
/**
* Class for testing the Problem2
*/
public class Problem2Tester {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Print the month
as number: ");
int month = sc.nextInt();
System.out.println("Print the day
as number: ");
int day = sc.nextInt();
Problem2 pr = new Problem2(month,
day);
System.out.println("Season is: " +
pr.calcSeason());
}
}
Please comment for any further queries. Thanks