Results 1 to 9 of 9
Thread: HeadFirst Java code
- 11-21-2012, 11:30 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
HeadFirst Java code
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:
Java 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:Java 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
Java 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!Java Code:for (DotCom dotComToSet : dotComsList){ ArrayList<String> newLocation = helper.placeDotCom.(3); dotComToSet.setLocationCells(newLocation); }//close for loop }//close setUpgame method
Last edited by JavaJimme; 11-22-2012 at 12:30 AM.
- 11-22-2012, 12:09 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: HeadFirst Java code
That line doesn't make a lot of sense. What is it supposed to do?Java Code:ArrayList<String> newLocation = helper.placeDotCom.(3);
- 11-22-2012, 12:26 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
Re: HeadFirst Java code
Last edited by JavaJimme; 11-22-2012 at 12:29 AM.
- 11-22-2012, 06:03 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: HeadFirst Java code
Then the general syntax to use is <instance>.<method>(<arguments>). Ie the second "dot" in the expession you used shouldn't be there.
- 11-22-2012, 07:29 AM #5
Re: HeadFirst Java code
Since the problem isn't IDE-specific, I've moved the thread here from the Eclipse section.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-22-2012, 11:15 PM #6
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
Re: HeadFirst Java code
Thank you for your reply and sorry for wrong section, I was abit in a hurry when I posted.
Anyway that "dot" I must have typed it by mistake when I was copy pasting this code cos it wasnt there in the original code.
So placeDotCom(3) is still underscored with red, but eclipse says: The method placeDotCom(int) is undefined for the type GameHelper. Why? :((((
- 11-23-2012, 02:39 AM #7
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: HeadFirst Java code
I have try to compile your code and it compile successfully. Maybe you can clean up you eclipse work space and try compile it again.
Last edited by wsaryada; 11-23-2012 at 02:43 AM.
Website: Learn Java by Examples
- 11-23-2012, 07:26 PM #8
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
- 11-23-2012, 07:30 PM #9
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
Re: HeadFirst Java code
Ah I just solved the mystery :) I had 2 GameHelper classes in 2 different projects and the one that had the correct code was in another project.. Jesus how noob of me :( Thanks for the tip for checking out the eclipse workspace, I thought it was the code that made the error until I read your post ;)
Similar Threads
-
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01-31-2012, 08:36 PM -
Translate Vb.net code into Java code
By Radu in forum New To JavaReplies: 5Last Post: 04-12-2011, 09:27 AM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks