Lesson 01 Introduction MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Lecturer in Management & IT M.Sc. In IS (SLIIT), PGD in IS (SLIIT), BBA (Hons.) Spl. in IS (SEUSL), MCP
Programs Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them. Programs are written using programming languages. MIT 12043 by S.Sabraz Nawaz 2
Programming Languages Machine Language Assembly Language High-Level Language Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code, so you have to enter binary codes for various instructions. Programming with native machine language is a boring process. Moreover the programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this: 1101101010011010 MIT 12043 by S.Sabraz Nawaz 3
Programming Languages Machine Language Assembly Language High-Level Language Assembly languages were developed to make programming easy. Since the computer cannot understand assembly language, however, a program called assembler is used to convert assembly language programs into machine code. For example, to add two numbers, you might write an instruction in assembly code like this: ADDF3 R1, R2, R3 MIT 12043 by S.Sabraz Nawaz 4
Programming Languages Machine Language Assembly Language High-Level Language The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that computes the area of a circle with radius 5: area = 5 * 5 * 3.1415; MIT 12043 by S.Sabraz Nawaz 5
Popular High-Level Languages COBOL (COmmon Business Oriented Language) FORTRAN (FORmula TRANslation) BASIC (Beginner All-purpose Symbolic Instructional Code) Pascal (named for Blaise Pascal) Ada (named for Ada Lovelace) C (whose developer designed B first) Visual Basic (Basic-like visual language developed by Microsoft) Delphi (Pascal-like visual language developed by Borland) C++ (an object-oriented language, based on C) C# (a Java-like language developed by Microsoft) Java MIT 12043 by S.Sabraz Nawaz 6
How a Computer Program Works The Program you write is executed by the CPU Since the CPU can only understand machine code your program needs to be converted into machine code MIT 12043 by S.Sabraz Nawaz 7
How a Computer Program Works Dim radius as integer Dim area as integer radius = txtradius.text area = 22/7*radius*radius msgbox area Source Code C8 B7 23A7 D7 AA 89 DD 89D23 A9 F4 A9... Machine Code MIT 12043 by S.Sabraz Nawaz 8
How a Computer Program Works Programmer writes the code in a high level language such as C++, Java, Visual Basic etc. Then using a software tool this high level code is converted into machine code The software tools that does the conversion to machine code comes in two flavours o Compilers o Interpreters MIT 12043 by S.Sabraz Nawaz 9
Compiling Source Code A program written in a high-level language is called a source program. Since a computer cannot understand a source program. Program called a compiler is used to translate the source program into a machine language program called an object program. The object program is often then linked with other supporting library code before the object can be executed on the machine. MIT 12043 by S.Sabraz Nawaz 10
Compilers and Interpreters Compiler o A compiler converts your entire program to machine code at one go o After compilation you have an executable file in machine code e.g. an EXE, COM file for PCs Interpreter o An Interpreter converts your program to machine code one instruction at a time MIT 12043 by S.Sabraz Nawaz 11
Developing Programs that run on Multiple Platforms You need to find a language that is supported by different platforms e.g. Visual Basic is only for PCs, C, C++ on the other hand are available for any type of computer MIT 12043 by S.Sabraz Nawaz 12
Developing Programs that run on Multiple Platforms We say that languages like C, C++ are portable at the source level o i.e. Since these languages have standards the program you write (source code) could be compiled on different platforms with little or no modifications MIT 12043 by S.Sabraz Nawaz 13
What makes Java Special? Java is platform independent at the binary level o i.e. If you write a program in Java and compile it you could run your compiled code on any type of computer When you compile a java program it does not produce machine code for a specific type of computer MIT 12043 by S.Sabraz Nawaz 14
How Java works? It instead creates machine code for a general virtual computer o This is referred to as java byte code o There are Java Interpreters available for almost all types of computer platforms To run your Java program you need the Java Interpreter installed in that computer It would convert the java byte code to the specific machine code of your computer MIT 12043 by S.Sabraz Nawaz 15
How Java works? MIT 12043 by S.Sabraz Nawaz 16
Program Development Process Text editor Saves Java statements Source code (.java) Java compiler Produces Byte code (.class) Java Virtual Machine Results in Program Execution MIT 12043 by S.Sabraz Nawaz 17
A Simple Java Program MIT 12043 by S.Sabraz Nawaz 18
Creating, Compiling, and Executing a Java Program MIT 12043 by S.Sabraz Nawaz 19
The Compiler and the Java Virtual Machine Most compilers translate source code into executable files containing machine code The Java compiler translates a Java source file into a file that contains byte code instructions Byte code instructions are the machine language of the Java Virtual Machine (JVM) and cannot be directly executed directly by the CPU MIT 12043 by S.Sabraz Nawaz 20
The Compiler and the Java Virtual Machine Byte code files end with the.class file extension The JVM is a program that emulates a micro-processor The JVM executes instructions as they are read JVM is often called an interpreter Java is often referred to as an interpreted language. MIT 12043 by S.Sabraz Nawaz 21
Java Virtual Machine The Java Interpreter is more popularly known as the Java Virtual Machine (JVM). It is also known as the Java Runtime This is available with the operating system of most computers today MIT 12043 by S.Sabraz Nawaz 22
Java Virtual Machine (a) Java source code is translated into bytecode. (b) Java bytecode can be executed on any computer with a Java Virtual Machine. MIT 12043 by S.Sabraz Nawaz 23
Sample Program MIT 12043 by S.Sabraz Nawaz 24
PRACTICAL WORK Example done for you
We use Java SE 7, Sun s latest version of Java language Basic types of Java programs. Java console applications Java GUI applications Java applets
Requirements.. In order to write a Java program we need: o o o An editor A Java compiler A Java Runtime Environment/Java Platform Editor Notepad, WordPad, etc. Java Compiler and Platform Sun s Java Development Kit
Java Development Kit This is a software package designed to help with the complete process of developing and running Java programs. It combine the functions of Compiler Interpreter Applet viewer Debugger
Example 1 Write a program to print, Hello, Welcome to MIT 12043 This program demonstrates how to write a simple java program.
Exercise 1 class Hello { public static void main(string[] args) { System.out.println("Hello, Welcome to MIT 12043"); } }
Example 1 class Hello { public static void main(string[] args) { System.out.println(" Hello, Welcome to MIT 12043 "); } } Name of Class
Example 1 class Hello { public static void main(string[] args) { System.out.println(" Hello, Welcome to MIT 12043 "); } } { - begin } - end
Example 1 class Hello { public static void main(string[] args) { System.out.println(" Hello, Welcome to MIT 12043 "); } } Main method (function). Java will perform the commands under this method.
Example 1 class Hello { public static void main(string[] args) { System.out.println(" Hello, Welcome to MIT 12043 "); } } Semi Colon ; indicates end of a command
Example 1 class Hello { } public static void main(string[] args) { System.out.println(" Hello, Welcome to MIT 12043 "); } Print Command in Java What ever in quotations will be printed as it is. i.e. Hello, Welcome to JAVA
Example 1 class Hello { public static void main(string[] args) { System.out.println(" Hello, Welcome to MIT 12043 "); } } Java like C and C++ is case sensitive i.e. if you type the first part of the as Public static void.. Your program will not work
Example 1 class Hello { public static void main(string[] args) { System.out.println(" Hello, Welcome to MIT 12043 "); } }
Compiling a Java Program To Compile your Java Program use the JDK s javac tool. E:\myjava>javac Hello.java The compiler would create java byte code. These files have an extension of.class
Running a Java Program To run your Java Programs you need to call the Java Interpreter. E:\myjava>java Hello
Example 2 Write a program that produces the following output Hello World! It's been nice knowing you. Goodbye world!
Example 2 - Solution 2 class HelloWorld { public static void main (String args[]) { System.out.println("Hello World!"); System.out.println("It's been nice knowing you."); System.out.println("Goodbye world!"); } }
Example 2 - Solution 2 class HelloWorld { } public static void main (String args[]) { } System.out.println("Hello World!\nIt's been nice knowing you.\ngoodbye world!"); \n is for new line
Exercise 1 Write a Java program using one println() statement to print your name and age on the screen. Your program should format the output as follows. Hello! I am SaNa. I am 36 years old.
Try these out Ex 01 MIT 12043 by S.Sabraz Nawaz 44
class DrawPicture { public static void main (String args[]) { System.out.println("* * * * * * "); System.out.println("* * * * * * * "); System.out.println("* * * * * * * *"); System.out.println("* * * * * * * * * *"); } }
MIT 12043 by S.Sabraz Nawaz 46
End of Lecture