Thursday, April 16, 2015

blog post about arrays

An array is a "list" of data values ranging from: Strings, Ints, Chars, Doubles, Shorts, and even Objects.


Each item in an array is called an element, and each element is accessed by its numerical index.

The number corresponding to each position is called an index or a subscript.
Array indexes always begin at zero. That means the value stored at index 5 is actually the sixth value in the array.

To find the length you type: y.length; where y is the name.

To access the elements you type: y[z]; where y is the name and z is the index.

To iterate through an array type: for (int i = 0; i < y.length; i++) {} where y is the array.

You can't just print an array. However, you can iterate through an array like this:

for (int i: testArray) {
   System.out.println(i); // this will print every value inside of testArray on a new line.
}

To compare values, you would still abide by the normal procedures. If you have a String[] array, you'd use .equals, and == for mostly anything else.
The declaration of an array first states its data type, using one of the data type keywords, followed by " [] "to express that it will be an array variable

Friday, February 6, 2015

Chapter 4

A Class Is The Blueprint For An Object.  When you are writing or already wrote a class you are describing how the JVM should make an object of that type.
Methods can act differently based on the value of the instance variables. You can pass values into your methods. Using the example from the book if you want to control how many times the dog will bark. "d" being the dog, "bark" being the variable, and in the () will be the value of the vairable. so like in the book , d.bark(3) it telling the dog to bark3 times.
     Also with methods you can get thins back from them.Every method gets a return type. As well as being to return things from a method you can send more than one thing to a method. Methods have multiple parameters. In order for them to work they have separated by a comma so an example would be t.takeTwo(11, 54)


Thursday, January 29, 2015

Chapter 3



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.
The code created a dog object uses variables to define the dog and it's actions, then it 'prints' the strings. The variable x, which is defined as a interger, 0,  is used to determine the number of dogs. that are created.

Friday, January 16, 2015

head first java chapter 2


This chapter helped me understand the basics of Object Oriented Programming (OOP). I really like the concept of OOP, it turns programming to an organized set of classes and objects that can be changed or easily. 

Some other things I learned include:

  • main() should only be used to test real classes and/or to launch and start a Java application
  • Objects can "communtcate" to teach other by calling methods on each other
  • "Global" variables don't really exist in Java OOP, but later we'll learn how usingpublic and static can make a variable behave like a global one
  • Subclasses can inherit instance variables and methods from superclasses











Monday, January 12, 2015

what i learned chapter 1 java

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.

Sunday, January 11, 2015

beersong java head first

Beersong

 I fixed a code that prints the words to the Beer Song.
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, towards the end it prints "Pass it around, 1 bottles of beer on the wall" because the variable "word" is set at the beginning of the loop rather than after the beer count changes so it  doesn't change the word bottle.

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");
            }
        }
    }
}