I was presented with this problem: Create a Java class MonthNames with a single
ID: 3747319 • Letter: I
Question
I was presented with this problem:
Create a Java class MonthNames with a single static method getMonthName which Takes a single integer corresponding to a month of the year, 1 representing January through 12 representing December, and Returns a string for the name of the month.
This is what I have so far
public class MonthNames {
public static String getMonthName (int numMonth) {
return "month"; }
public static void main (String [] args) {
System.out.println ("begin tests");
System.out.println ("Enter Month 1");
System.out.println ("Expect: January");
System.out.println ("Actual: " + getMonthName (1));
System.out.println ("Enter Month 6");
System.out.println ("Expect: June");
System.out.println ("Actual: " + getMonthName (6));
System.out.println ("Enter Month 12");
System.out.println ("Expect: December");
System.out.println ("Actual: " + getMonthName (12));
System.out.println ("End tests");
if(numMonth = 1) { System.out.println("January"); }
else(numMonth = 2) { System.out.println("February"); }
else(numMonth = 3) { System.out.println("March"); }
else(numMonth = 4) { System.out.println("April"); }
else(numMonth = 5) { System.out.println("May"); }
else(numMonth = 6) { System.out.println("June"); }
else(numMonth = 7) { System.out.println("July"); }
else(numMonth = 8) { System.out.println("August"); }
else(numMonth = 9) { System.out.println("September"); }
else(numMonth = 10) { System.out.println("October"); }
else(numMonth = 11) { System.out.println("November"); }
else(numMonth = 12) { System.out.println("December"); }
else {System.out.println("Not a month"); }
What else do i need to do to this code in order to make it work? (JAVA)
Explanation / Answer
public class MonthNames { public static String getMonthName(int numMonth) { if (numMonth == 1) { return "January"; } else if (numMonth == 2) { return "February"; } else if (numMonth == 3) { return "March"; } else if (numMonth == 4) { return "April"; } else if (numMonth == 5) { return "May"; } else if (numMonth == 6) { return "June"; } else if (numMonth == 7) { return "July"; } else if (numMonth == 8) { return "August"; } else if (numMonth == 9) { return "September"; } else if (numMonth == 10) { return "October"; } else if (numMonth == 11) { return "November"; } else if (numMonth == 12) { return "December"; } else { return "Not a month"; } } public static void main(String[] args) { System.out.println ("begin tests"); System.out.println ("Enter Month 1"); System.out.println ("Expect: January"); System.out.println ("Actual: " + getMonthName (1)); System.out.println ("Enter Month 6"); System.out.println ("Expect: June"); System.out.println ("Actual: " + getMonthName (6)); System.out.println ("Enter Month 12"); System.out.println ("Expect: December"); System.out.println ("Actual: " + getMonthName (12)); System.out.println ("End tests"); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.