• 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

Chapter 6th Head First Java, class GameHelper, method placeDotCom(int comSize)

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Gosh !! This code is making me crazy.
Can anyone suggest me a web-link where a fair amount of explanation is given on code given in the 6th Chapter of Book Head First Java written by Kathy Sierra and Bert Bates.
Actually, the code needs a fair amount of explanation, which isn't provided in the book itself.

So, if someone has read the book or already read it before , please help me in understanding the code. Actually its a small exemplary application called "Sink a Dot-Com" which is basically a Guessing Game, which has total of 3-classes. but Still its almost difficult to write the code here or even copy paste here, if someone want , I could do that also.
The code can be downloaded from the official website of Head First Labs from O'Reilly : webpage Please note : the code is in chapter-6th, so you need not to find each one of those Chapters, and the sad thing is one class is missing in the code.

The problem is in understanding the logic given in method placeDotCom(int comSize) in calss GameHelper.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the sad thing is one class is missing in the code.


The sad thing is that you are asking something about that particular class

If nobody answers, post the GameHelper code here.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:
If nobody answers, post the GameHelper code here.



Oh ! I can't wait for so long, so I would rather write the whole code here to make the whole understanding process get going !
Here I am going to present the code before you.
The classes are as follows :



and the main method is present in the following DotComBust class :



and Finally the last class is as follows :

 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now what I couldn't understand is the logic present in method placeDotCom in GameHelper class !
How the whole grid is made, where all these dotComs are placed, i mean Is the array used forms the matrix or something ??
How while loops run ?
Can I have line to line explanation of placeDotCom method ?
I shall be very thankfull, if someone take this pain to make me understand !
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't blame you for not getting this one. Well needed comments are missing, leaving just useless comments (// end of inner while loop)....


...xxx.
.......
.......
x......
x..xxx.
x......
.......


This code generates locations for "dot coms (three letters word)" in a matrix of 7x7.
A row is identified by a number, a column by a letter.
"dot coms" will be placed vertically or horizontally.
"dot coms" cannot overlap, not being broken in several rows or columns.
The matrix is represented by a 49-sized array.


This determines the orientation of the "dot com" in the matrix. First, vertically, then horizontally, then vertically, and so on.


This attempts to place a "dot com" in the matrix. A "dot com" is made of three points, so it will loop until these three locations are found, or until the number of tries has reached its limit (200 times).

Attempting to find an unoccupied location (made of three points) for a "dot com"

Converting the locations into coordinates (a1, b2....)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who knows what's going to happen when the maximum tries (200) has been reached and no proper location has been found.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rubbal Bhusri wrote:
Oh ! I can't wait for so long, so I would rather write the whole code here to make the whole understanding process get going !
Here I am going to present the code before you.
The classes are as follows :




For each DotCom object call placeDotCom(3), to get the location of this DotCom.. Each DotCom covers 3 grid either horizontally or vertically...


I have added comment to your code to explain each line to you as much as possible.. You can go through it and see if you can understand//
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't blame you for not getting this one. Well needed comments are missing, leaving just useless comments (// end of inner while loop)....



Agree with Christophe, life would have been simple if the logic was commented at different places . I hope the publishers , correct it in the next edition
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops.. And I was the 2nd to answer that... And as I figure out it almost took me 15-20 minutes to answer..
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fisrt of all, I am very thankful to both Christopher and R.Jain, who took their precious time to see this code and comment it properly.
Really guys ! both of you did a commendable job, and I almost has no words for your such superb effort.
Now I would like to ask my few doubts about this code, and please pay little more time to help me.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:
This code generates locations for "dot coms (three letters word)" in a matrix of 7x7.
A row is identified by a number, a column by a letter.
"dot coms" will be placed vertically or horizontally.
"dot coms" cannot overlap, not being broken in several rows or columns.
The matrix is represented by a 49-sized array.



We have used just a simple array and I think i.e.

How could this form a Matrix of 7 x 7 ??
How could I visualize to place DotComs in vertical and horizontal positions ??
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What does this argument in method placeDotCom(3) means ??
Does it signify the positions occupied by the DotCom on the Matrix ( or array: ) ??
As far as I see, our DotCom is of String type and this grid is of type int , please explain this ??
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What kind of input does user suppose to give ??
I mean, Should user inputs a numeral or directly inputs the name of the DotComs ???

 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about running this method line to line assuming a value from our own. !!!
That would help you to read my mind and then correct me wherever I would be wrong.
So lets get set !

Suppose a call comes to this method with an argument of 3 i.e.
Now, here I don't clearly understand the purpose of argument , the only thing I understood about this number limits the occupancy of DotComs.
From here, the thing comes into my mind is that , there would be some String array, where we get just 3-places to keep our Dotcom.
now you tell me, Is it so ??
then comes the line:

this creates an arrayList of String types which we are suppose to return, here alphaCells s a reference to the ArrayList object, which further contains objects of String type.
then comes the line:

Here we have created an array of String type of size-3, which means alphacoords[0] will contain some String, alphacoords[1] will contain some other String and alphacoords[2] will also contain some another String.
May be e.g. alphacoords[0] = "Facebook"; alphacoords[1] = "."; alphacoords[2] = "com";
Am I Right ?
then comes the line:

Don't know about this !
then comes the line:

this is an array of type int with size of 3, but still I don't know what it is for ??
then comes the line:

these are the variables used in further coming loops.
then come the lines:

here comCount was equal to 0 and recent it was updated to 1 and here we are testing the condition: ((comCount % 2)==1).
Fisrt of all tell me, Does 1 % 2 gives 1 ?
if yes then that means the condition-((comCount % 2)==1) becomes true and incr will be assigned gridLength which is 7, What does that signify ??

I would now like to stop here and take a break, because if I continue to write and ask everything one time , that will cause difficulty for you to read and respond altogether.
Please explain me upto this and then I will start again for rest of the code.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before reading this, check what the Battleship game is.

How could this form a Matrix of 7 x 7 ??
How could I visualize to place DotComs in vertical and horizontal positions ??


It's a trick. You are right, it's an array. A matrix looks like it has two axis, but what happens if you join all lines ? You get a simple array :
Before :

ABC
DEF
GHI

After : -> ABCDEFGHI

What does this argument in method placeDotCom(3) means ??


It means that one "dot com" will be represented by 3 locations in the matrix. It occupies three seats if you prefer Here, I have represented one "dot com", made of three locations :

.......
.......
.......
..XXX..
.......
.......
.......

If the code would have been placeDotCom(4), you'd have to guess 4 locations before sinking one down.


As far as I see, our DotCom is of String type and this grid is of type int , please explain this ??


The "grid" instance variable is used to tell whether a location is already occupied or not. To avoid placing to "dot coms" on the same location. It is made of 0 and 1 values. 0 means that the location is free, not occupied. 1 means that it is already occupied, so another "dot com" cannot be placed on it.

What kind of input does user suppose to give ??


Coordinates. For example : a1, b2, g5... Have you ever played a Battleship game ? Look at the pictures in the Wikipedia link. You'll understand what I mean.


From here, the thing comes into my mind is that , there would be some String array, where we get just 3-places to keep our Dotcom.
now you tell me, Is it so ??


Yes.

here alphaCells s a reference to the ArrayList object


You can forget about alphaCells because it is not used. Leaving unused variables is another nice way to confuse everybody reading the code

May be e.g. alphacoords[0] = "Facebook"; alphacoords[1] = "."; alphacoords[2] = "com";


It actually contains the coordinates of each location of one "dot com". For example, "d2","d3","d4" (horizontally). Or "e1","f1","g1" (vertically).

String temp = null; Don't know about this


"temp" is just a temporary variable, used to concatenate the row and column location.

coors, I don't know what it is for


"coords" holds the index of each available location in the grid array. When we look for three available locations, we first use the "grid" variable to check if the location is empty. If it is, we store the location index in "coords".

Does 1 % 2 gives 1 ?


Yes. (comCount % 2) == 1) is a trick to switch between vertical and horizontal positioning. "%" is called modulus. The value on the left will roll from 0 to 2(excluded) :
0%2=0
1%2=1
2%2=0
3%2=1
4%2=0
...
You can see that the result switches between 0 and 1. This can be used to switch between horizontal and vertical positioning.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow ! Christophe Verré , you are really amazing !
You are bringing me on real track.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look at this code , where we called placeDotCom(3) for the first time.

What value is it returning here ??
I can see here the return type is an ArrayList<String>, which means return type is an object of ArrayList whch will be containing String object and newLocation is a reference to that object, which will be brought by helper.placeDotCom(3).

Is it so ? Right ?
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are the locations(or postions) of this ArrayList contiguous ??
or
Are they random ?
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rubbal Bhusri wrote:Please look at this code , where we called placeDotCom(3) for the first time.

What value is it returning here ??
I can see here the return type is an ArrayList<String>, which means return type is an object of ArrayList whch will be containing String object and newLocation is a reference to that object, which will be brought by helper.placeDotCom(3).

Is it so ? Right ?



Is it like : helper.placeDotCom(3) will be returning some String objects e.g. String a, String b, String c etc. hich wil be stored in ArrayList.
Is it so ??
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I can see here the return type is an ArrayList<String>, which means return type is an object of ArrayList whch will be containing String object and newLocation is a reference to that object, which will be brought by helper.placeDotCom(3).


Yes. And as said before, this array contains the coordinates (a1, a2, a3...) of the three locations of one "dot com".

Are the locations(or postions) of this ArrayList contiguous ?? or Are they random ?


As said before, contiguous. Horizontally or vertically connected locations.

helper.placeDotCom(3) will be returning some String objects e.g. String a, String b, String c etc. hich wil be stored in ArrayList. Is it so ??


Yes. If you look at the placeDotCom method, it returns "alphaCells", which contains String coordinates.


Do you have a debugging environment. Stepping through the code with a debugger should help you a lot.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't used any debuggers before, since I am a novice in programming.
But if you consider it good for me, then suggest me some link, I will try that also for further learning process.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:

Rubbal Bhusri wrote:Does 1 % 2 gives 1 ?


Yes. (comCount % 2) == 1) is a trick to switch between vertical and horizontal positioning. "%" is called modulus. The value on the left will roll from 0 to 2(excluded) :
0%2=0
1%2=1
2%2=0
3%2=1
4%2=0
...
You can see that the result switches between 0 and 1. This can be used to switch between horizontal and vertical positioning.


ok, that explains, the switching behavior of condition (comCount % 2) == 1).
But
What is purpose of the immediate next statement, which assigns gridLength(i.e. value-7) to an int variable : incr ??
What are we doing exactly by updating the value of incr to 7 ??
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
+7 means "go to the next line in the matrix". It's used to search for empty locations vertically.
Look : an empty matrix. Indexes starting from 0 to 48. Remember, we are actually using an array, but it's easier to picture it like a matrix.

.......
.......
.......
.......
.......
.......
.......

With all lines joined to make it look like an array :
.................................................
First, we get a location randomly. Let's say 23 :

.......
.......
.......
..X....
.......
.......
.......

Lines joined :
.......................X.........................

Next, we want to check if the second location down vertically is free. 23+7=30:

.......
.......
.......
..X....
..X....
.......
.......

Lines joined :
.......................X......X..................

and so on.


(about the debugger, forget it for the moment. You'll think about it when once this problem is cleared)
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See, this part of code calls the method placeDotCom(3) for three times because we have three dot-coms to be placed. Right !

This means when we called helper.placeDotCom(3) for first time, the new grid of size 49 is made, loops run, and places are decided. Right !
then for 2nd time we called helper.placeDotCom(3) and then for the third time. Right !
Every-time the whole process of method takes a fresh start does all the things in the same way.
The thing which put me in doubt, which part of the code , restricts to book the already occupied places, I mean, where is the record kept of already occupied positions.
Can you please show that particular piece ??

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Every-time the whole process of method takes a fresh start does all the things in the same way.


No, not really. You are using the same "helper" instance. So instance members won't be cleared. This is important to keep state, and prevent to place two "dot coms" at the same location.

The thing which put me in doubt, which part of the code , restricts to book the already occupied places, I mean, where is the record kept of already occupied positions.


As explained above, The "helper" instance is not cleared. You are calling the same method three times, but instance members are not cleared. The instance member "grid" is not cleared to it keep records of the occupied locations.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alrighty Christophe, I totally gotcha your point.
You are really really awesome and genius man !
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way , Christophe, Where are you from ?
And Where are you studying( I mean which college or University) ?
Are you on facebook also ?
Are you doing Engineering in computer sciences or already did that ?
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets say The grid forms something like this :

0, 1, 2, 3, 4, 5, 6
13, 12, 11, 10, 9, 8, [7]
20, 19, 18, 17, 16, 15, [14]
27, 26, 25, 24, 23, 22, [21]
34. 33, 32, 31, 30, 29, [28]
41, 40, 39, 38, 37, 36, [35]
48, 47, 46, 45, 44, 43, [42]

I put some positions in square brackets , because , at these particular positions, the condition (location % 7 == 0) is satisfied, and this condition is used in code snippet shown below :


This above given code Snippet is taken from method placeDotCom(int comSize) which is shown below as :



Whenever Math.random method happens to produce a location say: [7],[14].[21],[28],[35],[42]
Lets say Maths.random method produces number 7, ok,
success is set true,
then
inner while loop comes into action which is as folllows :

the condition is satisfied and the control enters the while loop, right !
then
comes the if-condition which is:

Now, since our location is: 7 and lets say the (grid[7] == 0) is true,
then
coords[0] will be assigned the location 7 as per the statement :

and x is increased to 1
then
location is updated as per the statement:

since the value of incr was updated to 7 previously as shown:

therefore, the new value of location, after incr being added to old location(i.e. 7 ), becomes 14, Right !
then comes the condition

This condition fails to run , since 14 < 49(i.e. gridSize), therefore control jumps to other next if-condition which is:


Now my real question and doubt comes here .
Here the condition (location % gridLength == 0) becomes true
since our value of locaion is now 14 and 14 % 7 == 0 is absolutely true, and as far as other condition (i.e. x >0) is concerned that is obviously true, Right !
So, from here
success variable is turned to false, and because of this the control jumps to the outer loop to go for a new random location, avoiding the 14th postion, why ?

This is my doubt !
Why we avoided 14th postion ??
Isn't it the suitable place after 7th postion ??
Christophe, please look to it thoroughly !
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Campbell !
Can you also look to my problem and see if you could also share your knowledge upon this topic ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Please do not ask ask other staff or ranchers personnally to look at your problems. Everybody is looking and will help you when they can)

avoiding the 14th postion, why ?


Nice catch. I think you've found a bug

"if (x > 0 && (location % gridLength == 0))" will check if the three locations of a "dot com" will break between different lines.
This works well horizontally:

......X
XX.....
.......
.......
.......
.......
.......

In the example above, the second location shifts to the next line, and we want to avoid it. But as you've pointed out, it does not work vertically.

Can you correct the program to check that the locations do not break vertically ?
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:
Can you correct the program to check that the locations do not break vertically ?


HINT: - You have to ensure that, you don't reach the horizontal end (when you move horizontally) or vertical end(When you move vertically)
Nice catch By the way, as Christophe told
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:

"if (x > 0 && (location % gridLength == 0))" will check if the three locations of a "dot com" will break between different lines.
This works well horizontally:

......X
XX.....
.......
.......
.......
.......
.......



Wait a minute ! What if the Math.random() had produced number : 6 and not 7
then
the condition : would have failed and new location would be updated to 6+ 7 = 13(which is also in the next line) abd still the condition: (location % gridLength == 0) could not have stopped the placement of number to a position in another line, which means this condition : isn't good enough to prevent the dot-com to break in different lines.
Am I right ???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're misunderstanding the term "break in another line". Vertically, it means locations on different columns. Horizontally, it means locations on different rows. If the first location is 6, the next will be 13, which is one the same column.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea, you are right, I got your point.
and with this I declare this topic Resolved.
I present my hearty thanks to Christophe, who spend his very precious time and helped me throughout this topic.
i would also like to thanks R. Jain.
 
reply
    Bookmark Topic Watch Topic
  • New Topic