IX - Computer Applications - Promotional Exam Notes
IX - Computer Applications - Promotional Exam Notes
IX - Computer Applications - Promotional Exam Notes
…………………….. */
…………………………………
…………………………*/
[or]
Datatype variable=value;
Variable1=value;
Variable2=value;
……..
Variable=value;
if statement if(condition)
{
statements;
}
else
Statement;
if(Condition)
statements;
}
else if(condition)
{
statements;
}
……….
else
{
statements;
statements;
break;
.
case labeln:
statements;
break;
default:
statement;
}
do while statement do
{
statements;
}while(expression);
System.out.println(b);
Note: Logical errors occurs due to wrong logic in the program which needs to be
corrected by the user to get the exact output required by the user.
Example:
if(age>=17)
System.out.println(“Minor”);
else
System.out.println(“Major”);
Like this many errors can be made by the user depending upon the logic.
Keyword Description
byte Represents smallest integer in the range -128 to 127. Its size in bits is 8
short Represents integer value in the range -32767 to 32768. Its size in bits is
16 or 2 bytes.
int Represents integer value in the range -231 to 231-1. It is the default data
type in java among integers. Its size in bits is 32 or 4 bytes.
long Represents integer value in the range -263 to 263-1. Its size in bits is 64 or
8 bytes.
char Represents characters found in all the human languages which is called
Unicode in the range 0 to 65, 535. It also represents standard set of
characters known as ASCII in the range 0 to 127. Its size in bits is 16 or 2
bytes.
boolean Represents logical values true and false. Java reserves 8 bits, but uses
only 1 bit. 1 represents true value and 0 represents false value.
switch Used to test the value of the given variable or expression against a list of
case values.
default Gets executed when none of the case labels/constants matches with the
input.
continue Used to force early iteration in for loop. When used in while and do while
block, the control gets transferred to while expression.
while It repeats the block of statements while the expression that controls is
true.
Scanner class: The Scanner class is a class in java.util, which allows the user to read
values of various types. The Scanner looks for tokens in the input. A token is a series of
characters that ends with what Java calls whitespace. A whitespace character can be a
blank, a tab character, or the end of the line. Thus, if we read a line that has a series of
numbers separated by blanks, the scanner will take each number as a separate token.
Numeric and String Methods
Method Description
short nextShort() Returns the next token as a short value (16 bits).
int nextInt() Returns the next token as an int value (32 bits).
long nextLong() Returns the next token as a long value (64 bits).
float nextFloat() Returns the next token as a float value (32 bits).
double nextDouble() Returns the next token as a double value (64 bits).
Example:
import java.util.*;
class sccanner1
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter an integer");
int a=sc.nextInt();
System.out.println("Enter a float value");
float b=sc.nextFloat();
System.out.println("Enter a double value");
double c=sc.nextDouble();
System.out.println("Enter a long value");
long d=sc.nextLong();
System.out.println("Integer value "+a);
System.out.println("Float value "+b);
System.out.println("double value "+c);
System.out.println("long value "+d);
}
}
import java.util.*;
class sccanner2
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name");
String a=sc.next();//reads characters until first whitespace
System.out.println("Your name is "+a);
}
}
import java.util.*;
class sccanner3
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name");
String a=sc.nextLine();//reads characters including whitespaces till the user press
//enter key.
System.out.println("Your name is "+a);
}
}
Methods:
Example:
System.out.println (Math.round(5.6f)); //Output: 6
System.out.println (Math.round(4.5)); //Output: 5