0% found this document useful (0 votes)
25 views83 pages

Ypcc Icse 9th Comp Notes - 2022 - 23

The document outlines a comprehensive curriculum for ICSE Class 9 Computer Applications (JAVA), featuring well-planned notes, mini test papers, and various teaching methods including group and one-on-one classes. It includes detailed chapters on Java basics, data types, operators, and object-oriented programming, along with practical exercises and resources for further learning. The document also provides links to social media and contact information for the instructor, Yogesh Verma.

Uploaded by

bandanajha09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views83 pages

Ypcc Icse 9th Comp Notes - 2022 - 23

The document outlines a comprehensive curriculum for ICSE Class 9 Computer Applications (JAVA), featuring well-planned notes, mini test papers, and various teaching methods including group and one-on-one classes. It includes detailed chapters on Java basics, data types, operators, and object-oriented programming, along with practical exercises and resources for further learning. The document also provides links to social media and contact information for the instructor, Yogesh Verma.

Uploaded by

bandanajha09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

JMD-HGGM

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/

YOGESH VERMA : 8329686472, 7798453624, 7798161873


Index Page Sub: Computer Applications
JMD HGGM
ICSE Class 9th

Contents

Ch. No. Chapters Page No.


2 1. Introduction to JAVA Basics 3
2. How To Download Blue-J & Java 7
3. Values and Data Types 8
4. Operators in Java. 25
1. How to solve Java Expressions 29
5. Input in Java 36
6. Mathematical Library Methods. 43
7. Conditional Statements in Java(if).
1. if Statement 50
2. Switch Statement 57
8. Iterative / Loop Constructs in Java. 62
1. for loop 63
2. While loop 66
3. Do-while loop 68
9. Nested for loops 70
1. Jump statements (break, continue). 75
10. OOPS(Object Oriented Programming) in Java. 77

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 Programming Language


Java is a high level programming language (HLL) introduced by Sun Microsystems.
3
a. It is written and designed by James Gosling in year 1991.
b. It is pure Object Oriented programming language (OOP) & platform independent .
c. It is case sensitive language and robust language.
d. Old name of Java was Oak.
Ordinary Compiler:
In Ordinary compiler, source code is converted into machine code which is different for all the platforms.
C/C++/VB Source code (HLL) → Compiler → Machine Code (LLL)
Java Compiler:
In Java compiler, source code is converted into byte code in one go, which is same across all the platforms i.e. it can
be executed on any platform without changing it, by Java virtual machine.
Java source code → Compiler → Class File (Byte Code) → Run(through JVM) → Machine Code
abc.java abc.class
Java Interpreter:
Interpreter converts source code into byte code (object code) line by line which is same across all the platforms i.e. it
can be executed on any platform without changing it, by Java virtual machine.
JVM: JVM stands for Java Virtual Machine. It is java run time system. Its work is to convert byte code into proper machine
code. It is a virtual processor for interpreting and executing java byte-code across various platforms.
Main concept of java:
WORA (Write Once Run Anywhere) means java byte codes can be executed on any platform without changing it, by Java
virtual machine. JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→3
Ch 1 : Introduction To JAVA Basics
JMD HGGM
ICSE Class 9th Sub: Computer Applications

Java Characteristics / Java Features

a. OOP (Object Oriented Programming):


4 It is a very user friendly and a powerful approach to solve basic to difficult problems. It bundles data and
methods together in a single unit which is known as data Encapsulation.
• It follows bottom up approach in program design.
• It has access specifiers named Public, Private, Protected, etc.
• Object Oriented Programming allows some special features such as Polymorphism, Inheritance, Data
hiding and Abstraction.
b. Platform Independent:
After compilation the Java source code is converted in to Bytecode which is same across all platforms i.e. it
can be executed on any operating system without changing the code. So Java is known as platform
independent language.
c. Compiled as well as Interpreted:
Java source codes are compiled to bytecodes and bytecodes are interpreted by the JVM to give an output.
d. Simple:
Java syntax is very easy to learn and understand. It removes the complex features of the previous
programming languages like multiple inheritance and pointer.
e. It is More Secure, Portable, Reliable and Robust(Error free).

Name two types of Java programs:


a) Java Applets / Internet Applet: It is a java program which can run in a java enabled web browser. It is specifically
designed to be embedded in an HTML document.
b) Java Applications / Standalone system: They are standalone programs that can directly run on the machine (JVM).
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→4
Ch 1 : Introduction To JAVA Basics
JMD HGGM
ICSE Class 9th Sub: Computer Applications

Java Theory Points

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

Java Theory Points

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

Values and Data Types

Name the character set used by Java:


8 Character set used by java is Unicode. It is a 2 bytes
character set which represents all the characters present in
possible human languages.

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.

Primitive / Primary / Non-primitive / Secondary / User defined /


fundamental Data type Reference / Composite/Derived Data type

➢ Integer (byte → short → int → long) ➢ Function / Methods


Numeric Types
➢ Floating number (float → double) ➢ Class and Object
➢ String
➢ Characters (char) ➢ Array
Non-Numeric Types
➢ Boolean (true/false) JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→8
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications

Data Types Information

Difference between Primitive & Composite data-type


9
Primitive Data-type Composite Data-type
The data types that are already provided by java are The data types which are created by the programmer using
primitive data types. primitive data type are composite data types.
The size is fixed. The size depends on the number of variables & their types.
When a variable of primitive type is declared only one When a variable of class type is declared memory allocated
memory location is allocated. may vary.
‘new’ keyword is not used. ‘new’ keyword is used to declare the variables.

Rules for Data Types Character Rules:


Integer/whole numbers & Float/Double/real numbers Rules: ➢ A character constant is a single alphabet,
➢ An integer/float constant must have at least one digit. a digit or a single special symbol.
➢ Integer must not have a decimal point but float must have decimal point. ➢ It must have only one character.
➢ Both can be either positive or negative. ➢ It encloses within single inverted quotes.
➢ Default sign is positive. ➢ Both the inverted quotes should point to
➢ No commas or blanks are allowed within an integer/float constant. the left (‘ ‘).
➢ Eg: Integers → 426, +782, -8000 ➢ Range is 0 to 65536 (\u0000 - \uffff).
➢ double / Float → 34.5, -456.0, +34.543 etc. ➢ Eg : 'A' , 'i' , '5' , '=' , ‘$‘ 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

Data Types Information

All information about Data Types (40Q in one table)


10
Wrapper Classes Data Types Size (bit/byte) Range Default values

Boolean boolean 8 bits (1 byte) true - false false


Byte byte 8 bits (1 byte) -128 to +127 0
Character char 16 bits (2 bytes) 0 to 65536 ‘\u0000’
Short short 16 bits (2 bytes) -32768 to +32767 0
Integer int 32 bits (4 bytes) -231 to 231 - 1 0
Long long 64 bits (8 bytes) -263 to 263 - 1 0l / 0L
Float float 32 bits (4 bytes) -3.4E+38 to 3.4E+38 0.0f / 0.0F
Double double 64 bits (8 bytes) -1.7E+308 to 1.7E+308 0.0d / 0.0D

Ascending order of Primitive Data Type:


✓ boolean < byte < char < short < int < long < float < double
Wrapper class:
It wraps or encapsulate primitive data type in a form of a class. They are used to convert primitive type to reference type
or vice versa. They are found in java.lang package which is default package in Java.
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→10
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

Differentiate String literal & Character literal.


12
String Literal Character Literal
It is a set of characters enclosed within double quotes. It is a single character enclosed within single quote.
It stored in a variable with String datatype. It stored in a variable with char datatype.
E.g. String s = “hello”; E.g. char ch = ‘a’;

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.

Variable / Identifier Naming Rules


➢ A variable name is any combination of alphabets, digits, underscores(_) or dollar symbol ($) .
➢ The first character in the variable name must be an alphabet(a/A – z/Z), underscore ( _ ) or dollar( $ ). It can not be a
number. It is case sensitive & can be of any length.
➢ Blank spaces, commas, keywords (reserved words) or special symbol except an underscore or dollar are not allowed
within a variable name.
➢ E.g.: Correct variables → si_int , m_hra , pop_e_89 , $a , _b etc.
➢ Wrong variables → 1a, emp sal, no., if etc.
Variable Declaration
Syntax: Data type variable_name1, var2…….; final keyword / constant variable
Ex: int a; char ch; float per, sal; When a variable is declared using
Variable Initialization final keyword it becomes a constant
a. Static Initialization → Variable is initialized during the compile time. i.e. its value cannot be changed
Eg: int a = 10; during execution of the program.
char ch = ‘b’; double per = 66.5; E.g. final double pi = 3.14;
b. Dynamic Initialization → Variable is initialized during the runtime.
Eg: int a = 10, b=20; int c = a + b ; here c variable is initialized dynamically.
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→13
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications

Differentiate ASCII & Unicode.

ASCII Unicode Character ASCII Code


14
It stands for American standard code for It stands for Universal code. A to Z 65 to 90
information interchange. Each character is The Unicode is a wide representation
assigned a specific numeric value, called the of characters in the numeric form. a to z 97 to 122
ASCII code. 0 to 9 48 to 57
It has 0 to 127 characters. It has 0 to 65536 characters.
Space 32

JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→14
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications

Basic Structure of Java Program


Syntax: public static void main():
15 class class_name The program execution always starts from
{ main method & other sub functions are
public static void main(String args[ ]) // (String[ ] args) executed from the main method. It is static so
{ we don’t need to create an object to invoke it.
Program coding
To Save a Program:
}
Program file name should be same as class
}
name. Extension of java file should be .java.
Example: File extension of source code &
class student intermediate code of Java:
{ Source code → .java
public static void main(String args[ ]) Intermediate code/Byte code → .class
{
System.out.println(“Hello Java”); String[] args : Java main method accepts a
System.out.println(“Welcome to YP computer Classes”); single argument of type String array. This is
} also called as java command line arguments.
}
o public → It represents that method will be accessible from anywhere.
o static → It is a special method which is invoked without object.
o void → It does not return any value.
o main() → It is the special function from where program execution starts.
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→15
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications

Java Printing Statement

System.out.print() statement: println() → It is used to print new line.


16
It is used to print message and variable value. Outputs
Eg: Working of println();
Syntax a) System.out.print( “hello” );
System.out.print(“msg”); System.out.println( “Java” );
System.out.print(varname);
System.out.print(“msg” + varname); b) System.out.println( “hello” );
System.out.print( “Java” );
Example
Outputs
int a = 10; c) System.out.print( “hello” );
1. System.out.print(“value of a = ” + a); System.out.print( “Java” );
2. System.out.print(a + “is the value of a”);
d) System.out.println( “hello” );
int a = 15, b = 50;
System.out.println( “Java” );
3. System.out.println( a + b );
4. System.out.println( “ Sum = ” + a + b );
5. System.out.println( “ Sum = ” + (a + b) );
6. System.out.println( a + b + “ is sum” );
7. System.out.println( a + “ ” + b);
8. System.out.println( “ ” + a + b);
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→16
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications

Java Basic Programs

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

Programs using function arguments and without static keyword

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

Programs using function arguments and with static keyword

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 Sequence Characters

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

Whitespace: - It is tab, space, or newline. Java does


not enforce any indentation rules.

JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→21
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications

Arithmetical Expressions / Expression

Arithmetical Expression and Arithmetical Statement


22 The combination of operators & operands together is known as an Arithmetical expression / expression. When an
arithmetical expression is assigned to a variable then it is called an Arithmetical Statement.
Examples of Operator, Operand and Expression.
E.g. c = a + b; c = a + b;
This line is an Expression where a, b & c are operands and + and = are operators.
Arithmetical Expression
There are two types of expressions.
Pure Expression / Pure Arithmetical Expression:
The expression with similar type of operands is known as pure expression. Eg: 5 + 10; int a=10; int b= a/3;

Impure Expression / Mixed Mode Expression / impure Arithmetical Expression :


The expression with different types of operands is known as Mixed Expressions. Eg: 5 + 6.2; int a=10; double b= a/3;

Hierarchy of Data Types:


byte < char < short < int < long < float < double

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

Implicit type Conversion Explicit type Conversion


Conversion of one primitive datatype to another Conversion of one primitive type to another forcefully by the
automatically by the compiler is known as implicit conversion. programmer is Explicit conversion.
Destination type is larger than the source type. Source type is greater than the destination type.
The two types are compatible. The two types are not compatible.
Eg: int a = 2; Eg: double a = 2.5;
double x = a; int x = (int) a;
//a is converted to double type by compiler. //a is converted to int using typecast ‘()’ operator.

Hierarchy of data types indicates the increasing order of data types.


byte < char < short < int < long < float < double
If two data of different data types are operated upon then the result
automatically gets converted to their highest data type.
Eg1: int a = 10; double b = 20.5;
c = a + b; // here variable c must be declared as double type.
Eg2: What will be the data type of variable d in the image.
? JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→23
Ch 3 : Values & Data Types
JMD HGGM
ICSE Class 9th Sub: Computer Applications

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.

Operators can be classified in two ways:


1. Operation to be performed. 2. Number of operands.

➢ Arithmetic Operators → /, %, *, +, - ➢ Unary Operator (One Operand)


➢ Relational Operators → >, <, >=, <=, ==, != ➢ Binary Operator (Two Operands)
➢ Logical Operators → !, &&, || ➢ Ternary Operator (Three Operands)
➢ Assignment Operator → =
➢ Short hand/Compound assignment Op. → /=, %=, *=, +=, -=
➢ Conditional / ternary Operators → ?:
➢ Increment & Decrement Operators → ++, --

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)

Arithmetical Expression and Arithmetical Statement


28
The combination of operators & operands together is known as an Arithmetical expression / expression. When an
arithmetical expression is assigned to a variable then it is called an Arithmetical Statement.
Examples of Operator, Operand and Expression.
E.g. c = a + b; c = a + b;
This line is an Expression where a, b & c are operands and + and = are operators.
There are two types of expressions. Arithmetical Expression
Pure Expression / Pure Arithmetical Expression:
The expression with similar type of operands is known as pure expression. Eg: 5 + 10; int a=10; int b= a/3;
Impure Expression / Mixed Mode Expression / impure Arithmetical Expression :
The expression with different types of operands is known as Mixed Expressions. Eg: 5 + 6.2; int a=10; double b= a/3;
Precedence of operators and Name the operator with highest & lowest precedence.
The order in which the operands are operated by operators in an expression is known as precedence of operators.
Operators with same precedence are executed from left to right.
Operator with highest precedence is ( ), Operator with lowest precedence is =. In Maths:
Left to right
Q1→ Evaluate following expressions. In computer: 1. ()
Precedence of 2. /
2 – 10*3/2 + 100/11*2 and show steps. Operators→ 1. ( )
2. ++, -- 3. *
2 – 30/2 + 9*2 4. +
3. /, *, %
2 – 15 + 18 5. -
4. +, -
–13 + 18 Ans = 5 5. = 6. =
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→28
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications

Precedence of Operators:

29
Operators Precedence Associativity 8. Bitwise AND &

1. Bracket ( ), [ ] left to right 9. Bitwise exclusive OR ^

2. Increment, 10. Bitwise inclusive OR |


Decrement, ++ -- + - ! right to left
and unary
11. Logical AND &&
3. Multiplicative */% left to right

4. Additive + - left to right 12. Logical OR ||

5. Shift << >> >>> left to right 13. Ternary Operator ?:

6. Relational < > <= >= left to right = += -= *= /= %=


14. Assignment &= ^= |= <<= left to right
7. Equality == != left to right >>= >>>=

JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→29
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications

How to solve Java Expressions


Q6→ Write the java expressions for the following.
Q2→ Evaluate following expressions.
30

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

Q6→ 5 + 3 > 7 && 7 >= 4 + 3 -= a-=10


/= a/=10
Q7→ 5 != 5 && 3 == 4
%= a%=10
A3) 3, Q4) 0 , Q5) -2 Q6) True Q7) False *= a*=10
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→30
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications

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.

Differentiate Pre-increment & Post-increment operator.


Pre-increment operator: The value of operand is first incremented & then used.
E.g1. int a = 5; int x = ++a; value of x & a will be 6.
Post-increment operator: The value of operand is first used& then incremented.
E.g2. int a = 5; int x = a++; value of x will be 5 & a will be 6.

JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→31
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications

Practice Exercise of ++, -- Operators

Give the output of the followings: Q4: int a = 10, x = 20, b;


32
Q1: int a = 10; i) b = a++ + ++a + a++ + ++a;
i) b = a++; System.out.println (b+“ ” + a); //__________
System.out.println(b+“ ”+a); //__________ ii) x -= --x + x-- + ++x + ++x +x--;
ii) b = ++a; System.out.println (“x = ” + x); //__________
System.out.println (b+“ ”+a); //__________
Q2: int a = 20; Q5: Give the output of the followings:
i) b = --a; a) int x =20, y = 5, z;
System.out.println(a+“ ”+b); //__________ What is the value of z in
ii) b = a--; z = ++x * (y – –) – y ; Show the steps.
System.out.println(a+“ ”+b); //__________ b) int a = 10, b = 4, c, d;
iii) b = b--; c = ++a – b-- * 2;
System.out.println( b ); //__________ d = a-- / 2 * 5 - --b;
Q3: int x =20, y = 10, z; System.out.print (c + “ ” + d+“ ”+a);
What is the value of z in c) write the values of i, j, k, x.
z = ++x * (y – –) – y ; Show the steps. int i = 10;
Ans: z = ++x * (y – –) – y 9 int j = 15 + i++;
int k = ++j – i++%5;
21 * 10 - 9 A4) i) 48 14 ii) x = -77
z = 201 int x = i++ + 2 * i; A5) a) 101 b) 3 23 10
210 - 9

JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→32
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications

Practice Exercise of ++, -- Operators

33 Q6→ Give the output of the followings: f) Evaluate x if a = 12, x=2


a) y += ++y + y-- + -- y; when int y=8 x *= a-- + a++ + --a + ++a + a--;
System.out.print (“value of x and a = ” + x + a);
b) int a = 7;
a += a++ + ++a + --a + a--;
int b = 10; g) What is the value of x1 if x=5.
b = b++; x1 = ++x – x++ + --x;
System.out.print(a + “ ”+ b);
h) Give the output of the followings:
c) If a = -3, b = -10, c=0,
int a =5;
find the output for X and Y.
a++;
X = ++a – b++ + c-- + ++b * (--c) *3/4
System.out.println(a);
Y = c-- + b++ + a - 2 - ++c +a
System.out.print(a++);
d) int x=5; a -= (a--) - (--a);
x= ++x + x++ +6; System.out.println(a);
System.out.print(“X=”+x); A6 a)33 b)39 10
int b = 3*x++ + ++x * 5; c) X=20, Y=-14
System.out.print(“B=”+b); i) int a = 4; d) X=18 B=154
e) m=5
int b = a-- + ++a%5 + a++;
e) What is the value of m after evaluating f) value of x and a = 116 11
System.out.print(“B=” + b); g) 6
the following expression: (int m=10, n=6) System.out.print((a++ * ++a)/5); h) 6
m - = 9%++n + (n++)/2; 65
i) B=12 7
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→33
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;

Q1: int a = 10, x = 20;


i) int b = (x < 30) ? 100 : 200 ;
System.out.println( b ); //__________
ii) int b = (!(x != 30)) ? x*10 : a+20 ;
System.out.println( b ); //__________
iii) char x = ‘A’; int a;
a = (x==‘a’) ? ‘a’ : ‘A’;
System.out.println( a ); //__________

Q2: boolean b; int a=20


i) b = (a+10 < 30) ? false : true ;
System.out.println( b ); //__________ Bitwise operator:
ii) int c = (!(a >= 20) && (a < 20)) ? a*100 : a+20 ; Bitwise operators act as special operators for the
System.out.println( c ); //__________ manipulation of data at bit level. They can be applied
only on data types like byte, short, long & char. E.g. &
(bitwise And), | (bitwise Or), ^ (XOR).
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→34
JMD HGGM
Ch4: Operators in Java ICSE Class 9th Sub: Computer Applications

35

JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→35
JMD HGGM
Ch5: Input in Java ICSE Class 9th Sub: Computer Applications

Different ways to input in Java


There are various ways to input the data values in Java. They are Q3→ WAP to find and display the simple interest
36
given below. and amount by using function argument when
1) By using a Function Argument principal, Interest and time are input from console.
2) By using a Scanner Class class Swap
3) By using a InputStreamReader Class {
Programs using function arguments and without static keyword public void main(int p, int r, int t)
{
Q1→ Find Subtraction and Division of given numbers. double si, amt;
class FirstPro si = (p*r*t)/100.0;
{ amt = p+si;
public void main(int a, int b) System.out.println(“The Simple interest = “+si);
{ System.out.println(“The amount = “+amt);
int sub, div; }
sub = a - b; }
div = a / b;
System.out.println(“Subtraction = " + sub);
System.out.println(“Division = " + div);
System.out.println(sub + “ is a subtraction ");
}
}

Q2→ Write the SOP line to print 10 - 20 = -10


in above Program.
JMD-HGGM
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→36
JMD HGGM
Ch5: Input in Java ICSE Class 9th Sub: Computer Applications

Programs using function arguments and with static keyword

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

Input Data at run time using scanner Class

Eg: import java.util.Scanner; // import java.util.*;


39
class Input_Data Scanner Methods To Input String, word and character.
{ next().charAt(0) → used to get character.
public static void main(String args[ ]) next() → used to get word.
{ nextLine() → used to get sentence/string.
Scanner sc = new Scanner (System.in);
Scanner Methods To Input All Type Numeric values
System.out.println("Enter Number "); nextByte() → Used to input byte type number.
int a = sc.nextInt(); nextShort() → Used to input short type number.
System.out.println("Enter fractional Number "); nextInt() → Used to input Integer value.
double b = sc.nextDouble(); nextLong() → Used to input long type number.

System.out.println("Enter a character "); nextFloat() → Used to input float type number.


char ch = sc.next().charAt(0); nextDouble() → Used to input double type number.

System.out.println("Enter a word ");


String wrd = sc.next();

System.out.println("Enter a String/Sentence ");


String str = sc.nextLine();

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.

SYNTAX OF FUNCTION EXAMPLE


Return_type Function_name (Arguments)
1. int Math.min(int, int) int n = Math.min(4, 6); sop(n); // n = 4
2. double Math.min(double, double) double n = Math.min(4.7, 6.3); sop(n); // n = 4.7
3. int Math.max(int, int) int n = Math.max(6, 4); sop(n); // n = 6
4. double Math.max(double, double) double n = Math.max(4.7, 6.3); sop(n); // n = 6.3
double n = Math.pow(2.0, 3.0); sop(n); // n = 8.0
5. double Math.pow(double, double)
double n = Math.pow(5.0, -2.0); sop(n); // n = 0.04
double n = Math.sqrt( 4.0 ); sop(n); // n = 2.0
6. double Math.sqrt(double, double) double n = Math.sqrt( 6.25); sop(n); // n = 2.5
double n = Math.sqrt( -25.0); sop(n); // NaN
double n = Math.cbrt( 27.0 ); sop(n); // n = 3.0
7. double Math.cbrt(double, double) double n = Math.cbrt( 15.625 ); sop(n); // n = 2.5
double n = Math.cbrt( -3.375 ); sop(n); // -1.5
8. double Math.log( double ) double n = Math.log( 6.25 ); sop(n); // n = 1.8325
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→44
ICSE Class 9th Sub: Computer Application
JMD HGGM
Ch6: Mathematical Library Methods

SYNTAX OF FUNCTION EXAMPLE


45 Return_type Function_name (Arguments)
double n = Math.abs( 4.66 ); sop(n); // n = 4.66
9. double Math.abs( double )
double n = Math.abs( -6.25); sop(n); // n = 6.25
int n = Math.abs( 4 ); sop(n); // n = 4
10. int Math.abs( int )
int n = Math.abs( -6); sop(n); // n = 6
double n = Math.ceil( 8 ); sop(n); // n = 8.0
double n = Math.ceil( 8.49); sop(n); // n = 9.0
double n= Math.ceil( 8.5 ); sop(n); // n = 9.0
double n= Math.ceil( 8.99 ); sop(n); // n = 9.0
11. double Math.ceil( int/double )
double n= Math.ceil( -8); sop(n); // n = -8.0
double n= Math.ceil( -8.5); sop(n); // n = -8.0
double n= Math.ceil( -0.45); sop(n); // n = -0.0
double n= Math.ceil( -8.99 ); sop(n); // n = -8.0
double n = Math.floor( 8 ); sop(n); // n = 8.0
double n = Math.floor( 8.49); sop(n); // n = 8.0
double n = Math.floor( 8.5 ); sop(n); // n = 8.0
double n = Math.floor( 8.99 ); sop(n); // n = 8.0
12. double Math.floor( int/double )
double n = Math.floor( -8.0 ); sop(n); // n = -8.0
double n = Math.floor( -8.5); sop(n); // n = -9.0
double n = Math.floor( -0.45 ); sop(n); // n = -1.0
double n = Math.floor( -8.99 ); sop(n); // n = -9.0
ICSE Class 9th Sub: Computer Application
JMD HGGM
Ch6: Mathematical Library Methods

Math Methods → ceil() & floor()


46

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)

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→47


ICSE Class 9th Sub: Computer Application
JMD HGGM
Ch6: Mathematical Library Methods

Math Methods – round(), rint()


48

SYNTAX OF FUNCTION EXAMPLE


int n = Math.round( 8.0 ); sop(n); // n = 8
int n = Math.round( 8.49); sop(n); // n = 8
long n = Math.round( 8.5 ); sop(n); // n = 9
int n = Math.round( 8.99 ); sop(n); // n = 9
13. int / long Math.round(float/double)
int n = Math.round( -8.0 ); sop(n); // n = -8
long n = Math.round( -8.5); sop(n); // n = -8
int n = Math.round( -8.51 ); sop(n); // n = -9
int n = Math.round( -8.99 ); sop(n); // n = -9
double n = Math.rint( 8.0 ); sop(n); // n = 8.0
double n = Math.rint( 8.49); sop(n); // n = 8.0
double n = Math.rint( 8.5 ); sop(n); // n = 8.0
double n = Math.rint( 8.99 ); sop(n); // n = 9.0
14. double Math.rint( double )
double n = Math.rint( -8.0 ); sop(n); // n = -8.0
double n = Math.rint( -8.5); sop(n); // n = -8.0
double n = Math.rint( -8.51 ); sop(n); // n = -9.0
double n = Math.rint( -8.99 ); sop(n); // n = -9.0
System.out.println( Math.round(23.50) ); → 24 System.out.println( Math.rint(23.50) ); → 24.0
System.out.println( Math.round(24.50) ); → 25 System.out.println( Math.rint(24.50) ); → 24.0
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→48
ICSE Class 9th
JMD HGGM
Ch6: Mathematical Library Methods Sub: Computer Applications

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

Decision Making statements


50 As soon as the command is given to execute a program, the control reaches the first statement of the program. It keeps
executing the statements sequentially unless end of the program is reached. Such movement of control is called as flow of
control. There are three ways of ‘flow of control’.
1. Normal flow of control 2. Conditional flow of control 3. Multiple branching of control
Normal flow of control:
It is the normal procedure where the control keeps on executing each and every statement of the program from top to bottom.

Conditional flow of control / Selection Statement


A selection or decision making statements allows selective execution of the statements based of given condition. It enables
subsequent selection of some of the several possible actions. So it is called conditional flow of control.

Conditional flow of control statements are divided into following ways:


1. if statement
2. If and only if statement
3. If - else statement
4. If – else - if ladder statement / multiple if If statement
5. Nested if

One way if Two way if Multiple if Nested if

If statement If - else statement If - else – if statement


Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→50
Ch 7 : Conditional statement in Java ICSE Class 9th Sub: Computer Application
JMD HGGM

One way if statement - Syntax & Examples


51
If statement
It is used when a statement or a block of statements, to Q1- Find the output of e.g3 and e.g4 if
be executed based on a given condition, holds true. In a. per = 40
case, the condition is false, it ignores the block and b. per = 60
moves forward with the normal flow of control.
Eg3: Outputs
if(per>50)
Syntax:
{
if(condition)
System.out.print(“pass”);
{
System.out.print(per);
code
}
code
} Eg4:
Eg1: if(per>50)
if(per>50) System.out.print(“pass”);
{ System.out.print(per);
System.out.print(“pass”);
} Block or Compound statement:
Eg2: It is a set of two or more statements enclosed within a pair
if(per>50) of curly braces “{ }”.
System.out.print(“pass”);
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→51
Ch 7 : Conditional statement in Java ICSE Class 9th Sub: Computer Application
JMD HGGM

if and only if statements, Two way if - Syntax & Examples


52

If and only if statements Two way if – else


It is a logical situation in which multiple statements can be It is a logical situation in which either of the two actions is
operated selectively. Here all conditions have been checked to be performed depending upon specified condition.
one by one. When the condition is true, it performs on set of
statements, otherwise, it performs another set of
Syntax: Eg: statements.
if(condition) if(n>0) Eg1:
{ Syntax:
{ if(per>50)
code if(condition) {
System.out.print(“Positive”);
code { System.out.print(“pass”);
}
} code }
if(n<0)
if(condition) code else
{
{ } {
System.out.print(“Negative”);
code else System.out.print(“fail”);
} }
code {
if(n==0) Eg1:
} code
{ if(per>50)
if(condition) code
System.out.print(“Zero”);
} System.out.print(“pass”);
{ } else
code System.out.print(“fail”);
code Note : Curly bracket is used for multiple statement. If we
} have single statement then no need bracket.
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→52
Ch 7 : Conditional statement in Java ICSE Class 9th Sub: Computer Application
JMD HGGM

Multiple if - Syntax & Examples


53
Multiple if/ if-else if ladder
It has multiple conditions. It is executed from top to down. As soon as the condition in ‘if’ is true, the statements associated
with that ‘if’ are executed and rest of the if statements are bypassed. If non of the conditions are true statement in else are
executed. If the ‘else’ is not present, then no action is performed.
Eg1:
Syntax:
if(per>85)
if(condition) Eg2:
{
{ if(n>0)
System.out.print(“Grade A”);
code {
}
} System.out.print(“Positive”);
else if(per>70)
else if(condition) }
{
{ else if(n<0)
System.out.print(“Grade B”);
code {
}
} System.out.print(“Negative”);
else if(per>50)
else }
{
{ else if(n==0)
System.out.print(“Grade C”);
code {
}
} System.out.print(“Zero”);
else
{ }
System.out.print(“fail”);
}
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→53
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 ).

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→54


Ch 7 : Conditional statement in Java ICSE Class 9th Sub: Computer Application
JMD HGGM

Nested if - Syntax & Examples


55
‘if’ statement within if or else statement is called as nested ‘if’ statement.
Syntax: if(condition)
{
if(condition) Q12→ WAP to input name, gender(M/F) and age of a person and print output
{
according to following conditions.
code
}
else
{ Gender
code
}
} M/m F/f
else
{
if(condition)
{ age>21 age<=21 age>20 age<=20
code
}
else
He is major He is minor She is major She is minor
{
code
}
}
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→55
Ch 7 : Conditional statement in Java ICSE Class 9th Sub: Computer Application
JMD HGGM

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

Multiple branching of control - SWITCH Statement


57 Switch statement is an alternative of multiple if statement. Switch is used to select sections of code depending on specific
integer or character values. It is multiple branch selection statement. The value of the variable is compared with the
constant value and the corresponding case is executed. If there is no matching case, default is executed.

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.

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→57


Ch 7 : Conditional statement in Java ICSE Class 9th Sub: Computer Application
JMD HGGM

switch statement

Multiple branching of control - SWITCH Statement


58

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

Multiple branching of control - SWITCH Statement


59
Q2→ Find the output of following code snippet.
a) int a = 2; b) char x = ‘c’; int a = 10; c) int a = 3;
switch(a) switch(x) switch(a++)
{ { {
case 1: case ‘a’: case 1:
a++; a++; ++a;
break; break; break;
case 2: case ‘b’: case 2:
a+=2; a+=2; a+=2;
case 3: case ‘c’: case 3:
a++; a++; --a;
case 5: case ‘d’: case 5:
a*=3; a*=3; a*=3;
break; default: default:
default: a++; a++;
a++; } }
} System.out.println(“A = “ + a); System.out.println(“A = “ + a);
System.out.println(“value of a = “ + a);

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→59


Ch 7 : Conditional statement in Java ICSE Class 9th Sub: Computer Application
JMD HGGM

switch statement

Multiple branching of control - SWITCH Statement


60 Q3→ WAP to input an alphabet and check whether it is vowel or not using switch.
import java.util.*; import java.util.*;
class Weekdays class Weekdays
{ public static void main() {
{ Scannner sc = new Scanner(System.in); public static void main()
System.out.println(“Enter a number ”); {
char ch = sc.next().charAt(0); Scannner sc = new Scanner(System.in);
switch(ch) System.out.println(“Enter a number ”);
{ char ch = sc.next().charAt(0);
case ‘a’: switch(ch)
System.out.println(“vowel”); break; {
case ‘e’: case ‘a’:
System.out.println(“vowel”); break; case ‘e’:
case ‘i’: case ‘i’:
System.out.println(“vowel”); break; case ‘o’:
case ‘o’: case ‘u’:
System.out.println(“vowel”); break; System.out.println(“vowel”); break;
case ‘u’: default:
System.out.println(“vowel”); break; System.out.println(“not vowel”);
default: }
System.out.println(“not vowel”); } } } }}
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→60
Ch 7 : Conditional statement in Java ICSE Class 9th Sub: Computer Application
JMD HGGM

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.

Nested Switch Statement:


Switch statement within switch statement is called nested switch statement.

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→61


ICSE Class 9th Sub: Computer Application
JMD HGGM

Ch 8: Iterative Constructs in Java

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.

There are three Looping / Iteration statements.


1. for loop 2. while loop 3. 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

Ch 8: Iterative Constructs in Java

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

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→63


ICSE Class 9th Sub: Computer Application
JMD HGGM

Ch 8: Iterative Constructs in Java

For loop - Questions


64 Q1→ WAP to print numbers from 1 to 10 and 10 to 1.
Q2→ WAP to print odd numbers up to N numbers.(1, 3, 5, 7………..N)
Q3→ WAP to print even numbers up to N numbers.(2, 4, 6, 8………..N)
Q4→ WAP to print squares of odd numbers up to N numbers.(12, 32, 52, 72………..N2)
Q5→ WAP to print cubes of even numbers up to N numbers.(2 3, 43, 63, 83………..N3)
Q6→ WAP to print even numbers from N to 2 numbers.(N……….6, 4, 2)
Q7→ WAP to print squares of odd numbers from N to 12numbers.(N2……72, 52, 32, 12)
Q8→ WAP to print the sum of following series
a) 1 + 2 + 3 + 4 ……….N
b) 13 + 33 + 53 + 73………..N3
c) 1/22 + 1/42 + 1/62 + 1/82………..1/N2
Q9→ WAP to print sum, product, average of first N natural number.
Q10→ WAP to input a number and print its factorial.
Q11→ WAP to input a number and its power and calculate the power.
Q12→ WAP to input a number and print its multiplication table.
Q13→ WAP to print Fibonacci series up to N terms. 0, 1, 1, 2, 3, 5, 8, 13, ………..N terms
Q14→ WAP to print Tribonacci series up to N terms. 0, 1, 2, 3, 6, 11, 20, 37, ………..N terms
Q15→ WAP to input a number and check whether it is prime number or not.
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→64
ICSE Class 9th Sub: Computer Application
JMD HGGM

Ch 8: Iterative Constructs in Java

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++ );

Eg2: for( int i = 1 ; i<1000 ; i++ )


{
}
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→65
Ch 8: Iterative Constructs in Java ICSE Class 9th Sub: Computer Application
JMD HGGM

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

while loop - Questions


67
Q1→ WAP to input a number and print its factorial using while loop.
Q2→ WAP to input a number and find out the sum of its digits and total digits.
eg. if n = 2543 then sum of digits = 2+5+4+3 = 14, total number of digits = 4
Q3→ WAP to input a number and find out the sum of even and odd of its digits and total of even and odd digits separately.
eg. if n = 25438 then sum of even digits = 2+4+8 = 14, total number of even digits = 3,
sum of odd digits = 5 + 3 = 8, total number of odd digits = 2
Q4→ WAP to input a number of three digits and find out number is Armstrong or not Armstrong number.
(An Armstrong number is number which is equal to the sum of the cube of its digits.)
Example: consider the number 153, Sum of the cube of digits = 1 + 125 + 27 = 153. 153 is Armstrong number)
Q5→ WAP to input a number of any numbers of digits and find out number is Armstrong or not Armstrong number.
Q6→ WAP to input a number and find out reverse number and also check number is palindrome of not.
(A Palindrome number is number which is equal to the its reverse number.)
Example: consider the number 123, reverse number = 321 (123 is not Palindrome number)
Q7→ WAP to input a number and find out maximum or minimum digit.
Q8→ WAP to input starting number, ending number and step value and print series.
Eg. Starting no. = 2, ending no. = 20, step value = 4 then series will be 2, 6, 10, 14,18.
Q9→ WAP to input starting number, number of terms and step value and print series.
Eg. Starting no. = 5, number of terms = 6, step value = 3 then series will be 5, 8, 11, 14,17, 20.
Q10→Write a program to accept a number and check and display whether it is Super Six number or not.
(A Super Six number is number which contains at least two or more sixes.) Example: 2366, 60166 etc.

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→67


Ch 8: Iterative Constructs in Java ICSE Class 9th Sub: Computer Application
JMD HGGM

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

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→68


Ch 8: Iterative Constructs in Java ICSE Class 9th Sub: Computer Application
JMD HGGM

While loop

Differences & Similarity between loops


69

for loop 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.

while loop do - while loop

It first checks the condition & then executes the statements. It is used when number of iterations is unknown.

It is entry controlled loop. It is exit controlled loop.

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

Q1 → 1 1 1 1 1 Q2→ 1 2 3 4 Q3→ ABCDE


2 2 2 2 2 1 2 3 4 ABCDE
3 3 3 3 3 1 2 3 4 ABCDE
4 4 4 4 4 1 2 3 4 ABCDE
1 2 3 4

int x, y ; int x, y ; int x, y ;


for( x = 1; x <= 4; x++ ) for( x = 1; x <= 5; x++ ) for( x = 1; x <= 4; x++ )
{ { {
for( y = 1; y <= 5; y++ ) for( y = 1; y <= 4; y++ ) for( y=1; y<=5; y++ )
{ { {
System.out.print( x ); System.out.print( y ); System.out.print((char) (y+64));
} } }
System.out.println(); System.out.println(); System.out.println();
} } }

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→71


Ch 9: Nested Loop ICSE Class 9th Sub: Computer Application
JMD HGGM

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

Q16 → A B C D Q19 → 7 6 5 4 3 2 1 Q22 → A Q25 → 1 2 3 4 5


E F G H 6 5 4 3 2 1 A B 2 3 4 5
I J K L 5 4 3 21 A B C 3 4 5
M NO P 4 3 2 1 A B C D 4 5
3 2 1 A B C D E 5
Q17 → 1 2 1
1 Q23 → 6 Q26 → 1
1 2 2 3
6 5
1 2 3 Q20 → 1 2 3 4 5 6 5 4 4 5 6
1 2 3 4 1 2 3 4 7 8 9 10
6 5 4 3
1 2 3 4 5 1 2 3 11 12 13 14 15
6 5 4 3 2
1 2 6 5 4 3 2 1
Q18 → 6 1
5 6 Q24 → 5 4 3 21 Q27→ #
4 5 6 Q21 → 1 5 4 3 2 # *
3 4 5 6 2 1 5 4 3 # * #
2 3 4 5 6 3 2 1 5 4 # * # *
1 2 3 4 5 6 4 3 2 1 5 # * # * #
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→73
Ch 9: Nested Loop ICSE Class 9th Sub: Computer Application
JMD HGGM

Series Questions Q61 → 1


74 01
Print the following series. 010
Q1 → 0, 1, 2, 3, 6, 11, ……………………….n terms. 1010
Q2 → 1, 2, 4, 7, 11, 16, ………………………n terms.
10101

Q3 → 2, 22, 222, 2222, ………………………10 terms.


Q4 → 3, 5, 7, 11, 13, 17, 19, …………………n terms.
Find sum of the following series.
Q5 → 1! + 2! + 3! + 4! + 5! + 6! + . . . . . . . . . . .n!.
Q6 → 11 + 32 + 53 + 74 + 95 . . . . . . . . . . N terms.
Q7 → 11/2! + 22/4! + 33/6! . . . . . . . . . . . N terms.
Q8 → 1 + 1 + 2 + 3 + 5 + ………………. To N terms.
Q9 → 0 – 3 + 8 – 15 + …………………….to 10 terms.
Q10 → 1 + (1+2) + (1+2+3) + (1+2+3+4) + ……..(1+2+3+4+……n).
Q11 → 1 + (1+2)/2! + (1+2+3)/3! + . . . . . . . . (1+2+3+4+……n)/n!.
Q12 → a1 – a3 + a5 – a7 + ……………............ To N terms.
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→74
Ch 9: Nested Loop ICSE Class 9th Sub: Computer Application
JMD HGGM

Jump Statements - break & continue statement


75
break→
Break statement is used to exit from loop. Break statement will ignore the execution of statements used after break
statement in a loop. If break statement is found in inner loop then it terminates the inner loop only. Syntax→ break
Eg1: Eg2: Eg3:
for(int i=1;i<10;i++) for(int i=1; i<5; i++) for(i=1; i<10; i++)
{ { {
if(i>=5) for(int j=1; j<10; j++) System.out.print(i, “ ”);
break; { if(j>=5) break;
System.out.print(i+“ ”); break; }
} System.out.print(j, “ ” );
Output ___________ } Output ___________
} Output __________
continue statement→
It is used to continue with the next iteration value of the loop. Continue statement will ignore the execution of statements
used after continue statement in a loop. Continue statement cannot be used in switch. Syntax – continue
Eg1: for(int i=1; i<=10; i++) Eg2: for(int i=1;i<11;i++)
{ {
if(i>=5 and i<=8): System.out.print(i+“ ”);
continue; continue;
System.out.print(i+“ ”); }
} Output _________ Output _______________
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→75
Ch 9: Nested Loop ICSE Class 9th Sub: Computer Application
JMD HGGM

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

PROCEDURAL ORIENTED PROGRAMMING (POP)


Procedural programming paradigm focuses on breaking down a programming task into a collection of small modules known
as sub routines or procedures / function. It follows top down approach in program design. It uses procedures to operate on
data. It doesn’t have any access specifier. In POP, Overloading is not possible. It doesn’t provide Data Hiding so provides
less security. Examples are ALGOL, COBOL, BASIC, PASCAL, C etc.

OBJECT ORIENTED PROGRAMMING (OOP)


It is a very user friendly and a powerful approach to solve basic to difficult problems. It bundles data and methods together
in a single unit which is known as data Encapsulation. It follows bottom up approach in program design. It has access
specifiers named Public, Private, Protected, etc. Object Oriented Programming allows some special features such as
Polymorphism, Inheritance and Data hiding. In OOP, Overloading is possible in the form of function overloading and
Operator overloading. It provides Data Hiding so provides more security. The three most common examples are Smalltalk,
C++ and Java.

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.

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→77


ICSE CLASS - IX
Object Oriented Programming

JMD
HGGM Components of OOP Computer Application
78

The major components of Object Oriented Programming are as follows:


1. Class 2. Object 3. Data Members & Methods 4. Access Specifier and Visibility Modes
CLASS
Class is a user defined data type in which data members and member functions/methods are combined together in a single
unit. It is also known as Data Encapsulation. Class ➔ Data Members + Member Functions / Methods
Class helps to keep the data members in various visibility modes as private, public and protected. Usually, data members of
a class are kept in private or protected visibility modes and methods are kept in the public visibility mode. In java, Default
access specifier is public.

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.

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→78


ICSE CLASS - IX
Object Oriented Programming

JMD
HGGM Creating Class & Object Computer Application
79

HOW TO CREATE A CLASS :


Syntax:
public class class_name
{
private Data members declaration;
public Member functions declaration;
}
OBJECT :
Object is an instance (variable) of a class that is capable of holding actual data in memory locations. All objects of the same
class have same properties & functions. Objects can access only public members of the class.
Class and objects are related to each other in the same way as data type and variables.
SYNTAX TO CREATE A OBJECT
Class_name Object_name1 = new Class_name();
Eg: Student s1 = new Student();
NEW: It is a keyword which is use to create an object and helps to allocate memory to an object.
Object can access the public member data & function of class using dot(.) operater.
How To Access : Example:
Syntax: S1.input();
ObjectName . publicMemberName; S1.x;
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→79
ICSE CLASS - IX
Object Oriented Programming

JMD
HGGM OOPS IN JAVA Computer Application
80

Object Oriented Programming


In object oriented programming, programs can be divided in to smaller segments & then connected together to make a
whole conceptualized unit.
Process Oriented Object Oriented
The program revolves around the code. The program revolves around the data.
It follows top-to-bottom approach. It follows bottom-to-top approach.

Eg: C, Basic, Fortran Eg: C++, Java


Class: A class is a blueprint or a prototype that defines characteristics & behaviour common to all objects of a certain kind.
It is the fundamental building block of Object Oriented Programming.
Eg: class - Student
characteristics: name, roll, std, marks.
Behaviour: calc_tot(), calc_per().
Object:
Object is an identifiable entity with some characteristics & behavior. It can be a place, a thing or a human.
Eg. Dog have state(name, colour, breed) & behaviour (barking, fetching, wagging the tail).

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→80


ICSE CLASS - IX
Object Oriented Programming

JMD
HGGM OOPS IN JAVA Computer Application
81

Objects related to a class: Principles of OOP & their benefits:


A class is a blueprint or prototype that defines variables & Encapsulation:
methods common to objects of same kind. It combines objects & Wrapping and bundling of data & functions together
methods for manipulating the data in to one neat package. in to a single unit called class is known as
Differentiate class & objects: Encapsulation. It prevents data from outside use &
interference. Encapsulation is implemented through
Class Objects access-specifiers.
Benefits: Modularity & Information Hiding.
A class is a template for an A object is an instance of a
object. class.
Inheritance: It is an ability of object of one class to
Class does not exist in the Object exist in the computer acquire properties of object of another class. It
computer memory. memory. follows the concept of hierarchy.
Benefits: It helps in code re-usability.
Object factory:
A class is known as object factory as you can create numerous Polymorphism: It means one thing many forms. It
objects of similar characteristics & behaviour from a class. A class is an ability of one object to respond to same
determines the nature of an object. message in one or more different ways.
Class encapsulate: Benefits: Function Overloading & Overriding.
A class encapsulates state & behaviour of an object i.e. data
members & member methods.
Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→81
ICSE CLASS - IX
Object Oriented Programming

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.

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→82


ICSE CLASS - IX
Object Oriented Programming

JMD
HGGM OOPS IN JAVA Computer Application
83

Object implementation in software terms:


In software terms the state of an object is stored in a variable & behaviour is determined using member methods. The
variables & methods are encapsulated in a class.
User defined data-type
Java allows us to create new data types with the help of primitive data types. Such data types are known as user-defined
data types. Size of these data types is not fixed. They are also called as composite data type or Reference data type.

Class known as a user defined type:


A class binds one or more primitive data type to be used as a single data type. A class can contain variables of different
data types so it is called as a composite data type. We can declare objects as variable of class type.
Eg:
class Emp
{
int empid;
String name;
double bsal;
}

Practice Makes Perfect WWW.YPComputerClasses.in 7798161873 p→83

You might also like