• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Head First Java p.149 - question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone,

I just started studying Java using the Head First Java book. Here comes my question and I hope someone can help me:
P. 149 is about how to create coordinates in a two-dimensional grid, in the end it shall look like A5, F6 and so on. The aim is to place 3 DotComs in this grid by coincidence. To do this one uses two arrays. One array represents one DotCom, the other represents the grid's size, in this case 49 (7x7).
The code ends like this:

...
ArrayList<String> alphaCells = new ArrayList<String>();
...
int x = 0;
int row = 0;
int column = 0;
while (x<comSize) {
grid[coords[x]] = 1; // mark master grid points as used
row = (int)(coords[x] / gridLength);
column = coords[x] % gridLength;
temp = String.valueOf(alphabet.charAt(column));
alphaCells.add(temp.concat(Integer.toString(row));
x++;
}
return alphaCells;
}
(Source: Head First Java, p.148-149)

I don't understand why row and column are calculated like this. Furthermore I don't understand why the column is used to generate a character, because columns are marked with numbers and rows are marked with characters, so in my understanding it should be the other way round.
I would be very happy if someone can help me with this!

Thanks and kind regards,
Juna :-)
 
Marshal
Posts: 79260
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What makes you think that a char is a character? If you look in the Java® Language Specification (=JLS), you find it is a number. (That part of the JLS is quite easy to read, unlike some parts. So you can do arithmetic on chars.

But you don't seem to be doing any char arithmetic in that example. What you are doing can be worked out if you read from right to left.
  • Get a number i.
  • Use that number to find the ith letter in a String probably ABCD…
  • Turn that letter into a String with valueOf, so 'C' becomes "C"
  • Then you string that together with the number for the row. When you use the word string like that, you find out why writing is called a String.

    You can do it more simply with the + operator which will turn your int number into part of a String.

    alphaCells.add(String.valueOf(alphabet.charAt(column)) + row);
    You cannot simply add the char to an int otherwise you are doing arithmetic and you get a number (e.g. A6 would come out as 65 + 6 = 71).
     
    Campbell Ritchie
    Marshal
    Posts: 79260
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you have a number like 73 and you are trying to fill a grid with 11 columns, then you can divide it by 11. That gives you 6; remembering that the top row is numbered 0, you are now on the 7th row.
    If you use the % operator you get the remainder after dividing by 11, which gives you 7. Remembering that the leftmost column is numbered 0, you are now on the 8th column.
     
    Juna Burk
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dear Campbell Ritchie,
    thanks a lot for your answer, I now understand it much better :-)
    There's just one question left: if I have a grid like this
    A
    B
    C
    D
    E
    F
    G
    0 1 2 3 4 5 6
    e.g. I get a value like 36. So I calculate the row by 36/7 = 5 and the column by 36%7 = 1. So I get row = 5 and column = 1. If A equals 0 in my grid I should get F1.
    If I use the column to get the letter I take the first letter of my String="abcdefg", so it's b because column = 1. After concatenating I get B5.
    If I had used the row for getting the letter the result would have been F1, that's what I expected at the beginning . So why do I have to use the column instead of the row?

    Greetings,
    Juna
     
    Campbell Ritchie
    Marshal
    Posts: 79260
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Juna Burk wrote:Dear Campbell Ritchie,
    thanks a lot for your answer, I now understand it much better :-)

    You're welcome

    . . . So I get row = 5 and column = 1. . . .

    No, 5 gives you the 6th row and 1 gives you the second column.
    Remember that the first row is numbered 0, and the first letter in ABDCE… is numbered 0 too. If you count 0123456789 rather than 123456789 10, it will all work out correctly. The % operator, the position of elements in arrays and lists, and the position of chars in a String, are all designed to work correctly together.

    Some things are labelled with columns first and rows later, some things with row first and column second. Chessboards and spreadsheet squares are marked column→row but in this example Sierra and Bates chose to use row→column.
     
    Juna Burk
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh great, I got it! Thanks a lot, your answer is really helpful! :-)
     
    Campbell Ritchie
    Marshal
    Posts: 79260
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well done
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic