You are on page 1of 20

Course Title: Programming Fundamentals

Course Code:COMP1112

Course Instructor: Rabia Tariq


rabiatariq.vf@ue.edu.pk
Class: MSc IT 1st

Institute: University of Education Lahore,


Multan Campus
TOOLS THAT WE WILL NEED:
 To write a program we need a platform where we
can write a code, edit, save, compile and run.
 This platform is called integrated development
environment(IDE).
 Different IDE: are available

 We will use Netbeans+JDK

 Java -version
LIFE CYCLE OF A JAVA PROGRAM:
 There are three main stages in the life cycle of a
java program.
They are:
 Editing the program

 Compiling the source code

 Executing the byte code


JVM:
 JVM is platform dependent
 The Java Virtual Machine provides a platform
independent way of executing code
 JVM interprets the byte code into the machine code
depending on the underlying operating system and
hardware combination.
CHARACTERISTICS OF OBJECT ORIENTED
LANGUAGE
 Objects: OOP provides the facility to do programming on
the basis of objects. An object is an entity that consists of
data and functions.
 Classes: A class provides the structure of the object and
define the prototype of the object. Classes are design to
create objects. Properties and functions of objects are
specified in classes.
 Real World Modeling:
OOP is based on real world modeling. As in real world,
things has state and behaviors. Similarly, an object has
data and functions.
Data Represent state and functions represents behavior.
 Reusability: object oriented programming introduce the concept to
reuse the data and the code. Inheritance is the technique that allows
a programmers to use a code of existing program to create new
programs.
 Real Life Class>Subclass

Vehicle

Car Truck Bus Motorcycle

 Wheels and motors(inherited) from vehicle


 Every subclass has its own characteristics also
Bus has (seats for many people)
truck(heavy load hauling)
 Information Hiding: OOP allows the programmers to hide
important data from the user. This concept is called encapsulation.

 Polymorphism: it is an ability of an object to behave in multiple


ways. In simple words, we can define polymorphism as the ability
of a message to be displayed in more than one form. Real
life example of polymorphism is:
Human can be > student, teacher, customer at a same time. It is
called polymorphism.

Human

Student Techer
Customer
OBJECT AND CLASS
 An object is an entity that can be anything(Person, Place or thing)
 Every Object has two things:
State and Behavior.
For Example:
1. Dog is an Object.
2. Dog state :Name, Color, Age
3. Dog Behavior: Bark, sleep, running, eat

State=Properties= charactristics =Data


Behavior=Method=Function

An object in software has data and related functions and methods.


 A class provides the structure of the object and define the prototype of
the object.
 In other words, a class defines
1. A set of attributes also called states, properties represented by
variable.
2. Set of associated behaviors which is represented by the method or
function.
In other words a class only defines the prototypes of the objects but
actually don't exist and helps to create an object. For example:

A dog is class (No Actual Existence)

Black Dog and Red Dog are objects that we create with the help of the
class.
SOURCE CODE
Source code:
 A program that is written in high level language is called source
code. It is also know as the sourse program.
 The source code cannot be executed by the computer directly. It
must be converted into object code to be executed.
Byte Code:
 Java bytecode is the instruction set for the Java Virtual Machine.
 It acts similar to an assembler which is an alias representation of
a C++ code.
 As soon as a java program is compiled, java bytecode is generated.
In more apt terms, java bytecode is the machine code in the form
of a .class file. With the help of java bytecode we achieve platform
independence in java.
LANGUAGE PROCESSORS:
 As we know that a computer understand only machine
language. A program that is written in high level
language or assembly language cannot be run by the
computer directly.
 It must be converted into a machine language first
before to execute.
 Language Processor or translators is a software that
converts a source code into a object code.
 Every language has its own translators.
1.COMPILER:
 A compiler is a program that converts the instructions of a high
level language into a machine language as a whole.
 A program that is written in high level language is called source
program.
 Compiler converts the source program into a machine code which
is known as object programs.
 The compiler checks the each statement in the course program
and generate the machine instructions.
 It also checks for the syntax error in the program. A source
program that contains an error cannot be compiled.
 Every language has its own written compiler and a compiler
translates the programs of only that language for which it is
written.
 For example C++ compiler translates only programs that are
written in C++
2.INTERPRETER:
 It is a program that converts one statement at a time.
 It executes this statement first before to translate the next
statement of the source program.
 If there are any errors in the statements. The interpreter will stop
working and displays an error message first.
 Advantage is that an error is found immediately. So the
programmer can correct the error during the program development
 Disadvantage is that it is not very efficient. It does not produce an
object program. It must convert the program each time it is
executes. Visual basics uses the interpreter.
 Compiler transforms code written in a high-level programming
language into the machine code, at once, before program runs,
whereas an Interpreter coverts each high-level program
statement, one by one, into the machine code, during program
run.
 Compiler creates objects file while assembler do not.
 Compiled code runs faster while interpreted code runs slower.
 Compiler displays all errors after compilation, on the other
hand, the Interpreter displays errors of each line one by one.
 Compiler converted high level program can be executed many
times, while interpreter converts the high level program each
time it is executed.
3.Assembler:
It is a translating program that translates instructions of an
assembly language into machine language,

Assembely
Language Program Object Program
Assembler
THE FIRST JAVA PROGRAM:
package program1;
public class Program1 {

public static void main(String[] args) {


System.out.println(“Welcome in Java");
}

}
PARAMETERS USED IN FIRST JAVA PROGRAM

 class keyword is used to declare a class in java.


 public keyword is an access modifier which represents visibility. It
means it is visible to all.
 static is a keyword. If we declare any method as static, it is known as
the static method. The core advantage of the static method is that there
is no need to create an object to invoke the static method. The main
method is executed by the JVM, so it doesn't require to create an object
to invoke the main method. So it saves memory.
 void is the return type of the method. It means it doesn't return any
value.
 main represents the starting point of the program.
 String[] args is used for command line argument. We will learn it later.
 System.out.println() is used to print statement
ANOTHER PROGRAM:
package program1;
public class Program1 {

public static void main(String[] args) {

System.out.println(“Welcome in Java");
System.out.println(“Programming is fun");
System.out.println(“Problem Driven");

}
ANOTHER PROGRAM:
package program1;
public class Program1 {

public static void main(String[] args) {


System.out.println((10.5 + 2 * 3) / (45 – 3.5));

You might also like