import java.util.*;
class demo
{
public static void main(String aa[])
{
int x;
double d;
float f;
String name;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter integer
value”);
x = sc.nextInt();
System.out.println(“Enter double
value”);
d = sc.nextDouble();
System.out.println(“Enter float value”);
f = sc.nextFloat();
System.out.println(“Enter string
value”);
name = sc.next();
System.out.println(“Following are the
values:”);
System.out.println(“Name ---- ”+name);
System.out.println(“Roll no. ---- ”+x);
System.out.println(“Double value ----
”+d);
System.out.println(“Float value ---- ”+f);
}
}
Structure
of Program
import java.io.*;
class demo
{
public static void main(String aa[])
{
System.out.println(“ ”);
}
}
·
public
is an access specifier that declares the
main() method as unprotected. Therefore, making it accessible to all other
classes.
·
static
is a keyword which declares this method
as one that belongs to the entire class and is not a part of any object of the
class. The main() must always be declared as static since the interpreter uses
this method before any object is created. So, static is required since it
executes before instance object.
·
void
is a keyword which means it can’t return
any value.
·
String
type array is used to store the command line
arguments.
Length
of array containing command line arguments
import java.io.*;
class command
{
public static void main(String aa[])
{
int sum =0;
for(int i=0; i<aa.length;i++)
sum+=Integer.parseInt(aa[i]);
System.out.println(“ ”+sum);
}
}
Note:
Class that can be used as both data-type as well class is known as “Rapper class”.
e.g.
Integer class can be used as:
int a; //datatype
a = Integer.parseInt(aa[i]);
·
Integer = class
·
parseInt = method
(static since no need for object)
Using
non-static method within static
Note:
·
Static can’t call
non-static member function.
·
No method can be
declared inside another method. It can only be called by another method.
·
A class’s object can be
created inside the same class.
·
Non-static method can
be used in static method by:
o Creating
object of class in static method, or
o Declaring
the non-static method as static.
Statement
is a group of tokens. There are 7 types
of statements:
1. Simple
Statement
2. Declarative
Statement
3. Assignment
Statement
4. Expression
Statement
5. Conditional
Statement
6. Repetitive
Statement
7. Jumping
Statement
e.g.
import java.io.*;
class statement
{
public
static void main(String aa[])
{
int
a,b; //declarative
int c=0; //assignment
Scanner
sc = new Scanner(System.in);
System.out.println(“Enter
2 nos.”); //simple
a
= sc.nextInt();
b
= sc.nextInt();
c
= a+b;
System.out.println(“Sum
is ”+c);
c
= a-b;
System.out.println(“Diff.
is ”+c);
c
= a*b;
System.out.println(“Product
is ”+c);
c
= a/b;
System.out.println(“Quotient
is ”+c);
}
}
If Statement
An if statement consists of a Boolean expression followed by one or
more statements. If statements have the following syntax:
if(Boolean_expression)
{
//Statements
will execute only if the Boolean expression is true
}
If-else
The if statement can be followed by an
optional else statement, which
executes when the Boolean expression is false. The syntax for an if-else looks
similar to:
if(Boolean_expression)
{
//Executes
when the Boolean expression is true
}
else
{
//Executes
when the Boolean expression is false
}
If-else-if
ladder
The else block can also contain another
if statement, creating a series of if-else statements in which only one if
block of code will execute. This is used when there are multiple conditions
with range. The syntax looks similar to:
if(Boolean_expression)
{
//Statements
}
else if(Boolean_expression)
{
//Statements
}
else if(Boolean_expression)
{
//Statements
}
//And so on,
until
else
{
//Statements
}
When using an if-else statement as in
the format above, the final else block is optional.
Nested If
The body of If statement can further
include another If statement. The syntax looks similar to:
if(Boolean_expression)
{
if(Boolean_expression)
{
if(Boolean_expression)
{
//Statements will execute only if all the 3 Boolean
expressions are true
:
//and so on
}
//Statements will execute only if outer 2 Boolean
expressions are true
}
//Statements will execute only if the outermost Boolean expression is
true
}
We can include
as many if-statements as we want.
Switch
Statement
A switch statement allows a variable to
be tested for equality against a list of values. Each value is called a case, and the variable being switched
on is checked for each case.
The syntax for
a switch statement looks similar to the following:
switch(variable)
{
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
A for loop is a repetition control
structure that allows you to efficiently write a loop that needs to execute a
specific number of times. The syntax of a for loop is:
for(initialization; Boolean_expression; update)
{
//Statements
}
While
Loop
A while loop is a control structure
that allows you to repeat a task a certain number of times. The syntax for a
while loop is:
initialization;
while(Boolean_expression)
{
//Statements
update;
}
When a while
loop is first reached, the Boolean expression is checked. If the Boolean
expression is true, the statements in the body of the loop execute. The flow of
control then goes back up to the Boolean expression, which is checked again. If
it is still true, the statements in the loop execute again. This process repeats
until the Boolean expression is false.
Do-while
Loop
A do-while loop is similar to a while
loop, except that a do-while loop is guaranteed to execute at least one time.
The syntax of a do-while loop is:
initialization;
do
{
//Statements
update;
}while(Boolean_expression);
Notice that
the Boolean expression appears at the end of the loop, so the statements in the
loop execute once before the Boolean is tested. If the Boolean expression is
true, the flow of control jumps back up to do, and the statements in the loop
execute again. This process repeats until the Boolean expression is false.
Syntax using
for loop:
for(initialiazation;
condition; inc/dec)
{
//Statements
for(initialiazation; condition;
inc/dec)
{
//Statements
}
// Statements
}
Ques. WAP to:
·
Enter train name
·
Booking person’s name
·
Choose type of ticket:
o
1 tier = 3000/_
o
2 tier = 2500/_
o
3 tier = 2000/_
o
Sleeper = 1000/_
·
No. of tickets
·
Cal. total amount
·
Also, provide option for cancellation, with cancellation fee = 10% of
booking charges
No comments:
Post a Comment