Go to Post I can tell you that I feel happiest when I am giving or doing for others. I do not expect anything in return but just knowing that I can make things better, in my small way, makes me happy. - Steve W [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #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
  #2   Spotlight this post!  
Unread 06-11-2013, 00:00
dcarr's Avatar
dcarr dcarr is offline
#HoldStrong
AKA: David Carr
FRC #3309 (Friarbots)
Team Role: Mentor
 
Join Date: Dec 2010
Rookie Year: 2009
Location: Anaheim
Posts: 954
dcarr has a reputation beyond reputedcarr has a reputation beyond reputedcarr has a reputation beyond reputedcarr has a reputation beyond reputedcarr has a reputation beyond reputedcarr has a reputation beyond reputedcarr has a reputation beyond reputedcarr has a reputation beyond reputedcarr has a reputation beyond reputedcarr has a reputation beyond reputedcarr has a reputation beyond repute
Re: Java Code Question (Method Calling)

If I understand your question correctly - planets[i] references a Planet object, so it's going to call the display() method in that class.
__________________
Team 3309
2016 Los Angeles Chairman's Award Winner
2016 Orange County Regional Winner with 3476 & 6220
Team3309.org
Orange County Robotics Alliance
Reply With Quote
  #3   Spotlight this post!  
Unread 06-11-2013, 00:57
markmcgary's Avatar
markmcgary markmcgary is online now
Software Mentor
FRC #4322 (Clockwork Oranges)
Team Role: Mentor
 
Join Date: Feb 2012
Rookie Year: 2012
Location: Fullerton, CA
Posts: 179
markmcgary is just really nicemarkmcgary is just really nicemarkmcgary is just really nicemarkmcgary is just really nicemarkmcgary is just really nice
Re: Java Code Question (Method Calling)

This site is pretty helpful for learning and practicing Java in general. After you get the hang of Java, go here for FRC-specific Java information and samples.
Reply With Quote
  #4   Spotlight this post!  
Unread 06-11-2013, 09:49
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
Re: Java Code Question (Method Calling)

@dcarr, Thanks a bunch. That seems to make a lot of sense now.

@markmcgary, thanks for the resources.
__________________
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
  #5   Spotlight this post!  
Unread 06-11-2013, 09:52
BigJ BigJ is offline
Registered User
AKA: Josh P.
FRC #1675 (Ultimate Protection Squad)
Team Role: Engineer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Milwaukee, WI
Posts: 947
BigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond reputeBigJ has a reputation beyond repute
Re: Java Code Question (Method Calling)

To expand further, to call SolarSystem's display method inside the SolarSystem class, you'd use just "display()" or "this.display()".

"this" is a reserved word that is a reference to the current object that the code is running in/from, and is usually implied, but there are cases where you'd want to use it (like a method having arguments named the same thing as class variables)
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 13:01.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi