Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Leap Year/Calendar Program. toString() method error.

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Leap Year/Calendar Program. toString() method error.

    I am implementing a class called GregorianCalendar. However, I am stuck on the method toString(). When I run my code has i get the output:
    1, March 19, 2012 occurred in a leap year. I need the '1' to convert to the day, 'Monday'.

    package gregoriandatetesterapp;
     
    /**
     *
     * @author Jarrod
     */
    public class GregorianDate 
    {
        /**
         * integer representing the day
         */
        private int day;
     
        /**
         * integer 1-12 representing the month
         */
        private int month;
     
        /**
         * integer representing the year
         */
        private int year;
     
        /**
         * creates a date object that represents January 1, 1970
         */
        public GregorianDate()
        {
            month = 1;
            day = 1;
            year = 1970;
        }
     
        /**
         * creates a date object.
         * @param mm - the month of the specified date
         * @param dd - the day of the specified date
         * @param yy - the year of the specified date
         * @throw java.lang.IllegalArgumentException - when date is not valid
         */
        public GregorianDate(int mm, int dd, int yy)
        {
            if (mm < 1 || mm > 12)
                throw new IllegalArgumentException("This is not a valid month");
            if (dd < 1 || dd > 31)
                throw new IllegalArgumentException("This is not a valid day");
            if ((mm == 4 || mm == 6 || mm == 10 || mm == 11) && dd == 31)
                throw new IllegalArgumentException("This is not a valid date");
            if (mm == 2 && isLeap() && dd > 28)
                throw new IllegalArgumentException("This is not a valid date");
            if (mm == 2 && dd > 28)
                throw new IllegalArgumentException("This is not a valid date");
     
            month = mm;
            day = dd;
            year = yy;
        }
     
        /**
         * gives a number representing the day of the week of this date
         * @return a number 0-6 representing the day of the week on which this date occurs. 0 = Sunday, 1 = Monday, etc
         */
        public int dayOfWeek()
        {
           int u = 2*(3-((year/100)%4));
           int v = year%100;
           int w = v/4;
           int x = 0;
     
           if (isLeap() == false)
           {
                if (month == 1)
                    x = 0;
                if (month == 2)
                    x = 2;
                if (month == 3 || month == 11)
                    x = 3;
                if (month == 4 || month == 7)
                    x = 6;
                if (month == 5)
                    x = 1;
                if (month == 6)
                    x = 4;
                if (month == 8)
                    x = 2;
                if (month == 9 || month == 12)
                    x = 5;
                if (month == 10)
                    x = 0;
           }
           else
           {
               if (month == 1)
                   x = 6;
               if (month == 2)
                   x = 2;
               if (month == 3 || month == 11)
                    x = 3;
               if (month == 4 || month == 7)
                    x = 6;
               if (month == 5)
                    x = 1;
               if (month == 6)
                    x = 4;
               if (month == 8)
                    x = 2;
               if (month == 9 || month == 12)
                    x = 5;
               if (month == 10)
                    x = 0;
           }
     
           int y = u + v + w + x + day;
     
           return y%7;
        }
     
        /**
         * determine whether a year is a leap year.
         */
        public boolean isLeap() 
        {
           if (year%400 == 0 || (year%100 != 0 && year%4 == 0))
               return true;
           else 
               return false;
        }
     
        /**
         * gives the month in words
         * @param monthNum - a number 1-12 representing a month
         * @return the month in words with only its first letter in uppercase.
         */
        public java.lang.String monthToString(int monthNum)
        {
            month = monthNum;
     
            if (monthNum == 1) 
                return "January";
            if (monthNum == 2)
                return "February";
            if (monthNum == 3)
                return "March";
            if (monthNum == 4)
                return "April";
            if (monthNum == 5)
                return "May";
            if (monthNum == 6)
                return "June";
            if (monthNum == 7)
                return "July";
            if (monthNum == 8)
                return "August";
            if (monthNum == 9)
                return "September";
            if (monthNum == 10)
                return "October";
            if (monthNum == 11)
                return "November";
            if (monthNum == 12)
                return "December"; 
            else 
                return "Invalid Month";
        }
     
        /**
         * gives the day of the week in words
         */
        public java.lang.String weekdayToString(int dayNum) 
        {
            day = dayNum;
     
            if (dayNum == 0)
                return "Sunday";
            if (dayNum == 1)
                return "Monday";
            if (dayNum == 2)
                return "Tuesday";
            if (dayNum == 3)
                return "Wednesday";
            if (dayNum == 4)
                return "Thursday";
            if (dayNum == 5)
                return "Friday";
            if (dayNum == 6)
                return "Saturday";
            else
                return "Invalid Day";
        }     
        /**
         * gives a string representation of this object
         */
        public java.lang.String toString() 
        {
           String ds  = new Integer(dayOfWeek()).toString();
           ds = ds + ", ";
           ds = ds + monthToString(month);
           ds = ds + day;
           ds = ds + ", ";
           ds = ds + year;
           return ds;  
        }
    }

    package gregoriandatetesterapp;
     
    import java.util.*;
    import java.lang.*;
     
    public class GregorianDateTester 
    {
        public static void main(String[] args) 
        {
            int month, day, year;
     
            Scanner keyb = new Scanner(System.in);
            System.out.print("Enter numeric values for the month, day and year> ");
            month = keyb.nextInt();
            day = keyb.nextInt();
            year = keyb.nextInt();
     
            GregorianDate gd = new GregorianDate(month, day, year);
     
            if (gd.isLeap())
                System.out.println(gd + " occurred in a leap year.");
            else
                System.out.println(gd + " occurred in a non-leap year.");
     
        }
    }

    So output is currently looking like this:
    Enter numeric values for month, day and year of a date> 3 20 2012
    2, March 20, 2012 occurred in a leap year.

    I need output to look like this:
    Enter numeric values for month, day and year of a date> 3 20 2012
    Tuesday, March 20, 2012 occurred in a leap year.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Leap Year/Calendar Program. toString() method error.

    Change the toString method to return the day of the week instead of an Integer value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Leap Year/Calendar Program. toString() method error.

    Can you show the correction?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Leap Year/Calendar Program. toString() method error.

    The code in the toString() method should be changed to return the values you want to see.
    The thing you want to change is the first part of the String that is now an Integer.
    Where is the value you want first in the String instead of a number?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with My Leap Year Calculation
    By LittleGirl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 7th, 2012, 10:57 AM
  2. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  3. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  4. Leap year program
    By cyspope in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 22nd, 2010, 10:20 PM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM