3 Replies - 776 Views - Last Post: 07 February 2018 - 09:29 AM Rate Topic: -----

#1 Abhavya  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 21-January 18

Create a month class with the following method

Posted 06 February 2018 - 09:51 PM

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. 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 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.
I am writing it step by step but this is where i stop.please assist me..!How do I set the monthNumber field to the correct corresponding value?
 public class Month
{
	private int monthNumber;
	enum MonthName {JANUARY, FEBRUARY,MARCH, APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER}
	public Month(){
		monthNumber=1;
	}
	public Month (int numMonth){
		monthNumber=numMonth;
		if(numMonth<1 || numMonth>12)
		{
			monthNumber=1;
		}
		else
		{
			monthNumber=monthNumber;
		}
	}
	public Month(String monthName){
		
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Create a month class with the following method

#2 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 798
  • View blog
  • Posts: 3,262
  • Joined: 13-June 14

Re: Create a month class with the following method

Posted 06 February 2018 - 10:17 PM

Well, what's the logic to determine the month number given its name?
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3541
  • View blog
  • Posts: 16,135
  • Joined: 20-September 08

Re: Create a month class with the following method

Posted 07 February 2018 - 03:35 AM

Note that you have not been asked to use enum - which is a pity, since it's a lot stronger (if used correctly)
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7163
  • View blog
  • Posts: 14,928
  • Joined: 16-October 07

Re: Create a month class with the following method

Posted 07 February 2018 - 09:29 AM

I'm not real clear where you're stuck. Here's a quick layout. Read the comments.
public class Month {
    private int monthNumber;
// A no-arg constructor that sets the monthNumber field to 1.
    public Month() { this(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 numMonth) {
        monthNumber = numMonth;
        if (numMonth < 1 || numMonth > 12) {
            monthNumber = 1;
        } else {
            // this is a bug, look again
            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 Month(String monthName) {
        // if ("January".equals(monthName)) {
        // your code here
    }
    // 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 void setMonthNumber(int n) { /* your code here */ }
// A getMonthNumber method that returns the value in the monthNumber field.
    public int getMonthNumber() { /* your code here */ }
// 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() { /* your code here */ }
// A toString method that returns the same value as the getMonthName method.
    public String toString() { return getMonthName(); /* this is a gimme */ }

    // why don't you do the next three
// 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.

}



There are many clever ways to implement the set with month name. However, if you're confused, try clear before clever. You are already familiar with if-then. You should be able to get there with that alone.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1