JAVA ONLY Stringing data in arrays. Printing the month name given a month number
ID: 3706247 • Letter: J
Question
JAVA ONLY
Stringing data in arrays.
Printing the month name given a month number as keyboard input
For this assignment, you will obtain an integer between 1 and 12 as keyboard input. Based on that input, you will print the corresponding month name, following the additional requirements below:
Requirements:
1. The month number must be obtained via keyboard input in the main method and must be validated to be an integer between 1 and 12
2. The month names must be stored in a String array using a method that you create.
3. A method named printMonths must be coded and used to print the entire array of month names
3. A method named findMonth must be used to find the correct month name when passed the input month number
4. The month name must be printed as part of the findMonth method
Note: ONLY use methods and structures we have covered in this class for the assignment! You should have 3 methods + a main method to receive credit for this assignment.
Sample output:
Enter the month number you would like translate to a month name: 12
Printing array of month names:
January, February, March, April, May, June, July, August, September, October, November, December
Month number 12 corresponds to December
Explanation / Answer
package January;
import java.util.Scanner;
// class DisplayMonthName definition
public class DisplayMonthName
{
// Creates a string array to store month names
String monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
// Method to return true if month number is valid otherwise return false
boolean validateMonthNumber(int monthNumber)
{
// Checks if the month number is greater than or equals to 1
// and less than or equals to 12 then return true
if(monthNumber >= 1 && monthNumber <= 12)
return true;
// Otherwise return false
else
return false;
}// End of method
// Method to print month names
void printMonths()
{
// Loops till end of the string array monthNames
for(int x = 0; x < monthNames.length; x++)
// Display each month name stored at x index position
System.out.print(monthNames[x] + ", ");
}// End of method
// Method to display the month name of the corresponding month number passed as parameter
void findMonth(int monthNumber)
{
// Display the one less index position of the month number
// because month number start from 1 for January
// Array index position starts from 0 for January
System.out.printf(" Month number %d corresponds to %s", monthNumber, monthNames[--monthNumber]);
}// End of method
// main method definition
public static void main(String[] ss)
{
// Creates an object of class
DisplayMonthName dmn = new DisplayMonthName();
// Scanner class object created to accept data from console
Scanner sc = new Scanner(System.in);
// To store the month number
int monthNumber;
// Loops till valid month number entered by the user
do
{
// Accepts a month number from the user
System.out.print(" Enter the month number you would like translate to a month name: ");
monthNumber = sc.nextInt();
// Calls the method to validate the month number
if(dmn.validateMonthNumber(monthNumber))
// If valid month number then come out of the loop
break;
// Otherwise display the error message and continue
else
System.out.print(" Invalid month number. Try again.");
}while(true); // End of do - while loop
// Calls the method to display all month names stored in string array
System.out.println(" Printing array of month names:" );
dmn.printMonths();
// Calls the method to display the month name corresponding month number
dmn.findMonth(monthNumber);
}// End of main method
}// End of class
Sample Output:
Enter the month number you would like translate to a month name: 14
Invalid month number. Try again.
Enter the month number you would like translate to a month name: 12
Printing array of month names:
January, February, March, April, May, June, July, August, September, October, November, December,
Month number 12 corresponds to December
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.