user2844903
user2844903

Reputation: 11

Java class for Month Object

I'm working on a Java class for a Month object but it does not return the actual month, can you help?

Here is the Private methods class:

public class Month
{
private static int numInstantiated = 0;
private int monthNumber = 0;

public Month()
{
    monthNumber = 1;
    numInstantiated++;
}
public Month(int num)
{
    monthNumber = num;
    numInstantiated++;
}

public void setMonth(int newMonth)
{
    monthNumber = newMonth;
}
public int getMonth()
{
    return monthNumber;
}

public String toString()
{
switch(monthNumber)
{
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: return "No month specified";
}
}

}

And here is the main:

import java.util.Scanner;

public class UseMonth
{
public static void main(String[] args)
{
    Scanner kbd = new Scanner(System.in);
    Month m1 = new Month();
    Integer newMonth;

    //kbd.nextLine();
    System.out.print("Please enter a numeric representaion of the Month? (ex. 1 for January)");
    newMonth = kbd.nextInt();
    m1.setMonth(newMonth);

    System.out.println("You entered: " + newMonth + ", which is the month of " + m1.getMonth());
}
}

Thanks in advance - Fred

Upvotes: 1

Views: 3883

Answers (7)

Basil Bourque
Basil Bourque

Reputation: 339342

java.time.Month

Java 8+ offers the java.time.Month enum.

Get the current month.

Month currentMonth = LocalDate.now().getMonth() ;

Produce localized text of the month’s name.

String name = 
    currentMonth
        .getDisplayName( 
            TextStyle.FULL_STANDALONE , 
            Locale.of( "fr" , "CA" )      // French language, Canada culture.
        );  

septembre

Get the number of the month, 1-12 for January-December.

int currentMonthNumber = currentMonth.getValue() ;

9

Upvotes: 0

grepit
grepit

Reputation: 22392

I think all the previous answers are correct and have provided you with a good feedback/answers. There's more than one way to skin a cat. So, I have cleaned up your code a and remove some of the unnecessary method and I have also tested the program to make sure it works for you. Here are the the two classes for you.

public class Month {
    private static int numInstantiated = 0;

    public Month() {
        numInstantiated++;
        getMonthTextual(numInstantiated);
    }

    public String getMonthTextual(int monthNumber) {
        switch (monthNumber) {
        case 1:
            return "January";
        case 2:
            return "February";
        case 3:
            return "March";
        case 4:
            return "April";
        case 5:
            return "May";
        case 6:
            return "June";
        case 7:
            return "July";
        case 8:
            return "August";
        case 9:
            return "September";
        case 10:
            return "October";
        case 11:
            return "November";
        case 12:
            return "December";
        default:
            return "No month specified";
        }
    }

}

here is the main class:

import java.util.Scanner;
    public class test {
            public static void main(String[] args)
            {
                Scanner kbd = new Scanner(System.in);
                Month m1 = new Month();
                Integer newMonth;

                //kbd.nextLine();
                System.out.print("Please enter a numeric representaion of the Month? (ex. 1 for January)");
                newMonth = kbd.nextInt();

                System.out.println("You entered: " + newMonth + ", which is the month of " +   m1.getMonthTextual(newMonth));
            }
            }

Upvotes: 0

Sameer Sawla
Sameer Sawla

Reputation: 729

Replace the final line of your code with this :

System.out.println("You entered: " + newMonth + ", which is the month of " + m1.toString());

In your existing code, m1.getMonth() will return you the number of the month. Using m1.toString() will give the respective String equivalent of the entered Integer number.

Hope this helps.

Upvotes: 0

Rafa Romero
Rafa Romero

Reputation: 2716

You have ovewritten the toString method. If you just call your getMonth method, this one does not call automatically the toString method, so it will return the number of the month.

If you want yo see the name of the month, you must do one of this two options:

Change your getMonth method:

public String getMonth()
{
    return this.toString(monthNumber);
}

Or in your main method, change the System Out call:

System.out.println("You entered: " + newMonth + ", which is the month of " + m1.toString(m1.getMonth()));

Hope it helps!!

Upvotes: 0

user1675825
user1675825

Reputation:

try this..

System.out.println("You entered: " + newMonth + ", which is the month of " + m1.toString());

in your main class.

Upvotes: 0

OldCurmudgeon
OldCurmudgeon

Reputation: 65851

This would be a perfect opportunity to use an enum:

enum Month {
  January,
  February,
  March,
  April,
  May,
  June,
  July,
  August,
  September,
  October,
  November,
  December;

  public static Month get(int n) {
    return Month.values()[n-1];
  }

}

public void test() {
  for (int i = 1; i <= 12; i++) {
    System.out.println("Month " + i + " = " + Month.get(i));
  }
}

Upvotes: 1

SBI
SBI

Reputation: 2322

Well, you're calling getMonth() which returns an int. You'll want to call m1.toString(), which returns the string representation you're trying to print.

Upvotes: 3

Related Questions