Problem 101

Month Name

Write a function. It will return the name of a month of the year, given the month number, according to the table below. Make sure you do not put any input or output statements in the function; the month number will be passed in and the string containing the name will be returned.

Number        Month
1             January
2             February
3             March
4             April
5             May
6             June
7             July
8             August
9             September
10            October
11            November
12            December
anything else error

The function must be called monthName(), and must have one parameter (an int), and return a String.

And finally, here’s some starter code.

MonthName.java

    public static void main(String[] args) {
        System.out.println("Month 1: " + monthName(1));
        System.out.println("Month 2: " + monthName(2));
        System.out.println("Month 3: " + monthName(3));
        System.out.println("Month 4: " + monthName(4));
        System.out.println("Month 5: " + monthName(5));
        System.out.println("Month 6: " + monthName(6));
        System.out.println("Month 7: " + monthName(7));
        System.out.println("Month 8: " + monthName(8));
        System.out.println("Month 9: " + monthName(9));
        System.out.println("Month 10: " + monthName(10));
        System.out.println("Month 11: " + monthName(11));
        System.out.println("Month 12: " + monthName(12));
        System.out.println("Month 43: " + monthName(43));
    }

What you should see

Month 1: January
Month 2: February
Month 3: March
Month 4: April
Month 5: May
Month 6: June
Month 7: July
Month 8: August
Month 9: September
Month 10: October
Month 11: November
Month 12: December
Month 43: error

◄ 100: Distance Formula 138: Basic Arrays 0 ►



Adapted from ProgrammingByDoing.com
©2013 Graham Mitchell

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.