Friday, January 30, 2015

Cat Simulator 2015

I worked together with my friend Tim to make a 'cat simulator' of sorts in Java. After naming your virtual cat, you can use text commands to pet it, give it things to play with / eat, or watch it interact with things you've given it. Here's an example run of the program:

You got a new pet cat! What do you want to name him?
Tom
You named him Tom.
What do you want to do?
(pet, give, watch, exit)
give
What item is it?
paper ball
Is it edible?
no
Tom plays idly with the paper ball.
(pet, give, watch, exit)
give
What item is it?
catnip
Is it edible?
yes
Tom stares at the catnip.
(pet, give, watch, exit)
watch
Tom eats the catnip. Viciously.
(pet, give, watch, exit)
watch
Tom meows.
(pet, give, watch, exit)
pet
You pet Tom and he purrs softly.
(pet, give, watch, exit)
pet
You pet Tom the wrong way, and he scratches you!
(pet, give, watch, exit)

The program is split into three .java files and three separate classes; a main class, a cat class, and an Object class. The main class handles command inputs and stores objects, the cat class decides what it wants to do with with the objects it's given, and the Object class stores information about each object.

The project code can be viewed in this Eclipse project.

Wednesday, January 28, 2015

Chapter 3

In experimenting with the Dog program example, I learned that Java reads all the class definitions (variables, constructors, methods, etc.) before executing public static void main (String[] args). This means you can have the main program inside a class, and that the program will start with one copy of that particular class. This makes me wonder if you could refer to itself in an array.

class Dog {
   public String name = "Ben";
   public static void main (String[] args) {
      Dog[] myDogs = new Dog[2];
      myDogs[0] = new Dog("Bob");
      myDogs[1] = this;
      int x = 0;
      while (x < myDogs.length) {
         System.out.println(myDogs[x].name); // Would this print "Bob" and then "Ben"?
      }
   }
   public Dog(String newnname) {
      this.name = newname;
   }
}

     

Tuesday, January 20, 2015

Chapter 2

In the second chapter of Headfirst java, I learned about subclasses and superclasses, and how subclasses can override superclass methods. For example, if we have a superclass called Shape, we can have subclasses Square and Triangle. The subclasses can have their own unique variables and classes, while preserving any of Shape's methods assuming they haven't been overridden.

Tuesday, January 13, 2015

Chapter 1 findings

In the first chapter of Headfirst Java, the two biggest things I learned about are casts and declaring arrays. Casts allow you to change numbers from one data type to another, such as from Float to Int.
Example:

float x = 32.25f;
if ((int) x == 32) {
     System.out.println("This will print because 32.25 rounds to 32.")
}
if (x==32) {
     System.out.println("This won't print because x is still 32.25.")
}

Arrays in Java are created somewhat differently than other languages- You have to tell the code you want an array after the primitive type. This means the array will only accept one type of data (String, integer, etc).

String[] animals = {"cats", "dogs", "pigs"}
int[] numbers = {1, 30, 25, 22.5} // This will throw an error, since 22.5 is a float in an integer array.

Friday, January 9, 2015

Beersong

So today I fixed a program that prints the words to the Beer Song, AKA 99 Bottles of  Beer.
Original code:

public class BeerSong {
    public static void main(String[] args) {
        int beernum = 99;
        String word = "bottles";
        
        while (beernum>0) {

            if (beernum==1) {
                word = "bottle";
            }

            System.out.println(beernum + " " + word + " of beer on the wall");
            System.out.println(beernum + " " + word + " of beer");
            System.out.println("Take one down");
            System.out.println("Pass it around");
            
            beernum = beernum - 1;

            if (beernum > 0) {
                System.out.println(beernum + " " + word + " of beer on the wall");
            } else {
                System.out.println("No more bottles of beer on the wall");
            }
        }
    }
}

This works well, except towards the end it prints "Pass it around, 1 bottles of beer on the wall" because the variable "word" is updated at the beginning of the loop rather than after the beer count changes.

public class BeerSong {
    public static void main(String[] args) {
        int beernum = 99;
        String word = "bottles";
        
        while (beernum>0) {
            System.out.println(beernum + " " + word + " of beer on the wall");
            System.out.println(beernum + " " + word + " of beer");
            System.out.println("Take one down");
            System.out.println("Pass it around");
            
            beernum = beernum - 1;
            if (beernum==1) {
                word = "bottle";
            }
            if (beernum!=0) {
                System.out.println(beernum + " " + word + " of beer on the wall");
            } else {
                System.out.println("No more bottles of beer on the wall");
            }
        }
    }
}