You are on page 1of 18

NASIR UMER

COMPUTER APPLICATIONS

Java / blue j

A computer program is a collection of instructions that performs a specific task when


executed by a computer
Programming language is a unique vocabulary and a set of rules for writing computer
programs. It is nothing but set of instructions with their own protocols/rules known as
Syntax(correct way of writing program).

Computer Language Types


Machine/ Low Level Language
Written in binary code (0,1)
Only language understood by the computer
Eg: 1010011110001
Assembly Language
Instructions are written using symbolic names
Assembler software converts assembly language to machine language
Eg: STA B, SUB D
High Level Language
Uses English like words
Interpreter of compiler converts this to machine language
Machine independent
Eg: Java, C++, C

Types of programming styles


Non- structured
List of instructions which perform some action
Difficult to read the program at once because it is lengthy
Eg: Basic, Fortran

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

Object Oriented Concepts


Object and Class
A class is a set of similar things (blueprint to create similar things) like say a set
of fans
The object can be a small fan, a big fan, a table fan etc. (similar type under one
class)
It has attributes and functions
Eg: Attributes (Properties)- Roll_no, Age, Gender etc.
NASIR UMER
COMPUTER APPLICATIONS

Functions- sleep(), play(), eat(), study() etc

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.

Jvm(java virtual machine)


Byte code is independent of the machine on which the program is run. Jvm is a virtual processor which
processes the byte code to machine code instruction which is uniform across all the platform.

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.

Jdk (java development kit - libraries)(api – application programming interface)


Also called java libraries contains functions

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

Types : integer, real, character, string and boolean.

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).

Questions : write the program along with the mnemonic codes.


1. Store name, rollno, gender and class of a student and print them.
2. Store rollno, name and marks of 3 subjects. Calculate total and average. Print rollno,
name, total and average.
3. Store height and base of a triangle. Calculate and print area.
4. Store length and breadth of a rectangle. Calculate and print area and perimeter.
5. Store side of a square. Calculate and print area and perimeter.
NASIR UMER
COMPUTER APPLICATIONS

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

Float -- 32 bits – 232 – upto 6 decimal places


Double -- 64 bits – 264 – upto 15 decimal places

B) Non-primitive(reference)/composite/udt-user defined data type


Class, array, string and interface
Non primitive type are called reference data types. They are
Based on fundamental(primitive) data types.

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

Input / accepting data from the user


Streams(sequence of data) in java are input or output information. Stream classes like scanner,
inputstreamreader and bufferedreader etc are stored in util package and io package respectively.

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.

Package / library class


A package/library is a collection of a related classes. It contains functions that can be used by the
programmer.

Scanner ob = new scanner(system.in);


In the above example object is created of class scanner to accept data from keyboard.

Operators
A) Arithmetic
B) Relational
C) Logical
D) Assignment

a) arithmetic operators : help in performing arithmetic calclations.


+ addition : add two numbers
- subtraction : subtract one number from another number
* multiplication : multiply two numbers
/ quotient : divide one number by another number
% modulus(remainder) : divide one number by another number and returns remainder

b) relational operators : help in setting realtion between two


number/variables and returns result in true or false.
< : a<b
> : a>b
== : a==b
<= : a<=b
>=: a>=b
!= : a!=b ( ! Not)
NASIR UMER
COMPUTER APPLICATIONS

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

D) assignment operators : assign value to a variables.


= : a=10;
+=: a+=10 : a=a+10
-=
*=
/=
%=
Above operators are also called shorthand / condensed operator.

Oop( object oriented programming)


Principles of oop
Object
Class
Encapsulation
Abstraction
Polymorphisc
Inheritance

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.

Maths class and its functions


Math class is a class of package lang which includes various functions for caluclation. Following
are functions which are the part of portion.
Abs() : returns absolute value.
Sysemtem.out.println()(math.abs(-10)); --- 10
Floor() : returns the largest whole number/ value that is less than or
equal to the argument.
System.out.println()(math.floor(10.6)); 10.0
system.out.println()(math.floor(10.2)); 10.0
Ceil() : returns the smallest whole number/value that is greater than
or equal to the argument.
System.out.println()(math.ceil(10.6)); 11.0 ----
system.out.println()(math.ceil(10.2)); 11.0
Max() : returns largest value.
System.out.println()(math.max(10,3)); --- 10
Min() : returns smallest value .
System.out.println()(math.min(10,3)); --- 3
Pow() : returns power, number raised to another number.
Sysemtem.out.println()(math.pow(10,2)); --- 100.0
Cbrt() : returns cube root.
Sysemtem.out.println()(math.cbrt(27); --- 3.0
Random() : returns random values.
1) System.out.println(math.random());
Random number between 0 – 1(excluding 1)
2) System.out.println((int)(math.random()*2));
random numbers between 0 – 2(excluding 2)
3) System.out.println(1+ (int)(math.random()*51));
Random numbers between 1 to 50(including 50)
NASIR UMER
COMPUTER APPLICATIONS

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.

Escape sequence characters


Are non printing characters. These characters are used with the help of ‘\’(backslash) they cannot be used
directly in the program like enter key, backspace key etc.

\n : new line (same as pressing enter key)


\t : tab (same as pressing tab key)
NASIR UMER
COMPUTER APPLICATIONS

\” : double quotes system.out.println("\”nasir\""); “nasir”


\’ : single quote system.out.println("\’nasir\’"); ‘nasir’
\f : form feed ( to clear the previous output(terminal window)
\\ : back slash system.out.println("\\nasir\\"); \nasir\

decision making/ conditional / selection statements


1. If else –if else and nested if
2. Switch
3. Ternary operator (?) / conditional operator

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”);
}
}

If-else if / if else ladder

if else if ladder without if else if ladder


If(age <18) if(age <18)
system.out.println()(“ children not allowed”); system.out.println()(“ children not
allowed”);
Else if(age <25) if(age >=18 && age <25)
system.out.println()(“ allowed one time”); system.out.println()(“ allowed one time”);
Else if(age <45) if(age >= 25 && age <45)
system.out.println()(“ allowed two time”); system.out.println()(“ allowed two time”);
Else if(age >=45)
system.out.println()(“ allowed unlimited”); sysemtem.out.println()(“ allowed
unlimited”);

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.

Example : give the output


1.
Class sw1
NASIR UMER
COMPUTER APPLICATIONS

{
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”);
}}}

Ternary operator(conditional operator) ?

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.

Syntax / general form :

Variable x = (expression) ? Value if true: value if false;

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

Nested loop ( pattern programs )

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

Data conversion ( type casting ) :

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

2) explicit ( type casting / force cast ) :


explicit conversion done by user through type casting
operators from higher to lower precision data types.
Example :
float x=10.50f, y=20; int z = 0;
z = (int) (x+y);
System.out.println(z);

output
30

Conversion of string to primitive and vice-versa

1) conversion of string to primitive data type :


string x = “123” ;
Int y;
Y = integer.valueof(x);
(or)
Y = integer.parseint(x);
NASIR UMER
COMPUTER APPLICATIONS

Example :
Int z=0 ;
String p="5";
Z = integer.valueof(p); // or integer.parseint(p)
System.out.println(z+z);

Output
10

2) conversion of primitive data type to string :


double x = 123 ;
String y;
Y = string.valueof(x);
(or)
Y = x+””;
(or)
Y=double.tostring(x)
Example :
Int x = 123 ;
String y;
Y = string.valueof(x);
System.out.println(y+y);
Output
123123

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.

a - z 65 to 90 (26 alphabets 65+25=90


a - z 97 to 122
0 - 9 48 to 57
special characters 32 to 47(32 is blank space)

You might also like