public class Month { private int monthNum; public Month() { this.monthNum = 1; }
ID: 3711185 • Letter: P
Question
public class Month
{
private int monthNum;
public Month()
{
this.monthNum = 1;
}
Month(int month) throws InvalidMonthException
{
if(month >= 1 && month <= 12)
this.monthNum = month;
else
throw new InvalidMonthException("The month number was incorrect");
}
Month(String monthName) throws InvalidMonthException
{
if(monthName.equalsIgnoreCase("january"))
{
monthNum = 1;
}
else if(monthName.equalsIgnoreCase("february"))
{
monthNum = 2;
}
else if(monthName.equalsIgnoreCase("march"))
{
monthNum = 3;
}
else if(monthName.equalsIgnoreCase("april"))
{
monthNum = 4;
}
else if(monthName.equalsIgnoreCase("may"))
{
monthNum = 5;
}
else if(monthName.equalsIgnoreCase("june"))
{
monthNum = 6;
}
else if(monthName.equalsIgnoreCase("july"))
{
monthNum = 7;
}
else if(monthName.equalsIgnoreCase("august"))
{
monthNum = 8;
}
else if(monthName.equalsIgnoreCase("september"))
{
monthNum = 9;
}
else if(monthName.equalsIgnoreCase("october"))
{
monthNum = 10;
}
else if(monthName.equalsIgnoreCase("november"))
{
monthNum = 11;
}
else if(monthName.equalsIgnoreCase("december"))
{
monthNum = 12;
}
else
{
throw new InvalidMonthException("Invalid month name was used");
}
}
public int getMonthNumber()
{
return monthNum;
}
public void setMonthNumber(int monthNum) throws InvalidMonthException {
if(this.monthNum >= 1 && monthNum <= 12)
this.monthNum = monthNum;
else
throw new InvalidMonthException("The month number was incorrect");
}
public String getMonthName() throws InvalidMonthException
{
String monthName = "";
switch(monthNum)
{
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;
}
case 12:
{
monthName = "December";
break;
}
default:
{
throw new InvalidMonthException("The month number was incorrect");
}
}
return monthName;
}
@Override
public String toString()
{
String monthname = "";
try
{
monthname=getMonthName();
} catch (InvalidMonthException e)
{
e.printStackTrace();
}
return monthname;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + monthNum;
return result;
}
public boolean equals(Month other)
{
if (monthNum != other.monthNum)
return false;
return true;
}//end module
}//end class
can you write the javadocs i.e. @exception @param @return?
Explanation / Answer
I have added the javadocs.. Please let me know if you have any issue...
/**
* @author
*
*/
public class Month {
private int monthNum;
public Month() {
this.monthNum = 1;
}
/**
* @param month
* @exception InvalidMonthException
*/
Month(int month) throws InvalidMonthException
{
if (month >= 1 && month <= 12)
this.monthNum = month;
else
throw new InvalidMonthException("The month number was incorrect");
}
/**
* @param monthName
* @exception InvalidMonthException
*/
Month(String monthName) throws InvalidMonthException
{
if (monthName.equalsIgnoreCase("january"))
{
monthNum = 1;
}
else if (monthName.equalsIgnoreCase("february"))
{
monthNum = 2;
}
else if (monthName.equalsIgnoreCase("march"))
{
monthNum = 3;
}
else if (monthName.equalsIgnoreCase("april"))
{
monthNum = 4;
}
else if (monthName.equalsIgnoreCase("may"))
{
monthNum = 5;
}
else if (monthName.equalsIgnoreCase("june"))
{
monthNum = 6;
}
else if (monthName.equalsIgnoreCase("july"))
{
monthNum = 7;
}
else if (monthName.equalsIgnoreCase("august"))
{
monthNum = 8;
}
else if (monthName.equalsIgnoreCase("september"))
{
monthNum = 9;
}
else if (monthName.equalsIgnoreCase("october"))
{
monthNum = 10;
}
else if (monthName.equalsIgnoreCase("november"))
{
monthNum = 11;
}
else if (monthName.equalsIgnoreCase("december"))
{
monthNum = 12;
}
else
{
throw new InvalidMonthException("Invalid month name was used");
}
}
/**
* @return monthNum
*/
public int getMonthNumber() {
return monthNum;
}
/**
* @param monthNum
* @exception InvalidMonthException
*/
public void setMonthNumber(int monthNum) throws InvalidMonthException {
if (this.monthNum >= 1 && monthNum <= 12)
this.monthNum = monthNum;
else
throw new InvalidMonthException("The month number was incorrect");
}
/**
* @return monthName
* @exception InvalidMonthException
*/
public String getMonthName() throws InvalidMonthException
{
String monthName = "";
switch (monthNum)
{
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;
}
case 12: {
monthName = "December";
break;
}
default: {
throw new InvalidMonthException("The month number was incorrect");
}
}
return monthName;
}
/**
* @return String
*/
@Override
public String toString() {
String monthname = "";
try {
monthname = getMonthName();
} catch (InvalidMonthException e) {
e.printStackTrace();
}
return monthname;
}
/**
* @return int hashcode
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + monthNum;
return result;
}
/**
* @param other
* @return boolean
*/
public boolean equals(Month other) {
if (monthNum != other.monthNum)
return false;
return true;
}// end module
}// end class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.