Reputation: 53
New week, new assignment - new troubles I need help 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:
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 initialize - will it help if I do? I tried initializing 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 suggesting the actual code and explaining why, as I'm really new and hope to learn from the 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!
PS - will change case on my methods to lower - just have them upper at the moment as I find them easier to see.
public class Month
{
private int monthNumber;
String[] monthName = {
"January", "Februry", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December"
}; //Months.
int MonthNumberInt = 0;
public static String lastMonthCreated;
/**
A no-arg constructor that sets the monthNumber field to 1.
*/
public Month() {
monthNumber = 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.
*/
public Month(int monthNumber) {
if ((monthNumber < 1) || (monthNumber > 12)) {
monthNumber = 1;
} else {
monthNumber = monthNumber;
}
}
/**
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.
*/
public String Month(String monthName[]) {
int index = 0;
monthNumber = (monthName[(index + 1)++]);
}
/**
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.
*/
public int setMonthNumber(int monthNumberInt) {
if ((monthNumber < 1) || (monthNumber > 12)) {
return monthNumber = 1;
}
monthNumberInt = monthNumber;
}
/**
A getMonthNumber method that returns the value in the monthNumber field.
*/
public int getMonthNumber() {
return monthNumber;
}
/**
A getMonthName method that returns the name of the month. For example, if
the monthNumber field contains 1,
then this method should return "January".
*/
public String getMonthName() {
return monthName[monthNumber - 1];
}
/**
A getLastMonthCreated method that returns the value in the
lastMonthCreated field.
*/
public String getLastMonthCreated() {
for (int index = 0; index < monthName.length; index++) //Check through
the array. {
if (monthName = monthName[index]) getLastCreated = monthName[index - 1];
return getLastCreated;
}
}
/**
A toString method that returns the same value as the getMonthName method.
*/
public String toString() {
String str = "monthName: " + getMonthName();
}
/**
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.
*/
public boolean Equals(Month m1) //Month needs to go here.
{
if (monthNumber == m1.getMonthNumber()) return true;
else 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.
*/
public boolean GreatThan(Month m1) //Month needs to go here.
{
if (monthNumber > m1.monthNumber) return true;
else 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.
*/
public boolean LesserThan(Month m1) //Month needs to go here.
{
if (monthNumber < m1.monthNumber) return true;
else return false;
}
}
DEMO1
public class MonthDemo1
{
public static void main(String[] args)
{
// Use the no-arg constructor.
Month m = new Month();
System.out.println("Month " + m.getMonthNumber() +
" is " + m);
// Set the month number to the values 0 through 12
// (0 is invalid), and display the resulting month name.
for (int i = 0; i <= 12; i++)
{
m.setMonthNumber(i);
System.out.println("Month " + m.getMonthNumber() +
" is " + m);
}
}
}
DEMO 2
public class MonthDemo2
{
public static void main(String[] args)
{
// Use the 2nd constructor to create two objects.
Month m1 = new Month(10);
Month m2 = new Month(5);
System.out.println("Month " + m1.getMonthNumber() +
" is " + m1);
System.out.println("Month " + m2.getMonthNumber() +
" is " + m2);
// Test for equality.
if (m1.equals(m2))
System.out.println(m1 + " and " + m2 + " are equal.");
else
System.out.println(m1 + " and " + m2 + " are NOT equal.");
// Is m1 greater than m2?
if (m1.greaterThan(m2))
System.out.println(m1 + " is greater than " + m2);
else
System.out.println(m1 + " is NOT greater than " + m2);
// Is m1 less than m2?
if (m1.lessThan(m2))
System.out.println(m1 + " is less than " + m2);
else
System.out.println(m1 + " is NOT less than " + m2);
}
}
DEMO 3
public class MonthDemo3
{
public static void main(String[] args)
{
// Use the 3rd constructor to create three objects.
Month m1 = new Month("March");
Month m2 = new Month("December");
Month m3 = new Month("Bad Month");
System.out.println("Month " + m1.getMonthNumber() +
" is " + m1);
System.out.println("Month " + m2.getMonthNumber() +
" is " + m2);
System.out.println("Month " + m3.getMonthNumber() +
" is " + m3);
Month m4 = new Month("May");
System.out.println("The last month created" +
" is " + m4.getLastMonthCreated());
}
}
Upvotes: 1
Views: 5070
Reputation: 1
String[] monthName = {"January", "February",
"March", "April", "May", "June", "July",
"August", "September", "October", "November",
"December"};
Calendar cal = Calendar.getInstance();
String month = monthName[cal.get(Calendar.MONTH)];
Upvotes: 0
Reputation: 24167
I can only say that you are very confused. There are couple of problems with your code:
public String Month(String monthName[])
.monthNumber
and MonthNumberInt
. MonthNumberInt
should be monthNumberInt
.Month
in loop as you cannot use same instance for every month.StackOverflow
so that others can have a look.Here is the rewritten piece that works:
public class Month {
String[] monthName = { "January", "Februry", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December" }; //Months.
int monthNumberInt = 0;
public static String lastMonthCreated;
/**
A no-arg constructor that sets the monthNumber field to 1.
*/
public Month()
{
monthNumberInt = 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.
*/
public Month(int monthNumber)
{
if((monthNumber < 1 ) || ( monthNumber > 12)) {
this.monthNumberInt = 1;
} else {
this.monthNumberInt = monthNumber;
}
}
public Month(String monthName)
{
monthNumberInt = monthName.indexOf(monthName);
}
public int getMonthNumberInt() {
return monthNumberInt;
}
public void setMonthNumberInt(int monthNumberInt) {
this.monthNumberInt = monthNumberInt;
}
/**
A toString method that returns the same value as the getMonthName method.
*/
public String toString()
{
return "monthName: " + monthName[monthNumberInt];
}
/**
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.
*/
public boolean Equals(Month m)//Month needs to go here.
{
if(this.monthNumberInt == m.getMonthNumberInt())
return true;
else
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.
*/
public boolean GreatThan(Month m1)//Month needs to go here.
{
if(monthNumberInt > m1.monthNumberInt)
return true;
else
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.
*/
public boolean LesserThan(Month m1)//Month needs to go here.
{
if(monthNumberInt < m1.monthNumberInt)
return true;
else
return false;
}
}
And I tested it as:
public static void main(String[] args) {
Month m;
// Set the month number to the values 0 through 12 // (0 is invalid), and display the resulting month name.
for (int i = 0; i <12; i++) {
m = new Month();
m.setMonthNumberInt(i);
System.out.println("Month " + m.getMonthNumberInt() + " is " + m);
}
}
And the output is:
Month 0 is monthName: January
Month 1 is monthName: Februry
Month 2 is monthName: March
Month 3 is monthName: April
Month 4 is monthName: May
Month 5 is monthName: June
Month 6 is monthName: July
Month 7 is monthName: August
Month 8 is monthName: September
Month 9 is monthName: October
Month 10 is monthName: November
Month 11 is monthName: December
Upvotes: 2