-3

I have seen several examples using switch statements already with cases for 3 months at a time being a different season. But I want to create a method using a switch statement where the seasons start from specific date, for example:


December 21 - March 20 = Winter

March 21 - June 20 = Spring

June 21 - September = Summer

September 21 - December 20 = Autumn


I want to do it using a user-inputted int variable for each the month, and the date.

Please could someone tell me how I could go about doing this? Please bare in mind I am fairly new to coding

My current code includes a method to check if the date is valid but is still a work in progress:

package seasonclassification;

import java.util.Scanner;

public class SeasonClassification {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the month in terms of numbers (e.g March = 3) you wish to check the season of:");
    int calMonth = scan.nextInt();
    System.out.println("Enter the day in terms of numbers (e.g 27th = 27):");
    int calDay = scan.nextInt();
}

public static boolean isValidDate(int month, int day) {
    boolean x;
    if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12) && (day >= 1) && (day <= 31)) {
        x = true;
    }

    if ((month == 4) || (month == 6) || (month == 9) || (month == 11) && (day >= 1) && (day <= 30)) {
        x = true;
    }

    if ((month == 2) && (day >= 1) && (day <= 29)) {
        x = true;
    } 

    else {
        x = false;
    }

    return x;
}

public static String findSeason(int month, int day) {
    switch (month,day) {
    }
}
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
Jellson
  • 3
  • 1
  • 2

3 Answers3

1

First, you're prematurely optimizing. Second, it's ugly (really ugly). But, an example is what you need so you could

public static void main(String[] args) {
  java.util.Date[] testDates = new java.util.Date[] {
      new java.util.Date(114, 11, 20),
      new java.util.Date(114, 11, 21),
      new java.util.Date(114, 2, 20),
      new java.util.Date(114, 2, 21),
      new java.util.Date(114, 5, 20),
      new java.util.Date(114, 5, 21),
      new java.util.Date(114, 8, 20),
      new java.util.Date(114, 8, 21) };
  for (java.util.Date d : testDates) {
    System.out.print(d);
    System.out.print(" ");
    System.out.println(getSeasonName(d));
  }
}

private static String getSeasonName(
    java.util.Date date) {
  java.util.Calendar c = java.util.Calendar
      .getInstance();
  c.setTime(date);
  switch (c.get(java.util.Calendar.MONTH)) {
  case java.util.Calendar.MARCH:
    if (c.get(java.util.Calendar.DAY_OF_MONTH) > 20) {
      return "Spring";
    }
    return "Winter";
  case java.util.Calendar.APRIL:
  case java.util.Calendar.MAY:
    return "Spring";
  case java.util.Calendar.JUNE:
    if (c.get(java.util.Calendar.DAY_OF_MONTH) > 20) {
      return "Summer";
    }
    return "Spring";
  case java.util.Calendar.JULY:
  case java.util.Calendar.AUGUST:
    return "Summer";
  case java.util.Calendar.SEPTEMBER:
    if (c.get(java.util.Calendar.DAY_OF_MONTH) > 20) {
      return "Autumn";
    }
    return "Summer";
  case java.util.Calendar.OCTOBER:
  case java.util.Calendar.NOVEMBER:
    return "Autumn";
  case java.util.Calendar.DECEMBER:
    if (c.get(java.util.Calendar.DAY_OF_MONTH) < 21) {
      return "Autumn";
    }
  }
  return "Winter";
}

Outputs

Sat Dec 20 00:00:00 EST 2014 Autumn
Sun Dec 21 00:00:00 EST 2014 Winter
Thu Mar 20 00:00:00 EDT 2014 Winter
Fri Mar 21 00:00:00 EDT 2014 Spring
Fri Jun 20 00:00:00 EDT 2014 Spring
Sat Jun 21 00:00:00 EDT 2014 Summer
Sat Sep 20 00:00:00 EDT 2014 Summer
Sun Sep 21 00:00:00 EDT 2014 Autumn
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

A switch statement isn't something that you would use in this situation.

I would use the Date.compareTo() function to check if the date is within a range (usage explained here: http://www.mkyong.com/java/how-to-compare-dates-in-java/)

Something like:

SUMMER_START, SUMMER_END = // separate date for start, end of each season

if(date.compareTo(SUMMER_START) > 0 && date.compareTo(SUMMER_END) < 0){
    // date is in summer..
} else if(/* Another season */){
    // date is in this season..
}

Note that for this, you have to convert your dates into a Date object, probably like this (it will depend on your input..) What's the right way to create a date in Java?

Community
  • 1
  • 1
Will Richardson
  • 7,780
  • 7
  • 42
  • 56
  • Ah right okay, I'm new to programming at the moment so unaware of all the functions. I've been specifically asked to do it using a switch-statement however, is it impossible or just impractical? – Jellson Feb 11 '14 at 01:44
  • A switch statement should be used when there is a number of discrete possibilities that a variable could be, for example an `int` could be either 1, 2 or 3, and so you could `switch` to perform a different action depending which one it is equal to. You wouldn't use a switch to check if said `int` was between 0 and 4, though. That's what `if` statements are for. – Will Richardson Feb 11 '14 at 01:51
  • Okay, so could there possible be a way to use if statements to group the dates in terms of seasons and assign an int/string to each to then use a switch statement on? – Jellson Feb 11 '14 at 01:57
  • Yes, you could assign an `int` to a value in each `if` statement (0 to 3, for example) and then switch based on the value of that. – Will Richardson Feb 11 '14 at 02:10
0

You could use a switch-statement on month and then use an if-statement in the cases where a month could be part of more than one season. For example:

public static String findSeason(int month, int day) { 
    switch (month) {
        case 1: // January - Same season; Fall through to next case
        case 2: // February
            return "Winter";
        case 3: // March
            // Season change - check day of month
            return (day <= 20 ? "Winter" : "Spring");
        /* The rest of your cases here */ 

I guess it's possible to use a switch on day in place of an if-statement but again, it wouldn't be appropriate for that situation. You'd also have to write about 30 or so cases for each day of the month and that wouldn't be fun nor would it be an elegant solution.

Edit: That's a ternary operator in case 3. You can read about it here.

PakkuDon
  • 1,627
  • 4
  • 22
  • 21