Question: Write a program that prompts the user for one of the numbers 1, 2, 3, ..., 12 and outputs the corresponding month name January, February,

Write a program that prompts the user for one of the numbers 1, 2, 3, ..., 12 and outputs the corresponding month name January, February, March, ... , December.

Implementation: Create a class with a constructor that takes the number and a method that returns the month string. Make a very long string "January February March ... ", in which you add spaces such that each month name has the same length. Then use substring to extract the month you want. You will need to figure out a little arithmetic to get the correct limits for the substring.

Hints:
in your class
make a constructor to initialize the month number and the long string
make the long string (make each month 10 characters long, padded by spaces)
make an instance method getMonth() which computes the correct indices to give to substring(), then return the substring containing the correct month name.
in main
prompt user for month number
read in month number
create object passing month number to constructor
print result by calling instance method getMonth().

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!