• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java class 'Month' - still not getting returns... and a few other things  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

New week, new assignment - new troubles I need hep with Assignment is:
Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. The class should also have a static field named lastMonthCreated that holds the number of the month which was last constructed. In addition, provide the following methods:

A no-arg constructor that sets the monthNumber field to 1.
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 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.
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.
A getMonthNumber method that returns the value in the monthNumber field.
A getMonthName method that returns the name of the month. For example, if the monthNumber field contains 1, then this method should return "January".
A getLastMonthCreated method that returns the value in the lastMonthCreated field.
A toString method that returns the same value as the getMonthName method.
An equals method that accepts a Month object as an argument. If the argument object holds the same data as the calling object, this method should return true. Otherwise, it should return false.
A greaterThan method that accepts a Month object as an argument. If the calling object's monthNumber field is greater than the argument's monthNumber field, this method should return true. Otherwise, it should return false.
A lessThan method that accepts a Month object as an argument. If the calling object's monthNumber field is less than the argument's monthNumber field, this method should return true. Otherwise, it should return false.
Demonstrate the Month class by using a set of three test programs, MonthDemo1.java, MonthDemo2.java and MonthDemo3.java, which can be downloaded from the MonthDemo folder below this assignment link.

Basically I have had a crack at this (hopefully a good one) and I'm coming up with quite a few errors when I compile - I've got them from 48 down to 6.

Errors are:
Line 47: unexpected type monthNumber = (monthName[(index + 1)++]); the error is pointing to index - so I tried creating a variable named index and initializing before using it, but no change.
required: variable
type: value

Line 91: incompatible types, String cannot be converted to String[] if (monthName = monthName[index]) (I get what this is saying but not sure how to change it to get what I need)

Same error on same line again

Line 92: cannot find symbol return getLastCreated; (I defined this at the top but didn't initialise - will it help if I do? I tried initialising to 0 and null and got errors)


Line 93 same again

Basically I'm looking for two things, firstly any hints for how to deal with those errors, and in general, an idea of if I'm on the right track with this, or if there are parts that need changing to make it work. If you suggest changes, do you mind explaining why, as I'm really new and hope to learn fromt he suggestions not just implement.

All your help greatly appreciated - I've learned so much from this forum over the last few weeks since starting Java!





DEMO 1


DEMO 2




Thanks,

A


 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amanda allen wrote:
Errors are:
Line 47: unexpected type monthNumber = (monthName[(index + 1)++]); the error is pointing to index - so I tried creating a variable named index and initializing before using it, but no change.
required: variable
type: value


The post increment operator is for the increment of variables. And while "index" is a variable, "index + 1" is *not* a variable. "index + 1" is an expression, and there isn't a concept of incrementing an expression.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amanda allen wrote:
Line 91: incompatible types, String cannot be converted to String[] if (monthName = monthName[index]) (I get what this is saying but not sure how to change it to get what I need)


Setting an array to an element of the array doesn't make sense. And we have no idea what to tell you to change it to what you need, as there is no way to determine what you need, based on setting an string array reference to an element of a string array. What are you trying to accomplish?

Henry
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you started all your method names with capital letters? You only do that in C#.
If you change Equals to equals, you have problems; the equals method must be overridden which means it needs to have Object as its parameter type. You also need to override hashCode, thought you can probably get away with simply using the month number as the hash code.
In equals, you have some poor style. Don't write if (something) return true; else return false; You simply write return something; Have a look at this old style guide (§10.5.2).
 
amanda allen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses - have changed all methods to lower case - that was one thing I could manage on my own

I played around with the other two areas of error, but just really have no idea. If it's not too much trouble and I explain a bit more, does anyone mind suggesting a fix and why you'd take that approach - I'm just new at java and just can't seem to get my head around some parts (arrays particularly) - so frustrating!

Tried changing that first error in line 47 to this, but no luck - still errors - now String cannot be converted to int... the purpose of this method is to accept the month name as an argument then set the month number accordingly - completely lost on it!



For the second one, I just have no idea, I believe the purpose is to return the the last month - assuming the month previous would be referred to as 'the last one created'..



Full code above. If you can, please provide suggestions to fix as I am just floundering!

Cheers,

Amanda
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

amanda allen wrote:
Tried changing that first error in line 47 to this, but no luck - still errors - now String cannot be converted to int... the purpose of this method is to accept the month name as an argument then set the month number accordingly - completely lost on it!




Okay, let's start with "accept the month name as an argument" -- what do you think your instructor means by that? Does it mean something like accept a name like "January"? If so, why does the method have a String array as it parameter? Shouldn't it be something like a String as the parameter?

Also, why does it return a String? Isn't the method supposed to return a month number? Is there a better data type for that?

Henry
 
Saloon Keeper
Posts: 3336
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
amanda allen wrote:
Tried changing that first error in line 47 to this, but no luck - still errors - now String cannot be converted to int... the purpose of this method is to accept the month name as an argument then set the month number accordingly - completely lost on it!




Okay, let's start with "accept the month name as an argument" -- what do you think your instructor means by that? Does it mean something like accept a name like "January"? If so, why does the method have a String array as it parameter? Shouldn't it be something like a String as the parameter?

Also, why does it return a String? Isn't the method supposed to return a month number? Is there a better data type for that?

Henry

According to the comment this should be a "constructor", but you have a method.
 
amanda allen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much Henry - had a think on some of your comments and now have it running for demo 1 -now to sort out demo 2 and 3!

Thanks s always!

A
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!