Java Program!!! Design a class named Month. The class should have the following
ID: 3885960 • Letter: J
Question
Java Program!!! Design a class named Month. The class should have the following private members: -name: a string object that holds the name ofthe month, such as January. -monthName: an integer value that holds thenumber of the month. For example, January would be 1, Februarywould be 2, and so on. Valid values for this variable should befrom 1 to 12. In addition, provide the following member functions: -a default constructor that sets monthNumber to1 and name to “January” -a constructor that accepts the name of the month as an argumentand finds and prints how many days in that month. For example,January has 31 days, and so on.
Explanation / Answer
Month.java
public class Month {
private String name;
private int monthNumber ;
public Month() {
monthNumber = 1;
name = "January";
}
public Month(String name) {
this.name = name;
if(name.equalsIgnoreCase("January")) {
monthNumber = 1;
} else if(name.equalsIgnoreCase("February")) {
monthNumber = 2;
} else if(name.equalsIgnoreCase("March")) {
monthNumber = 3;
}else if(name.equalsIgnoreCase("April")) {
monthNumber = 4;
}else if(name.equalsIgnoreCase("May")) {
monthNumber = 5;
}else if(name.equalsIgnoreCase("June")) {
monthNumber = 6;
}else if(name.equalsIgnoreCase("July")) {
monthNumber = 7;
}else if(name.equalsIgnoreCase("August")) {
monthNumber = 8;
}else if(name.equalsIgnoreCase("September")) {
monthNumber = 9;
}else if(name.equalsIgnoreCase("October")) {
monthNumber = 10;
}else if(name.equalsIgnoreCase("November")) {
monthNumber = 11;
}else if(name.equalsIgnoreCase("December")) {
monthNumber = 12;
}
System.out.println("Number of days in a month: "+getNumberOfDays(monthNumber));
}
public int getNumberOfDays(int month){
int days = 0;
if(month ==1 || month == 3 || month ==5 || month == 7 || month ==8 || month == 10 || month == 12){
days = 31;
}
else if(month == 4 || month == 6 || month == 9 || month ==11){
days = 30;
}
else if(month == 2){
days = 28;
}
return days;
}
public static void main(String a[]) {
new Month("April");
}
}
Output:
Number of days in a month: 30
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.