Results 1 to 10 of 10
Like Tree5Likes
  • 1 Post By awinston
  • 1 Post By FlipPoker@gmail.com
  • 1 Post By cselic
  • 1 Post By FlipPoker@gmail.com
  • 1 Post By JosAH

Thread: Month class

  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Post Month class

    Hi All! Here is the exercise which asks to transform numbers from 1 to 12 into the corresponding month names january, feb and etc. Need to implement class whose constructor parameter is the month number and whose getName method returns the month name. It also gives hint: Make a very long string "January February....", in which you add spaces such that each month name has the same length.Then use substring to extract the month you want.

    basically, I just have the beginning only:
    Month class:

    Java Code:
    import java.util.Scanner;
    
    public class Month {
    	public Month()
    	{
    		Scanner in = new Scanner(System.in);
    		System.out.println("Please enter month digit");
    		digit = in.nextInt();
    		
    	}
    	public String getName()
    	{
    		String monthName = new String("JanFebMarAprMayJunJulAugSepOctNovDec");
    		String january = monthName.substring(0,2);
    		String february = monthName.substring(3,5);
                    //the same method for other months as above
    	
    		return monthName;
    	}
    	private int digit;
    
    }
    and MonthTester class:

    Java Code:
    public class MonthTester {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Month month = new Month();
    		month.getName();
    
    	}
    
    }
    Ok, I understand what should I do but I cannot digit with the month I need, what should I use here? Integer.parseInt, Integer.toString? How to connect them? Thanks for help!

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Month class

    Are you required to follow the "hint"?

    I ask because this seems like a very complex way of going about a simple task. A better way to solve the problem is by utilizing an array: Arrays (The Java Tutorials > Learning the Java Language > Language Basics)
    FOX427 likes this.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default Re: Month class

    I am following the book, I didn't study arrays yet, so I better to use the hint :)

  4. #4
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default Re: Month class

    It looks like this is an exercise in loops. The first thing you should do is fix your month string. Since "September" is the largest name, you have to pad your month names to 9 spaces: "January February March April May ...
    FOX427 likes this.

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Month class

    From this: String monthName = new String("JanFebMarAprMayJunJulAugSepOctNovDec"); make new array of Strings. For example lets call it monthArray.
    monthArray[0] = "Jan";
    monthArray[1] = "Feb";
    ...
    ...
    monthArrat[11] = "Dec";

    then, when you need to show for example 4th month you can just return monthArray[3].
    FOX427 likes this.

  6. #6
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default Re: Month class

    There are many ways to solve this problem. But the original requirement is to declare a month string and walk it using the substring method. Here's one way to do this:

    Java Code:
        
    public static String getMonthName(int i) {
        	if ((i < 1) || (i > 12))  // user must enter 1 - 12
        	{
        		System.err.println("error - invalid month index:  " + i);
        		return "";
        	}
        	
        	String monthName = "January  February March    April    May      June     July     August   SeptemberOctober  November December "; 
        	int startIndex = (i-1)*9;
        	int endIndex = startIndex + 9;
        	return monthName.substring(startIndex, endIndex);
    
    }
    The key to understanding this is lines 8-10.
    FOX427 likes this.

  7. #7
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default Re: Month class

    thanks, but I haven't study arrays yet...

  8. #8
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default Re: Month class

    Thanks, but I haven't study decisions yet, have to use hint for exercise...

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Month class

    Enums? Or haven't you studied them either? Using enums you don't need arrays of Strings nor any hanky panky with single Strings.

    kind regards,

    Jos
    FOX427 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default Re: Month class

    I haven't studied them either! :D It's ok, I will move to the next exercise and will come back to this after I learn enum, arrays ordecisions :))Thanks!

Similar Threads

  1. [Req] a timer that Day Month Year
    By AhrimanSefid in forum New To Java
    Replies: 5
    Last Post: 04-13-2011, 10:17 AM
  2. Verifying of Month
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 2
    Last Post: 11-02-2010, 04:34 PM
  3. get the value from a table in a month range
    By javastuden in forum JDBC
    Replies: 1
    Last Post: 02-24-2010, 07:53 AM
  4. Current month
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 08:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •