-5

The task is to create a function.

The function takes two arguments:

  • current father's age (years)
  • current age of his son (years)

Сalculate how many years ago the father was twice as old as his son (or in how many years he will be twice as old).

public static int TwiceAsOld(int dadYears, int sonYears){

    int dadYearsTemp = dadYears;
    int years = 0;
    int yearsAgo = 0;

    for (int i = 0; i <= dadYears; i++ ){
        if (dadYearsTemp / 2 == sonYears) {
            years = dadYearsTemp;
            yearsAgo = dadYears - years;
            System.out.println(yearsAgo);
            return yearsAgo;
        }
        else if (sonYears * 2 > dadYears) {
            years = (sonYears * 2) - dadYears;
            System.out.println(years);
            return years;
        }
        dadYearsTemp = dadYearsTemp -1;
    }

    return 42; // The meaning of life

}

For example, with an input of (30, 7) I would expect my function to return 16, because 16 years ago the father was 14, which means he was twice as old as his son now (7). But my function returns 15.

I guess it's not a big mistake but I honestly can't find out why it doesnt work, so I would apreciate some help.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Nikolai
  • 3
  • 1
  • 4
  • debug your code and find out – Stultuske Jun 04 '19 at 10:38
  • 2
    Hard to say without seeing all the values of each variable in each step. Just start your code with the IDEs debugger and you see whats up. Or add a lot of print statements to it. Note that you do integer division, not sure if that is what you want (rounds towards zero). – Zabuzard Jun 04 '19 at 10:41
  • 4
    This is an odd way to try and calculate something that should not require a loop. – khelwood Jun 04 '19 at 10:43
  • 3
    Note that you could just do `return dadYears - 2 * sonYears;`. – Zabuzard Jun 04 '19 at 10:44
  • Do it the other way round: Double up the age of the son, check if the father is older or younger than the result and then loop the years (either increment or decrement). – deHaar Jun 04 '19 at 10:46
  • 1
    Also note that your i starts at 0, not 1. Not sure if thats what you want, likewise does your loop go to <= and not <. Again, unsure what you want, but all of that can lead to your off-by-one error. – Zabuzard Jun 04 '19 at 10:47
  • 4
    Just sayin' -> OP has no idea how to calculate the task and i doubt this piece of code is of his own. For input `30, 7`, the answer is `+16` because by the time the father is 46 years old, the child will be at the age of 23 or twice less than his parent... – dbl Jun 04 '19 at 10:48

3 Answers3

2

Let father's current age = f

son's current age = s

let x years back, father was twice as old as son.

then 2(s-x) = (f-x) => x = 2*s - f

Note: if x comes negative, then after x years , father will be twice as old as son(test for input[25,5])

public static void main(String[] args) {
    int x = twiceAsOld(25, 5);
    System.out.println(x);
  }

  public static int twiceAsOld(int dadYears, int sonYears) {
    return 2*sonYears - dadYears;

  }
pankaj
  • 1,004
  • 12
  • 20
  • Mathematics says it all... Still i do believe that the OP should do the effort to find a solution of his own as this is more a technical programming task rather than a math task... – dbl Jun 04 '19 at 11:05
  • Agree, this is just a basic aptitude. – pankaj Jun 04 '19 at 11:07
0

Your function takes two arguments:

current father's age (years) current age of his son (years) Сalculate how many years ago the father was twice as old as his son (or in how many years he will be twice as old). The answer is always greater or equal to 0, no matter if it was in the past or it is in the future.

-2

Below is the sample code to calculate how many years ago father's age was twice as son with sample data.

 public static void main (String args[]) {
    int sonAge = 10;
    int fatherAge = 24;
    int yearsAgo = 0;
    while(sonAge*2 != fatherAge && fatherAge>0 ) {
        yearsAgo++;
        fatherAge = fatherAge-1;
    }
    System.out.println(yearsAgo);
}
  • 1
    Flagged as not an answer as **It is not a correct solution for the task**! – dbl Jun 04 '19 at 11:45
  • 2
    **[Wrong answers should not be deleted from VLQ!](https://meta.stackoverflow.com/q/287563/6296561)**. Downvote and move on. /cc @dbl – Zoe Jun 04 '19 at 14:44