Hello guys,
I have this assignment for my homework,
Write a class named Month. The class should have an int field named monthNumber which will hold the number of the month. For example, January would be 1, February would be 2, etc. In addition, provide the following methods:
· A default constructor that sets the monthNumber field to 1.
· A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument.
· A constructor that accepts the name of the month, such as “January” , “February”, etc., as an argument. It should set the monthNumber field to the corresponding value for the month named.
· A setMonthNumber method that accepts an int argument, which is assigned to the monthNumber field.
· A getMonthNumber method that returns the value in the monthNumber field.
· A toString method that returns the same value as the getMonthName method.
Also write exception classes for the following error conditions:
· A number less than 1 or greater than 12 is given for the month number.
· An invalid string is given for the name of the month.
I've wrote the code already and it's compiling and working perfectly, but I can't do the exception handling and would appreciate any help....
public class Month { private int monthNumber; public Month() { monthNumber = 1; } public Month(int number) { if (number < 1 || number > 12) { monthNumber = 1; } else { number = monthNumber; } } public Month(String monthName) { if (monthName.equals("jan")) { monthNumber = 1; } else if (monthName.equals("feb")) { monthNumber = 2; } else if (monthName.equals("mar")) { monthNumber = 3; } else if (monthName.equals("apr")) { monthNumber = 4; } else if (monthName.equals("may")) { monthNumber = 5; } else if (monthName.equals("june")) { monthNumber = 6; } else if (monthName.equals("july")) { monthNumber = 7; } else if (monthName.equals("aug")) { monthNumber = 8; } else if (monthName.equals("sept")) { monthNumber = 9; } else if (monthName.equals("oct")) { monthNumber = 10; } else if (monthName.equals("nov")) { monthNumber = 11; } else if (monthName.equals("dec")) { monthNumber = 12; } } public void setMonthNumber(int number) { monthNumber = num; } public int getMonthNumber() { return monthNumber; } public String getMonthName() { String monthName; switch (monthNumber) { case 1: monthName = "january"; break; case 2: monthName = "february"; break; case 3: monthName = "march"; break; case 4: monthName = "april"; break; case 5: monthName = "may"; break; case 6: monthName = "june"; break; case 7: monthName = "july"; break; case 8: monthName = "august"; break; case 9: monthName = "september"; break; case 10: monthName = "october"; break; case 11: monthName = "november"; break; default: monthName = "december"; } return monthName; } public String toString() { return getMonthName(); } public boolean equals(Month month) { boolean result; if (month.getMonthNumber() == monthNumber) { result = true; } else { result = false; } return result; } public boolean greaterThan(Month month) { boolean result; if (month.getMonthNumber() < monthNumber) { result = true; } else { result = false; } return result; } public boolean lessThan(Month month) { boolean result; if (month.getMonthNumber() > monthNumber) { result = true; } else { result = false; } return result; } }
Thanks!