Hi, I’m trying to learn java, and this is the first text-based programming language that I’ve gotten serious about. I am fairly proficient with labVIEW, so a lot of the syntax is new and kind of strange for me, but I digress. I have a particular question involving the following code. Also, if anyone could point me in the direction of any good learning resources for FRC java programming, that would be great. Thanks in advance!
package practicePackage;
class Planets {
int moons;
String name;
Planets (String name, int moons) {
this.name = name;
this.moons = moons;
}
void display() {
System.out.println(name + " has " + moons + " moons.");
}
}
class SolarSystem {
Planets planets];
SolarSystem() {
planets = new Planets[9];
planets[0] = new Planets("Mercury", 0);
planets[1] = new Planets("Venus", 0);
planets[2] = new Planets("Earth", 1);
planets[3] = new Planets("Mars", 2);
planets[4] = new Planets("Jupiter", 16);
planets[5] = new Planets("Saturn", 18);
planets[6] = new Planets("Uranus", 15);
planets[7] = new Planets("Neptune", 8);
planets[8] = new Planets("Pluto", 1);
}
void display() {
for (int i = 0; i < planets.length; i++ ) {
planets*.display();
}
}
}
class MilkyWay {
public static void main(String args]) {
SolarSystem solarSystem = new SolarSystem();
solarSystem.display();
}
}
Why/how is it that the display method in the SolarSystem class calls the display method in the Planets class? Why is it that it doesn’t call in itself recursively?
If it helps, this is the output of the program.
Mercury has 0 moons.
Venus has 0 moons.
Earth has 1 moons.
Mars has 2 moons.
Jupiter has 16 moons.
Saturn has 18 moons.
Uranus has 15 moons.
Neptune has 8 moons.
Pluto has 1 moons.*