Professional Documents
Culture Documents
COMPUTER APPLICATIONS
Java / blue j
Modular
List of related instructions are grouped together into units called modules
Eg: C, Pascal
Object Oriented
Modeled around real world entities
Uses concept called 'class' and 'objects'
Eg: C++, Java
Java
Java is develeoped by james gosling in 1995. Earlier name was oak. Java is widely used
programming language. It is an oop(object oriented programming) language. Java has both
interpreter as well as compiler.
Types of program :
1. Standalone( java application)
2. Internet applets(java applets)
Features of java
1. Oop language
2. Uses both compiler and interpreter
3. Platform independent
4. Wora(write once run anywhere) – works on any operating system.
5. Case sensitive
6. Contains standalone and applet programs
7. Portable
8. Secure
9. Mutithreaded
10. Robust ( doesn’t crash easily)
Features of oop
1. Stress given to data rather than functions
2. Programming is implemented using object.
Source code
The program code(java programs) written by a programmer. After compilation java creates object
code(byte code)
Byte code
Java program is compiled in to an intermediate code.
Compiler
NASIR UMER
COMPUTER APPLICATIONS
Translates whole source program to object program(machine language) not line-by-line. Translates the
program all at once and then display the list of errors.
Interpreter
Translates source program to object program line-by-line. Halts at the occurance of an error during the
translation of source code to object code.
Blue j
Is a free java environment(user interface) developed by monash university, australia.
It is an editor which allows to write, edit and run java program.
Features of blue j
Gui
Easy to use
Editor, debugger and output terminal
Tokens
It’s a smallest individual unit. Token could be remember with the word kilos ie keyword, identifier,
literal, operator and separator
Keyword
Are the reserve words of java that can’t be used as an identifier. It conveys speciall meaing to the
compiler. Examples switch, public, private, if, else etc.
Example :
final : variable becomes fix(constant) .
Identifier
Name given to class, function, objects and variables etc defined by the programmer.
Rules for naming identifier
A. Should start with an alphabet, dollar($) or _(underscore) sign.
B. Cant contain special word except dollar($) and _(underscore) sign.
C. Can contain digits but cannot start with the digit.
D. No blank spaces
E. It is case sensitive.( lower and upper case characters treat differently)
F. No reserve words are allowed
Literals
Are fixed(constant) values which don’t change during program execution. Literals are assigned to the
variables.
NASIR UMER
COMPUTER APPLICATIONS
Operators/operands
Are mathematical symbols/characters used in an experssion to compare, calculate or concatinate(join). In
the below example + and equal are the operators.
Examaple c = a+b
Details are given below
Operands
The quantity on which an operation is to be done.in the above example a and b are the operands.
Separators/punctuators
Are special symbols(characters) which define the structure of a program
A) () used in functions, test expression in decision statements etc.
B) {} used for block of statements for loop, if else etc.
C) [] used in arrays
D) : used in switch case
E) ; used in loop to terminate the statement.
F) , used in variables declaration of same data type.
G) . Used for package import and method call through object.
Variables
Reserve locations in the computer memory to store literals. Types are same as of literals. A=10; a is a
variable and 10 is literal(constant).
Question :
Give the output
1. Int a=10, b=20, c=0; a= b; b=a;a=c;b=a;system.out.println()(a + “ “+ b+” “+c);
2. Int a,b,c,d; a=15; b=a; c=b;system.out.println()(a + “ “+ b+” “+c);
3. Int p,q,r; p=10;q=20;r=30; p=q;q=r; r=p+q; system.out.println()(p + “ “+ q+”
“+r);
Data types
Type of literals(constant) and the amount of space to be allocated is called dt. Divided into two types.
A) Primitive : byte, short, int, long, float, double, char, boolean
Data type bytes intital(default) values
Byte 1 0
Short 2 0
Int 4 0
Long 8 0l/0l
Float 4 0.0f/0.0f
Double 8 0.0d/0.0d
Char 2 ‘\u0000’
Boolean 1 false
Static initialization :
Variables are declared first then literals/values are assigned.
Example : int a,b,c; a=10; b=20 ; c= a+b;
Dynamic initialization :
Literals/values assigned along with the declaration of variable .
Example : int a=10 , b=20 , c= a+b;
Compound statement
A group of zero or more statement written between a pair of curly brackets(block) is called compound
statement.
{
Statements;
}
NASIR UMER
COMPUTER APPLICATIONS
Import
to open any library import keyword is used which allows to use classes and functions of that particular
library class(package)
Example import java.util.*;
In this example all classes and functions etc. Will be imported in the memory when program is executing.
Since java is object oriented one must create an object of the class to access the properties and function of
that class.
Operators
A) Arithmetic
B) Relational
C) Logical
D) Assignment
C) logical operators :
Set logical relationship between two numbers/variables
&& - and : both the condition must be true to get output true.
|| - or : any one condition can be true to get output true.
! – not : negates an expression
Object
Unique run time entity. Instance of a class. Identifiable entity with some characteristics and behaviour.
Memory is allocated when object is created. An instance/object is created with the help of new operator
which creates memory to it.
Encapsulation(data hiding/class
Wrapping up of data and function into a single unit is called encapsulation(class)
Class
Is a blue print of an object. Is a set of similar objects. Class is prototype that defines the variables and the
methods common to all objects. No memory is created to a class.
Abstraction
Act of representing essential features without including background details
Polymorphism(function overloading)
NASIR UMER
COMPUTER APPLICATIONS
Is the process of using many function with the common name but with different parameters / data types /
arguments. Can be implemented through function overloading / construction overloading
Inheritance
Properities/behaviour of one class can be used(inherit) in another class. The class from which
properties/methods are derived is called as base/parent/super class. The class deriving is called
derived/child/sub class. The keyword extends is used for inheritance.
Class(object factory)
Without class object cant be created. Many objects are created from the class which is a template for the
objects.
4) System.out.println(50+(int)(math.random()*51));
Random numbers between 50 to 100(including 100)
Round() : returns nearest rounded integer value.
Sysemtem.out.println()(math.round(10.40)); --- 10,
system.out.println()(math.round(10.50)); --- 11
Sqrt() : returns square root.
Sysemtem.out.println()(math.sqrt(9)); --- 3.0
Comments
Comments are remarks given by the programmer. It is ignored by the java compiler. The are the
used give briefdescription about the statements. They are of the following types
a) Single line comment (//) : generally used to explain logical steps in short.
Examplesystem.out.println()(“nasir”) // displaying names
b) Multi line comment (/* */) : generally used to explain logical steps of the small
module.
N=ob.nextint();m=ob.nextint();
If(n%m==0)
Sysemtem.out.println()(“n is divisible by m”);
/* accepting 2 numbers
Displaying n is divisble by m if remainder is 0*/
c) [not in the portion ]documenting comment /** */ : generally used to provide brief
documentation along with the program like name of the programmer, date of updation
etc.
Are used to check conditions if condition is true then statements are executed. Condition is evaluated by
the java as true or false based on the input from the user.
1) If else if - else:
Are decision making statement is used to execute statement/s when the condition specified is
true/correct.
Syntax
If(condition)
{ statement/s;
}
Else if(condition)
{
statement/s;
}
Else
{
statement/s;
}
In the above example java start the condition from if, if if is false control comes to else-if part, again
false then else part.
NASIR UMER
COMPUTER APPLICATIONS
2) Nested if :
in if there is another if statement is called nested if.
example
if(age >=18)
{
if(gender==’m’)
{
sysemtem.out.println()(“you are male, u can particiapte”);
}
}
switch case
Are used to check conditions , unlike if else swtich case only checks(compare) constant values ie not
relational and logical values. Only character and integer variables/values can be used. Case is an option
that needs to be executed based on the value of the control variable with the switch.
Control variable : used along with swtich. Example swtich(x), x is control variable.
Break statement is used after each case if break is not used then, from beginning all the cases will be
executed until break is found – this process is called fall through.
{
public static void main()
{
Int ch = 10;
Switch (ch)
{
case 5 :
sysemtem.out.println()(“five”);break;
Case 10 :
sysemtem.out.println()(“ten”);break;
Default:
sysemtem.out.println()(“wrong input”);}
}}}
2.
Class sw2
{
public static void main()
{
Int ch = 10;
Switch (ch)
{
Case 1 :
Case 3 :
sysemtem.out.println()(“odd values”);break;
Case 2 :
Case 4 :
sysemtem.out.println()(“even values”);break;
Default:
sysemtem.out.println()(“wrong input”);
}}}
3.
Class sw3
{
public static void main()
{
Int ch = 10;
Switch (ch)
{
case 1 :
sysemtem.out.println()(“five”);
Case 10 :
NASIR UMER
COMPUTER APPLICATIONS
sysemtem.out.println()(“ten”);
Default:
sysemtem.out.println()(“wrong input”);
}}}
The java ternary operator functions like a simplified java if – else statement. Java ternary
operator is the only conditional operator that takes three operands. Use ternary operators only
when the resulting statement is short.
Loops / iteration
Repetition of statement/s for several time or till condition is true. Loop is called iteration. There are three
types of loop.
1. For
2. While
3. Do---while
For
Also called entry controlled loop. It checks the condition first then executes the statements which
are in the block of for loop. Variable used in the loop is called control variable. The loop starts
with the initial value assigned to the variable in the loop, loop continues till the condition in the
loop is true.
Example:
Condition and initial expression ---- for(i=1; i<=5;)
Condition - multiple expression ----- for(int i=1,a=2;i<=5; i++,a++ )
Bodyless / null loop --- for(i=1; i<=5;i++ ){ }
Endless/infinite loop ---for(i=1;i<=10;) or (; ;)
Delay loop ---- for(i=1;i<1000000;i++){}
While
NASIR UMER
COMPUTER APPLICATIONS
Also called entry controlled loop. It checks the condition first then executes the statements which are in
the block of while loop. The loop start with the initial value assigned to the variable before the start of
loop , loop continues till the condition in the loop is true.
Example
Int i=1;
While(i<=10)
{
system.out.println(i);
i++;
}
Do---while
Also called exit controlled loop because even if the condition is false loop runs atleast once. It checks the
condition at the end , first executes the statements which are in the block of do - while loop. The loop start
with the initial value assigned to the variable before the start of loop , loop continues till the condition in
the loop is true.
Example
Int i=1;
Do
{
system.out.println()(i);
i++;
} while(i<=10);
or
}
While(i<=10);
NASIR UMER
COMPUTER APPLICATIONS
Jump statements
Help in jumping(transfering) control of the program from one to another.
Example : contine, break and return
Continue : transfers control to the next iteration in the loop.
for(i=1;i<=5;i++)
{
if (i==3)
cotinue;
Else
Print i;
}
Break
Jumps out of the loop or switch block.
For(i=1;i<=5;i++)
{
if (i==3)
break;
Else
Print i;
}
NASIR UMER
COMPUTER APPLICATIONS
Loop within anonther loop is called nested loop. Programs stops if the outer loop becomes
false(outer loop gets over)
Example working note
For(i=1;i<=2;i++) i j
{ 1 1
For(j=1;j<=3;j++) 2
{ 3 when i is 1 j loop will run 3 times, when i is
System.out.println()(j); 2 1 2 j loop will run 3 times. Program
will
}} 2 stop when i will beome 3.
3
----------------------------------------------------------------------------------------------------------------
2
For(i=1;i<=2;i++) i j
{ 1 1 when i is 1 j loop will run 1 time, when i is
For(j=1;j<=1;j++) 2 1 2 j loop will run 1 time. Program will
{ stop when i will beome 3.
System.out.println()(j);
}}
---------------------------------------------------------------------------------------------------------------------
-----
For(i=1;i<=1;i++) i j when i is 1 j loop will run 3 times,
{ 1 1 program will stop when i will beome 2.
For(j=1;j<=3;j++) 2
{ 3
System.out.println()(j);
}}
NASIR UMER
COMPUTER APPLICATIONS
Is the process of converting one data type to another types. They are the of the following types :
1) implicit data conversion
2) explicit data conversion
1) implicit (coercion / type promotion / auto cast ) : is automatic conversion done by the
compiler
ie lowest data type to highest data type ( lower precision to higher precision data type ) .
Ollowing is
the order of higher to lower precision data types
double - float – long – int – short – byte – char
Example :
int x=10, y=20; float z = 0.0f;
z = x+y;
System.out.println(z);
output
30.0
output
30
Example :
Int z=0 ;
String p="5";
Z = integer.valueof(p); // or integer.parseint(p)
System.out.println(z+z);
Output
10
Ascii code
Ascii is an acronym for american standard code for information interchange. It is a code that uses
numbers to represent characters. A upper and lower case character are assigned different
numbers. Following is the table of ascii code that represent alphabets, numbers and special
characters.