Friday, January 30, 2015

Java project

This code picks a random number between 1 and 100, and sets it to numberToGuess. It also assigns three other variables, win, guess and numberOfTries. Every time a guess is made, that integer is set to the guess value, which is then checked if it is equal to the numberToGuess. If it is not the numberToGuess, then win remains false and two new methods are run. The first method checks if the guess is greater than the numberToGuess. If true, then it will tell you that your guess is too high. Else, if the guess is less than the numberToGuess, it will tell you that your guess is too low. Every time a guess is made, one is added to the numberOfTries. When the guess is equal to the randomly assigned numberToGuess, the boolean value win is set to true and the build ends after telling you the numberToGuess and the numberOfTries.



Wednesday, January 28, 2015

Chapter 3 Headfirst into Java


In chapter 3 you learn about variables and how you can declare them, assign them values, and use them. You have to be careful declaring a variable, you need to be aware of the types of variables. There are primitive variables and object reference variables. Values can be assigned to variables in similar ways to python: you can give a variable a numeric value by using an equals sign, equate it to another variable, and create an expression with other variables and numeric values. Remember: objects are not variables.

Dog Code:
class Dog {
    String name;
    public static void main (String[] args) {
        //make a Dog object and access it
        Dog dog1 = new Dog();
        dog1.bark();
        dog1.name = "Bart";

        //now make a Dog array
        Dog[] myDogs = new Dog[3];
        //and put some dogs in it
        myDogs[0] = new Dog();
        myDogs[1] = new Dog();
        myDogs[2] = dog1;

        //now access the Dogs using the array references
        myDogs[0].name = "Fred";
        myDogs[1].name = "Marge";

        //Hmmm... what is myDog[2] name?
        System.out.print("last dog's name is ");
        System.out.println("myDogs[2].name);

        //new loop through the array, and tell all dogs to bark
        int x = 0;
        while(x < myDogs.length) {
            myDogs[x].bark();
            x = x + 1;
        }
    }

    public void bark() {
        System.out.println(name + " says Ruff!");
    }

    public void cat();
    public void chaseCat();

Tuesday, January 27, 2015

Chapter 2 Headfirst into Java

In Chapter 2 you about how object oriented programming is considered better than procedural programming by the majority. A major benefit of object oriented programming over procedural is that you can change the code easily without unknowingly alter the code while changing one part. Object oriented programming has an object contain a method. Any code pertaining to that object can be run if the class is called for; in procedural, the code is in a linear pattern and usually has a significant amount of lines . Object oriented programming also allows you to assign similar objects to a class to simplify your code.Object oriented coding gives you the option to assign similar objects to classes to compact and simplify your code.

Chapter 1 Headfirst into Java

The 'Beer Song Activity' had us code the "99 bottles of beer" song using if-else-statements, while loops, and variables. As expected we ran into a problem; when there was one bottle left it would say "1 bottles of beer on the wall" as opposed to "1 bottle of beer on the wall". To fix this I changed the coding so that if the number of bottles is 1 change "bottles" to "bottle".


I learned that Classes and methods are defined within curled braces.
I learned that Variables have a type and name.
I learned that System.out.print and variables require capitalization.
I learned that blank spaces do not matter.
I learned that In after System.out.print results in a different sentence