Dear Programmers,
I have previously posted this matter, and seem to have ended up taking culpability for code, which although published to hard copy (Head First Java p150-2) and to the net (www.headfirstlabs.com Chapter 6) doesn't compile.
I was told not to send the code as a personal message to 'RanchHands' but to the Post Reply option, so this is what I am now doing.
I am quoting the code as I read it, and anyone if they have the book, or wishes to look at the site code can check thus, that it is not my fault if there are errors, besides if there were no errors, programs would compile, and I would not be asking about it.
I have installed the jdk 6.0 to my computer, and 'javac' wouldn't be legible as a command if it were not in the jdk's established classpath, which it is, but all the same compiler seems to point to the enhanced loops, and to establishment of input stream object, and arraylist classes though I was given to understand these are jdk6.0 legible.
Without wishing to put the cart before the horse, so's to speak, this is the response to my compiler re: DotCom, DotComBust and GameHelper (on pages 150, 148=9 & 152 HFJ, respectively) :-
***************************************************************************
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>cd c:\set PATH=c
rogramFiles\Java\jdk1.
6.0.05\bin;%PATH%
The filename, directory name, or volume label syntax is incorrect.
C:\Documents and Settings\Administrator>cd c:\Java Head First
C:\Java Head First>set PATH=C
rogram Files\Java\jdk1.6.0.05\bin;%PATH%
C:\Java Head First>cd c:\Java Head First\sub
C:\Java Head First\sub>javac DotCom.java
DotCom.java:5: <identifier> expected
private ArrayList<
String> locationCells;
^
DotCom.java:7: <identifier> expected
public void setLocationCells(ArrayList<String>loc)
^
DotCom.java:34: ')' expected
}
^
3 errors
C:\Java Head First\sub>javac DotComBust.java
DotComBust.java:5: <identifier> expected
private ArrayList<DotCom> dotComsList = new ArrayList<DotCom>();
^
DotComBust.java:21: ';' expected
for (DotCom dotComToSet : dotComsList)
^
DotComBust.java:26: illegal start of expression
}
^
DotComBust.java:25: ';' expected
}
^
DotComBust.java:40: ';' expected
for (DotCom dotComToTest: dotComsList)
^
DotComBust.java:54: illegal start of expression
}
^
DotComBust.java:53: ';' expected
System.out.println(result);
^
7 errors
C:\Java Head First\sub>javac GameHelper.java
GameHelper.java:17: ';' expected
inputLine is.readLine();
^
GameHelper.java:27: <identifier> expected
public ArrayList<String>placeDotCom(int comSize)
^
GameHelper.java:82: ';' expected
}
^
3 errors
C:\Java Head First\sub>
***************************************************************************
Here are the programs that caused this output, please bear in mind I am only using the text as writ, also I am not interested in finding fault with anyone, only to get through the book, which would be somewhat pointless if code don't work :-
***************************************************************************
import java.util.*;
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 sank" + name + "
");
}
else
{
result = "hit";
}
}
return result;
}
}
***************************************************************************
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)
{
ArrayList<String> newLocation = helper.placeDotCom(3);
dotComToSet.setLocationCells(newLocation);
}
}
private void startPlaying()
{
while(!dotComsList.isEmpty())
{
String userGuess = helper.getUserInput("Enter a guess");
checkUserGuess( userGuess);
}
finishGame();
}
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;
}
}
System.out.println(result);
}
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");
}
}
public static void main (String args [])
{
DotComBust game = new DotComBust();
game.setUpGame();
game.startPlaying();
}
}
*************************************************************************
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.print(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();
}
public ArrayList<String>placeDotCom(int comSize)
{
ArrayList<String> alphaCells = new ArrayList<String>();
String [] alphacoords = new String[comSize];
String temp = null;
int [] coords = new int [comSize];
int attempts = 0;
boolean success = false;
int location = 0;
comCount++;
int incr = 1;
if((comCount % 2 ) ==1)
{
incr = gridLength;
}
while (!success & attempts++ <200)
{
location = (int) (Math.random() * gridSize);
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
{
success = false;
}
}
}
int x = 0;
int row = 0;
int column = 0;
while (x < comSize)
{
grid[coords[x]] = 1;
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;
}
}
***************************************************************************
The above were written in notepad, so can be copied, saved as "xxx.java" and compiled, as per usual.
Hoping you can help to get them compiler compliant, so I can play 'Sink a Dotcom' all by myself.
Thanks.
Yours
Simon.