Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Java program that takes a month number as an input and outputs the prop

ID: 3547947 • Letter: C

Question

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.

import java.util.Scanner;

public class Month
{


   String monthName;
   int monthNumber;

   public void monthInput(){
   
      Scanner input=new Scanner(System.in);
      System.out.print("Please enter a Month Number:");
      monthNumber = in.nextInt();
   }
      
   public static void monthName(){
   
      if(monthName >= 1)
      {
         monthName = "January";
      }
      else if(monthName >= 2)
      {
         monthName = "February";
      }
      else if(monthName >= 3)
      {
         monthName = "March";
      }
      else if(monthName >= 4)
      {
         monthName = "April";
      }
      else if(monthName >= 5)
      {
         monthName = "May";
      }
      else if(monthName >= 6)
      {
         monthName = "June";
      }
      else if(monthName >= 7)
      {
         monthName = "July";
      }
      else if(monthName >= 8)
      {
         monthName = "August";
      }
      else if(monthName >= 9)
      {
         monthName = "September";
      }
      else if(monthName >= 10)
      {
        monthName = "October";
      }
      else if(monthName >= 11)
      {
         monthName = "November";
      }
      else
      {
         monthName = "December";
      }
   }
         
   public static void DisplayMonth()
   {
      System.out.print(" The Number of the Month you choose is" + monthName);
   }



}

Explanation / Answer

Hi,


In your code there is no main method. I have created one. Actually the concept is when you start your java program, JAVA search for main method first. So its mandatory to write main method in standalone java application :)


Another thing that u missed was : u have declared String monthName, and int monthNumber as the instance variable.

you cannot use the instance variable directly in the static method. So here u have to declare the variable as static.


I have created the main method and have called the methods.

Please take note of how I am calling static and non-static method.


Static method u can call directly in main method by simply writing

monthName();

DisplayMonth();


Whereas for non-static method u have to create the object first and than invoke the method