Week 1
Week 1
1 OBJECT
ORIENTED
PROGRAMMI
NG
IN
JAVA Instructor:
Nazish Basir
TOPICS
• INTRODUCTION TO OBJECT
ORIENTED PROGRAMMING
• I N T R O D U C T I O N T O J AVA
• I N S TA L L AT I O N O F J AVA
• GENERAL STRUCTURE OF
PROGRAM
• RUNNING YOUR FIRST
PROGRAM
• VA R I A B L E S A N D D ATA T Y P E S
• LITERALS
OBJECT ORIENTED
PROGRAMMING
• Programming paradigm based on the concept of
objects, which may contain data, in the form
of fields, often known as attributes; and code,
in the form of procedures, often known
as methods.
• Object-Oriented Programming (OOP) is a
programming language model, which
uses Objects and data as core components.
• The main idea behind OOP is to represent the
data and logic with objects instead of actions or
functions.
CLASS
• The class defines the shape and nature of an
object.
• A class is the template or blueprint from which
objects are actually made.
• A class is also defined as a new data type, a
user defined type which contain two things:
– Data Member
– Methods
• A class defines the properties and behaviors for
objects.
• The description of a number of similar object is
OBJECT
• An object is an instance of a class.
• An object represents an entity in the real world
that can be distinctly identified. For example, a
student, a desk, a circle, a button, and even a
loan can all be viewed as objects.
• Object to object communication is done via
methods.
OBJECT
• Software objects also have a state and a
behavior:
– The state of an object (also known as its properties or
attributes):
– is represented by data fields with their current values.
• A circle object, for example, has a data field radius , which is
the property that characterizes a circle.
• A dog has states -- color, name, breed
– The behavior of an object (also known as its actions):
– is defined by methods.
• To invoke a method on an object is to ask the object to perform
an action.
JAVA
• Object-oriented programming language
• Resembles C++ in many respects.
• Architecture-neutral means Java program should be able
to run on a any Operating System without recompilation.
• Java programs are compiled into machine-
independent byte code which is then run within a Java
interpreter, which is responsible for executing the byte
code instructions.
• The Java interpreter is typically referred to as the Java
Virtual Machine (JVM), and it must be present on each
computer that runs the program.
COMPILING JAVA
Compiler Interprete
r
Source Code Byte Code
JAVAC JAVA Execution
(.java) (.class)
ASSIGNMENT
• Write a 2 page report about Java
History.
• Include how java changed over
time by including versions and
releases.
INSTALLING JAVA
AND WRITING AND
COMPILING YOUR
FIRST PROGRAM
VARIABLES
• Named locations in memory where you
can store values.
• Can store one data value at a time
• Can change the values
• Variable Names (Identifiers) can be
short names (like x and y) or more
descriptive names (age, sum,
totalVolume)
DECLARING VARIABLES
• Declaring means giving a name
and a data type to a variable
before it can be used.
• Syntax:
datatype identifier;
datatype identifier1,
identifier2, ..;
INTEGER DATA TYPES
EXERCISE 2:
Write down a program which has 2 variable ‘a’ and
‘b’ assign them any value and evaluate the
following equation:
x = a2 + 2ab + b2
Create variable x and Display the result of ‘x’
EXERCISE 3:
Declare two short variable and try to assign their sum to byte variable and write
down what happened.
short a=10, b=20;
short c = a+b;
Declare a long variable with value with int range then declare another long
variable with value outside the range of int. Write down what happened.
Declare a float variable like this and write down what happened.
float a = 12.54;
Declare all types of integers and floating point variables and try to give those
values outside of their range and then write down the errors.
Create 5 character variable by giving it different hexadecimal codes and write
down what characters are shown on screen.
Declare and initialize two character variable with any alphabet value. Then use
this statement to print the result and write down what happened.
System.out.println (ch1 + ch2);
END