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

Thread: Overloading a method

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Overloading a method

    Hello, Previously I was asked to post a specific question.. so I hope this one specific enough

    This is in concern to the exercise mentioned here
    http://www.javaprogrammingforums.com...ogramming.html

    Now, here is my code:

     
    public class DayType {
     
    	final static int SUN = 1;
        final static int MON = 2;
        final static int TUE = 3;
        final static int WED = 4;
        final static int THU = 5;
        final static int FRI= 6;
        final static int SAT = 7;
     
        private int day;
     
        public DayType(int day) {
                this.day = day;
        }
     
        public void setDay(int day){
                this.day = day;
        }
     
        public int getDay() {
                return day;
        }
     
        public void print() {
                System.out.println(this.toString());
        }
        public int nextDay(){
                int next;
                if (day<7)
                {
                	next = (day + 1);
                }
                else
                {
                next = 1;
                }
                return next;
        }
        public int previousDay(){
                int prevDay;
                if (day>1)
                {
                	prevDay = (day - 1);
                }
                else
                {
                	prevDay = 7;
                }
                return prevDay;
        }
        public int addDays(int days) {
                return (day + days) % 7;
        }
                public String toString() {
                switch (this.day) {
                case SUN:
                        return "Sunday";
                case MON:
                        return "Monday";
                case TUE:
                        return "Tuesday";
                case WED:
                        return "Wednesday";
                case THU:
                        return "Thursday";
                case FRI:
                        return "Friday";
                case SAT:
                        return "Saturday";
                }
                return "";
        }       
        public static void main(String[] args) {
                System.out.println("******Test Day******");
                System.out.println();
                System.out.print("Set day: ");
                DayType d = new DayType(SUN);
                d.print();
                System.out.print("Next day: ");
                d.setDay(d.nextDay());
                d.print();
                System.out.print("Previous day: ");
                d.setDay(d.previousDay());
                d.print();
                System.out.print("After 5 days: ");
                d.setDay(d.addDays(5));
                d.print();
        }       
    }

    The output of this code is as follows:

    ******Test Day******
     
    Set day: Sunday
    Next day: Monday
    Previous day: Sunday
    After 5 days: Friday


    First of all, the problem is that my previous day comes out to be Sunday instead of Saturday, even though I don't see any problem with the code.

    Secondly, I don't know what overloading means in JAVA. I want to overload the methods for items A and C (from the question). The overloaded methods should accept and return an int value representing the day of week (e.g. Sunday = 1, Monday =2, …, Saturday = 7).

    Please help me!
    Thanks


  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: Overloading a method

    I don't know what overloading means in JAVA. I want to overload the methods for items A and C
    First you should spend some time researching what overloading methods means.
    http://docs.oracle.com/javase/tutori...O/methods.html

    Why do you think you want to overload methods?
    What method do you want to overload?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Overloading a method

    If I follow your logic correctly...
    Set day: Sunday
    Next day: Monday // d.setDay(d.nextDay()) == setDay(MON)
    Previous day: Sunday // So how could this be Saturday if you just set your day to Monday
    After 5 days: Friday
    Last edited by mariostg; April 15th, 2013 at 11:47 AM. Reason: typo + clarify

Similar Threads

  1. overloading an equals method
    By hannah87 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 15th, 2013, 02:54 PM
  2. Abstract method vs overloading
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: January 10th, 2013, 02:58 AM
  3. Overloading Method
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2012, 02:28 PM
  4. Advantages of method Overloading and Overriding
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2012, 04:55 AM
  5. Method Overloading - Doubt
    By vidya lakshman in forum Java Theory & Questions
    Replies: 2
    Last Post: January 31st, 2011, 09:32 AM