# Java Programing! I'm stuck...please help me!!!



## shayduh (Sep 27, 2009)

Your job is to transform numbers 1, 2, 3, . . ., 12 into the corresponding month names January, February, March, . . ., December. Implement a class Month whose constructor parameter is the month number and whose getName method returns the month name. _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.

Use the following class as your tester class: 
/**
This program tests the Month class.
*/
public class MonthTester
{
public static void main(String[] args)
{
Month jan = new Month(1);
System.out.println("Name: " + jan.getName());
System.out.println("Expected: January");
Month mar = new Month(3);
System.out.println("Name: " + mar.getName());
System.out.println("Expected: March");
Month dec = new Month(12);
System.out.println("Name: " + dec.getName());
System.out.println("Expected: December");
}
}

Can somebody help me to solve this question? I have no clue how to write the methods... I'm not looking for the answer (the actual code)...I just need some help... Thank You


----------



## Akihon (Dec 2, 2007)

I would do something like this, if you are allowed; -


```
public static void main(String[] args) {
        // TODO code application logic here
        TreeMap<Integer, String> monthMap;

        monthMap = new TreeMap<Integer, String>();
        monthMap.put(1, "January");
        monthMap.put(2, "February");
        monthMap.put(3, "March");
        monthMap.put(4, "April");
        monthMap.put(5, "May");
        monthMap.put(6, "June");
        monthMap.put(7, "July");
        monthMap.put(8, "August");
        monthMap.put(9, "September");
        monthMap.put(10, "October");
        monthMap.put(11, "Novemeber");
        monthMap.put(12, "December");
        System.out.println("The third month is: " + monthMap.get(3)); 


    }
```


----------



## shayduh (Sep 27, 2009)

The code that you wrote, didn't work...  but I made this code instead and the compiler passed.:up: Thank you anyways, you'd help me a lot. 

public class Month
{
/**
Constructs the names of months in a year.
@param aMonthNumber of the month
*/
public Month(int aMonthNumber)
{
monthNumber = aMonthNumber;
}

/**
Gets the name of the month.
@return name of the month
*/
public String getName()
{
return MONTHS.substring((monthNumber - 1) * spacesPerMonth,
monthNumber * spacesPerMonth);
}

private int monthNumber;

private static final int MONTHS_PER_YEAR = 12;
private static final String MONTHS =
" January" +
" February" +
" March" +
" April" +
" May" +
" June" +
" July" +
" August" +
"September" +
" October" +
" November" +
" December";
private static final int spacesPerMonth = MONTHS.length() / MONTHS_PER_YEAR;
}


----------



## DaBeers (Oct 31, 2008)

This will be much easier when you get to Arrays


----------



## strived (Oct 5, 2009)

Can anyone help please.
I read the code yet cant trace what the return type method here is doing:

public String getName()
{
return MONTHS.substring((monthNumber - 1) * spacesPerMonth,
monthNumber * spacesPerMonth);
}

Not too sure about the logic here. Any help would be appreciated in tracing the code. And explaining what is going on. All the rest is logical.


----------



## DoubleHelix (Dec 10, 2004)

Replying to your own thread under a different username doesn't change anything. You still have to do your own homework. It's only going to get harder.


----------



## strived (Oct 5, 2009)

First - This is not my own thread.
Second - The question has a working solution by the author. I am just asking how a piece of code works which is this : 

public String getName()
{
return MONTHS.substring((monthNumber - 1) * spacesPerMonth,
monthNumber * spacesPerMonth);
}

I am having a hard time figuring out the logic behind making the monthNumber - 1 and then multiplying it by the spacesPerMonth seperated string. 

Thanks.


----------



## JimmySeal (Sep 25, 2007)

The solution shayduh posted doesn't actually work correctly. It just compiles without error.

He/she seems to have missed this part of the assignment:


> in which you add spaces such that each month name has the same length


though the logic in the line that uses substring() is essentially correct.

I also think that arrays would be the most logical way to approach this problem, though maybe shayduh's class hasn't covered them yet. Using a TreeMap is overkill, since the month numbers are sequential numbers from 1 to 12.


----------

