Archive for the 'OOP' Category

Mar 19 2009

Battleship Game

Published by Michael Doyle under Java, OOP

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

  1. public class DotCom {
  2. int[] locationCells;
  3. int numOfHits = 0;
  4.  
  5. public void setLocationCells(int[] locs) {
  6. locationCells = locs;
  7. }
  8.  
  9. public String checkYourself(String stringGuess) {
  10. int guess = Integer.parseInt(stringGuess); // convert string to an int
  11. String result= "Miss"; //make the result a variable and set it to miss as default
  12. for (int cell : locationCells) { //repeat with each cell in the locationCells array
  13. if (guess == cell) { //compare the user guess to this element in the array
  14. result = "hit"; //this is a hit
  15. numOfHits++;
  16. break; //get out of the loop
  17. }
  18. }
  19. if (numOfHits == locationCells.length) {
  20. 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"
  21. }
  22. System.out.println(result); //display the result for the user
  23. return result; //return the result back to the calling method
  24. }//close method
  25. }//close the class
  26.  

Game.java

  1. class Game {
  2. public static void main (String[] args) {
  3. int numOfGuesses = 0; //track amount of guesses
  4. GameHelper helper = new GameHelper(); //class to get user input
  5.  
  6. DotCom theDotCom = new DotCom(); //make the dotCom object
  7. int randomNum = (int) (Math.random() * 5); //make a random number for the first cell, and use it to make the cell locations array
  8.  
  9. int[] locations = {randomNum, randomNum+1, randomNum+2};
  10. theDotCom.setLocationCells(locations); //give theDotCom its locations array
  11. boolean isAlive = true; // use boolean value to see if the game is still alive
  12.  
  13. while (isAlive == true) {
  14. String guess = helper.getUserInput("enter a number"); //get user input string
  15. String result = theDotCom.checkYourself(guess);
  16. numOfGuesses++;
  17. if (result.equals("Kill")) { //if it is a kill, set boolean value to false to stop loop and print amount of guesses
  18. isAlive = false;
  19. System.out.println("You took " + numOfGuesses + " guesses");
  20. }
  21. }
  22.  
  23. }
  24. }
  25.  

GameHelper.java (this is too advanced for me at the moment so I didn't comment it)

  1. class Game {
  2. public static void main (String[] args) {
  3. int numOfGuesses = 0; //track amount of guesses
  4. GameHelper helper = new GameHelper(); //class to get user input
  5.  
  6. DotCom theDotCom = new DotCom(); //make the dotCom object
  7. int randomNum = (int) (Math.random() * 5); //make a random number for the first cell, and use it to make the cell locations array
  8.  
  9. int[] locations = {randomNum, randomNum+1, randomNum+2};
  10. theDotCom.setLocationCells(locations); //give theDotCom its locations array
  11. boolean isAlive = true; // use boolean value to see if the game is still alive
  12.  
  13. while (isAlive == true) {
  14. String guess = helper.getUserInput("enter a number"); //get user input string
  15. String result = theDotCom.checkYourself(guess);
  16. numOfGuesses++;
  17. if (result.equals("Kill")) { //if it is a kill, set boolean value to false to stop loop and print amount of guesses
  18. isAlive = false;
  19. System.out.println("You took " + numOfGuesses + " guesses");
  20. }
  21. }
  22.  
  23. }
  24. }
  25.  

No responses yet

Mar 14 2009

The For Loop

Published by Michael Doyle under Java, OOP

  1. 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.

No responses yet

Mar 13 2009

Integer.parseInt()

Published by Michael Doyle under Java, OOP

Example:

  1. Integer.parseInt("3")
  • 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.

No responses yet

Mar 09 2009

Java: Pass-By-Value Examples

Published by Michael Doyle under Java, OOP

Pass-by-value means Pass-by-copy

  1.  
  2. public class XCopy {
  3. public static void main (String[] args) {
  4. int orig = 42;
  5. XCopy x = new XCopy();
  6. int y = x.go(orig);
  7. System.out.println(orig + " " + y);
  8. }
  9. int go(int arg) {
  10. arg = arg*2;
  11. return arg;
  12. }
  13. }
  14.  
  1.  
  2. class Clock {
  3. String time;
  4.  
  5. void setTime(String t) {
  6. time = t;
  7. }
  8.  
  9. String getTime() {
  10. return time;
  11. }
  12. }
  13.  
  1.  
  2. class ClockTestDrive {
  3. public static void main (String[] args) {
  4.  
  5. Clock c = new Clock();
  6.  
  7. c.setTime("1245");
  8. String tod = c.getTime();
  9. System.out.println("time: " + tod);
  10. }
  11. }
  12.  

No responses yet

Jan 25 2009

Java: Arguments and parameters

Published by Michael Doyle under Java, OOP

  • 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:

  1.  
  2. int theSecret = life.giveSecret();
  3. int giveSecret() { //the bits are returned from the giveSecret() method and land in the variable named theSecret</code>
  4. return 42;
  5. }
  6.  
  • 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.

No responses yet

Jan 21 2009

Java: Variables

Published by Michael Doyle under Java, OOP

  • 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,
    1. 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.

No responses yet

Jan 15 2009

Java: Classes and Objects

Published by Michael Doyle under Java, OOP

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.

No responses yet