We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Question about Map project in Visualizing Data
Page Index Toggle Pages: 1
Question about Map project in Visualizing Data (Read 541 times)
Question about Map project in Visualizing Data
Jul 1st, 2009, 6:31pm
 
Hello,

I'm looking at the Table class (http://benfry.com/writing/map/Table.pde) used in the Map project on page 33 and I'm a little confused by these first few lines:

Quote:
class Table {
  String[][] data;
  int rowCount;
  
  
  Table() {
    data = new String[10][10];
  }

  
  Table(String filename) {
    String[] rows = loadStrings(filename);
    data = new String[rows.length][];



Are these both constructors? Are these two methods with the same name? I guess I don't understand what

Quote:
  Table() {
    data = new String[10][10];
  }



is doing.
Re: Question about Map project in Visualizing Data
Reply #1 - Jul 1st, 2009, 10:28pm
 
Functions of same name but different parameters (in number and/or types) is called overloading: the compiler knows which function to call depending on the look of the call.
Some languages do that by using variable number of parameters (Java has that now) and untyped parameters (Java is strongly typed).
You can see that a lot in the reference: functions like image() can have three or five parameters. It is used a lot to omit parameters which then take a sensible default option. That's what is done in your Table example: if you do Table t = new Table();, it will create an empty data array.
Re: Question about Map project in Visualizing Data
Reply #2 - Jul 1st, 2009, 10:28pm
 
This sounds a little silly, but it means you have a choice.

I think about it like this: Let's say I'm trying to give directions. I can either give someone an absolute address, via a number and a street name, or I can say something weird like "3 streets down and then a right"

So, in code it looks like...

Code:
class Address{
Address(String absoluteAddress){
//Setup all fields according to the address
}
Address(int numberStreetsAway, int numberToTheRight){
//Setup all fields using this different way of describing it
}
}


It comes down to convenience; there are a lot of uses for different constructors. Of course, if you want to say that there is "only one way", then only make one constructor.

Was that enough?
Re: Question about Map project in Visualizing Data
Reply #3 - Jul 2nd, 2009, 9:12am
 
D'oh! Forgot about that. Thanks for your explanations (and patience)
Page Index Toggle Pages: 1