2

I have a method to find the day after but I don't know how to return all 3 values day,month,year.Please help me. Any help is appreciated.Thanks in advance.

public int dayInMonth(int day,int month,int year){
    //....
    return day;
}

public int theDayAfter(int day, int month, int year) {
    day++;
    int max=dayInMonth(day,month,year);
    if(day>max)
    {
        month++;
        if(month>12)
        {
            year++;
            month=1;
            day=1;
        }
    }
    return ;
}
Max
  • 81
  • 3
  • 4
    You could return an `int[]`, a `String` **or** a `Date`. What do you want to do with this? Because it looks like you're trying to re-implement `Calendar`. – Elliott Frisch Mar 29 '16 at 16:42
  • 2
    You can't return *three* `int` values from that method, since the return type is just `int`. Either return *one* `int` or change the return type. Perhaps to a custom class which itself contains three `int`s? (Which would also make a good method parameter in this case.) – David Mar 29 '16 at 16:42
  • return it as a `String`? or an `int[3]` with each index being something – 3kings Mar 29 '16 at 16:43
  • 2
    Ugh, *do not* return a `String` or an `int[]`. If those three values semantically form a single structure, then *make them a structure*. (A `class`.) – David Mar 29 '16 at 16:45
  • 1
    Not String, no. Never encode different values into a single string which then have to be parsed back out. C.f *Effective Java 2nd ed.*, #10. – markspace Mar 29 '16 at 16:48
  • @Elliott Frisch No, I'm just practicing – Max Mar 30 '16 at 03:44

1 Answers1

1

It is a valid doubt and a year ago even I was facing such a similar doubt.

I found out that the best way to return such values from a method is to return it in type of an Object. In this example, if I were to return day, month and year then I would create a class as follows:

public class DateOfYear{
   public int day;
   public String month;
   public int year;

   public DateOfYear(int day, String month, int year){
       this.day = day;
       this.month = month;
       this.year = year;
   }
}

Now I will use this class DateofYear instantiate it and send values as an object. So, if you would like to get the Day of the month, the function will be as follows:

public DateOfYear dayInMonth(int day,int month,int year){
    //....
    return new DateOfYear(day,month,year);
}

and the calling function can simply display the current day as follows:

System.out.println("Day: "+dayInMonth(day,month,year).day);

Similarly for the method theDayAfter simply change the return type and return the object of type DateOfYear.

public DateOfYear theDayAfter(int day, int month, int year) {
    day++;
    int max=dayInMonth(day,month,year);
    if(day>max)
    {
        month++;
        if(month>12)
        {
            year++;
            month=1;
            day=1;
        }
    }
    return new DateOfYear(day, month, year);
}

The method calling theDayAfter() can display the next day as follows:

 System.out.println("Next Day: "+theDayAfter(day,month,year).day);
 System.out.println("Month: "+theDayAfter(day,month,year).month);
 System.out.println("Year: "+theDayAfter(day,month,year).year);
srajappa
  • 484
  • 8
  • 19
  • Thanks a lot .It's look like type struct in C/C++,isn't it? – Max Mar 30 '16 at 03:28
  • Absolutely! Even in those two languages we could send out multiple values by sending out instantiated structs. It's a very useful know-how : to send multiple values from a function. Thanks for marking my response as the answer :) – srajappa Mar 30 '16 at 16:17