Professional Documents
Culture Documents
Comp Smart Notes
Comp Smart Notes
COMPUTER NOTES
LESSON 1
Concept of OOPs
Object-oriented programming: Object-Oriented Programming or OOPs refers to languages that uses objects
in programming. Object-oriented programming aims to implement real-world entities like abstraction ,
inheritance, encapsulation , polymorphism etc in programming. Java is an example of object oriented
language.
Ques What are the basic OOP (object oriented programming) concepts? OR Name OOPs principles
Ans : The basic OOP concepts are Abstraction, Encapsulation, Inheritance, Polymorphism
ICSEIMPNOTES
Ques How are objects implemented in software terms? Ans
The objects are implemented in software terms as
(i) Characteristics (attributes ) are implemented through member variables . (ii)
Behaviour is implemented through member functions called methods.
(iii) The values of these member variables defines the state of an object
(iv) Data and methods are encapsulated into one unit and given a unique name to give it identity.
ICSEIMPNOTES
Ques. Why are classes and object needed ?
Ans: Classes are defined as a blueprint of its object. We need classes because they contain all the statements and
functions that can be used by the objects of the class.
Objects are the instances of class .They are needed because they are used to access the data member and the member
function of the class without disturbing the actual structure of the class.
INTRODUCTION TO JAVA
Java is a powerful object oriented third generation programming language It was developed in 1991 by
James Gosling at Sun Microsystem by the name Oak and later renamed as Java.
Platform refers to the combination of hardware and system s/w . For eg. Windows NT on Intel Pentium 4.
Source Code: The program /code written in any language by the programmer to solve a problem is called source code.
Compilation : The process of converting source code into machine code is called compilation.
Java Byte code: When java programs are compiled, the java compiler converts the source code into platform
independent code called Java byte Code. This byte code is further interpreted and executed by JVM.
JVM: Programs written in Java are compiled into Platform independent JBC which is then interpreted by a
special Java interpreter for a specific platform. This java interpreter is known as JVM.
(ii) /*………comments………..
………………………… ……….*/ Multiline comments
ICSEIMPNOTES
Values and Types
Character set: is set of valid characters that a language can recognize. Java uses
Unicode character set. Unicode is two-byte character set.
Keyword
identifier
class Area
Punctuators/Seperator
{
void main( )
{ Literal/constant
int l=2, b=3, a;
a=l*b;
operator
System.out.println(“area=”+a);
}
1. Keywords: are the words that convey a special meaning to language compiler.
These are reserved for special purpose and cannot be used as an identifier. For
e.g. if, int , float, do, while, for etc .
2. Identifiers: Identifiers are the name given to different parts of program e.g.
variables, function, classes etc
Identifier forming rules of Java state the following:
a) Can contain alphabets, digits, dollar sign and Underscore.
b) Must not start with digit.
c) Cannot be Java keywords.
d) Can be of any length and are case sensitive i.e upper case and lower case
letters are treated differently.
ICSEIMPNOTES
3. Literals : Literals are the constants .Constant is a value that remains fixed.
Java allows several kinds of literals
a) Integer Literals : Integer constants are the whole numbers without any decimal
parts.
An integer constant must have at least one digit and cannot contain a decimal
point. It may contain a + or – sign. A number with no sign is interpreted to be
positive. No other symbol is allowed. For eg int x=2; here 2 is integer literal
(ii) Octal Integer Literal (base 8): is sequence of digits starting with 0 (zero).It
can contain numbers from 0 to 7.
For e.g 012
(ii) Hexadecimal Integer Literal (base 16): is sequence of digits starting with
0X.It can contain numbers from 0 to 9 and A to F. For e.g 0XC.
c) Boolean Literal : The Boolean type has two values, represented by literals
true and false.
boolean x=true;
Operators : are special symbols that carry out certain pre-defined operations / tasks .On
the basis of operations carried out, operators can be categorized as
Arithmetic operators e.g +. -,*,\ Relational
operators e.g >, <, <=, >=, != Logical
operators e.g && , | | , ! .
Data type : are the means of to identify the type of data and associated operations
of handling it .
Java supports eight types of primitive datatypes
(1) Primitive Datatype (Intrinsic, predefined or fundamental) : Primitive
datatype are the basic datatypes. Java supports following eight types of Primitive
datatypes
(i) Numeric Integral types
The numeric datatypes that are used to store numeric values fall under this
category .There are four numeric integral types in java.
Type Size Range Default Value
byte 1 byte -128 to +127 0
short 2 byte -32,768 to +32,768 0
int 4 byte -231 to 231-1 0
( - 2,147,483,648 to
2,147,483,647)
L (or l) suffix on an integer means integer is of long datatype thus 546 is int
datatype but 546L is long datatype
ICSEIMPNOTES
(ii) Fractional Numeric Data type(or floating datatypes): can store
fractional numbers i.e numbers having decimal points.
Type Size Range Default Precisio
Value n Digits
float 4 bytes -3.4E+38 to +3.4 6 digits
0.0f
E+38
double 8 bytes -1.7E+308 to +1.7 15 digits
0.0d
E+308
Reference datatype (or derived Datatype) are constructed from primitive datatypes for
e.g classes, array etc.
Default value of reference datatype is null.
ICSEIMPNOTES
Initialization of variables: Assigning an initial value to the variable while
declaration. datatype varname=constant value; eg int a=5;
Dynamic initialisation: The value assigned to a variable during run time
is called dynamic initialization. For eg. if int a=5.0,b=4.0;
double c=Math.pow(a,b) , here value of c is dynamically initialized by the
return value.
Variable Scope : Refers to the program region within which the variable is
accessible.
Class xyz
{ Variable of outer block are
accessible within inner block.
void main( )
Variable of inner block is not
{ accessible in outer block
int a,b;
for(……….)
Scope { Scope of
of p,q int p, q; variable a,b
}
}
ICSEIMPNOTES
Operators in java
Operators : are special symbols that carry out certain pre-defined operations /
tasks .On the basis of operations carried out, operators can be categorized as
Arithmetic operators are used to perform Arithmetic operations e.g +.-, *, \ (division
gives quotient), % (reminder )
For eg 7/3=2, 7%3=1
Relational operators are used to perform relational operations e.g >, <, <= , >= ,= =, ! =
and results in true or false.
For eg
System.out.print(5>2)
Output will be true.
Logical operators are used with relational expressions to combine them eg. && ,||, !
Logical AND Operator( && ): It results into true if all its operands are true
Logical Not ( ! ) :is an unary operator and negates the value i.e. true
becomes false and vice versa Eg !(5==5) will results into false.
Relational operator = = is used to check whether the values on both its sides are equal
or not.
Java Operators can be classified as unary , binary or ternary based on the number
of operands .
ICSEIMPNOTES
Q What are the potential problems in type
conversions? Ans Potential problems are as
follows
Conversion Problem
Bigger floating type to smaller floating type Loss of precision
(e.g double to float)
Floating point to integer type Loss of fractional part.
Bigger integer type to smaller integer type Original
value may be out of range for the target type
leading to the loss of information.
Associativity : When the operator have same precedence rating than associativity rule
determines the order of evaluation.
ICSEIMPNOTES
Ques What is rvalue & lvalue
Ans rvalue : data value stored at some location in memory is
rvalue lvalue (location value ): address in memory at
which its data value is stored . a
for eg
5
(rvalue)
8088 (lvalue)
2xy 2*x*y
|x| Math.abs(x)
𝒙𝒚
(x*y)/z
𝒛
√x Math.sqrt(x)
Math.cbrt(x)
𝒙𝒚 Math.pow(x,y)
𝒆𝒙 Math.exp(x)
sin(x) Math.sin(x)
cos(x) Math.cos(x)
tan(x) Math.tan(x)
sin(2x) Math.sin(2*x)
𝐬𝐢𝐧−𝟏 𝒙 Math.asin(x)
𝐜𝐨𝐬−𝟏 𝟐𝒙 Math.acos(2*x)
𝐭𝐚𝐧−𝟏 𝟐𝒙 Math.atan(2*x)
Math.ceil(5.3)= 6.0
Math.ceil(5.7)= 6.0
Math.ceil(-5.3)= -5.0
Math.ceil(-5.7)= -5.0
Math.ceil(5.0)= 5.0
Math.floor(5.3)= 5.0
Math.floor(5.7)= 5.0
Math.floor(-5.3)= -6.0
Math.floor(-5.7)= -6.0
Math.floor(5.0)= 5.0
Math.abs(-5.5)= 5.5
Math.abs(5.5)= 5.5
Math.rint(7.5)= 8.0
Math.rint(7.3)= 7.0
Math.rint(-7.8)= -8.0
Math.rint(7.0)= 7.0
Math.round(7.5)= 8
Math.round(7.3)= 7
Math.round(-7.8)= -8
Math.round(7.0)= 7
Math.random()= 0.4579719825200854
Math.random()= 0.9869581480576484
Math.random()= 0.3398321903307846
Math.min(5.5,6.9)= 5.5
Math.max(5.5,6.9)= 6.9
Math.min(5,6)= 5
ICSEIMPNOTES
Conditional Constructs in Java
Three construct that govern statement flow are :- 1.
Sequence : means statement are executed sequentially
2. Selection : means the execution of statements depending upon a test condition.
3. Iteration : means repetition of set of statements depending on the test condition .Till
the time a condition is true set of statement repeats again and again . When condition
becomes false the repetition stops.
Selection statements
if statement : syntax
if(expression)
statement;
Syntax of if-else statement
if (test condition)
{ statement1;
…
} else {
statement3;
…
}
The { } can be ignored if only a single statement is associated with either part.
Ques : WAP to calculate the area of rectangle or circle according to user’s choice.
import java.util.*;
public class AreaCal
ICSEIMPNOTES
{
void main( )
{
double l,b,r,ar,ac;
int ch;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 to calculate Area of rectangle \nEnter 2 to calculate Area of
Circle");
System.out.println("Enter choice ");
ch=sc.nextInt();
switch(ch)
{ case
1:
System.out.println("Enter length");
l=sc.nextDouble();
System.out.println("Enter breadth");
b=sc.nextDouble();
ar=l*b;
System.out.println("Area of Rect="+ar);
break;
case 2:
System.out.println("Enter radious of circle");
r=sc.nextDouble(); ac=3.14*r*r;
System.out.println("Area of Circle="+ac);
break; default:
System.out.println("Invalid Input");
}
}
}
OUTPUT
Enter 1 to calculate Area of rectangle
Enter 2 to calculate Area of Circle
Enter choice
1
Enter length
2
Enter breadth
3.0
Area of Rect=6.0
----- ----- ------
Enter 1 to calculate Area of rectangle
Enter 2 to calculate Area of Circle
Enter choice
ICSEIMPNOTES
2
Enter radious of circle
2.0
Area of Circle=12.56
Ques : WAP to calculate the area of rectangle or circle according to user’s choice.
(same program but here we have taken choice of user as char input)
A tech number has even number of digits. If the number is split in two equal
halves, then the square of sum
of these halves is equal to the number itself. Write a program to input and
check a four digit tech numbers. Example :
Consider the number 3025
Square of sum of the halves of 3025 = (30+25)
= (55)
(55)2= 3025 is a tech number.
Solution.
class TechNumber n x y s
{
public void main (int n)
3025 30 25 55
{ int x,y,s;
x=n/100; y=n%100; s=x+y;
if((int)Math.pow(s,2)==n)
System.out.println(n+” is a tech
no”); else
System.out.println(n+” is not a tech no”);
}
}
Write a program to calculate commission for the salesman. The
commission is calculated according to following rates :
ICSEIMPNOTES
Sales Commission Rate
class commission
{
public void main(double sales)
{
double com=0;
if(sales>=0 && sales<=5000)
{
com=0;
}
else if(sales>5000 && sales<=12000)
{
com=sales*3.0/100;
}
else if(sales>12000 && sales<=22000)
{
com=sales*7.0/100;
}
else if(sales>22000 && sales<=30000)
{
com=sales*10.0/100;
}
else if(sales>30000)
{
com=sales*15.0/100;
}
System.out.println("Commission = "+com);
}
}
ICSEIMPNOTES
Write a program in java to perform the following calculation of
Library fine :
For the first 5 days late Rs. 1 per book per day
For the next 5 days Rs. 2 per book per day
Late more than 10 days Rs. 5 per day per book
class libraryfine
{
void main(int d)
{
double fine=0.0 ; if
(d<=5) fine=d*1.0;
if (d>=5 &&
d<=10)
fine=5*1+(d-5)*2.0;
if (d>=10) fine=5*1.0+5*2.0+(d-
10)*5.0;
System.out.println(“Fine=”+fine);
}
}
ICSEIMPNOTES
Iterative Constructs in Java
Ques What are iteration statement? Name them.
Ans. Iteration statements (also known as looping statements or loops)are the
statements that allows a set of instructions to be executed repeatedly till
some condition is true. They are of three types for, while & do while.
Ques What are the elements needed to control the loop
Ans (i) The initialization expression (ii) The test expression (iii) The update
expression (iv) The loop body.
Initialization Statement : It is a statement that initializes the controlling variable with legal initial value.
Test Condition/ Test Expression : It is the expression which checks for a condition and executes the
loop until the condition becomes false.
Updation Statement : The statement which changes the value of the controlling variable is called
updation statement.
Body of the loop : contains the set of statement to be executed repeatedly .
[Controlling Variable : The variable which is responsible for the flow of the control within the loop is
called controlling variable. ]
Ques What is meant by entry controlled loop? Which java loops are
entry controlled ?
Ans An entry control loop is one for which the test expression is evaluated
before each iteration. The
for( initialization exp; test exp; update expression)
{body of loop} and the while
(condition) {body of the
loop} are the entry
controlled loops.
Q What is meant by exit controlled loop ? Which java loops are exit
controlled loop?
Ans. An exit control loop is one for which the test expression is evaluated after
each iteration. This loop always execute at least once.
do
{
body of the loop} }while
(condition); do while is the exit
controlled loop.
Q What do you mean by nested loop?
A A loop inside another loop is called nested loop.
ICSEIMPNOTES
For eg eg for(int i=0;i<5 ;i++)
{
for(int j=0;j<5 ;j++)
{
System.out.print(j);
}}
System.out.print(i );
break;
continue statement : is used to skip rest of the statement after it and takes the
control to the next iteration of the loop. For eg int i ;
for(i=1;i<=4;i++)
{ if(i=
=3)
continue;
System.out.print(i );
}
Here output will be
1
2 4 when i= =3, since continue will be executed then it will take the control
to the next iteration i.e i++, and the statement below it will be skipped thus
3 will not be printed.
ICSEIMPNOTES
Q Give similarity and differences between while and do while statement
Ans: while and do while both are iterative statements Following are the
differences between two
While do-while
In while loop, test expression is In do-while loop, test expression is
evaluated first and if in the evaluated in last thus it will be
beginning condition is false do loop executed at least once even if the
will not work. condition is false
while is an entry control loop do while is an exit control loop
}
if(c==2)
System.out.println(n+ " is prime");
else
System.out.println(n+ " is not prime");
}}
Write a menu driven class to accept a number from the user and check
whether it is a Prime or a Perfect number.
(a) Prime number— (a number is a Prime which is only divisible by 1
and itself ). Eg 7,13 etc
(b) Perfect number— (a number is called Perfect if it is equal to the
sum of its factors other than the number
itself.) Example : 6 = 1 + 2 + 3
import java.util.*;
class number
{ ICSEIMPNOTES
public static void main()
{
int ch,n,c=0,i,s=0;
Scanner sc=new Scanner (System.in);
System.out.println(“Enter 1 to check prime \n 2. to check perfect
no”); ch=sc.nextInt();
switch(ch)
{ case
1:
System.out.println(“Enter the Number :”); n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
System.out.println(n+ " is prime");
else
System.out.println(n+ " is not prime");
break;
case 2:
System.out.print(“Enter the Number :”);
n=sc.nextInt();
for(int i=1; i<n; i++)
{
if(n%i==0)
{ s=s+i;
}
} if(s==n)
System.out.println(n+“ is Perfect Number”); else
ICSEIMPNOTES
System.out.println(n+“ is Not a Perfect Number”);
break; default:
System.out.println("Invlid Input");
}
}
WAP to input a no and print its sum of digits.
For Eg Input 125
(1+2+5)
Output
8
WAP to input a no and print the sum of even and odd digits separately. Eg
Input 1245
Sum of even digits=6
Sum of odd digits=7
WAP to input a no and print the sum of first and last digit.
Eg
Input 2347
Output 9
ICSEIMPNOTES
n f
public class sumfirstlast1
{ 2347 7
234 4
public void main(int n) 23 3
{ 2 2
0
int s=0,f=0,l=n%10;
while(n>0)
{
f=n%10;
n=n/10;
}
s=f+l;
System.out.println("sum of first and last digits= "+s);
}
}
oR
public class firstlast
{ n=2347
c=4
public void main(int n)
{
2347/103
int t=n,c=0,f,l=n%10; =2347/1000
while(t>0) =2
{ c++; t=t/10;
}
f=n/(int)Math.pow(10,c-1);
System.out.println("sum of first and last digits= "+(f+l));
}
}
ICSEIMPNOTES
WAP to input a no and check whether it is an
automorphic no or not
152 =225 (15 is not an automorphic no)
52 =25 ( 5 is automorphic no)
252 =625( 25 is automorphic no
}
ex=b%(int)Math.pow(10,c);
if(a==ex)
System.out.println(a+" is Automorohic no");
else
System.out.println(a+" is not an Automorohic no");
}
}
//WAP to print the sum of following series x/1 + x2/2 +x3/3+…… xn/n
ICSEIMPNOTES
public class series4
{
public void main(int n,double x)
{ int
i,j,f;
double s=0.0;
for(i=1;i<=n;i++)
{
s=s+Math.pow(x,i)/i;
}
System.out.println("sum of series="+s);
}
}
WAP to print the sum of following series x/1! - x2/2! + x3/3! -…… xn/n! public
class series5
{
public void main(int n,double x)
{ int
i,j,f;
double s=0.0;
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=i;j++)
{
f=f*j;
}
if(i%2==0) s=s-
Math.pow(x,i)/f;
else
s=s+Math.pow(x,i)/f;
}
System.out.println("sum of series="+s);
ICSEIMPNOTES
}}
WA
P to
print
all
perf
ect
num
bers
from
a to
b
6=1+2+3 , 6 is a perfect no
}
}
}
ICSEIMPNOTES
WAP to print all prime numbers from a to b
ICSEIMPNOTES
WAP to print all the armstrong numbers from a to b
153 is Armstrong no
ICSEIMPNOTES
A tech number has even number of digits. If the number is split in two equal
halves, then the square of sum of these halves is equal to the number itself.
Write a program to print all four digit tech numbers. Example :
Consider the number 3025
Square of sum of the halves of 3025 = (30+25)
= (55)
(55)2= 3025 is a tech number.
ICSEIMPNOTES
WAP to check whether a no is a special no or not
145 is a special no //0+120+24+1
145=1!+4!+5! n=n/10;
=1+24+120 }
if(s==t
)
public class specialno t n r f s
{ 0
public void main(int n) 145 145 5 120 120
14 4 24 144
{ r will hold
1 1 1 145
int i,f,s=0,r,t=n; extracted
while(n>0) numbers first time 5,then 4
{ then 1
r=n%10; f=1;
Calculating factorial of
for(i=1;i<=r;i++) r , for eg if r=5
{ f=f*i; f=1*2*3*4*5= 120
} s=s+f;
System.out.println(t+" is special no");
else
System.out.println(t+" is not a special no");
}
}
ICSEIMPNOTES
Write a program to compute and display the sum of the
following series:
𝟏+𝟐 𝟏+𝟐+𝟑 𝟏+𝟐+𝟑+⋯..𝒏
+ +………
𝟏∗𝟐 𝟏∗𝟐∗𝟑 𝟏∗𝟐∗𝟑∗……𝒏
class series
{
public static void main (int n)
{
double s=0.0,nr,dr;
for(int i=2; i<=n; i++)
{
nr=0.0; dr=1.0;
for(int j=1; j<=i; j++)
{
nr=nr+j;
dr=dr*j;
}
s=s+nr/dr;
}
System.out.print("Sum of series="+s);
}
}
class xyz
ICSEIMPNOTES
{ Output
public static void main () 1 1
{ int i,j; 1 2
for( i=1; i<=3; i++)
{
2 1
for( j=1; j<=2; j++) 2 2
{ 3 1
System.out.println(i+” “+j); 3 2
}
}
} }
1
12
123
1234 12345
class Pattern
{
public static void main ()
{ int i,j;
for( i=1; i<=5; i++)
{
for( j=1; j<=i; j++)
{
ICSEIMPNOTES
System.out.print(j);
}
System.out.println( );
}
}
}
1
22
333
4444
55555
public class Pattern
{
public static void main( )
{ int i ,j; for (
i=1;i<=5;i++ )
{
for( j=1;j<=i;j++)
ICSEIMPNOTES
{
System.out.print(i);
}
System.out.println();
}
}
}
*
**
***
****
*****
class Pattern
{
public static void main ()
{ int i,j;
for( i=1; i<=5; i++)
{
for( j=1; j<=i; j++)
{ ICSEIMPNOTES
System.out.print("*");
}
System.out.println( );
}
}
}
WAP to print the following pattern
55555
4444
333
22
1 public class
Pattern1
{
public static void main( )
{ int i,j;
for ( i=5;i>=1;i-- )
{
for( j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
} ICSEIMPNOTES
}
}
ICSEIMPNOTES
WAP to print the following pattern
12345
2345
345
45
5
1 ICSEIMPNOTES
23
456
7 8 9 10
Class Pattern
{
public static void main ()
{ int i,j,k=1;
for( i=1; i<=4; i++)
{
for( j=1; j<=i; j++)
{
System.out.print(k+" ");
k++;
}
System.out.println( );
}
}
}
ICSEIMPNOTES
WAP to print the following pattern
****1
***12
**123
*1234
12345
class P6
{
public static void main ()
{
int i,j,k;
for( i=1; i<=5; i++)
{
for( k=4; k>=i; k--) //loop for printing *
{
System.out.print("*");
}
for( j=1; j<=i; j++) //loop for printing numbers
{
System.out.print(j);
}
System.out.println( );
}
}
}
ICSEIMPNOTES
WAP to print the following pattern
1
12
123
1234
12345
class P6
{
public static void main ()
{
int i,j,k; for(
i=1; i<=5; i++)
{
for( k=4; k>=i; k--)
{
System.out.print(" ");
}
for( j=1; j<=i; j++)
{
System.out.print(j);
}
System.out.println( );
}
}
}
ICSEIMPNOTES
WAP to print the following pattern
A
BC
DEF
GHIJ
KLMNO
class Pat7
{
public static void main ()
{ int i,j;
char ch='A';
for( i=1; i<=5; i++)
{
for( j=1 ;j<=i; j++)
{
System.out.print(ch);
ch++;
}
System.out.println( );
}
}
}
ICSEIMPNOTES
Q Explain break and continue statement with example
Ans : break statement terminates the loop and takes the control out of the loop
to the first statement after the loop . break statement is used in while , do
while, for and in switch statement. In switch case statement it is used to skip
subsequent case conditions. For ex:
int i ;
for(i=1;i<=4;i++)
{ if(i=
=3)
System.out.print(i );
break;
continue statement : is used to skip rest of the statement after it and takes the
control to the next iteration of the loop. For eg int i ;
for(i=1;i<=4;i++)
{ if(i=
=3)
continue;
System.out.print(i );
}
Here output will be
1
2 4 when i= =3, since continue will be executed then it will take the control
to the next iteration i.e i++, and the statement below it will be skipped thus
3 will not be printed.
ICSEIMPNOTES
Write Output
class breout
{
public static void main ()
{ int i,j;
for( i=1; i<=3; i++)
{
for( j=1; j<=3; j++)
{
if(i==j)
break;
System.out.println(i+" "+j);
}
WORKING
} 1 1 break ×
} 1 2 ×
1 3 ×
} 2 1
2 2 break ×
2 3 ×
3 1
3 2
3 3 ×
OP:- 16
3.0 19
3 ICSEIMPNOTES 20
Write Output
class breout
{
public static void main ()
{ int i,j;
for( i=1; i<=3; i++)
{
for( j=1; j<=3; j++)
{
if(i==j)
break;
System.out.println(i+" "+j);
}
break;
}
}
}
ICSEIMPNOTES
Write Output
class contout
{
public static void main ()
{ int i,j;
for( i=1; i<=3; i++)
{
for( j=1; j<=3; j++)
{
if(i==j)
continue;
System.out.println(i+" "+j);
}
WORKING
} 1 1 continue ×
} 1 2
1 3
} 2 1
2 2 continue ×
2 3
3 1
3 2
3 3 continue ×
ICSEIMPNOTES
Output
12
13
21
23
31
32
LESSON- 2
CLASS AS THE BASIS OF ALL
COMPUTATION
ICSEIMPNOTES
An object may be considered to be a material with
physical existence.
• Objects are distinguished from one another as
their sets of values of the attributes or variables are
different.
• They are different on the basis of their states.
• The states of two objects cannot be same.
Two integral features of every object are- State and
Behaviour.
Collection of a number of items into a single entity or
capsule is referred as Encapsulation.
Tokens are the smallest individual units of a
Java program. Example-
i. Literals
ii. Keywords iii. Identifiers iv. Operators
v. Punctuators.
Literals are data items which have constant or fixed
data values. Example-
i. Float
ii. Integer
iii. String iv. Character
ICSEIMPNOTES
v. Boolean
vi. Null literals
Reference variables are data locations which store
memory addresses of reference datatypes.
Initialization is the process of assigning a specific
initial value to a variable. It can be done in three ways-
i. Using a literal. Example- int x=5
ii. Using another variable as reference.
Example- byte y=1 int x=y iii. Calling a method and
using its return value as the initial value (dynamic
initialization)
Initial value-
• 0 for byte, short, int
• 0 l for long
• 0.0 f for float
• 0.0 d for double
• Null character (‘\u 0000’) for char
• False for Boolean
• Null for references type data
Instance variables or object variables are those
attributes which differentiate one object from another.
Illustration: ICSEIMPNOTES
Class studentTenB
{
String name; Int
age;
Int rollno.;
}
The instance variable in this class rollNo. As no two
students in this class can have same rollNo. But names
and ages of two objects may be identical.
Function of new operator- to create new objects
corresponding to a class.
Function of instanceOfoperator checks whether the
first operand (an object) is an instance of the second
operand (a class).
FORMAT: <name of object> instance of <name of a
class>
In Java Library, Math class is included in Java.lang
package. Some mathematical available in Math
class:
Function Description Example
ICSEIMPNOTES
PI Returns the value 3.142…
(double) of π
Log (x) Returns the (double) Log(2)=0.302…
value of log of x w.r.t
base e (natural log.
Returns the absolute
Abs (x) Abs(9.32)=9.32
value of any numeric
type data. Abs(-9.32)=
9.32
ICSEIMPNOTES
Pow (x,y) Returns the (double) Pow (3,4) = 81
value of x,y
Round (x) Rounds up to the Round (8.42) =
nearest integer 8
Round (8.74) =
9
Floor (x) Returns the integer Floor (27.92) =
which is less than or 27
equal to x
Ceil (x) Floor (5.07) = 5
Returns the smallest Ceil (2.25) = 3
integer greater than
or equal to x Ceil (2.79) = 3
LESSON-3
User Defined methods(Functions)
Syntax of Function Definiton
Functionname(v1,v2,…);
ICSEIMPNOTES
Ques: What are formal parameters ?
Ans : Formal parameters are the parameters that appear in function definition.
A function that does not change or modify the A function that change or modify the state of
state of their parameters received are pure their parameters received are Impure function
function
In call by value any change in formal parameter In call by reference any change in formal
is not reflected in the actual parameters parameter is reflected in the actual parameters
Primitive data type are passed in call by value Reference data type are passed in call by
reference
ICSEIMPNOTES
Ques : What is function signature?
Ans Function signature refers to the number and types of arguments passed and is a part of function prototype.
void area (int l,int b) here int l,int b is Function signature
Ques: Why do you think that function overloading must be a part of object oriented language? Ans: A function
overloading is a part of an object oriented language as it is the feature that implements polymorphism in an object
oriented language that is the ability of an object to behave differently in different circumstances.
Ques: Are function with the same name and same function signature but different return type are allowed in java?
Ans Function with the same signature & different return type are not allowed in java.
For eg int area(double a) double area(double b) // error,
since same signature.
Ques: Write the function prototype of the function abc( ) which takes two integer variables as its arguments and
returns a value of double datatype
Ans : double abc(int a,int b)
Ques Write a function call statement for the function whose prototype is void xyz(int a,double b) Ans xyz(a,b);
Ques Write java statement to compare 50 & 500 by calling the function void compare(int m,int n) Ans
compare(50,500);
Public access method “increment” which accepts an object of Myclass type as parameter
and does not return any value.
Ans. public void increment(Myclass ob)
3. Protected access method largest which accepts a float type, int type and
double type data as parameters and have a byte type as return type.
Ans. protected byte largest(float a, int b, double c)
4. Public access static method calculate which accepts a byte and int type
data type as parameters and return float type.
Ans. public static float calculate(byte b, int d)
5. Write the function prototype for the function “sum” that takes an integer
variable (x) asits argument and returns a value of float data type.
Ans. float sum(int x)
6. Write the prototype of a function which takes in 2 integers and 1 String arguments
and returns a value which is either ‘true’ or false’.
Ans. boolean function(int a,int b,String c)
ICSEIMPNOTES
Write the output of the following when main function is called public class
abc1 Output when main()
{ is called
void main( )
{ i am in fun
fun();
System.out.println("i am in main"); fun(); i am in main
System.out.println("i am back in main");
i am in fun
}
void fun( ) i am back in main
{
System.out.println("i am in fun");
}
}
WAP to create a function sum( ) which prints the sum of two numbers. Call this function from main() class xyz
{
void main()
{
sum();
}
void sum()
{
int a=2,b=3,s; s=a+b;
System.out.println(“Sum=”+s);
}}
Here sum() is a function without parameter and without return type
WAP to create a function void sum(int a, int b ) which receives two integer numbers
and prints the sum of two numbers. Call this function from main()
class xyz import java.util.*;
class xyz class xyz
{
{ {
void main()
void main(int x, int y) { void main()
{ sum(2,5);
{
sum(x,y); int x,y;
} Scanner sc =new Scanner (System.in);
} void sum(int a, int b)
void sum(int a, int b) System.out.println(“Enter 2 nos”);
{ x=sc.nextInt();
{ int s=a+b; y=sc.nextInt();
int s=a+b; System.out.println(“Sum=”+s); sum(x,y);
System.out.println(“Sum=”+s); } }
} } void sum(int a, int b)
} {
int s=a+b;
System.out.println(“Sum=”+s);
Here void sum(int ,int ) is a function wi th parameter and without return type }
}
WAP to create a function int sum(int a, int b ) which receives two integer numbers and returns the sum of two numbers. Call this
function from main() and print sum in main function.
ICSEIMPNOTES
class xyz
{
void main(int x, int y)
{
int k=sum(x,y);
System.out.println(“Sum=”+k);
}
int sum(int a, int b)
{ int
s=a+b;
return(s);
}
}
Here int sum(int a, int b) is a function with parameter and with return type
{
if(a>b)
return(a);
else
if(b>a)
return(b);
else
return (-
1); ICSEIMPNOTES
}
ICSEIMPNOTES
WAP to create a Function int isprime(int n) which
WAP to create a Function boolean
returns 1 if n is prime else returns 0.
isprime(int n) which returns true if n isNow
primecall this function from another function
else returns false. Now call this function from ) and print the message whether a number
main(
is a prime no or not in main( ) fun.
another function main() and print the
message whether a number class
is a prime prime_class_1
no or
not in main() fun. {
void main(int n)
{
class prime_class_1 if(k)
int k= isprime(n);
can be written in
{ place ofif(k==1)
void main(int n) System.out.println(n+" is prime"); else
if(k==true)
{ System.out.println(n+" is not prime");
boolean k= isprime(n); }
int isprime(int n)
if(k==true) { int
System.out.println(n+" is prime"); i,c=0;
else for(i=1;i<=n;i++)
{
System.out.println(n+" is not prime");if(n%i==0)
{
} c++;
boolean isprime(int n) }
{ } if(c==2)
int i,c=0; return(1); else
for(i=1;i<=n;i++) return(0);
{ }
if(n%i==0) }
{
c++;
}
if(c==2)
return(true);
else
return(false);
}
}
ICSEIMPNOTES
WAP to create a Function int isprime(int n) which returns 1 if n is prime else returns 0. Now call this
function from another function void printprime(int a, int b) Which prints all the prime numbers from a
to b
Eg a=6 b=15
Output
7
11
13
WAP to create a Function int firstlastdigit(int n) which returns sum of first and last digit
Now using this function create a function void main(int a,int b) which prints all the numbers
from a to b and their sum of first and last digit in the following format
Eg a=112 b=115
output
Number Sum of first and last digit
112 3
113 4
114 5
115 6
ICSEIMPNOTES
public class firstlastsumclass
{
void main(int a,int b)
{ int i,k;
System.out.println("Number\tSum of first and last digit"); for(i=a;i<=b;i++)
{
k=firstlastdigit(i);
System.out.println(i+"\t\t"+k);
}
}
int firstlastdigit(int n)
{ int l,f=0; l=n%10;
while(n>0)
{
f=n%10;
n=n/10;
}
return(f+l);
}
}
void SumSeries()
{
int i,j,s=0,f;
for(i=1;i<=20;i++)
{
f=1;
for(j=1;j<=i;j++)
{
f=f*j;
}
s=s+f;
}
System.out.println("sum of the series="+s);
}
void main()
{
SumSeries (3,2.0);
ICSEIMPNOTES
SumSeries ();
}
}
ICSEIMPNOTES
}
ICSEIMPNOTES
LESSON-4
Constructors
System.out.println(x);
}
}
(2) Parameterized constructor: A constructor that accepts one or more parameters is called parameterized
constructor.
Eg public class par
{
int x;
public par(int a)
{
x = a;
}
public void show()
{
System.out.println(x);
}
}
constructor methods(functions)
ICSEIMPNOTES
import java.util.*;
class ElectricBill void print ( )
{ {
String n; int units; double bill; void accept() System.out.println("Name of the customer:"
{ +n+ "\nNumber of units consumed:"+units+
Scanner sc=new Scanner (System.in); "\nBill amount:"+bill);
System.out.println("Enter name of the customer"); }
n=sc.nextLine(); public static void main()
System.out.println("Enter no of units consumed"); {
units=sc.nextInt(); ElectricBill EC=new ElectricBill();
} EC.accept();
void calculate() EC.calculate();
{ EC.print ();
if(units<=100) bill=units*2.0; }
if(units>100 && units<=300) }
bill=100*2.0+(units-100)*3.0; if(units>300)
{
bill=100*2.0+200*3.0+(units-300)*5.0; bill=bill
+(bill*2.5/100);
}
}
ICSEIMPNOTES
void calculate()
import java.util.*; class ShowRoom {
{ if(cost <=10000)
{
dis=0.05*cost;
String name; long mobno; double
}
cost,dis,amount;
else if(cost >10000 && cost <=20000)
{
public ShowRoom() dis=0.1*cost;
{ }
name = ""; cost=0.0; mobno=0l; else if(cost >20000 && cost <=35000)
dis = 0.0; {
amount=0.0; dis=15.0/100*cost;
} }
else if(cost >35000)
{
void input()
dis=0.2*cost;
{
}
Scanner sc=new Scanner (System.in); amount = cost - dis;
System.out.println("Enter Name"); }
name=sc.nextLine(); System.out.println("Enter void display() Mobile
No"); mobno=sc.nextLong(); {
System.out.println("Enter Cost"); System.out.println("Name :"+name);
cost=sc.nextDouble(); System.out.println("Mobile no is :"+mobno);
} System.out.println("Amount :"+ amount);
}
public static void main ()
{
ShowRoom ob = new ShowRoom();
ob.input(); ob.calculate();
ob.display();
} }
ICSEIMPNOTES
import java.util.*; void discount()
{
class FruitJuice
product_price=product_price-10;
{ }
int product_code,pack_size,product_price;
void display()
String flavour,pack_type;
{
public FruitJuice() System.out.println("Product
{ Code="+product_code);
product_code=0; System.out.println("Flavour="+flavour);
System.out.println("Pack type="+pack_type);
pack_size=0;
System.out.println("Pack size="+pack_size);
product_price=0; System.out.println("Product
flavour=""; Price="+product_price);
}
pack_type="";
} public static void main()
{
void input() FruitJuice ob=new
FruitJuice(); ob.input();
{ ob.discount();
Scanner sc=new Scanner(System.in); ob.display();
}
System.out.println("Enter product code");
}
product_code=sc.nextInt();
System.out.println("Enter pack size");
pack_size=sc.nextInt();
System.out.println("Enter product price");
product_price=sc.nextInt();
System.out.println("Enter Flavour ");
flavour=sc.nextLine();
ICSEIMPNOTES
System.out.println("Enter pack type");
pack_type=sc.nextLine();
}
ICSEIMPNOTES
import java.util.*; void display()
{ class
Mobike
System.out.println("Bike No.\t Phone No.\t
Name\t No. of days\t charge");
{
System.out.println(bno+"\t"+phno+"\t"+name+"
\t\t"+days+"\t"+charge);
String bno,name;
long phno; }
ICSEIMPNOTES
Programs based on Parameterized Constructor
Member methods: –
ShowRoom(String n, long mob, double c) – parameterised constructor to initialize data members
void calculate() – To calculate discount on the cost of purchased items, based on following criteria
Cost Discount (in percentage)
Less than or equal to Rs10000 5%
More than Rs 10000 and less than or equal to Rs 20000 10%
More than Rs 20000 and less than or equal to Rs 35000 15%
More than Rs 35000 20%
void display() – To display customer name, mobile number, amount to be paid after discount
Write a main method to create an object of the class and call the above member methods.
class ShowRoom
{
String name; long mobno;
ICSEIMPNOTES
double cost,dis, amount;
public ShowRoom(String n, long mob, double c) // parameterised constructor to initialize data members
{
name=n;
mobno=mob;
cost = c;
dis = 0.0;
amount =0.0;
}
void calculate( )
{
if (cost<=10000) dis= cost*5/100;
else if(cost>10000 && cost<= 20000) dis = cost*
10/100;
else if (cost>20000 && cost <= 35000) dis = cost*
15/100; else if (cost > 35000) dis = cost*20/100;
amount=cost-dis;
}
void display()
{
System.out.println("Name " +name);
System.out.println("Mobile No " +mobno);
System.out.println("Amount " +amount);
}
Autoboxing: Converting a primitive value into an object of the corresponding wrapper class is
called autoboxing.
For example, converting int to Integer class.
int i=100;
Integer j=i ; //Autoboxing
Unboxing: Converting an object of a wrapper type to its corresponding primitive value is called
unboxing.
For Example conversion of Integer to int.
Integer a = new Integer(10); // creating an Integer Object with value 10 int i = a; //
unboxing the Object
ICSEIMPNOTES
import java.util.*;
public class ASCII
{
char ch; void accept()
{
Scanner sc=new Scanner(System.in); System.out.println("Enter a character");
ch=sc.next().charAt(0);
}
public void print()
{
String x;
if(ch>=48&&ch<=57) x="digit"; else
if(ch>=65 && ch<=90) x=" upper case
ICSEIMPNOTES
letter"; else if(ch>=97 && ch<=122) x="
lower case letter"; else
x="special character";
System.out.println("Character \t ASCII value \tType of Character");
System.out.println(ch+"\t\t"+(int)ch+"\t\t"+x);
}
OUTPUT
public static void main()
Enter a character
{
A
ASCII ob=new ASCII(); ob.accept();
Character ASCII value Type of Character
ob.print();
} A 65 upper case letter
}
ICSEIMPNOTES