I am new to programming and was thrown into an online java course before I took the prerequisite course. I love programming so far, but am lost in java and would appreciate any help that anyone has. I have the following programming challenge to complete. As well as creating a driver program to test the program:


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 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 of 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 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.
-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.

Well, what do you have and what error/compiler messages is it producing? We are not simply going to do it for you.

commented: I never get tired of seeing counter-posts like this XP +4
commented: Couldn't agree more with Alex :D !!! +3

I so far I have two classes... main and month

Im not sure that I have them set up correctly... here is what I have so far:

package cjpmonthclass;



public class Main
{
        public static void demo1()
        {

                Month m = new Month();
                System.out.println("Month " + m.getMonthNumber() +
                                   " is " + m);

                for (int i = 0; i <= 12; i++)
                {
                        m.setMonthNumber(i);
                        System.out.println("Month " + m.getMonthNumber() +
                                   " is " + m);
                }
        }

        public static void demo2()
        {

                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);


                if (m1.equals(m2))
                        System.out.println(m1 + " and " + m2 + " are equal.");
                else
                        System.out.println(m1 + " and " + m2 + " are NOT equal.");


                if (m1.greaterThan(m2))
                        System.out.println(m1 + " is greater than " + m2);
                else
                        System.out.println(m1 + " is NOT greater than " + m2);


                if (m1.lessThan(m2))
                        System.out.println(m1 + " is less than " + m2);
                else
                        System.out.println(m1 + " is NOT less than " + m2);
        }

        public static void demo3()
        {

                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);
        }

        public static void main(String[] args) {
            System.out.println("DEMO 1: setMonthNumber test");
            demo1();
            System.out.println();
            System.out.println("DEMO 2: Constructors and comparisons");
            demo2();
            System.out.println();
            System.out.println("DEMO 3: Construction from month names");
            demo3();
        }
}

The following is in my monthClass....

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cjpmonthclass;

import java.util.Scanner;


/**
 *
 * @author Cory
 */
class Month {

    private int monthNumber;
    private String monthName;
    private boolean month;

    Month() {
       monthNumber = 1;
    }



    Month(int i) 
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("What is the month?");
        monthNumber= keyboard.nextInt();      

    }

    Month(String monthName) {
        String m1 = "January";

        String m2 = "February";
        String m3 = "March";
        String m4 = "April";
        String m5 = "May";
        String m6 = "June";
        String m7 = "July";
        String m8 = "August";
        String m9 = "September";
        String m10 = "October";
        String m11 = "November";
        String m12 = "December";
    }

    String getMonthNumber() {
        String Number = null;

        return Number;

    }

    boolean greaterThan(Month m2) {
        int m = 0;
        if(monthNumber > m)
            System.out.println("True");

    }

    boolean lessThan(Month m2) {
        int m = 0;
        if (monthNumber < m)
            System.out.println("False");
        return ("False");
    }

    void setMonthNumber(int i) 
    {
        monthNumber = 1;

    }




}

Use code tags please. And post the entire and complete compiler messages and/or exceptions you are getting.

commented: So much +1 in this thread it's not even funny =P +4

Use code tags please. And post the entire and complete compiler messages and/or exceptions you are getting.

I guess you forgot to mention relevant code this time too. . . . . :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.