Thursday, April 23, 2015

Java Arrays

An array is an object used to contain and organize data of the same data type, including objects (other arrays also) and primitive values. The values or elements in the array are located at indexes or subscripts, starting at position 0 and ending at the declared array size position.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.

 Sorting the elements in an array can be done through two different methods, selection sort and insertion sort. Selection sorting is accomplished using two for loops to pick an initial position, scanning each position in the array, selecting the correct value which should go there, and then repeating this forever position in the array. Insertion sorting is accomplished by shifting a value into its correct position into a "correct side" of the array which is created from only "verified" values


When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known

Friday, March 6, 2015

Headfirst Java Chapter 5

     This chapter discussed how to convert strings into integers using the parseInt() function. The game utilized "for" loops to check if the ships were hit or not, and would end the code if the ship was hit. There was also a bug that you could hit the same place multiple times and you would win since the number of hits was reached.



Age Checker Warm Up


Note that this was created to check on 3/5/2015. If you would like this to be accurate, you will have to change the day, month, and year input sub-strings.

Friday, February 13, 2015

Headfirst Java Chapter 4

In this chapter I learned about Instance and Local variables and how they act. Instance variables are declared as part of the class and have default values, while Local variables are defined within a method and have no initial value.
This Chapter was also about encapsulation, which is the use of "getter" and "setter" methods to return variables without accessing the instance variable directly. Encapsulation is used to protect the instance variables from being accessed from outside of its class.


Wednesday, January 28, 2015

Headfirst Java Chapter 3

  In this chapter I learned about primitive and reference variables. Primitive variables values are the bits representing the value. While a reference variable value is the bits representing a way to get to an object on the heap. A reference variable is null when not assigned an object. Also I learned about creating an array of variables and how it is always an object.

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();