Wednesday, April 20, 2011

LESSON 3: Algorithms and Psuedocode

What is an algorithm?


Put simply, an algorithm is just a set of steps you complete in order to solve a problem or perform an action. For instance, lets say the problem you are solving is that you need to determine what a two week pay check for a salaried employee should be:

1. Determine what the salary amount for the employee is
salary = 40,000
2. Determine what 1 week worth of pay is for that salary
weekly = 40,000 / 52 (or 769.23)
3. Multiply by two to figure the 2 week pay check
paycheck = weekly * 2 (or 1538.46)

So you could say that these 3 steps make up an algorithm! Obviously, algorithms can get really complicated depending on what it is you're trying to accomplish. But for the sake of understanding the concept, this should be enough.

What is Pseudocode?


Pseudocode is exactly what it sounds like. It is code that isn't really code. Some people will call this "structured english". Personally I think the two are different, but its not important as a codemonkey, just understand that it is fake code. So what does that mean? Well from my experience,

Pseudocode could be all english:


get salary
divide salary by 52
multiply result by 2
display result


or english structured like code


declare salary as integer
declare weekly as double
declare payCheck as double


set salary = 40000
set weekly = 40000/52
set payCheck = weekly * 2


or a combination of actual code and structured english


int salary;
double weekly, payCheck;

salary = get employee salary;
weekly = salary / 52;
payCheck = weekly * 2;
NumberFormat formatter = new DecimalFormat("$#,###.##");

display formatter.format(payCheck);

So you can see the point here, I think. Pseudocode allows you to structure the logic without worrying about proper code syntax. Mixing real code with pseudocode can help you remember specifics about how you were thinking it should be implemented.

When trying to figure out a complex problem or process, it is very helpful if you write out the pseudocode first, to help you get your ideas out quickly before you lose them! Sometimes the brain get in the way of your creativity when you start thinking about the details of implementing the algorithm. Just get it out in english first, then come back and convert it to actual code. I'll try to do this when we get into real coding, at least for the early stages.

Tuesday, April 19, 2011

LESSON 2: Naming Conventions

What are naming conventions?

Naming conventions are simply a standard format in which you name your Variables/References/Identifiers, etc...

While you can name these things anything that you wish, by using the standard conventions, you will save time and eliminate a lot of confusion when start getting into large coding projects. By using these conventions, you should be able to identify what variables contain, just by looking at how it is named. In addition, since you cannot use spaces in your variables, and you want to be as descriptive as possible, mashing words together can be very difficult to read and this will help that.

You won't understand all of these quite yet, but come back to this post when you start to see them.

Types of Notation

Camel Casing: first word all lower case, capitalize first letter of each subsequent word. Generally used for primitive type variables (will cover later)

camelCasing
helloWorld
myAge
payRate

Pascal Casing: capitalize the first letter of every word. Generally used for name classes and methods, depending on the language that you're using

PascalCasing
GetPayRate
MoveCharacter
CalculateInterest

Hungarian Notation: when dealing with objects, you prefix your identifier names with an abbreviated description of what type of object it is, and then capitalize the first letter of each subsequent word.

txfFirstName   >> a text field called FirstName
txaMessageBody >> a text area called MessageBody
btnSubmit >> a button called Submit
cboOptions >> a combo box called Options


If you're confused right now, ask questions. But don't worry about it too much at this stage as we will touch on these as we encounter them in later lessons.

LESSON 1: Algebra for Programming

There isn't really much to say for this quite yet. But you need a basic understanding of algebra. Don't get scared! When I say basic... I mean really basic.

What are variables, references, and identifiers?
Think back to grade school. Does this look familiar?

x = 10, y = 5

x + y = 15

If you understood what just happened there, then you're doing pretty good already.

In math, we call x and y "variables". In programming, we call them that too. But more specifically, we call them "references". They are a placeholder for a value. In this example 'x' held the value of 10 and 'y' held the value of 5. So saying x + y is the same as saying 10 + 5. And of course, 10 + 5 = 15 so x + y = 15 as well.

The reasons why we call them references is pretty broad, but in short we do it because

A) Sometimes these variables are not variable in that they do not "vary".
B) They don't actually hold a value

When you declare a variable or reference in programming, what you're actually doing is reserving space in memory that will contain the data in which you are referring to. What's the difference? Why does it matter? We'll get to that later. All that matters at this stage is knowing what I'm talking about when I say "variable" or "reference".

You will also see variables referred to as "identifiers". Identifiers are the same thing. You're essentially just making a name for the placeholder.

Okay, so back to declaring variables. In programming, "declaring" a variable is telling the computer to reserve memory in advance for the value that will be stored inside of this variable. By doing this, we allow ourselves to refer to that value, and in most cases, manipulate that value.

Lets practice:

x = 10
y = 2
z = 4

what is x + z - y?

answer: 12

what is (x / y) + z?

answer: 9 - don't forget, you always perform operations inside parenthesis first! Order of operations will be covered in a later lesson.

In programming, you generally want to be descriptive about your identifiers. Name them in a way that you will know what is contained within them.

myAge = 31
yourAge = 25
minimumAge = 21

what is myAge - yourAge?

answer: 6

what is yourAge + minimumAge?

answer: 46

--------------------------------------

In programming, the equation that you use  isn't generally designed to be solved. Its not like algebra where you say:

2x + y = 5, find the value of y.    **see later note

When you write an equation, you do it so that we can work with whatever the result of the equation turns out to be. For example, assume i have 3 variables: myAge, yourAge, and differenceInAge. Let's set values to everything but the last one

myAge = 31
yourAge = 25
differenceInAge = ?

We have two variables that now hold a value and a third that is completely empty and holds no value at all (also known as NULL). If i perform some arithmetic on the first couple variables, I'll end up with another number, floating in limbo. What do I do with it? In programming, there is a number of things you could do with that number. For the sake of this example, lets take that answer and store it into the null variable, differenceInAge:

myAge - yourAge = differenceInAge

Now what we're saying here is that the result of the math operation on the left side of the "=" sign is the SAME as the value stored in the variable "differenceInAge":

31 - 25 = 6 so... differenceInAge is no longer null, its value is 6.

There is one slight problem with this equation. Its syntax is wrong! In programming, the operation being performed MUST be on the RIGHT side of the "=" sign:

differenceInAge = myAge - yourAge

The logic behind this can be self-explained by reading this expression as follows: "differenceInAge BECOMES myAge minus yourAge." Meaning the variable differenceInAge now carries the same value as the resulting value of myAge - yourAge.

**back to our algebra example:
2x + y = 5
If we were to follow the syntax rules, we could still solve this:

y = (2 * x) - 5

Now, y holds a value and can be used. This is where your knowledge of algebra can come in handy. In order for this to fit the syntax, we had to isolate y.
More of this to come in later lessons.


So thats pretty much all you need to know about Algebra as it applies to programming... for now. Pretty easy, right?

Summary and add'l notes
  • Variables, References, and Identifiers are all pretty much the same thing. They are place holders or identifying names for placeholders.
  • Once a value is stored inside of a variable, the variable can mostly be used as if it was the raw value itself. This isn't ALWAYS true, but we will cover more about this once we talk about Data Types.
  • When naming your variable, choose something descriptive so that you will know what is contained (or supposed to be contained) within that variable, at a glance.
  • The results of any operation can be stored into another variable.
  • An equation will also be referred to as an "expression"
  • "Syntax" is how we describe the format of an expression. Most things must be written in a certain way for the compiler to understand what it is you're trying to say.

The Curriculum

This is a list of the topics I will attempt to cover. This list may be updated often, but I will always try to keep it in a logical order. Meaning, you should make sure that you've covered earlier topics before attempting the later topics as you need a firm grasp of the basics for anything else to make any sense! The blog is new and still coming together, so I apologize that not every topic is filled.



  1. Read Me First!!
  2. Algebra for programming
  3. Naming conventions
  4. Algorithms and Pseudo-code
  5. Data types
  6. Operators
  7. Loops
  8. Arrays
  9. Functions
  10. Object Oriented Programming (OOP)
  11. Choosing a programming Language
  12. GUI
  13. Threads
  14. Graphics Rendering
  15. Extending your Programming

Read Me First!

The internet is a great tool for finding information. But in the realm of programming, it can get pretty brutal. There are tons of resources out there that can help you to learn how to program, but they're not always so simple to follow. Often, they only cover a specific scenario and the folks on the forums assume that you're already an intermediate programmer and seem to speak in Swahili!!

What I'd like to do here is create a place that will offer you something a little more than tutorials. I'm going to share my formal education with you. I am not a certified instructor, nor do I hope to be. I do not claim to be a student in the best computer science school in the world. I do not intend to teach you all of the best practices and "right way" of doing everything. But I will try to share as much of best practice as I can, within the scope of this blog, and get you on the right path and teach you the basics. At least this way, you'll be able to hit the forums and know what you're looking for. At the very least, you should learn enough to knock out some basic applications or tools for you to use for yourself :)

My name is Jeremy. I am a CIS major at DeVry University. I'm currently in my late 300 and early 400 level CIS classes. I decided to this both to support the development community by bringing up the interest in programming, but also, I feel that the best way to learn something is to teach it to someone else. I spent a large part of my youth as a percussion composer and instructor for several high schools and I was forced to work in less than ideal circumstances with a wide variety of education and ability levels. I spent a lot of time refining my teach techniques, NOT conforming to the traditional methods, but rather to the methods that produced results while still covering important topics, but focusing on each individual student rather than the lot.

How I'm going to run this blog:
The ONLY way to learn how to program is really to start at the beginning. I'll spare all of you the prerequisite type class material that you'd have to learn in a university like the history and architecture of computers. Although, there are important concepts that I will attempt to explain as they become relevant.

I will refer you to any books that I think would help you. I won't torture you by referring you to technical books that are, in the end, nothing more than a huge reference book.

I will break the site up into lessons. Between lessons, I will post supplementals, off topics, opinions, etc. But for navigation purposes, I will make sure to label the subject lines with LESSON, and so on.

If there any comments or questions in replies, I'll do my best to answer them or even do a demo that outlines my answer to the question.

I will teach this in JAVA. Primarily because it is useful, but also because it will allow me to get deep enough into the manual coding so that you might actually learn something. My favorite language is C# (Microsoft's ripoff of JAVA), but Visual Studio does so much for you that you will end up lost, very quickly.

As I find time, and with any requests, I will attempt to add videos in each lesson or demo to help bring some of my ramblings to life. Be patient.