Ypcc Icse 9th Comp Notes - 2022 - 23
Ypcc Icse 9th Comp Notes - 2022 - 23
Blue-J
FEATURES:
✓ Well Planned Notes
✓ Mini Test Papers
ICSE Class – 9th ✓ Smart Practice Notes
Computer Applications (JAVA) ✓ Tricky Worksheets
✓ Group Classes with
Personal Attention
https://www.youtube.com/c/YPComputerClasses ✓ One to One Classes
https://www.instagram.com/yogesh.verma27/ ✓ YT Videos for Revision
✓ Sharing Student’s Progress
https://www.facebook.com/yogesh.verma.79219 with Parents
https://www.YPComputerClasses.in/
Contents
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→2
Ch 1 : Introduction To JAVA Basics Sub: Computer Application
JMD HGGM
ICSE Class 9th
Java Byte Code → Java source code is converted in to an intermediate code after compilation. This code is known as Byte
5 code. It is stored in file with .class extension & is same across all platforms. It can be executed only by Java Virtual Machine.
(source code is the code which is written by the programmer).
API → API stands for Application Programming Interface. It is the java library with pre-written classes and methods for easy
programming.
JDK → Java Development Kit (JDK) is an environment for building java application and applets. It includes tools for
developing & testing programs written in java.
JDK 1.1 → JDK 1.2 → JDK 1.3 → JDK 1.4 → JDK 1.5 → JDK 1.6 → JDK 1.7
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→5
Ch 1 : Introduction To JAVA Basics
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Types of Java
6 OOP, Interface, Package, Exception
CORE JAVA Handling, Threading, Applet,
JDBC, Beans etc.
JAVA Servlet, Networking, RMI,
ADV. JAVA
Swing, CORBA, JSP etc.
Hierarchy of Java
Developers
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→6
Ch 2 : How To Download Blue-J & Java
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Downloads
Blue J → Blue J is an Integrated Development Environment (IDE) for executing programs written in Java. It has a viewer,
7 editor & debugger.
Video Link:
https://www.youtube.com/watch?v=eVHF4K-E6XQ&list=PLzeLu0s-QMy_yQv8sG2Ou96lsbHXlPdLv&index=46
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→7
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Data → Raw facts and figures. Eg. (‘a’, ‘5’, ‘#’ etc).
Program → Collection of instructions (data). Delimiters are the
Software → Collection/set of programs. special characters.
They are used as -,
Data Types ; , : , ?, ., ( ), { }, [ ],
etc.
String→ String is a collection of characters. It encloses with double quotes. Eg: “123”, “Ram”, etc
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→9
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Tokens
Token: Token is a smallest individual unit of a program. Tokens are building blocks of Java. Types of tokens are:
11 Types of tokens:
a) Keywords
b) Identifiers
c) Literals
d) Separators
e) Operators
f) Punctuators
(KILSOP)
Keyword
Keywords are reserved words provide by Java which convey special meaning to the compiler. They cannot be used as
identifiers. E.g. int, void, while, case, do, int, double, if, else, final, void, char, for, while, switch, case etc.
Literal (constants)
Literals are data items which do not change its value during the execution of the program.
Type of literals: Integer Literal → 20
Floating point / Real Literal → 2.5
Character Literal → ‘a’
String Literal → “Java”
Boolean Literal → false
Null Literal → null
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→11
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Token→ Literals
Separators → Separators are the special characters in Java, which are used to separate the variables or characters.
Eg: ( ) (Brackets) – used to enclose any arithmetical or relational expressions.
{ } (Curly brackets) – used to enclose a group of statements which is called as compound/block statement
[ ] (Square brackets) – used to enclose subscript(index) or the cell number of a dimensional array.
, (Comma) – used to separate multiple variable under the same declaration.
Punctuators → Punctuators are the punctuation signs used as special characters in Java.
Eg: ? (question mark) – It is used in ternary operator.
; (semicolon) – It is a statement terminator. It indicates the end of a statement. Any line continued after semi colon
is treated as the next statement.
. (dot) - It is used to call the function with an object and used to access instance variables by using an object.
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→12
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Variable
Variable is a name given to memory location to store data by user. On the other hand, It is the quantity which can change
13 during program execution. A variable must be declared before its use. It cannot be declared again in the same scope.
Identifier → Identifiers are the symbolic names given to various parts of program like variable, function, class & object etc.
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→14
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Q1→ Find addition and product of given numbers. Q3→ WAP to swap to numbers.
17
class Swap
class FirstPro {
{ public static void main()
public static void main(String args[ ]) {
{ int A = 10, B = 20, C;
int a = 10, b = 20, sum, prod; System.out.println(“Before Swapping”);
sum = a + b; System.out.println(“A = " + A);
prod = a * b; System.out.println(“B = " + B);
System.out.println("addition = " + sum);
System.out.println("product = " + prod);
System.out.println(sum + “ is an addition ");
}
} System.out.println(“After Swapping”);
System.out.println(“A = " + A);
System.out.println(“B = " + B);
Q2→ Write the SOP line to print 10 + 20 = 30 in above Program }
}
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→17
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Q4→ Find addition and product of given numbers. Q6→ WAP to swap to numbers.
18
class Swap
class FirstPro {
{ public void main(int A, int B)
public void main(int a, int b) {
{ int C;
int sum, prod; System.out.println(“Before Swapping”);
sum = a + b; System.out.println(“A = " + A);
prod = a * b; System.out.println(“B = " + B);
System.out.println("addition = " + sum);
System.out.println("product = " + prod);
System.out.println(sum + “ is an addition ");
}
} System.out.println(“After Swapping”);
System.out.println(“A = " + A);
Q5→ Write the SOP line to print 10 + 20 = 30 System.out.println(“B = " + B);
in above Program. }
}
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→18
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Q7→ Find addition and product of given numbers. Q9→ WAP to swap to numbers.
19
class Swap
class FirstPro {
{ public static void main(int A, int B)
public static void main(int a, int b) {
{ int C;
int sum, prod; System.out.println(“Before Swapping”);
sum = a + b; System.out.println(“A = " + A);
prod = a * b; System.out.println(“B = " + B);
System.out.println("addition = " + sum);
System.out.println("product = " + prod);
System.out.println(sum + “ is an addition ");
}
} System.out.println(“After Swapping”);
System.out.println(“A = " + A);
Q8→ Write the SOP line to print 10 + 20 = 30 System.out.println(“B = " + B);
in above Program. }
}
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→19
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Programs
Q10→ WAP to input employee name and his salary of a month and print his annual salary.
20 class Employee
{
public static void main(String name, int salary, int asal)
{
int asal = salary*12;
System.out.println(“Employee Name = " + name);
System.out.println(“Annual Salary is = " + salary);
System.out.println(“Annual Salary is = " + asal);
}
}
Exercise→(Input using function arguments)
Q1→ Write a program (WAP) to input two numbers and calculate their sum, product, subtraction and division.
Q2→ WAP to input your name, gender and age and print these details.
Q3→ WAP to input rollno, name, marks of four subjects and calculate total & percentage.
Q4→ WAP to input distance in Meters and convert it into CM, KM, inch and feet.
(Hint cm = m*100, km = m/1000, inch = cm*2.5, feet = inch/12)
(1m=100cm, 1km=1000m, 1feet=12 inches, 1 inches=2.5cm)
Q5→ Temperature of a city in Fahrenheit degrees is input. Write a program to convert this temperature into Centigrade
degrees. T(°C) = (T(°F) - 32) × 5/9
Q6→ The length & breadth of a rectangle and radius of a circle are input. Write a program to calculate the area &
perimeter of the rectangle, and the area & circumference of the circle.
Q7→ If the total selling price of 15 items and the total profit earned on them is input, write a program to find the cost price
of one item.
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→20
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Escape sequences are non-graphical characters that Escape Sequences Non-Graphical Character
21
control the way how data gets printed on output device.
It always begins with a backward backslash ‘\’. \n New line
Eg: \n, \t, \” etc. Outputs \t Horizontal tab
System.out.println(" Hello \n Java "); Hello \” To print double quote
Java
System.out.println(" Hello \t Java "); Hello Java \’ To print single quote
System.out.println(" Hello \” Java "); Hello “ Java \\ To print backslash
System.out.println(" Hello \\ Java ");
\b Backspace
\0 Null
\r Carriage return
\f Form feed
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→21
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→22
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications
Type Conversion
Conversion of one datatype to another datatype is called type conversion.
23
There are two types of type conversions:
1. Implicit type conversion / type coercion 2. Explicit type conversion / narrowing conversion / type casting
Type Conversion
Explicit Conversion of Literals
24 Examples
float f = 0.123F
double d = 23.34D
int a = 12;
long b = 12L
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→24
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications
Operators
Operators:- Operators are special symbols which represents computation. They are applied on operand(s), which can be
25 values or variables.
Operand:- Operands are variables or constants on which operations are performed by the operator.
Unary Operator: The Operators which are associated with only one operand are unary operators. Eg: ++, --, +, -, !.
Binary Operator: The Operators which are associated with only two operands are unary operators.
Eg: /, %, *, + , -, <, >, etc. (Arithmetical and Relational Operators)
Ternary Operator: The Operators which are associated with only three operands are unary operators. Eg: ( ? : ) .
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→25
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications
Operators
Arithmetic Operators:
26 They are used for various mathematical calculations. They are /, % (Modulus / Remainder Op), *, +, -.
These operators are also called Binary Arithmetic Operator.
The result depends on the largest data type present in expression or left hand side variable.
a) System.out.println(11/2);
b) System.out.println(11.0/2); i) double a = 10 + 11/2;
c) System.out.println(11%2); System.out.println(a);
d) System.out.println(10%2); ii) double b = 10 + 11.0/2;
e) System.out.println(4%10); System.out.println(b);
f) System.out.println(4/10); iii) int c = 10 + 11.0/2;
g) System.out.println(‘A’ + 10); System.out.println(c);
h) System.out.println(‘b’ + ‘B’); iv) int ch = ‘d’ + 10;
i) System.out.println(‘1’ + 10); System.out.println(ch);
j) System.out.println(“A” + ‘b’ + 10);
k) System.out.println(‘b’ + 10 + “A”);
Relational Operators:
They are used for comparisons between two operands(values). It returns boolean value. They are >, <, >=, <=, ==, !=.
Assignment Operator: Assignment operators are used to assigning value to a variable. The left side operand of the
assignment operator is a variable and right side operand of the assignment operator is a value. Eg. A=10 10 = A
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→26
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications
Operators
Logical Operators: They are used for combining various ! (NOT) Operator: The not operator works by
27 conditions together using an operator. It returns a boolean inverting (or negating) the value of its operand.
value comparing two or more boolean values. ! Condition1 Result
Logical operators are &&(And), ||(Or), !(Not). T F
F T
&& (AND) Operator : It returns true if both conditions are true.
It doesn’t check second condition if first condition is false. Logical Operators Symbol Format
Condition1 && Condition2 Result
T T T NOT ! !(a==b)
T F F
AND && (a>b)&&(a>c)
F T F
F F F OR || (a==b)||(a==c)
|| (OR) Operator : It returns true if either first condition or Precedence of logical operators is NOT (!), AND (&&) and OR (||).
second condition or both are true. It doesn’t check second If a statement contains all the three logical operators then NOT is
condition if first condition is true. operated first.
Condition1 || Condition2 Result
T T T
T F T
F T T
F F F
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→27
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications
Arithmetical Expression(s)
Precedence of Operators:
29
Operators Precedence Associativity 8. Bitwise AND &
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→29
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications
2 – 10*3/2 - 100/9*2%3
2 – 30/2 - 11*2%3
2 – 15 - 22%3
–13 - 1
Ans = -14
Short hand/Compound assignment Operators:
Java provides some special Compound Assignment Operators,
also known as Shorthand Assignment Operators. It's called
Q3→ 2*3%4 –100/(10*3)/2 + 100%11*2
shorthand because it provides a short way to assign an
solve the expression.
expression to a variable. They are /=, %=, *=, +=, -=.
Q4→ 2 – 10*3%2 - 10/9*3%2
If int a = 5 then solve following expressions.
Q5→ 2*3%4 –100/10*(3/2) + 6*11%10 += a+=10 a = a+10 a = 15
Operators ++ and --
31
Increment(++) / Decrement(--)
Pre-Operator Post-Operator
++a, --a a++, a--
Note:
➢ ++ increases value by 1 and - - decreases value by 1.
➢ Pre-Operator uses/transfers always updated value.
➢ Post-Operator uses/transfers always original value then update itself.
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→31
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→32
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications
Operators ?:
Ternary operator:
34 Ternary operator or conditional operator requires 3 operands. It is an alternative to if-else statement. In ternary operator the
first operand must be a conditional expression. If the condition is true then operand 2(true expr) is executed otherwise
operand 3(false expr) is executed. It is also called the conditional assignment statement.
Syntax : var = condition ? true expr : false expr;
35
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→35
JMD HGGM
Ch5: Input in Java ICSE Class 9th Sub: Computer Applications
Q4→ Find addition and product of given numbers. Q6→ WAP to swap to numbers.
37
class Swap
class FirstPro {
{ public static void main(int A, int B)
public static void main(int a, int b) {
{ int C;
int sum, prod; System.out.println(“Before Swapping”);
sum = a + b; System.out.println(“A = " + A);
prod = a * b; System.out.println(“B = " + B);
System.out.println("addition = " + sum);
System.out.println("product = " + prod);
System.out.println(sum + “ is an addition ");
}
} System.out.println(“After Swapping”);
System.out.println(“A = " + A);
Q5→ Write the SOP line to print 10 + 20 = 30 System.out.println(“B = " + B);
in above Program. }
}
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→37
JMD HGGM
Ch5: Input in Java ICSE Class 9th Sub: Computer Applications
Programs
Q7→ WAP to input the time in seconds. Display the time after converting them into hours, minutes and seconds.
38 Sample input: Time in seconds 5430
Sample output: 1 Hour 30 Minutes 20 Seconds
class Time
{
public static void main(int sec)
{
int min, hr;
min = sec/60;
sec = sec%60
hr = min/60;
min = min%60;
System.out.println(hr + “ Hour " +min +” Minutes “+sec+” Seconds”);
}
}
Exercise→(Input using function arguments)
Q8→ Temperature of a city in Fahrenheit degrees is input. Write a program to convert this temperature into Centigrade
degrees. T(°C) = (T(°F) - 32) × 5/9
Q9→ The length & breadth of a rectangle and radius of a circle are input. Write a program to calculate the area &
perimeter of the rectangle, and the area & circumference of the circle.
Q10→ If the total selling price of 15 items and the total profit earned on them is input, write a program to find the cost price
of one item.
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→38
JMD HGGM
Ch5: Input in Java ICSE Class 9th Sub: Computer Applications
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→39
}
JMD HGGM
Ch5: Input in Java ICSE Class 9th Sub: Computer Applications
Programs
Q1→ Find addition, subtraction, product and division of given Q2→ WAP to swap to numbers.
40 numbers. import java.util.Scanner;
import java.util.Scanner; class Swap
class FirstPro {
{ public static void main(String args[ ])
public static void main() {
{ Scanner sc = new Scanner (System.in);
Scanner sc = new Scanner (System.in); int a, b, c;
int a, b, sum, sub, prod,div; System.out.println("Enter two Numbers ");
System.out.println("Enter two Numbers "); a = sc.nextInt();
a = sc.nextInt(); b = sc.nextInt();
b = sc.nextInt(); System.out.println(“Before Swapping”);
sum = a + b; System.out.println(“a = " + a);
sub = a-b; System.out.println(“b = " + b);
prod = a * b;
div = a/b;
System.out.println("addition = " + sum);
System.out.println(“Subtraction = “ + sub); System.out.println(“After Swapping”);
System.out.println("product = " + prod); System.out.println(“a = " + a);
System.out.println(“Division = “ + div); System.out.println(“b = " + b);
} } }
} Q3→ Write the SOP line to print 10 * 20 = 200
if a=10 and b=20; c=a+b;
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→40
JMD HGGM
Ch5: Input in Java ICSE Class 9th Sub: Computer Applications
Java Programs
Q1→ WAP to input your name, gender and age and print these details.
41 Q3→ WAP to input two numbers in A and B and swap these two numbers with using extra variable and without using
extra variable.
Q4→ WAP to input rollno, name, marks of four subjects and calculate total & percentage.
Q5→ WAP to input distance in Meters and convert it into CM, KM, inch and feet.
(Hint cm = m*100, km = m/1000, inch = cm*2.5, feet = inch/12)
(1m=100cm, 1km=1000m, 1feet=12 inches, 1 inches=2.5cm)
Q6→ WAP to input employee name and Basic Salary (BS).
Calculate TA(travelling Allowance), DA(Dearness Allowance), HRA(House Rent Allowance), Total Salary(TS),
IT(income tax), PF(profit & funds) and Net Salary(NS).
TA = 35%of BS, DA = 30%of BS, HRA = 25% of BS, TS = BS + TA + DA + HRA;
IT = 5.6%of TS, PF = 7.5%of TS), NS = TS – ( IT + PF );
Q7→ Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this
temperature into Centigrade degrees. T(°C) = (T(°F) - 32) × 5/9
Q8→ The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to
calculate the area & perimeter of the rectangle, and the area & circumference of the circle.
Q9→ If a four-digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of
this number.
Q10→ If the total selling price of 15 items and the total profit earned on them is input through the keyboard, write a
program to find the cost price of one item.
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→41
JMD HGGM
Ch5: Input in Java ICSE Class 9th Sub: Computer Applications
42
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→42
ICSE Class 9th
JMD HGGM
Ch6: Mathematical Library Methods Sub: Computer Applications
Methods / Functions
Methods/Functions are subprograms or small block of code which perform specific action. Or
43 A large program can be divided into smaller subprograms. These subprograms are called as function.
Advantages of function:-
➢ Complexity of program is reduced.
➢ Debugging (error checking) can be done easily.
➢ Memory is saved. As only a single copy of the function is present in
the memory but it can be called many times in a program.
Categories Of Functions / Methods:
There are two Categories of functions.
1. Built-in methods/ Library methods / Pre-defined methods
Functions/Methods which are pre-defined in ‘JAVA’ language.
Or The methods which are created by the system developers
are said to be library methods or built-in methods.
Example → Math.pow(), Math.abs(), Math.sqrt(), etc.
2. User defined function → Methods are defined by the users itself.
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→43
ICSE Class 9th Sub: Computer Application
JMD HGGM
Ch6: Mathematical Library Methods
The library methods (functions) are the in – built methods created by the developers. These predefined functions help the
44
user to perform certain tasks easily and quickly which are frequently used in Java Programming. Here we are going to learn
mathematical functions which are included in a class “Math” under “java.lang” package.
Functions/Methods Answers
1. Math.ceil( 3.4 );
2. Math.ceil( 8 );
3. Math.ceil(4.2);
4. Math.ceil(-0.95);
5. Math.ceil(65.5);
6. Math.ceil(654);
7. Math.ceil(2.9);
8. Math.ceil(12.01);
1. Math.floor(-4.7);
2. Math.floor(16.3);
3. Math.floor(2.9);
4. Math.floor(-99.51);
5. Math.floor(-0.84);
6. Math.floor(64.6);
7. Math.floor(456);
8. Math.floor(-12.01);
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→46
ICSE Class 9th Sub: Computer Application
JMD HGGM
Ch6: Mathematical Library Methods
Math.random( )
47 This function returns a random number between 0 and 1. It returns a double data type value.
Syntax – double Math.random( );
1. To get an integer random number between 1 and n:
int rd = (int) (Math.random( )*n) + 1;
2. To get an integer random number between max and min:
int rd = (int) (Math.random( )*( max – min ) ) + min );
3. To get 0 and 1 as head and tail of a coin randomly.
int n = (int) (Math.random( ) * 2 );
Math.round()
This method returns the value of the argument rounded to the nearest int value.
0.5 and above is rounded to nearest higher int value & below 0.5 is rounded off to nearest lower int value.
Syntax - long Math.round(double a), or int Math.round(float a)
Math.rint()
This method is used to round the argument to nearest integer in floating point.
In case of 0.5 at decimal places it will round off to nearest even double value.
Syntax - double Math.rint(double a)
49
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→49
Ch 7 : Conditional statement in Java ICSE Class 9th Sub: Computer Application
JMD HGGM
If statement - Questions
54
Q1→ WAP to input a number and find out whether it is an odd number or even number.
Q2→ WAP to input a number and determine whether the year is a leap year or not.
Q3→ WAP to input a number and check the number is +ve number, –ve number or zero.
Q4→ WAP to input two numbers and find out the greatest number.
Q5→ WAP to input three numbers and find out the smallest number.
Q6→ WAP to input an alphabet and check whether it is vowel or not.
Q7→ WAP to input a character and check whether it is capital alphabet, small alphabet, number or symbol.
Q8→ WAP to input a three-digit number to obtain the reversed number and to determine whether the original and reversed
numbers are equal or not.
Q9→ Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through
the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degrees.
Q10→ WAP to input length and breadth of a rectangle and to find whether the area of the rectangle is greater than its
perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter.
Q11→ WAP to input to a number and check the number is buzz number or not.
( Buzz is the number which is divisible by 7 or ends with 7 ).
System.exit(0)
System.exit(0)
56 When System.exit(0) function is invoked(called), it terminates the execution of the program at that moment. Thus, the
execution of remaining statements of the program is ignored.
Syntax: System.exit(0);
Eg: Q13→ WAP to display the square and cube of a positive number. If entered number is not positive then program terminates
automatically by printing appropriate massage.
import java.util.Scanner;
class Pro_exit
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
int n;
System.out.println("Enter a Number: ");
n = sc.nextInt();
if(n<0)
{
System.out.println(“It is a negative number. So the program terminates”);
System.exit(0);
}
System.out.println(“Square of a number = ”+(n*n));
System.out.println(“Cube of a number = ”+(n*n*n));
} }
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→56
Ch 7 : Conditional statement in Java ICSE Class 9th Sub: Computer Application
JMD HGGM
switch statement
Syntax: Note:
switch(variable / expression) ➢ Break statement is used to exit from case.
{ ➢ Default statement is executed when no case value is satisfied.
case value: ➢ Case values must be an integer or character. It can not be float and
statements string values.
break; // it is optional ➢ Cases can never have variable expressions.
case value: (for example case a +3: is wrong)
statements ➢ Multiple cases cannot use same expressions.
break; (case 2: case 1+1 is wrong)
case value: ➢ Multiple cases can use different expressions.
statements (case 2: case 1+2 is correct)
break; ➢ A switch with 10 cases would work faster than an equivalent if-else
---------- ladder
---------- ➢ A switch with two cases would work slower than an equivalent if-else.
default : // it is optional The limitations of switch are as follows:
statements o It doesn't allow ranges, Eg. case 90-100.
} o It requires either integers or characters and doesn't allow Strings & double.
switch statement
Q1→ WAP to input a number and print week days name according to inputted number. 1 for Monday, 2 for Tuesday, … ...
7 for Sunday. If number is not between 1 to 7 then print invalid number.
A → Import java.util.*;
Class Weekdays case 3:
{ System.out.println(“Wednesday”);
public static void main(String args[]) break;
{ case 4:
int n; System.out.println(“Thursday”);
Scannner sc = new Scanner(System.in); break;
System.out.println(“Enter a number ”); case 5:
n = sc.nextInt(); System.out.println(“Friday”);
switch(n) break;
{ case 6:
case 1: System.out.println(“Saturday”);
System.out.println(“Monday”); break;
break; case 7:
case 2: System.out.println(“Sunday”);
System.out.println(“Tuesday”); break;
break; default:
System.out.println(“Invalid number”); } } }
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→58
Ch 7 : Conditional statement in Java ICSE Class 9th Sub: Computer Application
JMD HGGM
switch statement
switch statement
switch statement
Switch - Questions
61
Q3→ WAP to input an alphabet and check whether it is vowel or not using switch.
Q4→ WAP to input a number and find out whether it is an odd number or even number using switch.
Q5→ WAP to input a month number and print month names like 1 for January, 2 for February……………..12 for December.
Q6→ WAP to input a month number and print season names like (12, 1, 2) for Winter, (3, 4, 5) for Spring, (6, 7, 8) for
Summer, (9, 10, 11) for Fall Season.
Q7→ WAP to print following menu.
1. Addition
2. Subtraction
3. Multiplication Q8→ WAP to input two numbers and one operator
4. Division and print result according to given operator.
5. Exit Eg: Enter two nos. 10 20 +
Enter your choice __ Result = 30
Enter two number ___ ____
Result is ___
Fall through:
If there is no break after a matching case construct, the statements of all the cases after a matching case are executed till
a break is encountered or the switch block is over. This is known as fall through.
These statements are used to perform a set of instructions repeatedly while the condition is true. iteration statements are
62 also called looping statements.
Types of Loop
1. Entry controlled loop (for, while loop)
➢ In this construct the test expression is evaluated first & then executes the statements (body of the loop.
➢ The loop is not executed if the condition is false for first time. Eg. for, while loop.
2. Exit controlled loop (do – while loop)
➢ In this construct the test expression is evaluated after the execution of the body of the loop
➢ The loop is executed if the condition is false for first time. Eg. do - while loop.
The loop has four different elements that have different purposes. These elements are:
i. initialization expression: Before entering in a loop, its variables must be initialized.
ii. Test Expression (Condition): The test expression decides whether the loop body will be executed or not. if
the test condition is true, the loop body gets executed Otherwise the loop is terminated. it means loop is
executed till the condition is true.
iii. The Body of the loop: The statements, which are executed repeatedly while the test expression evaluates to
true form the body of the loop.
iv. Modifier / increment / Decrement(++,- -) Expression: The increment/Decrement expression changes the
value of the loop variable.
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→62
ICSE Class 9th Sub: Computer Application
JMD HGGM
For loop
63
For loop is an entry controlled loop with all the expressions in one line. It is used for fixed number of iterations.
Syntax: for(initialization; Condition; ++/--) Block or compound statement
{ it is a set of two or more statements enclosed within a pair of curly
// loop body braces “{ }”.
} Eg. for(i=1;i<=4;i++)
{
Eg1: Print numbers from 1 to 10.
Block of statements;
for( int i = 1; i <= 10; i++)
}
{
System.out.print(i+ “ “); Eg4: Find outputs
} for( int i = 1; i <= 10; i+=2)
Eg2: Print numbers from 10 to 1. {
int i; System.out.print(i+ “ “);
for( i = 10; i >= 1; i- -) System.out.print(“Number”);
{ }
System.out.print(i+ “ “);
} Eg5: Find outputs
for( int i = 1; i <= 10; i+=2)
Note: System.out.print(i+ “ “);
System.out.print(“Number”);
There are different variations / types in the ‘for’ loop given below:
65 Infinite loop / Endless loop: We can specify more than one statements in a for loop
A loop that never terminates is known as infinite loop. for initialization and inc / dec. But they have to be comma
Eg1: for( int i = 1 ; ; i++ ) separated.
{ Eg1: int a, b;
System.out.println(“infinite loop”); for(a= 1, b= 10 ; a<=5 && b>=1; a++, b-- )
} {
Eg2: for( ; ; ) System.out.println(“Hello Java”);
{ }
System.out.println(“infinite loop”); Note: in loop, multiple conditions can be specified using
} logical operators (&&, || ) only.
Eg3: for( int i = 1 ; i<=10 ; )
System.out.println(“infinite loop”);
Null loop / Empty loop / Bodyless loop / Time Delay loop
A loop that doesn’t include any statement as body of the loop
is called null, delay, empty or bodyless loop. We can use semi
colon after the for loop to make empty loop.
Eg1: for( int i = 1 ; i<1000 ; i++ );
While loop
While loop
66
While loop is fundamental looping statement. All the expressions are written on separate lines. it is used when the
numbers of iterations are unknown. it is an entry control loop. The condition is checked and the loop executes till a
condition is true.
Syntax: How to get digits from given number:
Eg2: import java.util.*;
initialization Print 10 to 1
while(condition) class GetDigits
int i; {
{ i = 10;
Loop body public static void main()
while(i>=1) {
++/-- {
} Scannner sc = new Scanner(System.in);
System.out.print(i+” “); System.out.println(“Enter a number ”);
i--; int n = sc.nextInt();
Eg1:
} int dig;
Print 1 to 10
int i; while(n!=0) // while(n>0)
i = 1; {
while(i<=10) dig = n%10; // To get last digit
{ n = n/10; // To remove last digit
System.out.Print(i+” “); Note: System.out.println(dig);
i++; }
} }
}
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→66
Ch 8: Iterative Constructs in Java ICSE Class 9th Sub: Computer Application
JMD HGGM
While loop
Do …. While loop
Do…While loop Q2→ WAP to input name and age of persons. Continue the
68
input process according to user’s choices(y/n). Calculate
Do-while loop is an exit controlled loop. It first executes the average age of persons.
statements & then checks the condition. Thus it executes the import java.util.*;
loop at least once. it is used when number of iterations is class User
unknown. {
Syntai public static void main()
initialization Eg2:
{ Scannner sc = new Scanner(System.in);
do int i = 1;
String name; int age, cnt=0, sum=0; char ch;
{ do
do
Loop body {
{
++/-- System.out.print(i+” “);
System.out.println(“Enter a name and age”);
}While(condition); i++;
name = sc.neitLine();
Eg1: }while(i <= 10);
age = sc.neitint();
int i = 1; Output will be: sum = sum + age;
do 1 2 3 4 5 6 7 8 9 10 cnt++;
{ System.out.println(“Do u want to continue(y/n)”);
System.out.print(i+” “); ch = sc.neit().charAt(0);
i++; } while(ch==‘y’ || ch==‘Y’);
}while(i >= 10); Q1→ WAP to input a number System.out.println(“Average age =”+(sum/cnt));
Output will be: and calculate factorial of given }
1 number. }
While loop
It is used when number of iterations are known. It is used when number of iterations is unknown.
All the expressions are written on same lines. All the expressions are written on different lines.
It first checks the condition & then executes the statements. It is used when number of iterations is unknown.
It will not execute if condition is false for first time. It executes once even if the condition is false.
Similarity: Both the loops are used when number of iterations is unknown.
Comparing Do…While and While - The difference between do-while and while is that do-while evaluates its test
expression at the end of the loop instead of at the beginning. Therefore, the statements within the do block are always
executed at least once. Do-while is an exit controlled loop and while is an entry controlled loop.
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→69
Ch 9: Nested Loop ICSE Class 9th Sub: Computer Application
JMD HGGM
NESTED LOOP
70
A loop placed inside another loop is known as nested Rules to make patterns
loop. For every iteration of outer loop, inner loop
executes for n times. 1) Count total number of rows and columns.
Syntax: 2) Execute Outer loop for Rows and inner loop for Columns.
Outer loop Output
3) Print OUTER loop variable when we have Same values in
{ //outer loop body x y rows like -
inner loop 1 1
{ 1 2 11111
//inner loop body 1 3 22222
} 1 4
} 33333 and set outer loop first then inner loop
Example & Find output: 2 1
System.out.println( “ x y “ ); 4) Print INNER loop variable when we have Different values in
2 2
for(int x = 1 ; x <= 3; x++ ) 2 3 rows like -
{ 2 4
for(int y = 1 ; y <= 4; y++ ) 12345
{ 3 1 12345
System.out.println(x + “ “ + y); 3 2
} 12345 and set inner loop first then outer loop
3 3
System.out.println(); 3 4
}
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→70
Ch 9: Nested Loop ICSE Class 9th Sub: Computer Application
JMD HGGM
Pattern Questions
Pattern Questions
71
Pattern Questions
Q4 → 4 4 4 4 4 4 Q7 → A A A A Q10 → 1 2 3 4 Q13 → 7 7 7 7 7 7 7
72
3 3 3 3 3 3 B B B B 5 6 7 8 6 6 6 6 6 6
2 2 2 2 2 2 C C C C 9 1 2 3 5 5 5 5 5
1 1 1 1 1 1 D D D D 4 5 6 7 4 4 4 4
3 3 3
2 2
1
Q5 → 5 4 3 2 1 0 Q8 → D C B A Q11 → 1 Q14 → 1 1 1 1 1
5 4 3 2 1 0 D C B A 2 2 2 2 2 2
5 4 3 2 1 0 D C B A 3 3 3 3 3 3
5 4 3 2 1 0 D C B A 4 4 4 4 4 4
5 4 3 2 1 0 D C B A 5 5 5 5 5 5
Q6 → @ @ @ @ Q9 → D D D Q12 → 6 Q15 → D
@ @ @ @ C C C 5 5 C C
@ @ @ @ B B B 4 4 4 B B B
@ @ @ @ A A A 3 3 3 3 A A A A
@ @ @ @ 2 2 2 2 2
1 1 1 1 1 1
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→72
Ch 9: Nested Loop ICSE Class 9th Sub: Computer Application
JMD HGGM
Pattern Questions
73
Find Outputs
76
Q1→ for( i = 1; i <= 10; i++); Q4→ for( i = 1; i <= 20; i++)
System.out.print(i+” “); {
switch(i)
Output _________________ {
case 1: i += 3;
Q2→ int c = 0; case 2: i += 5; break;
for( i = 1; i <= 45; i++) default: i += 2;
{ }
c++; System.out.print( i + ” “);
} }
System.out.print(i+“ ”+c); output_________________
output__________________
Q5→ int s=0, k=10;
Q3→ int i = 1; do
while(i<=10) {
{ System.out.print( k + ” “);
System.out.print( i + ” “); s = s+5;
i++; }while(k<=20);
} output __________ A4→
output __________ 9 12 15 18 21
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→76
ICSE CLASS - IX
Object Oriented Programming
JMD
HGGM Oops In Java Computer Application
77
FEATURES OF OOP :
1. Class. 2. Object. 3. Data Hiding. 4. Data Encapsulation & Abstraction.
5. Inheritance. 6. Polymorphism.
DATA ENCAPSULATION
Wrapping up of data members and member methods together in a single unit which is known as Data Encapsulation.
JMD
HGGM Components of OOP Computer Application
78
Private members are available to only own class and not accessible to function outside the class.
Private member data & function cannot be accessed outside the class. It is known as Data Hiding. They can be accessed
only through public members of the class.
Public members are the members which accessible from anywhere either inside or outside.
Abstraction:
Abstraction is the act of representing essential features without getting in to complexity of the system.
Eg: We know how to drive a car. But we don’t know the mechanism that makes the car move.
JMD
HGGM Creating Class & Object Computer Application
79
JMD
HGGM OOPS IN JAVA Computer Application
80
JMD
HGGM OOPS IN JAVA Computer Application
81
JMD
HGGM OOPS IN JAVA Computer Application
82
INHERITANCE
Inheritance is a process of creating new class from the existing class. The existing class is called as super class or base
class or parent class. The newly created class is called as derived class or sub class or child class. Inheritance is the most
powerful feature of Object Oriented Programming. The process of Inheritance does not affect the base class.
Base class & Derived class:
Base class is an existing class from which new classes are derived. It is also known as super class or Parent class.
Derived class is a new class which inherits properties from a base class. It is also known as sub class or child class.
Super class
Sub class
➢ Derived class inherits the features of base class.
➢ Derived class has its own features also.
Advantages of Inheritance
➢ Reusability of code:
➢ Reusability of code saves money as well as time and increases program reliability.
➢ Protection is never decreased while deriving the class.
JMD
HGGM OOPS IN JAVA Computer Application
83