View Single Post
  #1   Spotlight this post!  
Unread 05-11-2013, 23:16
bhumudar's Avatar
bhumudar bhumudar is offline
Mentor/Programming/Electrical
AKA: Bryant Humud-Arboleda
FRC #0714 (Panthera)
Team Role: Mentor
 
Join Date: Feb 2012
Rookie Year: 2008
Location: Newark
Posts: 50
bhumudar is on a distinguished road
Java Code Question (Method Calling)

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!


Code:
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[i].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.
__________________
Bryant Humud-Arboleda
Stevens Institute of Technology
B.A - Electrical Engineering 14'
Mentor - FRC 714 (2012-Present)
Student - FRC 714 (2009-2010)

2013 Bridgewater-Raritan Winners (Thanks 1676 & 3314)
2012 MAR Regional Championship Judges Award
2012 Mount Olive District Excellence in Engineering Award
2012 Chesapeake Regional Finalist (Thanks 2068 & 358)
2012 Hatsboro-Horsham District Gracious Professionalism Award
Reply With Quote