Results 1 to 11 of 11
  1. #1
    tpfaff is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Exclamation Need help with constructors

    Edit: Added all instructions

    the program is now functional but I dont think it fits the requirements of how i was supposed to do it, so if you could point me in the right direction I'd appreciate it!

    Midterm Part II
    Problem Statement: Month Class

    Write a class named Month. The class should have an int field named monthNumber which will hold the number of the month. For example, January would be 1, February would be 2, etc. In addition, provide the following methods:

    · A default 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” , “February”, etc., as an argument. It should set the monthNumber field to the corresponding value for the month named.

    · [[[ 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 toString method that returns the same value as the getMonthName method.


    Class Demonstration // use default constructor, and each variation example: new Month(), new Month("March"); new Month(1);
    m0 => 3:March
    m1 => 2:February
    m2 => 5:May
    m3 => 10:October
    m4 => 1:January
    m5 => 11:November
    m6 => 1:January
    m7 => 4:April
    m8 => 9:September
    m9 => 1:January
    m10 => 7:July
    m11 => 8:August
    m12 => 10:October

    Month Class Demonstration //use the created Month objects to setMonthNumber() example: Month m0 = new Month("March"); m0.setMonthNumber(12);

    m0 => 7:July
    m1 => 5:May
    m2 => 5:May
    m3 => 7:July
    m4 => 2:February
    m5 => 1:January
    m6 => 9:September
    m7 => 1:January
    m8 => 6:June
    m9 => 3:March
    m10 => 3:March
    m11 => 7:July
    m12 => 11:November

    for the driver class...
    Java Code:
    package midtermpt2;
    
    import java.util.Scanner;
    public class MonthDriver {
       static int usernumber;
       
    
        public static void main(String[] args) {
            Months MonthsObj=new Months();
            getusernumber();
    //        MonthsObj.Months();
            MonthsObj.setmonthNum(usernumber);
            MonthsObj.getMonthName();
        }
    
        public static int getusernumber(){
            Scanner input=new Scanner(System.in);
            System.out.println("Month number");
            usernumber=input.nextInt();
            return usernumber;
                    
        }
    
    }
    and the other class...
    Java Code:
    public class Months {
    
        int monthNumber = 0;
        String monthName;
    
        public Months() {
            monthNumber = 1;
        }
    
    
        public int setmonthNum(int usernumber) {
            if (usernumber < 1 || usernumber > 12) {
                return monthNumber;
            } else {
                monthNumber = usernumber;
                return monthNumber;
            }//closes else
        }
    
        public String getMonthName() {
            switch (monthNumber) {
                case 1:
                    monthName = "January";
                    System.out.println(monthName);
                    return monthName;
                case 2:
                    monthName = "February";
                    System.out.println(monthName);
                    return monthName;
                case 3:
                    monthName = "March";
                    System.out.println(monthName);
                    return monthName;
                case 4:
                    monthName = "April";
                    System.out.println(monthName);
                    return monthName;
                case 5:
                    monthName = "May";
                    System.out.println(monthName);
                    return monthName;
                case 6:
                    monthName = "June";
                    System.out.println(monthName);
                    return monthName;
                case 7:
                    monthName = "July";
                    System.out.println(monthName);
                    return monthName;
                case 8:
                    monthName = "August";
                    System.out.println(monthName);
                    return monthName;
                case 9:
                    monthName = "September";
                    System.out.println(monthName);
                    return monthName;
                case 10:
                    monthName = "October";
                    System.out.println(monthName);
                    return monthName;
                case 11:
                    monthName = "November";
                    System.out.println(monthName);
                    return monthName;
                case 12:
                    monthName = "December";
                    System.out.println(monthName);
                    return monthName;
                default:
                    System.out.println("invalid number");
                    return monthName;
            }
        }
    }//close class
    So now I dont get what hes asking in the bolded type. Which way of doing it am I demonstrating with the above program?
    Last edited by tpfaff; 07-25-2010 at 01:16 PM.

  2. #2
    tpfaff is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    ok so now im reading over it again and the terminology is really confusing me but im pretty sure when it says an int field it means to make an array named monthnumber and have it go 0-11

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    constructors have no return values, not even void. so delete void from your constructors.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    There's a few issues here:

    First, what j2me mentioned: Don't assign a return type to the constructor--simply "public Months() {". Also, don't return a value. (See Understanding constructors - JavaWorld.)

    Second, you should be trying to hold a single integer value in the variable monthNumber; you are storing an array. (Read the first bit of your assignment: Write a class named Month. The class should have an int field named monthNumber.. Note that it says int, not int[].)

    Third, when it says to "sets the monthNumber field to", it means to assign a value to that variable. Take a look at the line "usernumber=input.nextInt();"--this is an example of setting a value to a variable.

    Let's start by fixing those--we'll get to the rest of the project in a bit.
    Last edited by Zack; 07-24-2010 at 06:24 PM.

  5. #5
    tpfaff is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Second, you should be trying to hold a single integer value in the variable monthNumber; you are storing an array. (Read the first bit of your assignment: Write a class named Month. The class should have an int field named monthNumber.. Note that it says int, not int[].)


    Wouldnt it seem better to do it the way I did it? I understand that thats not what its asking me to do but I'm a little confused as to why he'd want us to do it that way

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    int monthNumber[]={0,1,2,3,4,5,6,7,8,9,10,11,12};
    In this array, the value is the same as the index to the value.
    For example: monthNumber[2] = 2;

    The array is redundant.

  7. #7
    tpfaff is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    ah, haha that makes sense, so itd be better to just put [12] instead of listing them all right?

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    No, it would be better to just make it an int field, as your project suggests.

    int monthNumber = 0;

    Then set its initial value in your constructor.

  9. #9
    tpfaff is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Somebody pmed me and asked for the final code

    I didn't ever figure out how to compare two objects, my teacher showed me how but I don't have his version only mine. I would still like to know if someone wants to show me ;)

    Driver class

    Java Code:
    package part2;
    
    public class MonthDriver {
    
        public static void main(String[] args) {
            System.out.println("Constructor demo");
            Months m0 = new Months("MARCH");//accessing non default string constructor
            //looks for enum march, does what it says in there. Same for all 13 objects
            m0 = new Months(3);//accessing non default int constructor, does what is says at case 3. Same for all 13 objects.
            Months m1 = new Months("FEBRUARY");
            m1 = new Months(2);
            Months m2 = new Months("MAY");
            m2 = new Months(5);
            Months m3 = new Months("OCTOBER");
            m3 = new Months(10);
            Months m4 = new Months("JANUARY");
            m4 = new Months(1);
            Months m5 = new Months("NOVEMBER");
            m5 = new Months(11);
            Months m6 = new Months("JANUARY");
            m6 = new Months(1);
            Months m7 = new Months("APRIL");
            m7 = new Months(4);
            Months m8 = new Months("SEPTEMBER");
            m8 = new Months(9);
            Months m9 = new Months("JANUARY");
            m9 = new Months(1);
            Months m10 = new Months("JULY");
            m10 = new Months(4);
            Months m11 = new Months("AUGUST");
            m11 = new Months(6);
            Months m12 = new Months("OCTOBER");
            m12 = new Months(10);
            System.out.println();
    
            System.out.println("Month Class Demonstration");
    
            m0.setmonthNum(7);//sets month number to 7
            m0.getoMonthNumber();//returns month number
            m0.getMonthName();//gets the month name based on themonthNumber;
            m0.print();//prints monthnumber
            m0.toString();//prints monthname and returns it as a string
            //same steps for all 13 objects
            
            m1.setmonthNum(5);
            m1.getoMonthNumber();
            m1.getMonthName();
            m1.print();
            m1.toString();
            
            m2.setmonthNum(5);
            m2.getoMonthNumber();
            m2.getMonthName();
            m2.print();
            m2.toString();
            
            m3.setmonthNum(7);
            m3.getoMonthNumber();
            m3.getMonthName();
            m3.print();
            m3.toString();
            
            m4.setmonthNum(2);
            m4.getoMonthNumber();
            m4.getMonthName();
            m4.print();
            m4.toString();
            
            m5.setmonthNum(1);
            m5.getoMonthNumber();
            m5.getMonthName();
            m5.print();
            m5.toString();
            
            m6.setmonthNum(9);
            m6.getoMonthNumber();
            m6.getMonthName();
            m6.print();
            m6.toString();
    
            
            m7.setmonthNum(1);
            m7.getoMonthNumber();
            m7.getMonthName();
            m7.print();
            m7.toString();
    
            m8.setmonthNum(6);
            m8.getoMonthNumber();
            m8.getMonthName();
            m8.print();
            m8.toString();
            
            m9.setmonthNum(3);
            m9.getoMonthNumber();
            m9.getMonthName();
            m9.print();
            m9.toString();
            
            m10.setmonthNum(3);
            m10.getoMonthNumber();
            m10.getMonthName();
            m10.print();
            m10.toString();
            
            m11.setmonthNum(7);
            m11.getoMonthNumber();
            m11.getMonthName();
            m11.print();
            m11.toString();
            
            m12.setmonthNum(11);
            m12.getoMonthNumber();
            m12.getMonthName();
            m12.print();
            m12.toString();
    
            System.out.println();
            System.out.println("greater than less than equal to comparison");
            System.out.println();
    //        System.out.println("m0="+m0.setmonthNum(7)+" m1="+m1.setmonthNum(5));//
            System.out.println(""+m0+m1);// the objects are set to string values and I cant figure out why
            m1.equals(m0, m1);//none of these work correctly
            m1.lessThan(m1, m0);
            m1.greaterThan(m1, m0);
    
        }
    }
    Java Code:
    
    
    
    
    package part2;
    
    public class Months {
    
        public int monthNumber;
        public String monthName;
        public int oMonthNumber;
    
        enum Spectrum {//declares the values that are going to be used in the enum
    
            JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,
            AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
        }
    
        public Months() {
            monthNumber = 1;
        }
    
        public  Months(int monthNumber2) {//sets monthname from the objects int value and prints month name
            switch (monthNumber2) {
                case 1:
                    monthName="January";
                    System.out.println(":"+monthName);
                    //sets monthname from the objects int value and prints month name
                    break;
                case 2:
                    monthName="February";
                    System.out.println(":"+monthName);
                   //sets monthname from the objects int value and prints month name
                    break;
                case 3:
                    monthName="March";
                    System.out.println(":"+monthName);
                    //sets monthname from the objects int value and prints month name
                    break;
                case 4:
                    monthName="April";
                    System.out.println(":"+monthName);
                   //sets monthname from the objects int value and prints month name
                    break;
                case 5:
                    monthName="May";
                    System.out.println(":"+monthName);
                   //sets monthname from the objects int value and prints month name
                    break;
                case 6:
                    monthName="June";
                    System.out.println(":"+monthName);
                    //sets monthname from the objects int value and prints month name
                    break;
                case 7:
                    monthName="July";
                    System.out.println(":"+monthName);
                    //sets monthname from the objects int value and prints month name
                    break;
                case 8:
                    monthName="August";
                    System.out.println(":"+monthName);
                    //sets monthname from the objects int value and prints month name
                    break;
                case 9:
                    monthName="September";
                    System.out.println(":"+monthName);
                    //sets monthname from the objects int value and prints month name
                    break;
                case 10:
                    monthName="October";
                    System.out.println(":"+monthName);
                    //sets monthname from the objects int value and prints month name
                    break;
                case 11:
                    monthName="November";
                    System.out.println(":"+monthName);
                    //sets monthname from the objects int value and prints month name
                    break;
                case 12:
                    monthName="December";
                    System.out.println(":"+monthName);
                    //sets monthname from the objects int value and prints month name
                    break;
                default:
                    System.out.println("invalid number");
                                    break;
            }
    
        }
    
        public Months(String monthname) {
    //        A constructor that accepts the name of the month, such as “January” ,
    //                “February”, etc., as an argument. It should set the monthNumber
    //                field to the corresponding value for the month named.
    //        monthname=null;
            Spectrum month;
            month = Spectrum.valueOf(monthname);
            switch (month) {
                case JANUARY:
                    monthNumber = 1;
                    System.out.print(monthNumber);
                    break;
                case FEBRUARY:
                    monthNumber = 2;
                    System.out.print(monthNumber);
                    break;
                case MARCH:
                    monthNumber = 3;
                    System.out.print(monthNumber);
                    break;
                case APRIL:
                    monthNumber = 4;
                    System.out.print(monthNumber);
                    break;
                case MAY:
                    monthNumber = 5;
                    System.out.print(monthNumber);
                    break;
                case JUNE:
                    monthNumber = 6;
                    System.out.print(monthNumber);
                    break;
                case JULY:
                    monthNumber = 7;
                    System.out.print(monthNumber);
                    break;
                case AUGUST:
                    monthNumber = 8;
                    System.out.print(monthNumber);
                    break;
                case SEPTEMBER:
                    monthNumber = 9;
                    System.out.print(monthNumber);
                    break;
                case OCTOBER:
                    monthNumber = 10;
                    System.out.print(monthNumber);
                    break;
                case NOVEMBER:
                    monthNumber = 11;
                    System.out.print(monthNumber);
                    break;
                case DECEMBER:
                    monthNumber = 12;
                    System.out.print(monthNumber);
                    break;
                default://fix later
                    System.out.println("Month number 1");
                    monthNumber = 1;
                    monthname = "HELLO";
                    break;
            }
        }
    
        public void setmonthNum(int monthNumber2) {
     switch (monthNumber2) {
              
                case 1:
                    monthNumber=1;
                    break;
    
    
                case 2:
                    monthNumber=2;
                    break;
    
                case 3:
                    monthNumber=3;
                    break;
    
                case 4:
                    monthNumber=4;
                    break;
    
                case 5:
                    monthNumber=5;
                    break;
    
                case 6:
                    monthNumber=6;
                    break;
    
                case 7:
                    monthNumber=7;
                    break;
    
                case 8:
                    monthNumber=8;
                    break;
    
                case 9:
                    monthNumber=9;
                    break;
    
                case 10:
                    monthNumber=10;
                    break;
    
                case 11:
                    monthNumber=11;
                    break;
    
                case 12:
                    monthNumber=12;
                    break;
    
            
                default:
                    monthNumber=1;
                    break;
    
        }
        }
    
        public void print() {
            System.out.print(monthNumber);
        }
    
        public int getoMonthNumber() {
            this.monthNumber = monthNumber;
            return monthNumber;
    
        }
    
        public int getMonthName() {
         switch (monthNumber) {
                case 1:
                    monthName = "January";//the objects are making themselves equal to these strings and I dont know why.
                    monthNumber = 1;
                    return monthNumber;
    
                case 2:
                    monthName = "February";
                    monthNumber = 2;
                    return monthNumber;
                case 3:
                    monthName = "March";
                    monthNumber = 3;
                   
                    return monthNumber;
                case 4:
                    monthName = "April";
                    monthNumber = 4;
                   
                    return monthNumber;
                case 5:
                    monthName = "May";
                    monthNumber = 5;
                    
                    return monthNumber;
                case 6:
                    monthName = "June";
                    monthNumber = 6;
                    
                    return monthNumber;
                case 7:
                    monthName = "July";
                    monthNumber = 7;
                    
                    return monthNumber;
                case 8:
                    monthName = "August";
                    monthNumber = 8;
                    
                    return monthNumber;
                case 9:
                    monthName = "September";
                    monthNumber = 9;
                    
                   return monthNumber;
                case 10:
                    monthName = "October";
                    monthNumber = 10;
                    
                    return monthNumber;
                case 11:
                    monthName = "November";
                    monthNumber = 11;
                   
                    return monthNumber;
                case 12:
                    monthName = "December";
                    monthNumber = 12;
                    
                    return monthNumber;
                default:
                    System.out.println("invalid number");
                    return monthNumber;
            }
        }
    
    
        public String toString() {
            System.out.println(":" + monthName);
            return monthName.toString();
        }
    
        public boolean equals(Months m0, Months m1) {
            if (m0 == m1) {
                System.out.println("m0==m1");
                return true;
            } else {
                System.out.println("mo!=m1");
                return false;
            }
    
        }
    
    
            //returns true if the first object is greater than the second
        }//close class

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Somebody pmed me and asked for the final code
    I would report abuse for that. Technical questions should be kept on the forum. A PM suggests a homework moocher.

    db

  11. #11
    tpfaff is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Constructors?
    By annna in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 10:51 PM
  2. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 02:14 PM
  3. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 06:46 AM
  4. Help with constructors
    By Minime in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 07:59 AM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •