-1

I want it to return new Dillo and print out the length of new Dillo. When I compile the code, it will say: Error: Unreachable code for the line System.out.println(this.length); How can I fix this? Thank you

import tester.* ;

class Dillo {
    int length ;
    Boolean isDead ;

    Dillo (int length, Boolean isDead) {
      this.length = length ;
      this.isDead = isDead ;
    }

    // produces a dead Dillo one unit longer than this one
    Dillo hitWithTruck () {
      return new Dillo(this.length + 1 , true) ;
      System.out.println(this.length);
    } 
}

  class Examples {
    Examples () {} ;
    Dillo deadDillo = new Dillo (2, true) ;
    Dillo bigDillo = new Dillo (6, false) ;
 }
Bighuyou
  • 83
  • 2
  • 10
  • There is nothing wrong with your code, it only depends on what you _wnat_ it to do ... So, what is your question? – Dominik Sandjaja May 12 '15 at 20:15
  • I want it to return new Dillo and print out the length of new Dillo. When I compile the code, it will say: Error: Unreachable code for the line `System.out.println(this.length);` – Bighuyou May 12 '15 at 20:17

4 Answers4

3

You have the System.out after the return

Dillo hitWithTruck () {
    System.out.println(this.length);
    return new Dillo(this.length + 1 , true) ;
}
John
  • 3,769
  • 6
  • 30
  • 49
gaston
  • 498
  • 7
  • 15
1

you are returning value before the print statement, therefore you always exit the method before printing the length. Compilers sees this as an unreachable code because it will never execute. change the code from:

    // produces a dead Dillo one unit longer than this one
Dillo hitWithTruck () {
  return new Dillo(this.length + 1 , true) ;
  System.out.println(this.length);
}

to:

    // produces a dead Dillo one unit longer than this one
Dillo hitWithTruck () {
  System.out.println(this.length);
  return new Dillo(this.length + 1 , true) ;
}
nullptrex
  • 34
  • 3
1

To build upon gaston's answer:

Dillo hitWithTruck () {
    Dillo d = new Dillo(this.length + 1 , true);
    System.out.println(d.length);
    return d;
}

You were printing out the length after returning, so you were never getting the value. If you want to print out the length of the Dillo you are returning, you should try my snippit above.

John
  • 3,769
  • 6
  • 30
  • 49
  • Thank you. But this will give me an error: Error: Type mismatch: cannot convert from int to Dillo – Bighuyou May 12 '15 at 20:27
  • Thank you. It works now. But I have a new question that why if I put `return d;` in front of `System.out.println(d.length);`, then there will be error saying: Error: Unreachable code? – Bighuyou May 12 '15 at 20:33
0

Your print statement is never executed because you have a return statement before it.

// produces a dead Dillo one unit longer than this one
    Dillo hitWithTruck () {
      System.out.println(this.length+1);
      return new Dillo(this.length + 1 , true) ;

    } 

The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method. As such, it is categorized as a jump statement. Nothing after the return statement gets executed.

More Info

https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106