P2.19 Write a program that transforms numbers 1, 2, 3, ..., 12 into the correspo
ID: 3584688 • Letter: P
Question
P2.19 Write a program that transforms numbers 1, 2, 3, ..., 12 into the corresponding month names January, February, March, ..., December Hint: 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. Here's a sample run with your program: silo.cs.indiana.edu%java Months Please enter a month number from 1 to 12. 2 February silo.cs.indiana.edu%java Months Please enter a month number from 1 to 12. 12 December silo.cs.indiana.edu%java Months Please enter a month number from 1 to 12. 1 January silo.cs.indiana.edu%java Months Please enter a month number from 1 to 12. 14 Exception in thread "main" [...] silo.cs.indiana.edu%Explanation / Answer
This is the solun for your problem plz rate it..
import java.util.Scanner;
public class Month {
private String[] months;
private int number;
public Month(String[] months, int number) {
super();
this.months = months;
this.number = number;
}
public Month() {
super();
}
public String[] getMonths() {
return months;
}
public void setMonths(String[] months) {
this.months = months;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public static void main(String[] args)
{
String[] month={"January","February","March","April","May","June","July","Augest","Septmber","October","November","December"};
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no of month between 1 to 12 :");
Month m=new Month();
m.setMonths(month);
m.setNumber(sc.nextInt());
if(m.getNumber()<=12 && m.getNumber()>0 )
{
System.out.println("Your Selected month is :"+m.getMonths()[m.getNumber()-1]);
}
else
{
System.out.println("invalide month number");
}
}
}
output
Enter the no of month between 1 to 12 :
2
Your Selected month is :February
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.