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

    Default Need urgent help. This is due in an hour!

    Java Code:
    Driver class
    
    
    package midtermpart2;
    
    import java.util.Scanner;
    
    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.
            m0 = new Months(3);
            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");
            //need setters
            //fix tostring
            m0.setmonthNum(7);
            m0.getMonthName();
            m0.getoMonthNumber();
            m0.print();
            m0.toString();
            
            m1.setmonthNum(5);
            m1.getMonthName();
            m1.getoMonthNumber();
            m1.print();
            m1.toString();
            
            m2.setmonthNum(5);
            m2.getMonthName();
            m2.getoMonthNumber();
            m2.print();
            m2.toString();
            
            m3.setmonthNum(7);
            m3.getMonthName();
            m3.getoMonthNumber();
            m3.print();
            m3.toString();
            
            m4.setmonthNum(2);
            m4.getMonthName();
            m4.getoMonthNumber();
            m4.print();
            m4.toString();
            
            m5.setmonthNum(1);
            m5.getMonthName();
            m5.getoMonthNumber();
            m5.print();
            m5.toString();
            
            m6.setmonthNum(9);
            m6.getMonthName();
            m6.getoMonthNumber();
            m6.print();
            m6.toString();
    
            
            m7.setmonthNum(1);
            m7.getMonthName();
            m7.getoMonthNumber();
            m7.print();
            m7.toString();
    
            m8.setmonthNum(6);
            m8.getMonthName();
            m8.getoMonthNumber();
            m8.print();
            m8.toString();
            
            m9.setmonthNum(3);
            m9.getMonthName();
            m9.getoMonthNumber();
            m9.print();
            m9.toString();
            
            m10.setmonthNum(3);
            m10.getMonthName();
            m10.getoMonthNumber();
            m10.print();
            m10.toString();
            
            m11.setmonthNum(7);
            m11.getMonthName();
            m11.getoMonthNumber();
            m11.print();
            m11.toString();
            
            m12.setmonthNum(11);
            m12.getMonthName();
            m12.getoMonthNumber();
            m12.print();
            m12.toString();
    
    
            
            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);
            m1.equals(m0,m0);
            m1.equals(m1,m1);
            m1.lessThan(m1, m0);
            m1.greaterThan(m1, m0);
            m1.lessThan(m0, m0);
    
        }
    }
    Other class


    Java Code:
    package midtermpart2;
    
    public class Months {
    
        public int monthNumber;
        public String monthName;
        public int oMonthNumber;
    
        enum Spectrum {
    
            JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,
            AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
        }
    
        public Months() {
            monthNumber = 1;
        }
    
        public  Months(int monthNumber) {
            switch (monthNumber) {
                case 1:
                    System.out.println(":January");
                    this.monthNumber = monthNumber;
                    break;
                case 2:
                    System.out.println(":February");
                    this.monthNumber = monthNumber;
                    break;
                case 3:
                    System.out.println(":March");
                    this.monthNumber = monthNumber;
                    break;
                case 4:
                    System.out.println(":April");
                    this.monthNumber = monthNumber;
                    break;
                case 5:
                    System.out.println(":May");
                    this.monthNumber = monthNumber;
                    break;
                case 6:
                    System.out.println(":June");
                    this.monthNumber = monthNumber;
                    break;
                case 7:
                    System.out.println(":July");
                    this.monthNumber = monthNumber;
                    break;
                case 8:
                    System.out.println(":August");
                    this.monthNumber = monthNumber;
                    break;
                case 9:
                    System.out.println(":September");
                    this.monthNumber = monthNumber;
                    break;
                case 10:
                    System.out.println(":October");
                    this.monthNumber = monthNumber;
                    break;
                case 11:
                    System.out.println(":November");
                    this.monthNumber = monthNumber;
                    break;
                case 12:
                    System.out.println(":December");
                    this.monthNumber = monthNumber;
                    break;
                default:
                    System.out.println("invalid number");
                    this.monthNumber = monthNumber;
                    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 int setmonthNum(int monthNumber2) {
     switch (monthNumber2) {
              
                case 1:
                    monthNumber=1;
                    return monthNumber;
    
    
                case 2:
                    monthNumber=2;
                    return monthNumber;
    
                case 3:
                    monthNumber=3;
                    return monthNumber;
    
                case 4:
                    monthNumber=4;
                    return monthNumber;
    
                case 5:
                    monthNumber=5;
                    return monthNumber;
    
                case 6:
                    monthNumber=6;
                    return monthNumber;
    
                case 7:
                    monthNumber=7;
                    return monthNumber;
    
                case 8:
                    monthNumber=8;
                    return monthNumber;
    
                case 9:
                    monthNumber=9;
                    return monthNumber;
    
                case 10:
                    monthNumber=10;
                    return monthNumber;
    
                case 11:
                    monthNumber=11;
                    return monthNumber;
    
                case 12:
                    monthNumber=12;
                    return monthNumber;
    
            
                default:
                    monthNumber=1;
                    return monthNumber;
    
        }
        }
    
        public void print() {
            System.out.print(monthNumber);
        }
    
        public int getoMonthNumber() {
            this.monthNumber = monthNumber;
            return monthNumber;
    
        }
    
        public int getMonthName() {
         switch (monthNumber) {
                case 1:
                    monthName = "January";
                    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;
            }
    
        }
    
        public boolean lessThan(java.lang.Object m1,
                java.lang.Object m0) {
            if (true) {
                System.out.println("m1<m0");
                return true;
            } else {
                System.out.println("m1>m0");
                return false;
            }
        }
    
        public boolean greaterThan(java.lang.Object m1,
                java.lang.Object m0) {
            //automatically returns true if m1>m0, returns false if it is false.
            if (true) {
                return true;
            } else {
                return false;
            }
    
    
            //returns true if the first object is greater than the second
        }
    }//close class

    So the question is, in the driver class when Im trying to compare the two objects...they are set as string values and I can't figure out why.

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

    Default

    I guess its too late now.
    Did you comment in the code where the problem is?
    Did you give us a String (** ERROR HERE **) we could search for to find the comment?

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

    Default

    Yeah its too late, oh well. Anyways the problem was in the driver class while comparing the objects, at the very end of the class, the objects held strings values and I couldn't pinpoint what was causing that.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Next time if you are asking a question please be specific as much as possible. And also provide more details you have, like the complete error message and stuff. So that's easy to comment on the original question. :)

    And also next time use a much better title in your thread. ;)

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    They are only "string values" in the line
    Java Code:
            System.out.println(""+m0+m1);// the objects are set to string values and I cant figure out why
    and that's because you are forcing them into it by doing String concatenation. (+ when applied to String)

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

    Default

    interesting, when I printed them out with no concatenation they printed as like a really weird value that seemed like was a indicating an location

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Will this ever return false?
    Java Code:
        public boolean lessThan(java.lang.Object m1,
                java.lang.Object m0) {
            if (true) {
                System.out.println("m1<m0");
                return true;
            } else {
                System.out.println("m1>m0");
                return false;
            }
        }

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

    Default

    I had to turn it in and my teacher corrected it for me, if you guys would like the final code posted i can do that

Similar Threads

  1. 24 hour clock to 12 hour clock project.
    By bs3ac in forum New To Java
    Replies: 4
    Last Post: 01-08-2013, 10:10 AM
  2. computing hour & Salary
    By arshesander in forum New To Java
    Replies: 4
    Last Post: 08-07-2009, 10:17 PM
  3. HELP!! Assignment due in one hour!
    By murr0508 in forum New To Java
    Replies: 1
    Last Post: 02-26-2009, 09:31 PM
  4. [SOLVED] Very Simple 2-3 hour project $50
    By arbit3r in forum Jobs Offered
    Replies: 3
    Last Post: 08-05-2008, 03:20 AM
  5. Calculating per hour statistics
    By vipergt89 in forum New To Java
    Replies: 2
    Last Post: 05-01-2008, 06:25 AM

Posting Permissions

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