Computer Java Introduction For Class 9 Icse
Computer Java Introduction For Class 9 Icse
Java:- Java is a high level , robust, object oriented and secure programming language. Java was
developed by James Gosling and his colleagues at Sun Micro System in 1991.
Types of Java Applications:-
1. Standalone Application:- Program needs to install on every computer.
2. Java applets(Web application):- Runs on server side and can be used with the help of
a web browser.
3. Enterprise Application:- Runs on server and distributed and used in a network. eg.
Banking application.
4. Mobile Application: - Application developed for mobile devices.
Features of Java:-
1. It is object-oriented programming language.
2. It is case sensitive language.
3. It is two stage programming language i.e. compiled as well as interpreted.
4. It is platform independent programming language.
Java Package:- Java package is a collection of various classes. Each class contains different
functions. A package can be included in a program by using a keyword ‘import’.
eg. import java.io.*;
import java.util.*;
Some other packages are, java.lang, java.net, java.applet, java.awt, java.txt, java.math etc.
Keyword:- Keyword is a reserved word which is used for some specific purpose in the
programs. eg. class, if, System, break, else
Comment Lines:- Comment lines are non-executable lines written anywhere in the program
for describing the program or code.
It is of three types:-
I. Single Line Comment:- // used to write a comment in one line
II. Multiline Comment:- /* Used to write
comments in multiple lines */
III. Documenting Comment:- /** used to write
comments in paragraphs */
TOKEN:-
It is the smallest individual unit of a program.
eg. variable, constant, operator etc.
1. Identifier/ Variable
2. Assignment
3. Literal (Constant)
4. Punctuator ; . ?
5. Separator , () {} []
6. Operator
7. keyword
1)Primitive types:- These are basic data types which are independent of any other data type.
byte-----→1 byte(8 bits) ------------→ -128 to 127
short----→2 bytes(16 bits)---------→ -32768 to 32767
int ------→4 bytes(32 bits)----------→ -231 to 231-1
long----→8 bytes(64 bits)----------→ -263 to 263-1
float----→4 bytes(32 bits)--------→ upto 7 digits after decimal
double--→8 bytes(64 bits)-------→ upto 15 digits after decimal
char----→ 2 bytes(16 bits)-------→ Single Character
String--→ More than 16 bits-----→ Set of characters
ASCII CODES:-
A-Z (65-90)
a-z (97-122)
0-9 (48-57)
a= b + c * 4 - 10
Arithmetic Expression
Arithmetic Statement
Pure Expression:-An arithmetic expression that uses all its components of same data types.
Impure Expression(Mixed Expression):- An arithmetic expression in which one or more
components are of different data types.
[ Type Conversion/ Type casting]
Conversion of one data type into another.
1) Implicit type conversion: - In this conversion the data types are converted from lower data
type to higher data type automatically.
byte
char
short
int
long
float
double
(Hierarchy of Data Types- Lower to Higher)
[Operators]
Shorthand Operations:
a=a+b => a += b
a=a–b => a -= b
a=a*b => a *= b
a=a/b => a /= b
a=a%b => a %= b
eg.) if a = 8
a + = ++a + a++ +4;
a= a + ( ++a + a++ +4)
= 8 +(9 + 9 +4)
= 8 + 22 = 30
b) Binary Operators:- An arithmetic operator which deals with two operands.
+ addition
-subtraction
* multiplication
/ division
% modulus(remainder)
c) Ternary Operator:- This is a conditional operator which deals with three operands.
variable = (condition) ? expression1 : expression2
true
false
eg.) int a= 5, b= 6;
int c = ( a> b ) ? a : b ;
output c = 6;
d) Relational Operators:- An operator which compare two operands and determine the
relationship between them. It returns either a true value or a false value.
< less than
> greater than
<= less than or equal to
>= greater than or equal to
= = equal to
! = not equal to
eg.) a = 5, b = 7;
i) a > b (false)
ii) a + b = = 12 (true)
e) Logical Operators:- These operators return true or false depending upon the outcome of
different conditions.
AND (&&) (a>b) && (a>c)
OR ( | | ) (a>b) | | (a>c)
NOT ( ! ) ! (a==b)
1.) AND(&&)
Condition1 Condition2 Result
True False False
False True False
False False False
True True True
2.) OR( | | )
Condition1 Condition2 Result
True False True
False True True
False False False
True True True
3.) NOT ( ! )
Condition Result
True False
False True
import java.util.*;
public class Sum
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int a,b,c;
System.out.println(“Enter two numbers”);
a= sc.nextInt();
b=sc.nextInt();
c=a+b;
System.out.println(“Sum=” +c);
}
}
Mathematical Library Methods/Functions
The methods which are created by the system developers are called library methods or built-in
methods. Mathematical methods are used to carry out mathematical operations. All
mathematical methods are included in ‘Math’ class of java.lang package.
1.) Math.min()
This function returns the minimum of two numbers. Return type depends upon the arguments of
the function.
eg. int n=Math.min(12,22); then n=12
double n=Math.min(4.5, 2.5); then n= 2.5
2.) Math.max()
This function returns the maximum of two numbers. Return type depends upon the arguments of
the function.
eg. int n=Math.max(12,22); then n=22
double n=Math.max(4.5, 2.5); then n= 4.5
3.) Math.pow()
This function returns the value of first argument raised to the power to second argument. Return
type is always double data type.
eg. double n=Math.pow(2,3); then n= 8.0
4.) Math.sqrt()
This function returns the square root of a positive number. It will return a double data type value.
eg. double n= Math.sqrt(9.0); then n= 3.0
Note- Square root of a negative number is imaginary value.
5.) Math.cbrt()
This function returns the cube root of a positive or a negative number. It will return a double
data type value.
eg. double n= Math.cbrt(27.0); then n=3.0
double n= Math.cbrt(-27.0); then n= -3.0
6.) Math.abs()
This function returns the absolute value of an argument (i.e. a positive number). Return type
depends upon the arguments of the function.
eg. int n= Math.abs(8) then n=8
double n= Math.abs( -8.0) then n=8.0
7.) Math.log()
This function returns the natural logarithmic value of a given argument in double data type.
eg. double n= Math.log(6.25); then n=1.8325
8.) Math.random()
This function returns a random number between 0 and 1. It will return a double data type value.
eg. double n= Math.random();
It will return any fractional number between 0 and 1.
9.) Math.round()
This function returns the value in rounded off form. It will return a value either in int data type
or long data type.
Rules for positive numbers: -
i) fractional part lies between 0(inclusive) and 0.5(exclusive)
Then, integer part of same number.
eg. Math.round(8.0) => 8
Math.round(8.49) => 8
ii) fractional part is more than or equal to 0.5
Then , next higher integer number
eg. Math.round(8.5) => 9
Math.round(8.99) => 9
Rules for negative numbers: -
i) fractional part lies between 0(inclusive) and 0.5(inclusive)
Then, integer part of same number.
eg. Math.round( -8.0) => -8
Math.round( -8.5) => -8
ii) fractional part is more than 0.5
Then , next lower integer number
eg. Math.round( -8.51) => -9
Math.round( -8.99) => -9
10.) Math.rint()
This function returns the nearest integer value for a given argument. It will always return a
double data type value.
Rules for positive numbers: -
i) fractional part lies between 0(inclusive) and 0.5(inclusive)
Then, integer part of same number.
eg. Math.rint(8.0) => 8.0
Math.rint(8.5) => 8.0
Math.rint(7.5) =>8.0 (if odd integer then next higher even number)
ii) fractional part is more than or equal to 0.5
Then , next higher integer number
eg. Math.rint(8.51) => 9.0
Math.rint(8.99) => 9.0
Rules for negative numbers: -
i) fractional part lies between 0(inclusive) and 0.5(inclusive)
Then, integer part of same number.
eg. Math.rint( -8.0) => -8.0
Math.rint( -8.5) => -8.0
Math.rint(-9.5) => -10.0 (if odd integer then next lower integer)
ii) fractional part is more than 0.5
Then , next lower integer number
eg. Math.rint( -8.51) => -9.0
Math.rint( -8.99) => -9.0
11.) Math.ceil()
This function returns the higher integer value for a given argument. It will always return a
double data type value.
eg. For positive numbers:
Math.ceil(8) => 8.0
Math.ceil(8.1) => 9.0
Math.ceil(8.95) => 9.0
eg. For negative numbers:
Math.ceil(-0.41) =>-0.0
Math.ceil(-8) => -8.0
Math.ceil(-8.1) => -8.0
Math.ceil(-8.95) => -8.0
12.) Math.floor()
This function returns the lower integer value for a given argument. It will always return a double
data type value.
eg. For positive numbers:
Math.floor(8) => 8.0
Math.floor(8.1) => 8.0
Math.floor(8.95) => 8.0
eg. For negative numbers:
Math.floor(-0.41) =>-1.0
Math.floor(-8) => -8.0
Math.floor(-8.1) => -9.0
Math.floor(-8.95) => -9.0