Pregunta

Recently I received this assignment in my Beginning Computer Science Class:

Write a program that asks the user to enter a month (1 = January, 2 = February, and so on) and then prints the number of days of the month. For February, print “28 days”. Enter a month (1-12): 5 31 days Implement a class Month with a method int getDays(). Do not use a separate if or else statement for each month. Use Boolean operators.

I am not asking for someone to complete the mentioned assignment, but rather critique my own solution. I did some basic searching before asking this, but most if not all solution use the Calendar class and calculate the number of days that way. The object of this assignment is to convey the idea of conditional statements and boolean operators.

My Solution: Season.java

/**
 * @author Jared Holley
 * Date: 4/01/14
 * Period: 3rd
 * 
 * Write a program that asks the user to enter a month (1 = January, 2 = February, and
 * so on) and then prints the number of days of the month. For February, print
 * “28 days”.
 * 
 * With a method int getDays(). Do not use a separate if or else
 * statement for each month. Use Boolean operators.
 */

 //31: 1,3,5,7,8,10,12
 //30: 2,4,6,9,11
 public class Month {
  private int month;

  /**
   * The constructor for the Month class.
   * Simply takes in a monthNumber and sets it to the 
   * class variable.
   * @param monthNumber
   */
  public Month(int monthNumber){
    month = monthNumber;
  }

  /**
   * The method that converts a month number into the number of days within that month.
   * It first checks if the month is February so that it eliminates that from the        following conditions that would produce a false positive.
   * It then goes through and uses a rather odd conditional statement.
   * The first half of the if statements increments all the months by one and checks if they are even.
   * This would turn January into 2 which does have 31 days and trun April into 5 which has 30 days.
   * The second half just checks the even months beyond 7.
   * Lastly it will just return 30 otherwise.
   * @return The numbers of days within the month.
   */
  public int getDays(){
    if(month == 2)return 28;
    if( ((month + 1) % 2 == 0 && month < 9) || ((month % 2 == 0) && month >= 8))   return 31;
    return 30;

  }
}

The solution works just fine. I would just like to know if there is a more elegant way of going about things. My conditional statement just looks very ugly. Thanks for the help!

¿Fue útil?

Solución

Although this may not be an ideal answer, below one-liner uses only one Boolean operator (==) and it is concise.

int daysInMonth = 31 - ((month == 2) ? 3 : ((month - 1) % 7 % 2));

An explanation of this algorithm can be found here (Solution #3):

http://www.dispersiondesign.com/articles/time/number_of_days_in_a_month

Otros consejos

All roads tend south...

But I think you reached the goal of your assignment. In my point of view, there are more elegant ways (and maybe faster, I think I need to do some measurements), like using Maps or enums, so you avoid ugly if - else statements like that.

And yes, month should be final because you don't plan on modifying the variable in this class. And yes, it is okay to set it to private (when in doubt, it's allways private)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top