Results 1 to 7 of 7
  1. #1
    potrero is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Month Class with constructors/adders/getters

    Hi all,

    This is a homework problem and I do not want the whole solution. But I would really appreciate some help as to what the question is asking (there's no office hours or lab time until Thursday and I really want to get started before then).

    Write a class Month that represents one of the twelve months of the year. It should have three attributes for
    the name of the month,
    the number of days in the month, and
    the birthstone.
    Also add constructors and getter/setter methods to access the attributes.

    You may use the following code to test your class.

    Java Code:
    import java.util.*;
    
    public class Month
    {
    	
    	// ADD CODE HERE!!!
    	
    	public static void main(String[] args)
    	{
    		Month[] months = new Month[12];
    		months[0] = new Month("January", 31, "Garnet");
    		months[1] = new Month("February", 28, "Amethyst");
    		months[2] = new Month("March", 31, "Aquamarine");
    		months[3] = new Month("April", 30, "Diamond");
    		months[4] = new Month("May", 31, "Emerald");
    		months[5] = new Month("June", 30, "Alexandrite");
    		months[6] = new Month("July", 31, "Ruby");
    		months[7] = new Month("August", 31, "Peridot");
    		months[8] = new Month("September", 30, "Sapphire");
    		months[9] = new Month("October", 31, "Opal");
    		months[10] = new Month("November", 30, "Topaz");
    		months[11] = new Month("December", 31, "Blue Zircon");
    		
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Please enter a month (number 1 - 12): ");
    		int a = scan.nextInt();
    		if (a < 1 || a > 12)
    		{
    			System.out.println("Bad input!");
    			return;
    		}
    
    		System.out.println(months[a - 1]);
    	}
    }
    So what I have added so far is (under public class month { :)
    Java Code:
    String monthName, monthBirthStone;
    	int monthDays;
    	
    	public Month (String name, int days, String birthstone)
    	{
    		monthName = name;
    		monthBirthStone=birthstone;
    		monthDays=days;
    	}
    So I believe that is the constructor. I still do not understand several things:
    What would I need the getter and setter for?
    I tested it using just the above code, and using month 1 I got:
    Month@5a1cfb56
    This makes sense as I obviously didn't do anything in order to get it in a String format for the array. But I do not understand this still - how would I get the constructor to output a string (to then be in the array?)

    Thanks for your help, please let me know if you need any clarification.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Month Class with constructors/adders/getters

    Quote Originally Posted by potrero View Post
    So I believe that is the constructor.
    Correct.

    Quote Originally Posted by potrero View Post
    What would I need the getter and setter for?
    Getter methods are used to *get* attributes (variables) of a particular instance of a class. Setters are used to *set* those attributes. You should make your monthName, monthBirthStone, and monthDays variables private. Then if somebody wants to change, for example, the number of days in February, they would call your february month's setMonthDays() method, which would take a parameter.

    Quote Originally Posted by potrero View Post
    how would I get the constructor to output a string (to then be in the array?)
    Your constructor wouldn't output a String. You would override the toString() method and return whatever a month should look like as a String.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    potrero is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Re: Month Class with constructors/adders/getters

    Thank you so much! That really did help a lot, and I think writing the getters/setters should be easy. I do not quite understand the toString() aspect though. I am trying to leave the code the prof gave completely alone (as that is what is used to test it,) so would I use the toString method in the constructor? If so, how? Thanks, this is the first class I've written so I'm still very confused about a lot of it.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Month Class with constructors/adders/getters

    Look at this line:

    Java Code:
    System.out.println(months[a - 1]);
    You're telling Java to print out a Month. But Java doesn't know what a Month should look like printed out, so you have to tell Java how to convert a Month to a String. You do that by adding a toString() method to your Month class.

    The toString() method is completely separate from the constructor, just like the getter and setter functions are completely separate from the constructor. You wouldn't have to change anything your prof gave you, just like you didn't have to change anything to add the getter and setter functions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    potrero is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Re: Month Class with constructors/adders/getters

    Thanks! So I got it working (I can PM you the code if you want, I just don't want to post the whole thing as it is part of an assignment and my school has a zero tolerance policy,) but I still do not understand why.

    I created a toString() method, and that was fine, but I do not understand how when, in the main method, System.out.println(months[a-1]); actually calls the toString method. It obviously does as I tested it and it works well, but how/why is it called? To actually get the months names and all of that I had to use the getter methods that I made, but I never specifically called the toString method...

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Month Class with constructors/adders/getters

    Basically, Java calls the toString() method for you whenever you pass it into the println() function.

    If you want to know the details, the System.out.println() method looks like this:

    Java Code:
     public void println(Object x) {
            String s = String.valueOf(x);
            synchronized (this) {
                print(s);
                newLine();
            }
        }
    Notice that the function takes an Object as an argument.

    Your Month class is an Object (it has an invisible "extends Object" after the classname), which means that it **inherits** the functions inside the Object class. One of those functions is the default toString() function, which is where the ugly-looking Month@5a1cfb56 String was coming from.

    By adding a toString() method to your Month class, you're **overriding** the toString() function in the Object class, which basically says "don't do the default, do this stuff instead".

    Anyway, back to the println() function, notice that it passes your Month object into the String.valueOf() function, which looks like this:

    Java Code:
       public static String valueOf(Object obj) {
            return (obj == null) ? "null" : obj.toString();
        }
    Finally, that code calls your toString() function, which returns your custom String value.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    potrero is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Re: Month Class with constructors/adders/getters

    Thank you so much! I finally understand it - that was a great explanation!

Similar Threads

  1. what is the use of thread class constructors
    By me_shankara in forum Threads and Synchronization
    Replies: 2
    Last Post: 05-15-2013, 03:04 PM
  2. Replies: 3
    Last Post: 03-11-2013, 03:03 PM
  3. Need help with class Month assignment please!!
    By ndsmith20 in forum New To Java
    Replies: 5
    Last Post: 10-17-2012, 11:26 PM
  4. Month class
    By FOX427 in forum New To Java
    Replies: 9
    Last Post: 07-14-2012, 07:40 PM
  5. Class and constructors question.
    By Meta in forum New To Java
    Replies: 19
    Last Post: 04-29-2010, 03:58 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
  •