Friday, November 26, 2010

Programming- Lesson #6 ~ Creating Your Own Classes

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves.”- Alan Kay

I did not write any post about programming since long time ago and I keep delaying because it requires time because I read, learn, then write about what I've understood. Here is one post about Lesson #6, and there should be one more post to finish up the basics.

So what is Classes? and are we able to build them our own? Yes, you can. Classes are the structure of your program. The Hello World program is simple and small, you would find your way around even if it wasn't organized, but when you start doing more complex programs, you probably want your codes organized in classes so you find them easily. So it is important to understand how to create those classes. There is no right or wrong in making your classes.. Imagine you are in the library, and there are so many want to find a specific novel... the librarian may organized the books collection based on the alphabetic names, or the most recent books. So you can organize your program in any way you like.

In our small Hello World program, we will create a greeting class (just like how we greeted Diana, Hannah and Alex here).

Here is how you create a class in your program:

My application called " MyFirstApplication " , so whatever you names your file name, go toSolution Explorer window, right-click on the project you have then point at Add, then clickClass. Name it anything you like, I named it " Greeting.vb " then after that, you would see a window opened with the a class called " Public Greeting Class. "

For now, copy below to your new class and then we will explain it later on and why it looks this way.

Also, Modify your SubMain as follows:

When you run th program, you will see that nothing it changed, it is just this boring Hello Eric greeting, but the way we does this greeting is completely different as you noticed. We wrote the code in a very different way. This is how it will looks like:

As you saw in the code above, there is Dim theGreeting As Greeting. This is our new Main Sub. So now you are wondering " whats the difference between this type of greeting and the ones we did before?"

In this line of our class Dim theGreeting As Greeting the difference is the type of variable. Remmeber in the previous lessons here and here when we talked about Strings and Integers? Our variable here is Greeting not string or integer, there is no numbers or anything, right? It can't hold anything except the Greeting value, therefore; the 'slot' is empty and we have to fill it with something after the ( = ) sign like we did above. Here is what we added: Dim theGreeting As Greeting. This is an object that identify the greeting and what type it is. It is a class of an object. Our new object called instance of the greeting class. So when you want to use this term in VB, you call it instantiation. So we are instating a class here. See the module below.

This module shows the class we created and in what we will fill it. The property of the greeting is Recipient. So in order to do that we have to set he recipient to greet Eric like this: theGreeting.Display()

There should be one more post and we will finish the first part ( and basics ) for Visual Basic.

Friday, October 8, 2010

The Moon

Suddenly I was interested in taking pictures for the Moon. It was pretty fun; running from room to room to see which spot is the best to capture a clear picture for it. In the beginning; it wasn't easy for me to figure out the setting and where to stand to capture the entire circular moon. My friend had experiment it before and told me which is the exact setting for such images. I wasn't sure which one of those below pictures are the best, so I decided to post the best three.

The setting are:
Lens- Canon 70-300 IS
F-stop- f/22
Exposure time- 1/100 sec
ISO speed- 100
Focal length- 263 mm
White Balance- auto
Exposure program- manual

Thursday, September 9, 2010

A New Year MacLabing

Wohoo ! a new year in the mac lab, what a great year it would be. I almost thought I wont be in this class for this year but Mr.Skocko saved my life and got me in :)

I'm ready to whatever the Mac Lab will throw at me, I'm sure there will be many cool stuff that we will learn especially we have Photoshop CS5 and other programs that would make designing so much easier and creative. I'm willing to work on Photoshop more than anything else because it makes art pieces rich of colors and improvements. Also photography will take first priority from my time, I will be borrowing different type of lenses to capture amazing shots.

From Last year, I did the same thing and I took many pictures throughout the year. Some of them were worth looking at and my teacher added it to his Photography Gallery in the blog. One of them had been in the District Art Show. Well, I didn't win but I was happy that it was there for people to see. Click here here if you want to see it.

Different topic. I said I will be posting two more posts ( Lesson 6 and 7 ) about programming but I didn't have time for that. I hope I will be writing those posts sometime soon.

That's all I wanted to say. I hope this year in the Mac Lab will have students that create amazing art . Fun begins!

Wednesday, August 18, 2010

Programming- Lesson #5 ~ The For...Next Loop

"Good code is short, simple and symmetrical -the challenge is figuring out how to get there" - Sean Parent

Good programmers will choose the best path to complete a program. It's a good practice if you write concise or minimal code to achieve. This can be achieved by mastering the language features and learn by experience what the most efficient approach to achieve results is. But the problem is here; sometimes we can't know how to make it easy and simple to get the results we want, so we take the hard path and get to complicated steps. By experience and skill, it will be easy to figure out how write codes in a simple, short and symmetrical way.

The previous lesson is how to repeat or lets say " loop " the code block for many times. What if we want to repeat this block code only 2 times or for a certain number? Let's modify the Main method and greet 4 people only ( which means , the loop will be repeated 4 times only ). Look at the image below:

Let's link to the previous lessons and see how this example worked.

The counter variable i to the start value which is 1. VB will test it and see that it is less or equal than th end value, which is 4 here. If the start value is less than or equal the end value then the loop will work and the block code will be executed. When you press Enter ( after running the program ); VB will add 1 to the counter variable i and this way the For Loop will continue working and printing the certain number of statements on the screen. And the program will do the same steps again, see the block code if it is equal than or greater than and print the next statement and so for and so on.

The counter variable (i) is the tool that makes the loop repeat for certain number of times. If it is 1 time then it do it's own work, if it was 2 then it will do it's work also. Here is a table of the value variable (i) for the For Statement to check if it will loop again or not.

You can use any numbers you want for the start and end value. That was just an example of how many times you want the statement to stay looped or repeated.

School will start soon and I'm willing to write two more lessons and then I will try to keep giving lessons of the things I'm reading although I know I will be busy in my senior year and won't have that much time like now. The next lesson will be about classes and how to create your own.

Tuesday, August 17, 2010

Photography Update

Here's another photography update. Today I was trying to take pictures for the Float Project for my Digital Art class... I don't think I got good ones but this is the first day I started and I like to share them. Here are two pictures I took for my sister.

By using Fish Eye Lens, I lay down on the ground under the heated sun. I tried to get the sun beams clear but I couldn't do it in this picture.

I wanted to make them look like cartoons, so I just played with the sliders of contrast, brightness, shadow , saturation and the highlights. I did the same things for the first two pictures. Those are the camera information.

F-stop: f/7.1 , Exposure Time: 1/400 sec , ISO speed: ISO 100 , Flash Mode: No Flash, compulsory , Focal Length: 15mm , Mattering Mode: Partial , Attributes: A , Exposure Program: Manual

F-stop: f/22 , Exposure Time: 1/60 sec , ISO speed: ISO 100 , Flash Mode: No Flash, compulsory , Focal Length: 15mm , Mattering Mode: Partial , Attributes: A , Exposure Program: Shutter Priority

If you noticed; the sun beams are clear here and thats because the Exposure time and the F-stop are different in this picture. I liked how the body is curved with the jump.

This is not for my Float Project, it is just a picture I took in my father's birthday, but I guess it is good enough to include it in this post. I edited it of course by giving it black and white effect then kept the colors on the top fire of the candle.

F-stop: f/9, Exposure Time: 1/200 sec , ISO speed: ISO 400 , Flash Mode: Flash, Auto , red-eye , Focal Length: 40mm , Mattering Mode: Pattern , Attributes: A , Exposure Program: Normal

You can see those new pictures and the recent ones on my photoshop site
Comment and tell me what you think! :)

Tuesday, August 10, 2010

Programming- Lesson #4 ~ Looping and Iteration

" If you don't think carefully, you might think that programming is just typing statements in a programming language." - Ward Cunningham

When I first started leaning programming which is few months ago, I didn't know there is so much to learn and understand. We have to pay attention to each inch of the code because any mistake may cause an error. Programming isn't about writing a statement with wired signs only, it is an artificial language that designed for a computer only to understand. Programming language create programs that control the machine itself. They have form, meaning and errors. Don't be fooled by the statements that you only see, there is much more than what it looks!

What is looping? and what does it do with programming? Basically,the programs needs to do and preform tasks repetitively sometimes, the program itself do this. We call this behavior Looping. When there is instructions that are repeating, it will stop repeating till a current condition is met, and thats called a Loop. There is usually Boolean Expression in a loop so we know if it will continue looping or not. Here is an example of the whole thing I have:
Do you see the line where it says " Please enter your name " ? That's what we are talking about today; how to make one code or a task be repeated more than one time. When you run the program, it will tells you " Please enter your name: " you write any name you like and then press Enter. It will greet the name you wrote by Hi or Hello. You can try it as many times you like, and when you get bored just press Enter (without writing any name ) to dismiss the program. Let's take a look at the program.

Now, what is Console.Write and Console.ReadLine? Console.Write is similar to Console.WriteLine but it does not add aline that breaks the text. Console.ReadLine reads the console not write it and we use it when we keep pressing Enter so the same line gets repeated ( or in other words, the program pasues till the user press Enter).

The loop works this way...when we run the program, the Boolean expression ( false or true value ) will evaulate the code and tells us wether this value is false or true. When we run the program the expression will tell us the value is wrong and do the follwoing which is " enter your name ", when we enter the name and press Enter, the code will loop again ( go back ) and do the same thing, which is enetring the name and it.

As I noticed while experimenting the program above, I pressed Enter without writing anything...for exmaple, I run the program and it told me " Please enter your name". I didn't write any name and pressed enter.. that way the expression will be false and the return value from Write.ReadLine is empty string.

The next lesson will be also about loops. I hope I am making it as simple as possible.

Thursday, August 5, 2010

Programming- Lesson #3 ~ Functions and Controlling The Program

" Programming is similar to a game of golf. The point is not getting the ball in the hole but how many strokes it takes. " - Harlan Mills

The quote above means that, it is not a bout how many lines to do a program. If you did a program in one code line and someone else did it with 3 codes lines, then your program is better, because you consumed time, and memory from your computer. If you can make the same program with less codes then this is the smart way to do it, even when you fix it, it will take less time. And that's go to our lives too, if we can do things with less time, money and effort then we get the same result, aren't we going to take that path?

Now lets get into the functions.The subroutine were doing half of the work in Visual Basic Program, but you can also write methods and create functions (Imagine Method as an action or many actions grouped together). They are like the Sub except they can return a value. For example, if you want a piece of code to calculate the sum of two numbers and return the result you need to write a function because the sub does not return a value, but if you want to print the value only, then the sub is enough. The Subroutine and Function, they both have Input value and do something with it but the difference is that the function has an Output value. Rather than writing " Sub " we write " Function ". That tells the compiler that we need the value back. See the image bellow:

I tried this code above around 7 times till I got it right and print a result on the screen. It drove me nuts !! so don't be shocked if you faced the same thing.

Let me explain it to have a better picture.
First of all, you need to define the value of the function under the Sub Main, if you didn't then you wont have anything print of the screen. As I explained in previous lessons that Dim means Dimension, As Integer is type of data ( identify numbers ) and " = CalculateSum(3, 4)" are the numbers we want their value to return to us.

As I saw many examples on the internet and tried different codes, I figured that you need to write the function this way:

Function Sum(ByVal A As [ type of data ], ByVal B As [ type of data ] )

Like above example:
Function CalculateSum(ByVal A As Integer, ByVal B As Integer) As Integer

Write the Keyword " Function " then name it anything like " CalculateSum" , ByVal A or Car or Apple, anything you want; it is just a name. Then do the same thing after the comma.
Dim result ( or any message you want ) As [ any data type ] = ( the value you want ). By writing " Return message, or result or any name you put after the Dim word" , then the compiler will know to keep the value and return it to us ( to the Main ). End the function and run the program. The value returned by a function called result or returned value.

Let's jump to a different topic. Remember the first example I gave you about HelloWorld? Eric and Sandra? Well, we are back to it now and we will change the way we greeted Sandra and Eric. Let's re-write the correct function and add " If, Else " statement.
When we used the If statement, we were testing the value of toWhom. If toWhom is equal to Eric then the value returns one greeting and it will be like below picture.

In the If statement, the program compares the value toWhom variable against the string "Eric". So if you want to test two values to see if they are equal, you use equality operator.

There is other comparison operators in VB:

= : Equality. The expression has the value true when the left and right hand values are true.

<> : Inequality. The expression has the value true when the left hand is not equal to the right hand.

< : Less than. The expression has the value true when the left hand value has less value than the right hand value.

> : Greater than. The expression has the value true when the left hand value is greater than the right hand value.

<= : Less than or equal. The expression has the value true when the left hand value is less or equal to the right hand value.

>= : Greater than or equal. The expression has the value true when the left hand value is greater than or equal to the right hand value.

So let's say toWhom= " Eric " , then when the expression will work out, it will evaluated True, because right hand and left hand values are equal. If toWhom has other values then the expression will evaluate False. Boolean Expressions are the expressions that evaluate True or False value.

This what we should know for Functions and controlling your program. The next lesson will be about Looping and Iteration, those two things are also included on how your program should flow.

Wednesday, August 4, 2010


"I went to the zoo yesterday and took some pictures. Of course, I barley got 3 or 4 good pictures from all the 400 pictures I took. I took pictures for objects more than the animal." - Mr.Skocko - my Digital Art teacher

Here are the best 3 pictures I could share with you.

Randomly walking and captured this image from the left ( I was standing on the left side ) to make a good competition and show the rest of the way ( also the stairs ).Then I edited it and gave it black and white effect.

Does it looks like a real elephant?

That's my favorite picture. I tried like 30 times till I got the sun beams perfect like this. Especially this object was moving so it is hard to get the sun just in place. Did you notice the clouds and sky? they are kind of circular and that's because I used Fish Eyes lens. Then the rest, I just adjusted it's contrast.

Saturday, July 31, 2010

Programming- Lesson #2 ~ Mixed Variables and Errors

" Measuring programming progress by lines of code is like measuring an aircraft building progress by weight" - Bill Gates

Who would measure an aircraft building pro
gress by weight? this is just same as measuring a program by the code lines...because usually the code is so much longer than the line itself that appears on the screen. It happens alot in programmers lives especially the ones who get clients from other companies. Some clients ( that are not patient ), they tell the developers or the
programmer himself " why this takes along time to write a code fo
r !! it is only one line !!", and they don't know that it takes many hours to write codes in accurate way.

Why would we mix variables in a program while we have each type to it's own variable?
This actually confused me in the beginning, but Visual Basic allows some data types to be mixed to make it easier for the programmer so he can use different types with different variables...BUT there is an expections.. for example, if you want to declare String variable by
saying " message = 1"; although " 1 " is not a String, it is Integer..but it would understand it. But if you declare Integer as a String, it will give you an error and the computer will tell you there is a problem because you can't assign String value to Integer type. It will not be converted.

Let me give you another example of mixed data and an error types:

As we said previously, Boolean is a logical data value and String is a sequence of characters which is the word " Good ". So VB can't convert " Good " to Boolean because it is not a logical
statement, it is value type of String. It will give you bellow error:

" Dim " is a keyword that tells the computer we are about to have a variable. If you are putting more than 1 variable, then it will be easier for you if you put all Dim statements in the beginning of the Sub so you can see all the variables you would use. " Dim " stands for dimension of the space memory in the memory for variable.

While I was trying different Sub names and values, rather than putting " Sub Main " I put " Sub WrongDataType "... I thought it is ok to change the name as I read so wh
en I tried to run the program, it gave me error that the Sub Main cannot be found.
But what's right is you should put at least one Sub Main in the program, and then under it you can put other Subs with different names.

I like to share another mistake I did while learning... I wrote the " End Sub " after " End Module " which also gave me this error " 'End Sub' must be preceded by a matching 'Sub' " which means you can't put the End Sub after the Module.

Finally...when you pass value directly to WriteLine function you should put around it double quotation marks ("") but when you pass a variable you should not put double quotation marks. So now, WriteLine will know that this is a variable or a value due to the way its written.

That's it for this post...I hope I did not make it confusing and made it simple enough to be understandable. The next lesson will be about functions and how you control your program.

Friday, July 30, 2010

Programming- Lesson #1 ~ Creating Your First Program

"The function of good software is to make the complex appear to be simple" - Grady Booch

It is interesting how everything appear to us in the screen simpler than what it really is. Look now at the header of my blog, this has a long code. Look at the title of the blog, it has a code to be appeared this way. Everything on the computer, windows, and internet is edited by Computer Language, written as codes, tested and then appear to us in this way. We can use it easily by just clicking the link. I am sure almost anyone who use the computer knows this. But I like to share what I am learning now; how to program. It is not a very hard thing once you understand it; it is just like a chain. Things are linked and connected to each others...if you didn't understand the first step then you should not move on. I did not program anything yet, I am still learning and sharing my readings with you.

I first started reading about
Visual Basic Programming Language, because this language uses symantics that somehow more readable language than other programming languages which make it easier to grab and apply. So obviously, I will understand everything from the roots in a fun, easy way. You can download it from here to your computer so you can start programming. It's free! let's get started.

Open a new project to start with, but before that you need to make sure that the button "Start without Debugging" is one of the options of "Debug" in the tool bar.

This is a screenshot of the bar itself:

" Debug" is the process that trace the bugs (errors) and fix them so you use the debugger to fix them or find where they are. For example, if you have a minor mistake in the code, this debugger will fix it and tell you why the program didn't run as you expected. If you " Start Without Debugging " then the compiler will start the program without enabling the debugger to trace the errors. The next step is creating the simple program which beginner programmers always call it "Hello World!"... I found it funny, because it is really a new world (programming) to say "Hi" to.

As you insert the "Hello , world!" code between the
Sub Main , you can run the program by clicking " Start Without Debugging" in the tool bar. It is the button we added previously. And as you can see, whenever a code end or starts there should be an End Sub or Sub Main (starting the code). The "Sub" is actually a “subroutine" which what do the actual work in the program, everything in between is part of the subroutine.

Are you wondering what contains the subroutine? Well, there are two statements, they called "
Module" or "Class"…and what they do is having all the information and codes under them. And of course, there is "End Module" that ends the code. Yes, I told you everything is like a chain; everything is part of something and contains different information.

Now, you need to define the variable ( where it stores the data on the computer's memory like text, numbers, integer..etc) and specify the type of it. For example, I want to store this text value " Hello, world!". So what I do is define this variable and name it whatever I like, I would name it " World " or " Message " or any subject you want; in order to save this text in the variable, I will define it as String so I can store them on the computer. " String " is a type to define a variable, and there are many other types we will learn later on.

"Console.WriteLine" is a function in this VB Program that print the codes on the screen while running the program. So when you write Console.WriteLine(" Hello, world!") it will print " Hello , world! "on the screen. It basically print the value of the variable.

Those are the types of Variables:
1) Integer - values between -2,147,483,648 and 2,147,483,647.
2) Byte - values from 0 to 225.
3) Decimal - values with decimal places between ±1.0×10-28 and ±7.9×1028.
4) String - sequence of character.
5) Char - value of one character.
6) Boolean - logical value, True or False.

Let me give another example of different variable type.
I want to calculate the sum of 1 and 3 . Logically, they are integer, so I will use variable of type Integer. So I will write " Dim Total as Integer " - this tells the computer to define the variable as Integer type. Then I would use it as " Total = 4 ". The computer will finally calculate it and save the results as a Total variable.

And by the way, you can't use as a "Integer" value when you put quotes ("") around your variable. VB Program will receive it as a "String".

Now as you put the codes in the right places and define the variables correctly, this what you should see before running the program:

Press any key to dismiss the window.

That was our first lesson..I hope I will get to more complex programs after a while. My next lesson will be about how to use mixed Variables and deal with errors in your program.