# Some Clarification

I apologize in advance for two things
1) If this is in the wrong section, and there's somewhere specific for Computer Science homework questions
2) how long this is going to be.

I'm a first year comp-sci student, we just recently learned about if else, while, do-while, for stuff like that, anyway, on this assignment i got there's a question with about 9 parts, and it's not that i need help writing the code, it's just i need help with clarification. Cause it seems really long winded and kind of redundant. So anything will help. I'll number the parts to make it easier.

2. Write, test and document (internally and externally) a Java program to solve the following problem:

Write a Java class named Month. The class should have an int attribute named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods:

a. A no-argument constructor that sets the monthNumber field to 1.

c. A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value less than 1 or greater than 12 is passed, the constructor should set monthNumber to 1.

d. A constructor that accepts the name of the month, such as “January” or “February” as an argument. It should set the monthNumber field to the correct corresponding value.

e. A setMonthNumber method that accepts an int argument, which is assigned to the monthNumber field. If a value less than 1 or greater than 12 is passed, the method should set monthNumber to 1.

f. A getMonthNumber method that returns the value in the monthNumber field.

g. A getMonthName method that returns the name of the month. For example, if
the monthNumber field contains 1, then this method should return “January”.

h. An equals method that accepts a Month object as an argument. If the argument object holds the same data as the current object, this method should return true.
Otherwise, it should return false.

i. A greaterThan method that accepts a Month object as an argument. If the
current object’s monthNumber field is greater than the argument’s monthNumber field, this method should return true. Otherwise, it should return false.

j. A lessThan method that accepts a Month object as an argument. If the current object’s monthNumber field is less than the argument’s monthNumber field, this method should return true. Otherwise,it should return false.

That's all ONE question, so again, i apologize i can't be of more help, but if anyone could even help with the first couple, it might fall into place. I'm a first year student so it can't be that difficult i imagine. But it would be amazing if you could maybe summarize each step, in an easier way. Something. Again thank you for an input.

I apologize in advance for two things
1) If this is in the wrong section, and there's somewhere specific for Computer Science homework questions
2) how long this is going to be.

I'm a first year comp-sci student, we just recently learned about if else, while, do-while, for stuff like that, anyway, on this assignment i got there's a question with about 9 parts, and it's not that i need help writing the code, it's just i need help with clarification. Cause it seems really long winded and kind of redundant. So anything will help. I'll number the parts to make it easier.

2. Write, test and document (internally and externally) a Java program to solve the following problem:

Write a Java class named Month. The class should have an int attribute named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods:
It's hard to answer when I don't know how much you know about object oriented programming in Java.

So the class has one main "state variable". A variable that holds the state of the object. We're just talking about an int class variable called monthNumber. It should almost certainly be private, not public.
a. A no-argument constructor that sets the monthNumber field to 1.
You know how to write constructors? Pretty straightforward, I think. Make one that takes no parameters which does nothing but set the monthNumber variable to 1.

c. A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value less than 1 or greater than 12 is passed, the constructor should set monthNumber to 1.
A constructor that takes one int parameter. It should take that value and set the monthNumber class variable to the value of the parameter.
d. A constructor that accepts the name of the month, such as “January” or “February” as an argument. It should set the monthNumber field to the correct corresponding value.
Ok, here's our first bit that requires a bit of thought. There are different ways to do this, but the short of it is you want to accept a month name (so a parameter of type String).

One way would be to build a HashMap which has the month name as a key and the month number as the value. You look up the key (i.e. the String month name parameter you were passed) and get the associated month number as a result.

You could also search for the name in each entry in a list/array of Strings. Not usually efficient, but it's a rather small list. Due to a requirement farther down, I prefer this approach.

If you use a HashMap, make sure it's initialized before you use it (by adding the name/value pairs (Month name, Month number) to the HashMap).
e. A setMonthNumber method that accepts an int argument, which is assigned to the monthNumber field. If a value less than 1 or greater than 12 is passed, the method should set monthNumber to 1.
Pretty much what you did in the constructor that accepts an int representing the month number. You may want to write this function, then just call it from the appropriate constructor.
f. A getMonthNumber method that returns the value in the monthNumber field.

Pretty hard to put that any clearer. But if you don't understand, I can give an example to make it more obvious.
g. A getMonthName method that returns the name of the month. For example, if
the monthNumber field contains 1, then this method should return “January”.
Sort of leaning towards preferring using just an array rather than a HashMap for this. You could just have a HashMap and an array. But perhaps better in this case is just to have an array of the names, where the index where it is found is associated to the month number.

Ok, you have month numbers from 1 to 12. That's 12 values. If you put the names in an array, you can just take one off the month number to get indexes going from the proper 0 to 11. So let's say you get the number '3'. You can take one off that to make it 2, and look that up in an array of month names something like this:

String monthName = monthNames[monthNumber-1];

Note that you should check to make sure values passed into methods and constructors are valid before using them. So if you get a monthNumber of 13 or -1 or whatever, you should handle that. Possibly by throwing an appropriate exception.
h. An equals method that accepts a Month object as an argument. If the argument object holds the same data as the current object, this method should return true.
Otherwise, it should return false.
Any trouble with this? The function would just take one parameter. All you need to do is make sure the Month object passed in has the same monthNumber as the object being called with it's equals method.
i. A greaterThan method that accepts a Month object as an argument. If the
current object’s monthNumber field is greater than the argument’s monthNumber field, this method should return true. Otherwise, it should return false.

j. A lessThan method that accepts a Month object as an argument. If the current object’s monthNumber field is less than the argument’s monthNumber field, this method should return true. Otherwise,it should return false.
If you can do the equals one, these ones should be fairly easy.
That's all ONE question, so again, i apologize i can't be of more help, but if anyone could even help with the first couple, it might fall into place. I'm a first year student so it can't be that difficult i imagine. But it would be amazing if you could maybe summarize each step, in an easier way. Something. Again thank you for an input.

Hope I helped clarify a bit. I would suggest you just start building the class a bit, starting from the beginning. Set up the class called Month, declare it's monthNumber variable, set up an array of month name Strings in order, and start writing the first constructor. If you have trouble, just let us see what you came up with and we can go from there.

EDIT: Ooops, indeed this isn't the right forum for homework questions. Someone will probably move it soon. FYI, it should be in this forum:

Science Education > Homework & Coursework Questions > Engineering, Comp Sci, & Technology

Last edited:
jtbell
Mentor
I'm moving this thread right now.

Okay so I've tried it, and just have about half of it done, right now, i haven't wrote the program to test and run this yet, but according to Java there aren't any errors. However that isn't mean it's right. So. Is this what i should have?

public class Month
{
private int monthNumber;
private String monthName;
private int x;
private String y;

public Month()
{
monthNumber = 1;
}
public int x()
{
if (x < 1) monthNumber = 1;
if (x > 12) monthNumber = 1;
else monthNumber = x;
return monthNumber;
}
public int y()
{
if (y.toUpperCase().equals("JANUARY")) monthNumber = 1;
if (y.toUpperCase().equals("FEBRUARY")) monthNumber = 2;
if (y.toUpperCase().equals("MARCH")) monthNumber = 3;
if (y.toUpperCase().equals("APRIL")) monthNumber = 4;
if (y.toUpperCase().equals("MAY")) monthNumber = 5;
if (y.toUpperCase().equals("JUNE")) monthNumber = 6;
if (y.toUpperCase().equals("JULY")) monthNumber = 7;
if (y.toUpperCase().equals("AUGUST")) monthNumber = 8;
if (y.toUpperCase().equals("SEPTEMBER")) monthNumber = 9;
if (y.toUpperCase().equals("OCTOBER")) monthNumber = 10;
if (y.toUpperCase().equals("NOVEMBER")) monthNumber = 11;
else monthNumber = 12;
return monthNumber;
}
public int getmonthNumber()
{
return monthNumber;
}
public String getmonthName()
{
if (monthNumber == 1) return ("January");
if (monthNumber == 2) return ("February");
if (monthNumber == 3) return ("March");
if (monthNumber == 4) return ("April");
if (monthNumber == 5) return ("May");
if (monthNumber == 6) return ("June");
if (monthNumber == 7) return ("July");
if (monthNumber == 8) return ("August");
if (monthNumber == 9) return ("September");
if (monthNumber == 10) return ("October");
if (monthNumber == 11) return ("November");
return ("December");
}
}

Seems long winded, but at as long as i'll get the desired outcome. I don't want to continue if the first half isn't right.

EDIT
okay, there are supposed to be tabs there, they just didn't come up when i posted this. But they are there.

If you put your code inside code tags, it'll format properly. Just put it between [ code] and [ /code]. Remove the spaces after the square brackets. I just put them like that so the forum wouldn't gobble them thinking they're real code tags.

Here's what you have so far:
Code:
public class Month
{
private int monthNumber;
private String monthName;
private int x;
private String y;

public Month()
{
monthNumber = 1;
}
public int x()
{
if (x < 1) monthNumber = 1;
if (x > 12) monthNumber = 1;
else monthNumber = x;
return monthNumber;
}
public int y()
{
if (y.toUpperCase().equals("JANUARY")) monthNumber = 1;
if (y.toUpperCase().equals("FEBRUARY")) monthNumber = 2;
if (y.toUpperCase().equals("MARCH")) monthNumber = 3;
if (y.toUpperCase().equals("APRIL")) monthNumber = 4;
if (y.toUpperCase().equals("MAY")) monthNumber = 5;
if (y.toUpperCase().equals("JUNE")) monthNumber = 6;
if (y.toUpperCase().equals("JULY")) monthNumber = 7;
if (y.toUpperCase().equals("AUGUST")) monthNumber = 8;
if (y.toUpperCase().equals("SEPTEMBER")) monthNumber = 9;
if (y.toUpperCase().equals("OCTOBER")) monthNumber = 10;
if (y.toUpperCase().equals("NOVEMBER")) monthNumber = 11;
else monthNumber = 12;
return monthNumber;
}
public int getmonthNumber()
{
return monthNumber;
}
public String getmonthName()
{
if (monthNumber == 1) return ("January");
if (monthNumber == 2) return ("February");
if (monthNumber == 3) return ("March");
if (monthNumber == 4) return ("April");
if (monthNumber == 5) return ("May");
if (monthNumber == 6) return ("June");
if (monthNumber == 7) return ("July");
if (monthNumber == 8) return ("August");
if (monthNumber == 9) return ("September");
if (monthNumber == 10) return ("October");
if (monthNumber == 11) return ("November");
return ("December");
}
}
Seems long winded, but at as long as i'll get the desired outcome. I don't want to continue if the first half isn't right.
There are a number of issues, but it shows promise though!

Firstly, the following probably shouldn't be there. We'll get to the error handling shortly.
Code:
	private int x;
private String y;

You have the monthName as a private variable. It's not really necessary, but not really wrong. However, if you have it there, if should probably be set to a proper value when the object is instantiated (i.e. the constructors).

Code:
	public int x()
{
if (x < 1) monthNumber = 1;
if (x > 12) monthNumber = 1;
else monthNumber = x;
return monthNumber;
}
The name of this function is very poor. It should be named something descriptive. The logic doesn't quite make sense to me, either. The way you're using that x variable won't work. Not sure what you're going for, but the fact that it's named the same as a variable is a bad idea, but also offers you nothing special.

Let's look at how you might implement the constructor that allows setting monthNumber.
Code:
	public Month(int monthNumber)
{
setMonthNumber(monthNumber);
}
That's how I'd do it. Then the error checking and logic can be put in that one place (setMonthNumber). You can then call that anywhere, or allow a programmer to call it directly after the class has been instantiated. Note how I capitalize the first letter of every word. It's part of the Java Coding Standard, and I suggest doing that rather than "setmonthNumber" to make it more readable. It matters not that the variable it sets starts with a lower case letter (as it should, I might add).

Let's look at setMonthNumber.
Code:
public void setMonthNumber(int monthNumber)
{
if ((monthNumber < 1) || (monthNumber > 12))
{
throw new IllegalArgumentException();
}
this.monthNumber = monthNumber;
}
Note that the parameter 'monthNumber' shadows (hides, takes precedence over) the instance variable of the same name in that method. So if you want the value in the parameter, use 'monthNumber'. If you want to access the instance variable itself, you must use 'this.monthNumber'. In general, it's a bad idea to do that, but in this case it's a standard pattern which is used all over the place. I suggest doing it this way only in constructors and mutators unless you have a really good reason. Some people think you shouldn't do it at all, which is valid. You can always change the name of the parameter to, say, 'newMonthNumber' if you prefer it that way.

Also note the exception. Other than return values indicating an error (not always appropriate), that's how it's done. You can create your own exceptions if you want, as well. In this case, there's an existing one that's perfect for these purposes.

You also need a setMonthName method. The way you find the month number (in y() ) is acceptable, I think, more or less (if you want me to show you how to do it with a HashMap, let me know). So let's fix it up a little and make the method.

Code:
    public void setMonthName(String monthName)
{
String monthNameCaps = monthName.toUpperCase();

if (monthNameCaps.equals("JANUARY")) monthNumber = 1;
else if (monthNameCaps.equals("FEBRUARY")) monthNumber = 2;
else if (monthNameCaps.equals("MARCH")) monthNumber = 3;
else if (monthNameCaps.equals("APRIL")) monthNumber = 4;
else if (monthNameCaps.equals("MAY")) monthNumber = 5;
else if (monthNameCaps.equals("JUNE")) monthNumber = 6;
else if (monthNameCaps.equals("JULY")) monthNumber = 7;
else if (monthNameCaps.equals("AUGUST")) monthNumber = 8;
else if (monthNameCaps.equals("SEPTEMBER")) monthNumber = 9;
else if (monthNameCaps.equals("OCTOBER")) monthNumber = 10;
else if (monthNameCaps.equals("NOVEMBER")) monthNumber = 11;
else if (monthNameCaps.equals("DECEMBER")) monthNumber = 12;
else {
throw new IllegalArgumentException();
}
}
One thing I did was convert the string to upper case once at the start. No need to call it over and over. Other than adding the exception and turning most ifs to else ifs, it's mostly the same logic as you had. I would personally use a HashMap for that, or search an array, but that's fine. So nice to see you took into account capitalization, by the way!

Now you can call that from a constructor that accepts the month name as a parameter, and it can't end up with bad values in the class. That's important.

Now for getMonthName. There, I would definitely use an array lookup. It'll be so much simpler and shorter. So you can define an array of the month names like so:
Code:
    private String monthNames[] = {"January", "February", "March", "April",
"June", "July", "August", "September", "November", "December"};
Which turns getMonthName into this simple method:
Code:
    public String getMonthName()
{
return monthNames[monthNumber-1];
}
Of course, a getMonthNumber() method needs to be written, and I trust that shouldn't post any difficulties for you.

A few things to point out. Firstly, note that you don't need to store the month name string at all. Also note how it's impossible to get a bad value into that class. The state (i.e. month number) can't be set to a bad value since it all goes through the mutator methods. If someone tries to pass a bad value to a constructor, it will throw an exception and the class will not be instantiated.

Hopefully I got all that right. It should give you all you need to get the class written nicely, I think. Let me know if you have any problems, of course.

Code:
public class Month
public class Month
{
private int monthNumber;
private String monthName;

public Month(int monthNumber)
{
monthNumber = (monthNumber);
}
public void setMonthNumber(int monthNumber)
{
if((monthNumber < 1) || (monthNumber > 12))
monthNumber = 1;
else
monthNumber = monthNumber;
}
public void setMonthName(String monthName)
{
String monthNameCaps = monthName.toUpperCase();

if (monthNameCaps.equals("JANUARY")) monthNumber = 1;
else if (monthNameCaps.equals("FEBRUARY")) monthNumber = 2;
else if (monthNameCaps.equals("MARCH")) monthNumber = 3;
else if (monthNameCaps.equals("APRIL")) monthNumber = 4;
else if (monthNameCaps.equals("MAY")) monthNumber = 5;
else if (monthNameCaps.equals("JUNE")) monthNumber = 6;
else if (monthNameCaps.equals("JULY")) monthNumber = 7;
else if (monthNameCaps.equals("AUGUST")) monthNumber = 8;
else if (monthNameCaps.equals("SEPTEMBER")) monthNumber = 9;
else if (monthNameCaps.equals("OCTOBER")) monthNumber = 10;
else if (monthNameCaps.equals("NOVEMBER")) monthNumber = 11;
else monthNumber = 12;
}
private String monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

public String getMonthName()
{
return monthNames(monthNumber - 1);
}

public int getmonthNumber()
{
return monthNumber;
}

public String Equals;
{
String EqualName = EqualName.toUpperCase();

if (EqualName.equals(monthName))
System.out.println("True");
else
System.out.println("False");
}
public String GreaterThan;
{
String GreaterName = GreaterName.toUpperCase();

if (GreaterName.compareTo(monthName) > 0)
System.out.println("True");
else
System.out.println("False");
}

public String LesserThan;
{
String LesserName = LesserName.toUpperCase();

if (LesserName.compareTo(monthName) < 0)
System.out.println("True");
else
System.out.println("False");
}

}

AWhen i ran the code above, i received a error message saying

Month.java:38: cannot find symbol
symbol : method monthNames(int)
location: class Month
return monthNames(monthNumber - 1);
^
1 error

Which i can't seem to solve, i've tried modifying just about everything i can. But it won't go away.

Anyway though, how does the code look now?

Last edited:
Mark44
Mentor
You're using monthNames as if it were a function -- monthNames(). It's not -- it's an array, so use brackets, not parentheses.

Alright, got that taken care of, now i'm stuck on the last part with trying to get to compare two strings. the method i posted, isn't working out.

Mark44
Mentor
Hypnos_16 said:
h. An equals method that accepts a Month object as an argument. If the argument object holds the same data as the current object, this method should return true.
Otherwise, it should return false.

i. A greaterThan method that accepts a Month object as an argument. If the
current object’s monthNumber field is greater than the argument’s monthNumber field, this method should return true. Otherwise, it should return false.

j. A lessThan method that accepts a Month object as an argument. If the current object’s monthNumber field is less than the argument’s monthNumber field, this method should return true. Otherwise,it should return false.
You have declared something named Equals (should be equals per the requirements above). The reason I say "something" is that I don't know what the compiler makes of it. A method definition has to include a pair of parentheses after the name, even if there are no parameters. As described above, the equals method should have a parameter of type Month. Yours doesn't.

There are similar problems with your GreaterThan (should be greaterThan, per the reqts) and LesserThan (should be lessThan) methods.

All three of these methods should return a boolean value. Your three methods purport to return a String, but none of them actually does so.

I would advise taking another look at the requirements you posted in the first post of this thread. The requirements specify the names of methods - you should follow them exactly. There should also be perfect agreement on the number and types of method parameters, and all methods should return a value of the type specified.

Your instructor has described the interface to a Month class that he/she expects you to implement. It's possible that he/she will write a small program that exercises your implementation. If you have not followed the requirements to the letter (including the exact spelling of method names), the instructor's program will not compile and your score on this assignment could be affected.

Last edited:
Code:
	public String Equals;
{
String EqualName = EqualName.toUpperCase();

if (EqualName.equals(monthName))
System.out.println("True");
else
System.out.println("False");
}
Getting there. There's a few problems in there.

Firstly, it's a method, so you didn't declare it properly. The method 'equals' (lower case!) is a method in the base class 'Object' from which all other classes derive (implicitly, you don't have to inherit from Object explicitly). You want to override that function. It should be called 'equals'. Note how you used it in setMonthName.

There should not be a semi-colon after the method name. Rather, it needs the parameter for the Month object against which it is being compared. It also should return a boolean value.

I would write it something like this:
Code:
    public boolean equals(Month aMonth)
{
return this.monthNumber == aMonth.getMonthNumber();
}

The obvious way to write the body of that method is probably:
Code:
    if (this.monthNumber == aMonth.getMonthNumber)
{
return true;
} else {
return false;
}
But since the result of the expression in the if already gives the right boolean value as a result, you can just return that.

I should mention that all methods and variables should start with a lower case letter. Class names should start with an upper case letter. So 'String EqualName' should always be 'String equalName'.

That should give you what you need to do the other comparison methods.

EDIT: Also, try and keep all your class's member variables at the top of the class so they're easy to find. For example, that array should be up near the top, in my opinion. It's where most people will expect them to be.

Code:
public boolean equals(monthName newMonthName)
{
if (monthName.equals(newMonthName))
return true;
else
return false;
}

i have this, i'm trying to keep it as simple as possible, however it appears to be having a problem with the "monthName" part in the first line. It's not just because it's that variable either, no matter what i change it to, it has a problem.

Code:
public boolean equals(monthName newMonthName)
{
if (monthName.equals(newMonthName))
return true;
else
return false;
}

i have this, i'm trying to keep it as simple as possible, however it appears to be having a problem with the "monthName" part in the first line. It's not just because it's that variable either, no matter what i change it to, it has a problem.

Ok, if you've kept the monthName variable in the class, make 100% sure it's always set correctly when the class is instantiated or the monthNumber is changed. Worth repeating, just in case.

The problem is the newMonthName parameter and the way you've declared it. It should have a type (class name in this case) and the name of the variable that will be of that type. monthName is NOT a type or class. Clearly, you mean for newMonthName to be of the type/class 'String'. It should have the same type as the one you used for the class's monthName variable.

So turn this:

public boolean equals(monthName newMonthName)

Into this:

public boolean equals(String newMonthName)

And I believe that should work.

Note that the if isn't strictly necessary here, either. A simple:

return monthName.equals(newMonthName);

... should do the trick. That call to equals will return a boolean which is exactly the value your method needs to return. No need put in unnecessary logic. I point it out because it comes up really often.

Hope you're learning a lot from this. Your teacher gave you an excellent assignment that really gets to the basics of class design in Java. Once it's done, it'll be a good point of reference for later stuff.

EDIT: Actually, I'm sort of failing to see the point of that method. Is that in the specs your teacher gave you? Doesn't look like it. The equals I showed you is more than adequate. And that method you wrote doesn't take capitalization into account either.

Mark44
Mentor
Grep already wrote it for you.
Grep said:
Code:
public boolean equals(Month aMonth)
{
return this.monthNumber == aMonth.getMonthNumber();
}
Your problem was in the parameter list of your equals method. Each formal parameter (there is one in this method) is represented by a type (Month in this case) and the parameter name (Grep used aMonth).

Your parameter list had monthName, which is a variable, not a type.

Mark44
Mentor
Grep,
Per you suggestions in post 12, I'm reasonably sure what you have will work, but it doesn't meet the requirements, which state that the equals method should have a parameter of type Month.

Grep,
Per you suggestions in post 12, I'm reasonably sure what you have will work, but it doesn't meet the requirements, which state that the equals method should have a parameter of type Month.
hehe Yeah I missed that until after I posted my response. Then I looked at his code again and though "Wait a minute... That doesn't make sense..." Added an edit to the bottom of the post when I noticed.

Indeed, he should probably make sure he understands the .equals method I showed him and try to apply it to the greaterThan and lesserThan methods.

Code:
public class Month
{
private int monthNumber;
private String monthName;
private boolean equals;
private boolean greaterThan;
private boolean lessThan;

public Month(int monthNumber)
{
monthNumber = (monthNumber);
}
public void update (int monthNumber)
{
if ((monthNumber < 1) || (monthNumber > 12))
monthNumber = 1;
else
monthNumber = monthNumber;
}
public void setMonthName(String monthName)
{
String monthNameCaps = monthName.toUpperCase();

if (monthNameCaps.equals("JANUARY")) monthNumber = 1;
else if (monthNameCaps.equals("FEBRUARY")) monthNumber = 2;
else if (monthNameCaps.equals("MARCH")) monthNumber = 3;
else if (monthNameCaps.equals("APRIL")) monthNumber = 4;
else if (monthNameCaps.equals("MAY")) monthNumber = 5;
else if (monthNameCaps.equals("JUNE")) monthNumber = 6;
else if (monthNameCaps.equals("JULY")) monthNumber = 7;
else if (monthNameCaps.equals("AUGUST")) monthNumber = 8;
else if (monthNameCaps.equals("SEPTEMBER")) monthNumber = 9;
else if (monthNameCaps.equals("OCTOBER")) monthNumber = 10;
else if (monthNameCaps.equals("NOVEMBER")) monthNumber = 11;
else monthNumber = 12;
}
private String monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

public String getMonthName()
{
return monthNames[monthNumber - 1];
}

public int getmonthNumber()
{
return monthNumber;
}

public boolean equals(String newMonthName)
{
if (monthName.compareToIgnoreCase(newMonthName) == 0)
return true;
else
return false;
}
public boolean greaterThan(String newMonthName)
{
if (monthName.compareToIgnoreCase(newMonthName) > 0)
return true;
else
return false;
}
public boolean lessThan(String newMonthName)
{
if (monthName.compareToIgnoreCase(newMonthName) < 0)
return true;
else
return false;
}
}

the full code. Seems right to me, Any corrections would be nice, also want to keep it the way it is here, so using these variables, unless something major needs to be done to it.

Now i need help writing the main part of this program to test this thing out. Please i have no idea where to even begin with that.

Last edited:
Code:
public class Month
{
private int monthNumber;
private String monthName;
private boolean equals;
private boolean greaterThan;
private boolean lessThan;
I'm not sure why you're putting in these variables with the same name as methods. Perhaps if you can tell me why you're doing that I can clear up the confusion, because the last 3 variables there are 100% not used or necessary. Nor should you generally have variables with the same name as methods.

Also, is monthName ever set to anything? Doesn't look like it. As I said, it's totally unnecessary. All the class needs to store is the monthNumber. Getting the monthName String is quick and easy, so no need to store or "cache" it.
Code:
	public Month(int monthNumber)
{
monthNumber = (monthNumber);
}
As I said, that won't work. To refer to the monthNumber that belongs to the class (as opposed to the parameter of the same name), you *must* add this. in front of it. Also, the parentheses there do nothing and should be removed. The line should be:

this.monthNumber = monthNumber;

That or change the parameter name to newMonthNumber and use 'monthNumber = newMonthNumber'.

There's no parameter error checking in there, either. You should be calling a setMonthNumber method as I said before (which you've called, wrongly, update).

Also, you're missing some constructors that are in your specifications.
Code:
	public void update (int monthNumber)
{
if ((monthNumber < 1) || (monthNumber > 12))
monthNumber = 1;
else
monthNumber = monthNumber;
}
That method should probably be called from your constructor above that takes the monthNumber as a parameter. Also, it should be called setMonthNumber, which is the naming convention for mutators. And as above, 'monthNumber = monthNumber' makes no sense. Either rename the parameter to newMonthNumber and do 'monthNumber = newMonthNumber;' or you must do it this way:

this.monthNumber = monthNumber;

Code:
	public void setMonthName(String monthName)
{
String monthNameCaps = monthName.toUpperCase();

if (monthNameCaps.equals("JANUARY")) monthNumber = 1;
else if (monthNameCaps.equals("FEBRUARY")) monthNumber = 2;
else if (monthNameCaps.equals("MARCH")) monthNumber = 3;
else if (monthNameCaps.equals("APRIL")) monthNumber = 4;
else if (monthNameCaps.equals("MAY")) monthNumber = 5;
else if (monthNameCaps.equals("JUNE")) monthNumber = 6;
else if (monthNameCaps.equals("JULY")) monthNumber = 7;
else if (monthNameCaps.equals("AUGUST")) monthNumber = 8;
else if (monthNameCaps.equals("SEPTEMBER")) monthNumber = 9;
else if (monthNameCaps.equals("OCTOBER")) monthNumber = 10;
else if (monthNameCaps.equals("NOVEMBER")) monthNumber = 11;
else monthNumber = 12;
}
What happens if someone passes a string that isn't a month name at all? The class will decide that it's December. I don't see that in the specifications. It should not set the class to a valid state if it's passed a bad month name, in normal practice. Of course, if you can't use exceptions yet, that might be acceptable. But the specs say that if you get an invalid month number, you should set the month number to 1. Perhaps this should also set the month to January (1) if the string is invalid?
Code:
	private String monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
That should be up at the top with the other variables.
Code:
	public String getMonthName()
{
return monthNames[monthNumber - 1];
}

public int getmonthNumber()
{
return monthNumber;
}
Nothing wrong with these two methods, except that it shouldn't be 'getmonthNumber', but rather should be 'getMonthNumber'.
Code:
	public boolean equals(String newMonthName)
{
if (monthName.compareToIgnoreCase(newMonthName) == 0)
return true;
else
return false;
}
public boolean greaterThan(String newMonthName)
{
if (monthName.compareToIgnoreCase(newMonthName) > 0)
return true;
else
return false;
}
public boolean lessThan(String newMonthName)
{
if (monthName.compareToIgnoreCase(newMonthName) < 0)
return true;
else
return false;
}
Still with the strings? Read your specs. They should take objects of type Month as parameters, and it's the monthNumber values you should be comparing. Look at the equals method I wrote earlier. The greaterThan and lessThan methods are almost the same as equals, with only a tiny difference. Instead of the '==' operator, they will use '>' and '<' respectively.

As with the equals, no if statements are necessary. I'm not sure you understand how expressions that evaluate to true or false work. Let's take a brief look.

Take the expression:

(x == 1)

If x is 1, that will evaluate to true. Otherwise, false. Or:

(x > 1)

If x is larger than 1, it will evaluate to true. Otherwise false. Sure, you could do this:
Code:
if (x > 1)
return true;
else
return false;
Can you see why the ifs are totally redundant? That last example is like saying 'if true then return true else return false'. That code should be:
Code:
return (x > 1);

Fix everything I pointed out, and add back the missing constructors, and you should be ok.

Now as for testing it, I'm not sure how your teacher wants you to do it. Should it be tested from a separate tester class? Can you test it from the class's own main method?

I'll show you an example of what I did to do some quick checks by adding a main method to the class. It can be done in another separate class if you like. Whatever class it is, with a proper main method, it can be run with 'java Month', replacing Month by whatever class has the main and does the testing if it's not in Month itself.

Code:
    public static void main(String args[])
{
boolean testOk = true;
Month aMonth = new Month("mArCh");

System.out.print("Month(String) constructor and getMonthNumber() test: ");
if (aMonth.getMonthNumber() == 3)
{
System.out.println("OK");
} else {
System.out.println("FAIL!");
testOk = false;
}

// Did any of the tests fail?
if (testOk)
{
System.out.println("All tests passed.");
} else {
System.out.println("One or more tests failed!");
}
}

Exactly what you test is up to you. A good way to figure out what to test is to look at your specifications. Take the first one:

a. A no-argument constructor that sets the monthNumber field to 1.

So you should create a month object using the constructor with no arguments and then make sure the monthNumber was indeed set to 1.

Go through each requirement one by one and test that everything happens exactly as specified.

EDIT: Note that if you do it from a separate class, it will need to import the Month class before it can "see" it. Also, you will have to operate on Month through it's public interface. You can't access monthNumber directly, but must get it using getMonthNumber().

Last edited:
Mark44
Mentor
You're getting close, but there are still some things missing or not according to the spec you were given. I am including the items in the spec in your first post where you are missing something or not doing it as requested in the spec.
Hypnos_16 said:
2. Write, test and document (internally and externally) a Java program to solve the following problem:

Write a Java class named Month. The class should have an int attribute named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods:
You have this member variable, but you have some others that you either don't need or shouldn't have at all.
These should go away:
monthName
equals
greaterThan
lessThan

The equals, greaterThan, and lessThan variables are never used. They are different from the member functions with the same name.
Hypnos_16 said:
a. A no-argument constructor that sets the monthNumber field to 1.
Missing. You have only one constructor. You are supposed to have three of them.
Hypnos_16 said:
Don't know what this is, but it is presumably what comments you should include.
Hypnos_16 said:
d. A constructor that accepts the name of the month, such as “January” or “February” as an argument. It should set the monthNumber field to the correct corresponding value.
Missing.
Hypnos_16 said:
e. A setMonthNumber method that accepts an int argument, which is assigned to the monthNumber field. If a value less than 1 or greater than 12 is passed, the method should set monthNumber to 1.
Missing. Your update method should be renamed to setMonthNumber.

You have a setMonthName method whose name is misleading, and that should be removed.
Hypnos_16 said:
i. A greaterThan method that accepts a Month object as an argument. If the
current object’s monthNumber field is greater than the argument’s monthNumber field, this method should return true. Otherwise, it should return false.
This method doesn't work. For example, it will report that November is greater than December. Your method does lexicographic (i.e. dictionary) ordering. The method should compare the monthNumber properties for the two methods.
Hypnos_16 said:
j. A lessThan method that accepts a Month object as an argument. If the current object’s monthNumber field is less than the argument’s monthNumber field, this method should return true. Otherwise,it should return false.
This method doesn't work. For example, it will report that April is less than March. Your method does lexicographic (i.e. dictionary) ordering. The method should compare the monthNumber properties for the two methods.

Mark44
Mentor
As Grep and I have pointed out, you need to pay very close attention to the class specification. If the spec requires three constructors, and you supply only one, your implementation doesn't meet the requirements laid out in the spec, and I can pretty well guarantee that your instructor will give you a lower grade. If it specifies a function of a certain name, and you don't have it, you'll probably get dinged. If it specifies that a method should perform some action a specified way, and you provide a method that does something different, you'll probably get dinged.

Alright, so taking that all into account i'm trying to reform my workings, however, i'm having a little trouble with the constructors, do they follow the same format of the first?
Code:
	public Month(int newMonthNumber)
{
monthNumber = newMonthNumber;
}
with "public Month" at the beginning? or something else?

ALSO.
where it's a "No argument" constructor, could i just use "monthNumber = 1"? instead of "monthNumber = newMonthNumber"?

Yep, that's how it's done. Only comment is that you should be calling setMonthNumber so it does the error checking. As it is, someone could pass in -100 if they wanted, and the class would end up set to an invalid value (rarely if ever a good idea).

EDIT for your ALSO: Yes, that's right. And in that case in the no argument constructor, you don't need to call setMonthNumber since you have full control over the value set. In other words, a user of the class couldn't force a bad value into the class with it, so no checking necessary.

Mark44
Mentor
They will start with public Month( and differ only in the parameter lists. One constructor takes no parameters, so just has a pair of empty parentheses after Month. All it does is set monthNumber to 1.

Another takes a string containing the month name, and sets monthNumber appropriately. There should be some logic that compares the name passed to the constructor with the names in the array of month names. If the name is a valid month name, set monthNumber to the right value. If the name isn't valid, probably the simplest thing to do would be to write an error message to the console. Another approach would be to throw an exception, but I'm guessing that you haven't covered that in class, yet. Your instructor didn't provide any details about what to do if the month name used in the constructor isn't valid, so it would be a good idea to contact him/her for clarification.

Alright Great. again, sorry to be such a bother, but for the last three (==, > , <) i realize that it is redundant to do if ... return true, else false, but that's the kind of thing we're learning about here now, and i want to put them in there to show that i do in fact know how to use them. So is there an easy way to compare numerical values of the two variables in a if then else manner? Also i have to compare the actual months in the equals method, and just their numerical values in the greaterThan and lessThan methods.

PS. Re-revised Algorithm. Changed the very last thing, i realize it's redundant, but it's what we're learning i'm showing i know it.
Code:
public class Month
{
private int monthNumber;
private String monthName;

public Month()
{
monthNumber = 1;
}
public Month (int MONTHNUMBER)
{
MONTHNUMBER = monthNumber;
}
{
if ((monthNumber < 1) || (monthNumber > 12))
monthNumber = 1;
else
}
public Month (String monthName)
{
monthName = monthName;
}

public void setMonthName(String monthName)
{
String monthNameCaps = monthName.toUpperCase();

if (monthNameCaps.equals("JANUARY")) monthNumber = 1;
else if (monthNameCaps.equals("FEBRUARY")) monthNumber = 2;
else if (monthNameCaps.equals("MARCH")) monthNumber = 3;
else if (monthNameCaps.equals("APRIL")) monthNumber = 4;
else if (monthNameCaps.equals("MAY")) monthNumber = 5;
else if (monthNameCaps.equals("JUNE")) monthNumber = 6;
else if (monthNameCaps.equals("JULY")) monthNumber = 7;
else if (monthNameCaps.equals("AUGUST")) monthNumber = 8;
else if (monthNameCaps.equals("SEPTEMBER")) monthNumber = 9;
else if (monthNameCaps.equals("OCTOBER")) monthNumber = 10;
else if (monthNameCaps.equals("NOVEMBER")) monthNumber = 11;
else monthNumber = 12;
}
private String monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

public String getMonthName()
{
return monthNames[monthNumber - 1];
}

public int getMonthNumber()
{
return monthNumber;
}

public boolean equals(Month aMonth)
{
return this.monthNumber == aMonth.getMonthNumber();
}

public boolean greatThan(Month aMonth)
{
return this.monthNumber > aMonth.getMonthNumber();
}

public boolean lessThan(Month aMonth)
{
if (this.monthNumber < aMonth.getMonthNumber())
return true;
else
return false;
}
}

Last edited:
Alright Great. again, sorry to be such a bother, but for the last three (==, > , <) i realize that it is redundant to do if ... return true, else false, but that's the kind of thing we're learning about here now, and i want to put them in there to show that i do in fact know how to use them.
Fair enough. And you seem to be putting in good effort, so I don't mind.

So is there an easy way to compare numerical values of the two variables in a if then else manner?
Sure, you need to build the right expression. Something like:
Code:
if (a > b)
// It's true
else
// It's false
They're just numbers, so operators like <, >, ==, <= and >= make sense here.

Also i have to compare the actual months in the equals method, and just their numerical values in the greaterThan and lessThan methods.
No, you can compare the numerical values in equals as well. Look closely at the associated requirement. Two Month objects are equal if the monthNumber variables in both match.

Now for the code:
Code:
public class Month
{
private int monthNumber;
private String monthNames
You need to move the entire array declaration up here. Looks like it's truncated at the moment, and the array is still down where it was. It's not a huge thing, but it helps if programmers reading your code don't have to hunt them down in the middle of other code. So normal convention is to put them at the top.
Code:
	public Month()
{
setMonthNumber = 1;
}
public Month (int monthNumber)
{
setMonthNumber = monthNumber
}
Not quite right. Don't confuse methods with variables. They're two different things. What you meant was 'setMonthNumber(monthNumber);'.
Code:
	public Month (String monthName)
{
MonthName = monthName
}
And here you should call setMonthName with 'setMonthName(monthName);'.
Code:
	public boolean greatThan(Month aMonth)
{
return this.monthNumber > aMonth.getMonthNumber();
}
Typo. Guessing you meant 'greaterThan', not 'greatThan'.
Code:
	public boolean lessThan(Month aMonth)
{
if this.monthNumber < aMonth.getMonthNumber()
return true
else
return false
}
Missing parentheses around the expression, and missing semi-colons after the returns. it should look like this:

Code:
	public boolean lessThan(Month aMonth)
{
if (this.monthNumber < aMonth.getMonthNumber())
return true;
else
return false;
}
Takes a little while before the syntax comes naturally, but it'll happen.

Code:
public class Month
{
private int monthNumber;
private String monthName;

public Month()
{
monthNumber = 1;
}
public Month (int monthNumber)
{
setMonthNumber(monthNumber);
}
public void setMonthNumber (int newMonthNumber)
{
if ((monthNumber < 1) || (monthNumber > 12))
monthNumber = 1;
else
monthNumber = newMonthNumber;
}
public Month (String monthName)
{
setMonthName(monthName);
}

public void setMonthName(String monthName)
{
String monthNameCaps = monthName.toUpperCase();

if (monthNameCaps.equals("JANUARY")) monthNumber = 1;
else if (monthNameCaps.equals("FEBRUARY")) monthNumber = 2;
else if (monthNameCaps.equals("MARCH")) monthNumber = 3;
else if (monthNameCaps.equals("APRIL")) monthNumber = 4;
else if (monthNameCaps.equals("MAY")) monthNumber = 5;
else if (monthNameCaps.equals("JUNE")) monthNumber = 6;
else if (monthNameCaps.equals("JULY")) monthNumber = 7;
else if (monthNameCaps.equals("AUGUST")) monthNumber = 8;
else if (monthNameCaps.equals("SEPTEMBER")) monthNumber = 9;
else if (monthNameCaps.equals("OCTOBER")) monthNumber = 10;
else if (monthNameCaps.equals("NOVEMBER")) monthNumber = 11;
else monthNumber = 12;
}
private String monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

public String getMonthName()
{
return monthNames[monthNumber - 1];
}

public int getMonthNumber()
{
return monthNumber;
}

public boolean equals(Month aMonth)
{
if (this.monthNumber == aMonth.getMonthNumber())
return true;
else
return false;
}

public boolean greaterThan(Month aMonth)
{
if (this.monthNumber > aMonth.getMonthNumber())
return true;
else
return false;
}

public boolean lessThan(Month aMonth)
{
if (this.monthNumber < aMonth.getMonthNumber())
return true;
else
return false;
}
}

Alright! hopefully now, this is good. Now the testing of this is kind of tricky, because the way my prof wants it done is that she can input the variables ie. Month name and number. so i know i use the
Code:
import java.util.Scanner
but i'm having some issues with writing the body of it.

i tried the one that was given here, just to make sure everything worked well

Code:
public class MonthTester {
public static void main(String [] args) {
boolean testOk = true;
Month aMonth = new Month("mArCh");
System.out.println("Month(String) constructor and getMonthNumber() test: ");
if (aMonth.getMonthNumber() == 3)
System.out.println("OK");
else
System.out.println("FAIL!");
testOk = false;
// Did any of the tests fail?
if (testOk)
System.out.println("All tests passed.");
else
System.out.println("One or more tests failed!");

}
}
i got an answer of "OK" but also that "One or more tests failed!" i hope it's only a minor error, but my Terminal isn't saying anything is "wrong" with either program