askIT

Java toString

Lagre Download example files

Planet.java -> toString

public String toString(){
        return("Name: " + this.getName() + "\n" +
        "Orbital period around the sun: " + this.getDistanceFromSun() + "\n" +
        "Radius around the equator: " + this.getRadius() + "\n" +
        "Distance to the Sun: " + this.getDistanceFromSun() + "\n" +
        "Number of satellites: " + this.getNaturalSatellites()
        );
}

Solsystemet.java -> Print using toString
We can now use toString in our main class.

System.out.println(sun.toString());
System.out.println("--------");
System.out.println(mercury.toString());

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.