Tuesday, March 31, 2015

Arrays!

What is an Array?

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

Why bother using an array if I can just use normal variables?

By using arrays to can easily make lists of variables using only one command. for example I could use: String x[] = {"a","b","c"}: to create an array and reference it using x[1]. By doing so you can refer to variables quickly and without using too much space in a large program.

So how exactly do you reference variables in an array?

In Java all values, or data stored in an array is assigned a number value starting with 0. If i were to print x[0] from above the output would be "a". If i were to print x[1] the output would be "b".

Okay... so whats the catch?

Lets say you want to have a user input Int data to be organized into an array. To do so you would have to declare an empty array: intArray = new int[10] (the 10 specifies how long the array is, or how many different Int's you can store in it). This basically makes an array with 10 slots for data, and NONE MORE. Java can't keep adding slots to an array after your specified amount is entered. In Java you also cant store different types of data in the same array. If you declare the array to be an int array, the only values it can store are ints. 

Wednesday, February 11, 2015

Headfirst Java: Chapter 4

Classes: What are they? What do they do?

  • Classes are structures of code that can tell what exactly an object knows and or does
  • Things, or data that an object knows are refered to as "Instance Variables"
  • "Methods" are the things a specific object does
  • Methods use Instance Variables explicitly to differentiate between various objects.
  • Methods can have parameters like functions in Python. 
  • Methods in Java can have several parameters.
  • When using parameters they must match the variable type IN the method. (ex. If i use a float for a parameter and call it an int inside of the method, it wont compile)
  • Methods must return somthing unless it is explicity stated it wont with a  "void" command.

Activities:


- The first activity was relatively easy to decipher, side A being correctly coded, will output 42 84. Section B was moderately dificult however. The only error i spotted in the code was "void getTime()". The only reason this was flawed because it wasnt declaired as a string variable, so Java would not be able to read it correctly. It also seemed worth to note that GET methods must return a value of some sort.

- This section was hard for me to keep track of becasue the modification of many variables in quick sucession. I ended up sorting through it one line at a time plugging in each of the values to figure out Java's output for all of the commands.


Friday, January 30, 2015

Random Code Snipit

Since Chapter three was dedicated to making sure you knew what variables are, I decided that I would make a code that uses several class variables, involving strings and integers. Here is the code:
The code is designed to loop 5 times untill a grumpy farmer gets sick of hearing his dogs bark! after that he gets fed up and yells for them to be quiet! He needs some earplugs!

Thursday, January 22, 2015

Chapter 3: Variables (Fixed)

(above is the result of compiling the dog code)

Fundementals of Variables in Java:
  • There are several variable types in Java:
Float
Int
Double
Bool
Byte
Short
Long
Char
  • Float's use decimal values (ex: .25, .111, .8)
  • Int or Intiger uses full number values (ex: 1, 5, 25)
  • Doubles combine BOTH Float and Int values (ex: 1.25, 3.49, 6.64)
  • Bool or Boolean Variables use either True OR False (ex: While True, do this...)
  • Byte variables are 8-Bit variables, used to save memory in large arrays (ex: byte a = 100 , byte b = -50)
  • Small variables are 16-Bit and used to help conserve data. Its Minimum and Max value are -32,768, and 32,768
  • Long variables are 64-Bit and are also used to conserve data. Its minimum and Max values are - 2,147,483,648 and 2,147,483,648 
  • A char variable is a variable that uses a charector (ex. A, B, C, D)



Thursday, January 15, 2015

Chapter Two: A Trip to Objectville



        This chapter really emphesized Javas features as an object oriented programming language. Most of the reading was a story about two programmers assigned to accomplish a task. One programmer prefered to use procedureal based program, and the other liked OO. In the end the OO programmer had the advantage because he had the ability to program without having to touch a single piece of previously tested code, whereas a procedural programmer would have to rewrite the whole code to accomidate for the new specifications.
       
        Later on you also begin to read that all of Java code is defined as a class. One analogy that the book made to the setup is that a Class is to and Object, as a Recipee is to a Cookie. Objects can store information, and also execute tasks. Things objects know about themselves are known as instance variables. These variables represent the status of an object. Like functions in Python, things objects do are called Methods. Different classes can inherit conditions from other ones.

Friday, January 9, 2015

Headfirst Java: Chapter One

Reading chapter 1 of the book, Headfirst Java, has taught me much about the setup and structure of Java programming. Compared to Python, Java is organized much differently. Java is setup in the sequence of Source Files; within those files there are class files, and again within those class files there are Methods, and within methods there are statements.

Python, is very different in the organization. In python there are the Source files, and class files. The difference is in the class files, is that they aren't organized in methods. There are loops, and functions. Loops only active while the code is being read, and functions only activate when typed into the command prompt. Within either loops or functions, there are also statements, however the syntax also tends to be much shorter than Java's.

For example, to get a program to state a string, all you need to do is use the statement: print "Your statement here". In java you have to use the statement: system.out.print "Your statement here." Below is a snip-it of the code I worked on to help get to learn the syntax and structure.




Thursday, December 18, 2014

1.3.8 While loops


Conclusion 
1)     If you change between 1 and 20 from the previous program to between 1 and 6000, how many guesses will you need to guarantee that you have the right answer? Explain.
      You will need to have exactly 6000 guesses to assure that you undoubtably have the correct answer,
2)     Describe the difference between a while loop and a for loop.
      For loops repeats every time a specific event occurs, however while loops only repeat while a certian condition is true. The two can be used interchangibly based on your personal coding preferance.