Mar
19
2009
Here's the java application from Head First Java. It's a battleship style game.
It's using all the Java concepts I've learned for the past few months. Still trying to get my head around OO properly. I found it difficult to try and come up with the pseudo code to form the pseudo code for how it works. Hopefully it'll all come together soon.
As a side note, I'm seeing how simple the processing language is, but I do believe I need a firm grasp on Java and OO to create more complex processing applications.
DotCom.java
public class DotCom {
int[] locationCells;
int numOfHits = 0;
public void setLocationCells(int[] locs) {
locationCells = locs;
}
int guess =
Integer.
parseInt(stringGuess
);
// convert string to an int String result=
"Miss";
//make the result a variable and set it to miss as default for (int cell : locationCells) { //repeat with each cell in the locationCells array
if (guess == cell) { //compare the user guess to this element in the array
result = "hit"; //this is a hit
numOfHits++;
break; //get out of the loop
}
}
if (numOfHits == locationCells.length) {
result= "Kill"; //this is out of the loop, but checks to see if we're dead (hit 3 times) and change the result String to "Kill"
}
System.
out.
println(result
);
//display the result for the user return result; //return the result back to the calling method
}//close method
}//close the class
Game.java
class Game {
public static void main
(String[] args
) { int numOfGuesses = 0; //track amount of guesses
GameHelper helper = new GameHelper(); //class to get user input
DotCom theDotCom = new DotCom(); //make the dotCom object
int randomNum =
(int) (Math.
random() *
5);
//make a random number for the first cell, and use it to make the cell locations array
int[] locations = {randomNum, randomNum+1, randomNum+2};
theDotCom.setLocationCells(locations); //give theDotCom its locations array
boolean isAlive = true; // use boolean value to see if the game is still alive
while (isAlive == true) {
String guess = helper.
getUserInput("enter a number");
//get user input string String result = theDotCom.
checkYourself(guess
);
numOfGuesses++;
if (result.equals("Kill")) { //if it is a kill, set boolean value to false to stop loop and print amount of guesses
isAlive = false;
System.
out.
println("You took " + numOfGuesses +
" guesses");
}
}
}
}
GameHelper.java (this is too advanced for me at the moment so I didn't comment it)
class Game {
public static void main
(String[] args
) { int numOfGuesses = 0; //track amount of guesses
GameHelper helper = new GameHelper(); //class to get user input
DotCom theDotCom = new DotCom(); //make the dotCom object
int randomNum =
(int) (Math.
random() *
5);
//make a random number for the first cell, and use it to make the cell locations array
int[] locations = {randomNum, randomNum+1, randomNum+2};
theDotCom.setLocationCells(locations); //give theDotCom its locations array
boolean isAlive = true; // use boolean value to see if the game is still alive
while (isAlive == true) {
String guess = helper.
getUserInput("enter a number");
//get user input string String result = theDotCom.
checkYourself(guess
);
numOfGuesses++;
if (result.equals("Kill")) { //if it is a kill, set boolean value to false to stop loop and print amount of guesses
isAlive = false;
System.
out.
println("You took " + numOfGuesses +
" guesses");
}
}
}
}
Mar
14
2009
for (int cell:locationsCells) {}
For - repeat for each element in the "locationCells" array, take the next element in the array and assign it to the int variable "cell".
int cell - declare a variable that will hold one element from the array. Each time through the loop, this variable will hold a different element from the array until there are no more elements left or a break; statement is met.
: - means "in", the whole statement reads "for each in value in the locationCells"
locationCells - The array to iterate over in the loop.
Mar
13
2009
Example:
- Integer = a class that ships with java
- parseInt = a method in the Integer class that knows how to change a string into an in that it represents
- ("3") = this takes a string
This will only work if the String represents a digit.
Mar
09
2009
Pass-by-value means Pass-by-copy
public class XCopy {
public static void main
(String[] args
) { int orig = 42;
XCopy x = new XCopy();
int y = x.go(orig);
System.
out.
println(orig +
" " + y
);
}
int go(int arg) {
arg = arg*2;
return arg;
}
}
class Clock {
time = t;
}
return time;
}
}
class ClockTestDrive {
public static void main
(String[] args
) {
Clock c = new Clock();
c.setTime("1245");
System.
out.
println("time: " + tod
);
}
}
Jan
25
2009
- Things an onject does are it's methods (behaviour).
- Methods can use intstance variables so that objects of the same type can behave differently.
- A method can have parameters, so you can pass one or more values in to the method.
- The number and type of values you pass in must match the order and type of the parameters decalred by the method.
- Values passed in and out of methods can be implicityly promoted to a larger type or explicitly cast to a smaller type. For example, you can pass a byte where an int is expected. The caller won't care because the byte fits into the int the caller will use for assigning the result.
You can get things back from a method:
int theSecret = life.giveSecret();
int giveSecret() { //the bits are returned from the giveSecret() method and land in the variable named theSecret</code>
return 42;
}
- Values passed in and out of methods can be implicitly promoted to a larger type or explictly cast to a smaller type.
- The value you pass as an argument to a method can be a literal value (2, 'c', etc.) or a variable of the declared parameter type (e.g., x where x is an int variable).
- A method must decalre a return type. A void return type means the method doesn't return anything.
- If a method declares a non-void return type, it must return a value compatible with the declared return type.
Jan
21
2009
- There are two types of variables: Primitive and Reference
- Primitive: the bit representing a value
- Reference: the bits that represent a way to get an object
- A Reference variables is like a remote control. Using the dot (.) operator on a reference variable is like pressing a button on the remote control to access a method or instance variable. For example,
d.bark();
A reference variable has a value of null when it is not referencing any object.
An array is always an object, even if the array is declared to hold primitives. There is no such thing as a primitive array, only an array that holds primitives.
Jan
15
2009
I'm getting stuck into java and OOP. I'm using the book Head First Java.
Here's what I've learned so far....
- All Java code is defined in a class
- A class describes how to make an object of that class type. It is like a blueprint.
- An object takes care of itself. It knows things and does things.
- Things an object knows about itself are called instance variables. They represent the state of an object.
- Things an objects does are called methods. They represent the behaviour of an object.
- A class can inherit instance variables and methods from a more abstract superclass.
- A Java program is just objects talking to other objects.