Tuesday, January 31, 2012

There's Madness in the Methods

I just learned how to write methods. They seem to be handy for doing repetitive functions within a program. A good example that came up was the pre defined method to calculate the square root of something. Although there is a long mathematical way to calculate the square root, most people don't want to do this every time they want the square root of a number. Luckily someone came up with a method to do all the long mathematics for everyone, so now all I need to do is call the square root method which I believe is Math.sqrt(). I can see where this will be handy in situations where I have been copy and pasting code over and over within the same program. Now I can just refer to one method to do calculations for me. I don't really understand call stacks yet, but then again they are kind of like the method of a method... weird! Maybe I'll research stacks and post back on what I've found later. Things to remember: - Cannot have a method within a method (at least in Java) - different types of methods for example: int, double, string - you CAN have a method that doesn't return a value, but rather does something instead, like print something that is inputted. Things that are still confusing: - I'm slightly confused about the order that things occur while using methods, but I'm sure with a little practice it will make more sense - Quantum Mechanics =p

Tuesday, January 24, 2012

Shoop dah Loop

We talked about loops today in class. I already have some experience with them, but now I feel I can write them more confidently. After learning the different types of loops (for, while, do while) I think that the while loop is easiest to understand. Everything about how the loop operates is in one handy location, then you just have to add the loop body, everything is easy to pick out at just a glance. Even though a program runs in an extremely logical and linear (maybe?) fashion, as a programmer it is sometimes easier to code something out of order. I took notes today on a good approach to creating a loop. Creation of a loop: 1. What is it you need to do? (this will be the loop body) 2. How many times does it need to loop? (increment) 3. What will make it stop? (reaching a certain "sentinel" value) 4. How will it stop? (Boolean expression becoming false) Even though these things are usually not in this order when in the code, it is a good way to think about creating a loop until it becomes second nature (hopefully this will happen to me soon, must practice). Also another tip is to simplify errors in code piece by piece. Try to fix parts of an error, testing the program, and then fixing more parts until the error is gone. Don't try to fix an error with one big fix-it-hammer, it hurts to think about fixing a big problem all at once. My knowledge of Java is coming along nicely and I'm actually excited to get into the lab and test what I've learned so far.

Sunday, January 22, 2012

Codelab Catchup

I got confused between the lab portion on my CS class and the online CodeLab part (online homework assignments). I've been going to lab, but forgot to do the online assignments. Today I went online and did all of the assignments I had missed and almost all of the ones that are due next week. After programming for about two hours I felt I learned a lot. Some problems I ran into were: -integer division: one problem asked to create a program that converted Fahrenheit to Celsius then report it using the printf function (I think it's called a function, not sure...). This seemed simple enough, so I wrote it out, but whenever the answer printed out it appeared to be 0.00. The spacing and digits after the decimal were correct since I had some experience with printf in MatLab. I spent about a half hour looking over the 10 lines of code trying to find the error. After double checking that I used the correct conversion equation from degF to degC and milling over the printf statement, I decided to look in the textbook. To my relief the exact program was in the textbook and I instantly realized why it wasn't working the way I wanted it to. I was using 5/9 instead of 5.0/9 in the F to C conversion equation. Since Java is picky, it truncated the result of 5/9 to 0 because it used integer division, when I wanted the result to be a double. Now I know better! -remembering code: not exactly a problem, but since I'm still new at this I don't know exactly the reasons why I type some things (e.g. Scanner input = new Scanner (System.in);) and I have to resort to either copy/paste or try to use purely memory. Hopefully as I learn more I will understand the purpose for words and their order. -Eclipse: I figured out some of how the directory is laid out. I'm not really sure how to save my work (not that it is that precious yet) but it seems there is a file structure in place that includes a "Workspace" then "Project" into a "Package" and "Class". There is definitely a workflow structure but I don't know the purpose of anything yet. When Codelab asked me to submit multiple programs with the class as P3 I couldn't save them within the same package. This makes sense and is the same file system that computers have, but when I tried to make a new package (instead of using the default, which Eclipse recommended I do) and put a class called P3 in it, the Codelab didn't like that. -variables: I'm not sure how to allow an input to make an arbitrary number of doubles without previously creating the variables and filling them with values that are entered (which would limit the amount of variables that can be input to the number that were already in the program). My friend just told me that I need to make an array in order to do this, but neither him nor I know how to do this in Java. Alas... Anyway I have plenty to learn, but I'm learning at an alarming rate which is nice. AWESOME!

Wednesday, January 18, 2012

Codelab is very specific

In codelab today I met my coding partner and he seems pretty cool. He has coded a little in the past but never in Java, so we are about on the same level. We started coding the first few problems and they seemed easy. There were some times when the code was denied because of insufficient spacing in strings, which is sometimes hard to tell. In one instance there was suppose to be a println at the end of the code but we only had a print statement. It was hard to notice that this was an error because it is difficult to see a new line that is at the end of code. Eitherway we figured out how to do all the problems and I am getting a feel for some of the simple mistakes to be wary of when coding.

Tuesday, January 10, 2012

CS Blog created.

Just created this blog to document my adventures, trials, and tribulations while trying to learn the Java programming language.

Wish me luck!