Somebody read the book Head First Java? I can't seem to let the code beginning to work.
It's page 145-155 were I'm stuck, all the lines seems to work except one part, here is are the codes.
I'm sorry for this noob question but I'm stuck on this for days!:(-:
Main:
Code:import java.util.*;
public class DotComBust {
private GameHelper helper = new GameHelper();
private ArrayList<DotCom> dotComsList = new ArrayList<DotCom>();
private int numOfGuesses = 0;
private void setUpGame(){
DotCom one = new DotCom();
one.setName("Pets.com");
DotCom two = new DotCom();
two.setName("eToys.com");
DotCom three = new DotCom();
three.setName("Go2.com");
dotComsList.add(one);
dotComsList.add(two);
dotComsList.add(three);
System.out.println("Your goal is to sink three dot coms.");
System.out.println("Pets.com, eToys.com, Go2.com");
System.out.println("Try to sink them all in the fewest number of guesses");
for (DotCom dotComToSet : dotComsList){ //repeat with each DotCom in the list.
ArrayList<String> newLocation = helper.placeDotCom(3); //Ask the helper for a DotCom location (an ArrayList of Strings).
dotComToSet.setLocationCells(newLocation); //call the setter method on this DotCom to give it the location you got from the helper.
}//close for loop
}//close setUpgame method
private void startPlaying(){
while (!dotComsList.isEmpty()){
String userGuess = helper.getUserInput("Enter a guess");
checkUserGuess(userGuess);
} //close while
finishGame();
} //close startPlay methode
private void checkUserGuess(String userGuess){
numOfGuesses++;
String result = "miss";
for(DotCom dotComToTest : dotComsList){
result = dotComToTest.checkYourself(userGuess);
if (result.equals("hit")){
break;
}
if (result.equals("kill")){
dotComsList.remove(dotComToTest);
break;
}
}// close for
} //close methode
private void finishGame(){
System.out.println("All Dot Coms are Dead! Your stock is now worthless.");
if(numOfGuesses <= 18){
System.out.println("It only took you " + numOfGuesses + "guesses.");
System.out.println(" You got out before your options sank.");
}else {
System.out.println("Took you long enough. "+ numOfGuesses + " guesses.");
System.out.println("Fish are dancing with your options");
} //end else
} //end methode
public static void main(String[] args) {
DotComBust game = new DotComBust();
game.setUpGame();
game.startPlaying();
} //close methode
}// end class
Methodes:
Gamehelper:Code:import java.util.ArrayList;
public class DotCom {
private ArrayList<String>locationCells;
private String name;
public void setLocationCells(ArrayList<String> loc){
locationCells = loc;
}
public void setName(String n){
name = n;
}
public String checkYourself(String userInput){
String result = "Miss";
int index = locationCells.indexOf(userInput);
if (index >=0){
locationCells.remove(index);
if (locationCells.isEmpty()){
result = "kill";
System.out.println("Ouch! You sunk "+name + " : (");
}else{
result = "hit";
} //close if
}//close if
return result;
} //close methode
} //close class
Code:import java.io.*;import java.util.*;
public class GameHelper {
private static final String alphabet = "abcdefg";
private int gridLength = 7;
private int gridSize = 49;
private int [] grid = new int [gridSize];
private int comCount = 0;
public String getUserInput (String prompt){
String inputLine = null;
System.out.println(prompt + " ");
try{
BufferedReader is = new BufferedReader(
new InputStreamReader(System.in));
inputLine = is.readLine();
if (inputLine.length() == 0) return null;
}catch (IOException e){
System.out.println("IOException: " + e);
}
return inputLine.toLowerCase();
}
// method, placeDotCom, takes an int as a parameter, and returns an ArrayList of Strings
public ArrayList<String> placeDotCom(int comSize) {
// declare ArrayList alphaCells
ArrayList<String> alphaCells = new ArrayList<String>();
// declare an array to hold the coordinates, size is equal to comSize
String [] alphacoords = new String [comSize];
// declare a string to be used for concatenation
String temp = null;
// declare an array to hold candidate coordinates
int [] coords = new int[comSize];
// counter to keep track of how many attempts
int attempts = 0;
// if success is true, that means it's a good place to hide the "dot com"
boolean success = false;
int location = 0; // current starting location
// comcount is one of the member variables. It keeps track of how many dot coms are placed
comCount++; // nth dot com to place
// this section says to place the dot com vertically if it's on an odd number
int incr = 1; // set horizontal increment
if ((comCount % 2) == 1) { // if odd dot com (place vertically)
incr = gridLength; // set vertical increment
}
// this while loop figures out where in the array to place the dot com...
// ... it keeps going until success == true, or until it searches 200 times
while ( !success & attempts++ < 200 ) { // main search loop (32)
location = (int) (Math.random() * gridSize); // get random starting point
//System.out.print(" try " + location);
int x = 0; // nth position in dotcom to place
success = true; // assume success
while (success && x < comSize) { // look for adjacent unused spots
if (grid[location] == 0) { // if not already used
coords[x++] = location; // save location
location += incr; // try 'next' adjacent
if (location >= gridSize){ // out of bounds - 'bottom'
success = false; // failure
}
if (x>0 & (location % gridLength == 0)) { // out of bounds - right edge
success = false; // failure
}
} else { // found already used location
// System.out.print(" used " + location);
success = false; // failure
}
}
} // end while
// this section takes the successful find and turns it into coordinates for the game
int x = 0; // turn good location into alpha coords
int row = 0;
int column = 0;
// System.out.println("\n");
while (x < comSize) {
grid[coords[x]] = 1; // mark master grid pts. as 'used'
row = (int) (coords[x] / gridLength); // get row value
column = coords[x] % gridLength; // get numeric column value
temp = String.valueOf(alphabet.charAt(column)); // convert to alpha
// add the coordinate to the arraylist
alphaCells.add(temp.concat(Integer.toString(row)));
x++;
// System.out.print(" coord "+x+" = " + alphaCells.get(x-1));
}
// System.out.println("\n");
return alphaCells; // send the arraylist back to be used by the game
}
}
Eclipse seems to compile everything except one part of the main code:
The .placeDotCom(3); is underscored with red, and can't compile I did everything exactly the same as in the book, running it on version 5.0 but it just doesn't seem to work. Can somebody please help me I'm stuck on this for days!:(-:Code:for (DotCom dotComToSet : dotComsList){
ArrayList<String> newLocation = helper.placeDotCom.(3);
dotComToSet.setLocationCells(newLocation);
}//close for loop
}//close setUpgame method

