Friday, February 13, 2015

Chpt 4 Headfirst into Java



Chapter 4 is mainly about behaviors and states and how they relate.

Methods inside classes act differently based on the values of instance variables inside the class. Instance variables can be compared to what the class knows, and methods as what the class does - so, instance variables are the state, and methods the behavior, and the states can affect the behavior. This is seen in the two code snips below.



This code first makes the class Dog, in which the int variable "size" is created. The method "bark" is also defined; this method checks the value of size and changes the string output based on the value. The main class, DogTestDrive, defines three "Dogs", onetwo, and three, each of a different size. It then orders the method bark() to be run for each Dog. This shows how the instance variables, or the state, affects the behavior. The instance variable "size" changes the behavior of the code as the size variable is checked and the behavior, bark(), acts based on size

Methods use parameters; a caller passes arguments.
Arguments are passed into methods, and are also parameters - which is a local variable. If a method takes a parameter, it must have something passed to it. If a method is declared to return a value, the return value must be of the declared type, or compatible with the declared type (e.g., int theSecret, int giveSecret) Multiple values can be contained within a parameter, as long as they are separated with a comma. This includes variables, as long as their type is that of the parameter.Encapsulation; encapsulating is important in Java so that variables won't be assigned to something other than what they should be. This is done by marking your instance variables with public and private modifiers. In our dog code above, we can make the instance variable 'size' private just by adding 'private' before 'int.' What is the value of an instance variable before you initialize it? Instance variables will always get a default value; if a value is not assigned, it will default to one of four values, depending on the type.
Int = 0
float = 0.0
boolean = false
ref = null

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

Wednesday, December 10, 2014

Conclusion


Conclusion
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?
They are different because one of them was taken out of a complete string, one was taken from a list, and one was taken from a set of strings.
List: Elements of list can be changed
Tuple: Elements of tuple cannot be changed
String: Is a whole


2.      Why do computer programming languages almost always have a variety of variable types? Why can't everything be represented with an integer?
There are a variety of types so that if you need to put different values together you can make it easier. Everything can not be an integer because some things can not be represented by a number

Thursday, December 4, 2014

Tweet












1.       How many characters are in this sentence? Does it matter whether Python is storing the string as one byte per character or four bytes per character?
41 characters are in the sentence. It should not matter as long as it is consistently stored.



2.      This question asks you about something you have not learned. In fact, the question is asking about details that go beyond what you will learn in this course. However, wondering what is going on at a lower level of abstraction – and talking about it – can be a useful strategy when learning about computing.

Describe what you think occurs in memory when the following code is executed.

In []: a = 'one string'
In []: b = 'another'
In []: c = a[:3] + ' and ' + b
In []: print(c[6:10])

It sets 2 variables and the 3rd line takes certain characters out of the strings and sets it to 'C', which equals ona and another. It then says to print the 6th through the 10th character making it print: 'd an'.