O'Reilly Forums: Chapter 6 Dotcom Class With Arraylist - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 6 Dotcom Class With Arraylist program is wrong

#1 User is offline   Sithu 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 11-March 11

Posted 31 March 2011 - 02:06 AM

Wrong code:
New and improved version of SimpleDotCom, the DotCom class in Chapt.6 page 139(2nd edition). is not working.I feel something is wrong in the code in the book.Do anyone has any suggestions?.I tried copying from the online headfirstjava code as well.result is same!
0

#2 User is offline   Sithu 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 11-March 11

Posted 31 March 2011 - 02:26 AM

or please guide me on :
How to rewrite the below statement which is used in the main for SimpleDotCom
locationCells = {randomNum, randomNum+1, randomNum+2};

for "DotCom class with ArrayList"???
0

#3 User is offline   Wibbler 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 20-December 12

Posted 20 December 2012 - 09:35 AM

I'm bumping up this thread as I just ran into the same issue with the book and an answer doesn't appear to be online anywhere. After faffing around trying to come up with a tidy solution whereby the int[] is converted to an ArrayList<String> I gave up and just inserted the items of the array individually to the SimpleDotComGame class, code below:

//Early chapter 6 updated class to function with new arrayLists

import java.util.ArrayList;

public class SimpleDotComGame 
{
	
	public static void main(String[] args)
	{
		int numOfGuesses = 0;

		GameHelper helper = new GameHelper();
		SimpleDotCom theDotCom = new SimpleDotCom();
		
		//Get a start position, as the object is length 3 and the max position is 5, the range is 0-7
		int randomNum = (int) (Math.random() * 5);
		
		[b]ArrayList<String> locations = new ArrayList<String>();
		
		//Create object locations
		locations.add(String.valueOf(randomNum));
		locations.add(String.valueOf(randomNum+1));
		locations.add(String.valueOf(randomNum+2));

		theDotCom.setLocationCells(locations);[/b]
		boolean isAlive = true;
		
		//Begin the game loop
		while (isAlive)
		{
			String guess = helper.getUserInput("enter a number: ");
			String result = theDotCom.checkYourself(guess);
			numOfGuesses++;
			
			
			if (result == "kill")
			{
				isAlive = false;
				System.out.format("You took %d guesses", numOfGuesses);
			}	
		}
	}
	
	
}


0

#4 User is offline   jugo 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 08-January 13

Posted 08 January 2013 - 08:28 AM

the bug why the code example does not work may have been in the GameHelper class in the placeDotCom method. i changed the line below highlighted bold, and it happend to work in some cases. By changing some other conditions i hope it will work robustly

while(!success && attempts++<200){
location = (int)(Math.random()*gridSize);
//System.out.println("try: " + location);
int x = 0;
success = true;
while(success && x < comSize){
if(grid[location] == 0){
coords[x++] = location;
location+=incr;
if(location>=gridSize){
success=false;
}
if(x>0 && (location % gridLength == 0)){
success = false;
}
else{
System.out.println("used " + location);
success = false; // The problem is here; -- it should have been true.
}
}
}
}

This post has been edited by jugo: 08 January 2013 - 08:43 AM

0

#5 User is offline   jugo 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 08-January 13

Posted 09 January 2013 - 08:23 AM

The code part of the placeDotCom method which by the example of the book works properly. Highlighted and commented out the problems the book includes:
(not to mention the case if you make the code terminate instead of giving the user guesses you are asked for. in this case you might face NullPointerException by the method getUserInput)


while(success && x < comSize){
if(grid[location] == 0){
coords[x++] = location;
location+=incr;
if(location>=gridSize){
success=false;
break; //missing break; ArrayIndexOutOfBounds exception possibility
}
if(x>0 && (location % gridLength == 0)){
success = false;
break; //missing break; ArrayIndexOutOfBounds exception possibility
}
else{
System.out.println("used " + location);
success = true; //this one have to be true instead of false standing in the book
}
}
//missing else clause; infinite loop possibility
else{
success = false;
break;
}
}
0

#6 User is offline   Shirazi 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 17-January 13

Posted 17 January 2013 - 09:01 AM

I wonder why most of the books create their own libraries and do not use java's.
for us to learn, first we need a working program and also self sufficient codes.
at least they should give the book readers the courtesy of posting on its forums to resolve its issues.

supplementary codes for the book is not working due to this:

??????????????????????

import helpers.GameHelper;

??????????????????????

what are the two ant files that are not responding to run?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users