Results 1 to 3 of 3
  1. #1
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Angry User Inputting Integer, Returning Month Name That Goes With That Integer

    Ok so this is our assignment:
    1.Write a class called Month whose job it is to transform numbers 1, 2, 3, . . ., 12 into the corresponding month names January, February, March, . . ., December.
    2.Implement the class Month
    1.One Instance Variable called monthNumber that is an int representing the month ( 1, 2, 3 ... 12).
    2.Default constructor parameter that sets the monthNumber to 1.
    3.Parameterized constructor has one parameter an int that sets monthNumber
    4.An accessor and mutatator method for monthNumber
    5.A getName method
    1.Returns a String representing the month name.
    2.Hint: Make a very long string constant that contains "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.
    3.The returned String should not have any extra spaces, look in the Java String API class for a method that would remove extra spaces from the front of the String.
    4.DO NOT USE AN IF STATEMENT TO SOLVE THIS PROBLEM
    6.A constant int containing the number of months in a year may be helpful.
    3.Implement the class MonthTester
    1.Prompts the user to enter an int representing a month (1 - 12) no error checking required. (use the Scanner class)
    2.Print out a nicely formated String such as: Name for month 2 is February
    3.Repeat the code to allow the user to ask for 2 more month names, no looping required.

    This is what I have for the class:
    public class Month
    {
    public static final String months = "January February March April May June July August September October November December ";

    private int monthNumber;


    public Month()
    {
    monthNumber=1;
    }

    public Month(int m)
    {
    monthNumber=m;
    }

    public int getmonthNumber()
    {


    return monthNumber;
    }

    public void setmonthNumber (int amonthNumber)
    {
    monthNumber=amonthNumber;
    }

    public String getName()
    {

    int startIndex = (monthNumber*10)-10;
    int endIndex = monthNumber*10;
    return months.substring(startIndex, endIndex).trim();

    }

    public static final int MONTH_COUNT = 12;


    }

    And this is my tester:

    public class MonthTester
    {


    public static void main (String [] args)
    {

    Scanner months = new Scanner(System.in);
    int monthNumber;
    System.out.print("Enter month number: ");
    monthNumber = months.nextInt();
    Month m = new Month(monthNumber);
    System.out.println("Name for month "+m.getmonthNumber()+" is " + m.getName()+".");
    System.out.print("Enter month number: ");
    monthNumber = months.nextInt();
    System.out.println("Name for month "+ m.getmonthNumber()+" is " + m.getName()+".");
    System.out.print("Enter month number: ");
    monthNumber = months.nextInt();
    System.out.println("Name for month "+ m.getmonthNumber()+" is " + m.getName()+".");


    But for some reason every time it prompts to enter the month number if you type in "1"
    it says
    Name for month 1 is January
    if you type "2" it says the exact same thing. I think I have my algorithm to pull the correct month out but SOMETHING is wrong somewhere that is keeping the integer at 1 no matter what. PLEASE help, I've gone this far and this is the only thing keeping me from finishing this assignment :(

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: User Inputting Integer, Returning Month Name That Goes With That Integer

    You never change the monthNumber of the Month object referenced by the m local variable. Month has a method for doing this (look at your Month class code and try to find a "setter" method for this), one that should be called on the m variable when you wish to change its value.

  3. #3
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: User Inputting Integer, Returning Month Name That Goes With That Integer

    Quote Originally Posted by Fubarable View Post
    You never change the monthNumber of the Month object referenced by the m local variable. Month has a method for doing this (look at your Month class code and try to find a "setter" method for this), one that should be called on the m variable when you wish to change its value.
    Got it! Thank you so much! I noticed another class mate of mine posted on here and was having pretty much the same issues so between your answer and the other's on her post I've completed it :) yay!

Similar Threads

  1. Neeb question: returning an integer
    By Humphrey Bogart in forum New To Java
    Replies: 3
    Last Post: 02-25-2012, 01:01 AM
  2. Integer Comparison, Outputting Largest Integer Not Working
    By killingthemonkey in forum New To Java
    Replies: 4
    Last Post: 10-16-2011, 09:59 PM
  3. Replies: 5
    Last Post: 03-23-2011, 11:28 PM
  4. Read user input into integer array
    By varunb in forum New To Java
    Replies: 12
    Last Post: 07-09-2010, 01:50 PM
  5. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 01:50 AM

Posting Permissions

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