Java Programming I am having problems with creating the second class ShowMonth.
ID: 3557959 • Letter: J
Question
Java Programming
I am having problems with creating the second class ShowMonth. I have the main code correct and is working in Jgrasp without any problems.
Create a Java program that takes a month number as an input and outputs the proper month name. Your program should have two classes.
The first class, Month, should include the method MonthInput, which takes a user input and stores it in a class level variable. The second method MonthName of data type String, will accept one argument, a month number, and return the proper month name (use any decision structure type in this method body). The third method, DisplayMonth, will accept a string with the name of the month found and generate a user-friendly display screen with the user input entered and the name the month found.
The second class, ShowMonth, will have the main method. It will create an object from the Month class and call each of three instance methods defined in the Month class.
I will post the first class Month Code that I have correct:
import java.util.Scanner;
public class Month {
static String monthName;
static int monthNumber;
public void monthInput() {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a Month Number:");
monthNumber = input.nextInt();
}
public static void monthName() {
if (monthNumber == 1) {
monthName = "January";
} else if (monthNumber == 2) {
monthName = "February";
} else if (monthNumber == 3) {
monthName = "March";
} else if (monthNumber == 4) {
monthName = "April";
} else if (monthNumber == 5) {
monthName = "May";
} else if (monthNumber == 6) {
monthName = "June";
} else if (monthNumber == 7) {
monthName = "July";
} else if (monthNumber == 8) {
monthName = "August";
} else if (monthNumber == 9) {
monthName = "September";
} else if (monthNumber == 10) {
monthName = "October";
} else if (monthNumber == 11) {
monthName = "November";
} else {
monthName = "December";
}
}
public static void DisplayMonth() {
System.out.print(" The Month you choose is " + monthName + " and month number is " + monthNumber);
}
public static void main(String[] args) {
new Month().monthInput();
monthName();
DisplayMonth();
}
}
Now for ShowMonth I am getting any error message.
import java.util.Scanner;
public class ShowMonth {
public static void main(String[] args) {
//Error handling
try {
//Create the new object from the Month class
new Month().monthInput();
} catch (IOException e) {
e.printStackTrace();
}
// Execute the methods
Month.monthName();
Month.DisplayMonth();
}
}
Please assist with the second Class ShowMonth
Explanation / Answer
Besides what Chris has suggested, your program must also print number of days in that month. So here is the correct code:
import java.util.Scanner;
public class Month {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a month: ");
monthName = input.nextLine();
int month;
int Num_of_days;
switch (monthName) {
case "January": month = 1; break;
case "February": month = 2; break;
case "March": month = 3; break;
case "April": month = 4; break;
case "May": month = 5; break;
case "June": month = 6; break;
case "July": month = 7; break;
case "August": month = 8; break;
case "September": month = 9; break;
case "October": month = 10; break;
case "November": month = 11; break;
case "December": month = 12; break;
}
// Now calculate number of days in that month
if( month == 1 ) Num_of_days = 31;
else if( month == 2 ) Num_of_days = 28; // Standard February
else if( month == 3 ) Num_of_days = 31; // March
else if( month == 4 ) Num_of_days = 30; // April
else if( month == 5 ) Num_of_days = 31; // May
else if( month == 6 ) Num_of_days = 30; // June
else if( month == 7 ) Num_of_days = 31; // July
else if( month == 8 ) Num_of_days = 31; // August
else if( month == 9 ) Num_of_days = 30; // September
else if( month == 10 ) Num_of_days = 31; // October
else if( month == 11 ) Num_of_days = 30; // November
else Num_of_days = 31; // December
System.out.println("Month number is: " + month + "Number of days: " Num_of_days);
}
}
// Make sure no syntax errors.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.