Learning Programming with Sammy Hegab

Page 1

RGU:Software Development Society

0010101011110011010101010110000111 { Learning Programming; }

00101010111100110100010101011000010 By: Ahmed Sammy Hegab founder of umicom.com

Introduction I've created this short course for the absolute beginner someone who has no idea about software development but is keen to learn, we will cover the main fundamental principles of programming Why Learn Programming? You may have your own personnel reason but here is a few I thought of myself • •

Learning a programming language can be fun specially when you start seeing your development come to life It can be financially lucrative as technology advances with the likes of iPhone development, Android and new web technologies you can potential sit at home create a new App and see it bring in few hundred to few thousand every month while you sleep. Learning Programming can really benefit you in your journey of personnel development, to write code you have to be a good planner, creative, methodical, patient, problem solver and many more..

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


Which Language should I learn? There are hundreds if not thousands of programming languages out there, so which one should you learn. For this reason I will give you a quick intro in the main five programming languages, C/C++, C#, Objective C, JAVA and PHP In general each language was developed for a specific purpose, for example PHP is a very good easy to learn web development scripting language, its most common for most of the websites JAVA is great for network programming and now its the language for Android development and if you want to get into iPhone Development then you must know Objective C C++ is universal one of the oldest Object based languages which is in much use today across all fields, more on Object and non Object languages little later Then comes C# a Microsoft creation simplifying C++ language and enriching it with .NET frameworks making it a very powerful language used in developing advanced applications main strength for developing windows based applications, you can also develop web applications but they will need to run on a Windows based server which can be expensive, unlike PHP which can run on free Open Source Apache Server. What is common across all languages? The common between all languages is the C language, which was developed first then shortly after they introduced the Object Orientated concepts and called it C++, the fundamental building block in C is exactly the same across all languages as they all inherited from C language. For this reason some recommend learning C language first. However from this course we will cover the concepts more than writing codes which once you understand programming concept everything else should start to become easy and with this knowledge you can pick up other programming languages with speed. Difference in programming languages We will cover the differences then you can start to class languages based on their types Non-object programming languages I.e. C, Pascal, etc.. are examples of what is known as a procedural languages or linear programming languages. Non-Object programming languages can be interpreted or compiled this is also common in Object Programming language too. An Interpreter type – Executes one line at a time, if an error occurs it stops at the line with the error. 1. A Compiler type language – Converts all the source code into mainly binary format then runs or in the case of a language like JAVA it converts into Byte-code which gets executed with a JVM (Java Virtual Machine)

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


Programming Grammar (Syntax) Just like any language there are some fundamental grammar in programming we need to adapt to inorder for our instructions to execute. Just like when we end a sentence with a period, full stop '.' The Semi colon ( ; ) In all programming languages we end with a semi colon ';' The Curly Brace ( { } ) You will see the open curly bracket this would be start of a block of code, it will make sense when we start to go through programming building blocks but for now all I want you to know is when ever you see an open '{' you will have the end of block curly brace '}' The Quotation Marks ( “ ' ) In programming most strings of text are surrounded by quotation marks you can use single or double they have the same purpose but different effects depending on the purpose you use them for for the moment all I want you to remember is when ever you open a “ you must have an end “ of same time and if you wanted to for example to print out a “ you if you simply write “ “ “ it would cause an error because the compiler or interpreter will find the start of quote and end of quote then complain about the third quotation mark with no end. Escape Character ( \ ) For the case of quotations or having the program to ignore an effect of any kind of character you would use the escape character '\' for example “ \” “ the program will ignore the middle quotation mark. Comments ( \\ /* */ # <!-- --> ) Comment characters can be used in the code as a way to leave comments for other programmers and yourself, those comments will be ignored and have no effect on the actual program when executed. In most programming languages a single line comment is \\ followed by the comment if you want to leave a large block comment then you would include it in /* write a block comment */ Html comments would be between <!-- --> <!-- write a block comment here -- > Perl uses # hash symbol for comments so if you ever end up looking at a Perl script and find the # now you will know its only a comment.

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


Variables A variable is basically a container, just imagine a box that contains certain type of object in computer memory, this can be a number, a string or characters and we will look into all the kind of values and objects a variable can hold as we progress in the course. The main value types we will cover are different types of numbers (whole number, decimal numbers), boolean values (true/false), characters, strings and we will also cover a variable that contains several values of a certain value type, which is known as an array. When declaring a variable you would define its data types that you intend for it to hold, for example int i; int means integer which is whole number value type such as 1,2,3 and in the above statement we are declaring the 'i' to be a variable that would hold whole value numbers. Variables can be any name as long as they start with letters and not numbers. It is also good to give meaningful names when creating variables. Please note the only languages which we would be covering in this course that does not need any declaration of data type is PHP the web scripting languages. As an example of what variables look like in PHP is as follows $i; we simply put a $ dollar sign in front of a word and it becomes a variable name. What ever value we assign it, it will take that value type dynamically. More on Data Types Char means Character data type for example a variable that would hold characters like a,b,c or any other keyboard character. String is a collection of characters, words and would include spaces too etc.. double, decimal, float are decimal point numbers such as 1.5, 2.99, 5.4956 etc.. Boolean or bool take up the value of true or false All the above are common examples of primitive data types User defined data type The other data type is user defined data type also known as reference type (pointer) lets look at the difference between a normal variable data type and a pointer int x =5; int is the data type-array x is the variable name 5 is the value incase of reference type (pointer) RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


int * p=5; which basically means p is a pointer which points to the value in memory Dynamic data structures Other than the usual primitive data types and pointers we can also create all sorts of dynamic data structures such as: 1. Stack (FILO = First in last out) 2. Queue (FIFO = First in First out) 3. Linked List ( each node will hold data and pointer to the next node) 4. Double Linked List ( each node will hold data and pointer that points both ways) 5. Circular linked lists ( a chain of nodes) For the moment we are taking an overview and we will cover more of these concepts in a more advanced course. Now we covered some basic concepts lets now look next at printing on screen results and then we can dive into the fundamental building blocks also known as program constructs. Printing results to screen 1. Java System.out.println(“The Salary is “+S); 2. C# System.Console.writeLine(“The Salary is {0}”,s); 3. C Printf(“The salary is %d \n”,s); 4. C++ (you can use the same as c depending if you include in your project stdio.h library file or use the following) cout <<”The Salary is “<<s; 5. PHP echo (“The salary is”, $s); 6. Objective C NSLog (@”The Salary is %d \n”,s); What are the programming building blocks? Building blocks in programming also known as programming constructs which are common in syntax in all programming languages are really just four. We will briefly introduce it now and then elaborate further later on. 1. Conditions : The IF condition, ELSE and ELSE IF If statement is used to test for a condition, if the condition is true do something else if not do something else. if (condition) { do something….. } else { do something else … }

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


2. Loops We have four different types of loops we can use in our program which perform a repeat of a block of code a number of times while a condition is not true. 1. For loop for (int i=0; i<5; i++) { do something....until ' i ' is not less than 5 } 2. While loop : while a condition is true do something While ( x<10) {… do something and until x is not less than 10...} 3. Do …. While loop Do while loop is very similar to the while loop but just opposite way of doing test, it will execute a block of code then test for condition at the end. Do { ….do something … } while (x <10) 4. Foreach loop Used with an array list or database entries, so for each array element do something y={1,2,5,7}; foreach (int x in y) {… do something } 3. Switch / case statement One of the four cases can be true otherwise do some default case. int x = 4;

in php it would be $x=4;

switch (x) { case 1: …. do something break; case 2: …. do something break; case 3: …. do something break; default: …. do something for a default break; } 4. Exceptions (Error Handling) try { do something like try connect to database } catch (exception e) { .. print the exception or do something to continue with program ...}

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


What is Object Orientated Programming Language? Before we dive into programming building blocks (which is common across all major programming languages) lets first get a brief overview of what is Object Orientated Programming Languages. Object Language fall into the same category of either an interpreter based language or a compiler based language, very simply Object Language is procedural language + Object Orientated Concepts. For example like I mentioned before about C language plus Object concepts is C++ language. Objective C the languages used for Apple Mac, iphone, ipad development is also just C Language plus Object concepts. While you might be thinking C++ is the same as Objective C so learning one of those languages would be sufficient to program in both, while it can be true in the fundamental building blocks (the non object coding) it is not true for the Object programming syntax as it is completely different. The concept of programming is the same only the implementation of Object concepts can be different and we will look into that in more detail as we progress in the course. What is an Object? An object is simply the encapsulation of properties and methods. Everything can be classified as an object, for example a Pen is an object The properties of a pen can be the colour, length, make, weight The function of the pen is it writes, this purpose is what we term as the method Methods can be a function when it is required to return a value when executed or it can be a procedure if it just performs a task and does not return a value. Object Orientated Concepts There are five main concepts in object orientated programming languages which we will briefly introduce and discuss further in the course 1. Encapsulation We already mentioned that encapsulation is the very nature of an object (The encapsulation of properties and methods (function or procedures). 2. Inheritance Inheritance is when an Object inherits some properties / methods or characteristics from another Object which is known as a super class or a parent object. 3. Overloading Overloading is when you overload a method with a different signature lets assume we have two methods which are both called Get-salary, one will take in hours worked and wage_per_hour and other Get-salary takes in four parameters I.e. it can also take in overtime_hours and overtime_rate.

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


4. Overriding Overriding is using same method which we inherited from a parent object but the way we do the calculation would be different, for example lets assume for an employee we take in the hours worked and multiply it to the hourly-rate but in the came of a manager we also add a bonus amount. 5. Polymorphism An Instance of an object taking many forms, for example Manager object taking characteristics of an employee or creating a manager with an employee object. Object Orientated Programming : Encapsulation We will demonstrate the concept in five different languages, so lets get started :) Creating an Object (JAVA, C# & C++) Public class employee { public int employee_number; public string employee_name; public double getSalary(int hour, double rate) { return hour * rate; } } Note ** In C++ the difference is simply we add a semi-colon at the last curly brace }; Creating an Instance of the Object (constructor in Java, C#, C++) Public static void main() { employee e1 = new employee(); // constructor e1.employee_number=5; e1.employee_name=�bob; double s=e1.getSalary(40,20.25); // Put printing statement here based on the language you are using }

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


Creating Object in Objective C @interface employee:NSObject { int employee_number; string employee_name; } -(double) getSalary:(int)hour andRate:(double)rate; @end @implementation employee -(double) getSalary:(int) hour andRate:(double)rate { return hour * rate } @end Creating an Instance of the Object (constructor in Objective C) int main() { NSAutoreleasepool *pool:[[NSAutoreleasepool alloc] init]; employee *e1=[[employee alloc] init]; [e1 employee_number:5]; [e1 employee_name:@”Bob”]; double s=[e1 getSalary:40 andRate:20.25]; NSLog(@”The salary is %d””, s); [e1 release]; [pool drain]; return 0; } Creating Object in PHP <HTML> …. <BODY> <? class employee { $employee_number; $employee_name; function getSalary($hour, $rate) { return $hour*$rate; RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


}

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


Creating an Instance of the Object (constructor in PHP) $employee = new employee(); $eno=$employee->employee_number=5; $ename=$employee->employee_name=”Bob”; $s=$employee->getSalary(40,20.25); echo (“The salary “.$s); ?> …... </BODY> </HTML> Object Orientated Concept 2: Inheritance (C++, C#, Java, OBJECTIVE C, PHP) Lets assume we will create a new object called Manager and a manager has all the characteristics of an employee because a manager is an employee Java public class manager extends employee { public double bonus; } In the above we are basically saying that this new object type called manager inherited everything from employee plus a manager has the property called bonus, because only managers get bonus ontop of salary. PHP class manager extends employee { $bonus; } C# public class manager:employee { public double bonus; } C++ public class manager::employee { public double bonus; }; RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


Objective C @interface manager:employee { double bonus; } @end Object Orientated Concept 3: Overloading Like we explained before overloading is when you have one or more objects with the same method except for the signature. for example public double getSalary(int hour, double rate) { return hour * rate; } public double getSalary(int hour, double rate, int overtime_hours, double overtime_rate) { return hour * rate; } Depending on the signature the users passes then the program will select the appropriate method to use. Object Orientated Concept 4:Overriding Like overloading we will use identical method name but this time we will have getSalary in employee exactly same as getSalary in manager Public class employee { public int employee_number; public string employee_name; public double getSalary(int hour, double rate) { return hour * rate; } } Public class manager extends employee { public double bonus; public double getSalary(int hour, double rate) { return hour * rate+bonus; // notice here we add bonus when calculating getSalary } RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


}

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


Object Orientated Concept 5:Polymorphism Poly means many so here we can have an object taking many forms as it goes through stages the best way we can understand this is an employee can become a manager and then can be promoted to director. Lets assume we have two objects, an employee object and a manager object which inherits from employee object public static void main() { employee emp1, emp2,emp3,man4; // now we will create instances of the emp1,emp2,emp3,man4; emp1=new employee(); emp2=new employee(); emp3=new employee(); man4=new manager(); } Database basics (SQL) Now we briefly covered the main concepts in programming, we will briefly look at database SQL language (Structured Query Language), in java, c#, c++, php, objective c and other languages you can write programs that will communicate with a database system and you can also pass SQL commands. Here we will learn the common used SQL commands/keywords –

Create a database in SQL create database staff;

To see existing databases show databases;

To select a database (I.e. the database we just created called staff) use staff;

To create a table

create table employee(employee_id int, employee_name varchar(20), employee_address text, primary key(employee_id)); –

To add records to the table we just created insert into employee(001, “Sammy”, “65 Naylor Road”);

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


If we want to see all records select * from employee

To add an extra field to the employee table alter table employee add NI varchar(10);

To update a record in the table update employee set employee_name = “Imran” where employee_id=1;

Deleting unwanted records delete from employee where employee_id=1;

Assuming we wish to drop just part of the table for example we no longer need to hold National Insurance information we can use the following statement alter table employee drop NI;

To delete the whole table and/or delete the whole database we can use the following drop table employee; drop database staff;

Lets imagine we hold accumulated hours worked in the table and we want to list only the employees that have worked over 1000 hours at the company since they started we can do the following select * from employee where total_hours_worked > 1000;

What you should know 1. The difference between types of languages interpreter, compiler, object and non object languages. 2. Basic programming syntax I.e. { } // ; 3. Programming Constructs 4. The five Object Orientated Programming concepts 5. You should now be able to create a basic object in Java, C#, C++, Objective C and PHP 6. Basic Database commands Now you can proceed to learn any specific language with the basic background in general programming you been taught. I will soon create other course titles such as the following, please contact me to express your interest Learning C, Learning C++, Learning Java, Learning PHP, Learning Objective C, Learning Python, Learning JavaScript and many more titles to come soon...

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


Add me on www.facebook.com/softwaredevelopmentsociety Or you can email on admin@umicom.com or visit www.umicom.com

RGU:Software Development Society

- Introduction to programming - Ahmed Sammy Hegab


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.