Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started