Tuesday, April 14, 2015

Arrays

An array is a container object that holds a fixed number of values, unlike a regular variable that can only contain a single value. 

The length of an array is established when the array is created. After creation, its length is fixed. 

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.

Arrays are objects. To create an array the reference to the array must be declared.
The variable height is declared to be an array of integers whose type is written as int[].

All valuables stored in an array have the same type. An example is that you can create an array that can hold integers or an array that can hold strings but not an array that can hold both integers and strings.

Another important characteristic of arrays is that their size is held in a constant called length in the array object

One way you can declare an array is by:
int[] num = new int[7];

Here is a way you can assign value to arrays:
num[0] = 10;





Friday, February 6, 2015

Head First Chapter 4

A method uses parameters. A caller passes arguments. if a mthod takes a parameter you must pass it something. Getters and setters let you get and set things. Instance variables value. A getter sends back as a return value of whatever the value it wanted to get. A setter takes an arguement value and uses it to set the value of an instance value.


You can see if two primitives are the same by using the == to compare them.



Friday, January 23, 2015

Head First Chapter 3

Variables must have a type and also must have a name. It needs a name so you can use it in the code. Variables hold primitives and references to objects. There is actually no such thing as an object variable.  There’s only an object reference variable. An object reference variable holds bits that represent away to access an object.


Friday, January 16, 2015

Head First Java Chapter 2

A class is a blueprint for an object. It tells the virtual machine how to make an object of that particular type. You need two classes to create and use a object. One class for the type of object you want to use and a tester class to test your class. The tester class is where you put your main method.
A java application is basically objects talking to other objects.

Monday, January 12, 2015

Head First Java Chapter 1


I learned that a source file holds one class definition and that it represents a piece of the program. A class has one or more methods and the methods must be declared inside a class. Method is a set of statements, their like functions or procedures. Everything goes in a class. Running a program means it tells Java Virtual Machine to load a class and start executing its main ( ) method. Also java has three standard looping constucts, while, do-while, and for.

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 is the beer song it works by launching the main class and it has 99 bottles of beer on the wall. The code will count all the way to 0. Wheb it gets to 1 bottle on the wall it changes bottles to bottle so it is grammercally correct. Then the code is correct and there is no more issues. 

Thursday, December 18, 2014

1.3.7 For Loops




Lottery Ticket

import random
def matches(ticket):
winners = []
result = 0
for x in ticket:
winners.append(random.randint(1,60))
z = 0
for y in winners:
if x == y:
z = 1
if z == 1:
result += 1
ticket.sort()
winners.sort()
print 'the winning numbers are ' + str(winners)
if z == 1:
return "your ticket had 1 match" 
if z == 0 or z >=2:
return "your ticket had " + str(result) + "matches"


Hangman

def hangman_display(guessed, secret):
ret = ""
for i in secret:
x = 0
for j in guessed:
if i.lower() in j.lower():
x = 1
if x == 1 or (i in " !,.?"):
ret - ret + i
else:
ret = ret + "-"

return ret

Sometimes code using an iterative loop can be written without a loop, simply repeating the iterated code over and over as separate lines in the program. Explain the disadvantages of developing a program this way.

 Really inefficient and lots of work.

 Name a large collection across which you might iterate.
 A area holding a huge amount of items.


What is the relationship between iteration and the analysis of a large set of data?
A iteration is tasks completed one at a time. Iterative loops analyze huge sets of data.

Thursday, December 11, 2014

1.3.6 Tuples and Lists

import random
def guess_letter():
           alphabet = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
print random.choice(alphabet)


def roll_dice():
          a = random.randiant(1,6)
          b = random.randiant(1,6)
           print  a + b
1.       Consider a string, tuple, and list of characters.

In []: a = 'acbde'
In []: b = ('a', 'b', 'c', 'd', 'e')
In []: c = ['a', 'b', 'c', 'd', 'e']

The values of a[3], b[3], and c[3] are all the same. In what ways are a, b, and c different?

'a' uses a string to keep the letters. 'b' is a tuple that holds them. 'c' is a list to keep its values


2.      Why do computer programming languages almost always have a variety of variable types? Why can't everything be represented with an integer?

There is a variety because not all variables can be used in the same situation they might have only a certain one that can be used.