Package
Package
import java.util.Scanner;
if (x > 0) { //outer if
// x value is positive check its even or odd
System.out.println(x + " is positive");
if (x % 2 == 0) { //inner if
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
} else {
System.out.println(x + " is not positive");
}
}
}
____________________________________________________________________________________________
package conditionalstmts;
// for(int j=1;j<=10;j++){
// System.out.println("j value is "+j);
// }
int x=1;
// while(true) {
// if(x==5)
// break;
// System.out.println(x);
// x++;
// }
for(;;)
{
if(x==5)
break;
System.out.println(x);
x++;
}
}
}
___________________________________________________________________________________________________
// Print the tables between 1-5
for(i=1;i<=5;i++) // 1,2,3,4,5,6
{
System.out.println("Table : "+i);
for (int j=1;j<=10;j++)//j=1,2,3,...10,11
{
System.out.println(i+"x"+j+" = "+(i*j));//2X1=2,2X2=4,2X3=6..2X10=20
}
}
Looping/Iterative Statements:
1. Intialization
2. Condition
3. Updation
Types of loops:
infinite looping
1. while : checks the condition first then executes the block of statement
syntax:
initialization
while(condition){
statements;
updation;
}
2. do..while: first time it will execute the block of statements irrespective of the condition
while
syntax:
initialization
do{
statements;
updation;
}while(condition);
finite looping
3. for
syntax:
for(intilization;condition;updation)
statement;
Nested Loops:
Loop inside another looping
syntax:
for(intilization;condition;updation)//outer loop
{
for(intilization;condition;updation)//inner loop
statement;
}
Conditional/Ternary operator:
exp1?exp2 :exp3
Loops/Iterative Statments:
2. do..while: infinite loop, it executes the block of code first then checks condition is true or not
syntax:
initial value;
do{0
statements;
updation;
}while(boolexp);
3. for
a=8
a -= ++a + a++ +4
a = a- (++a)+(a++)+4
a = 8- (9)+(9)+4
= 8 - 22
= -14
a *=
a = a*(
x++(5) * ++x
4 * 5+1
4*6
a -= 8
a = a-8
Type Casting:
Internal/Implicit conversion:
byte --> short --> int --> long --> float --> double
External/Explicit:
Type conversion
22/7*Math.pow(r,2)
Math.pow((a+b),2)/(a-b)
Platform Independent:
Windows Mac/Unix/Linux
.java .class file
| |
compile/compiler executing/Interpreter --> Runtime
| |
.class output
ASCII codes
Java Code
.java code --> source code
| compiler
.class code/byte code
Packages:
java.io
java.lang
java.math
java.util
java.awt
java.net
java.text
ex:
import java.io.*; // import all the classes and its methods
import java.util.Scanner; // import only scanner class
if: to check the condition and if it is true then block of statements will be executed
else: it will be executed when the if condition is false
Looping statements
________________________________________________________________________________________________________
package programs;
import java.util.Scanner;
}
}
package programs;
import java.util.Scanner;
}
}
Object : Pomerian,lab,dalmation,....
Inheritance:
Existing class: super class/ parent class
New class : sub class/ child class / derived class /target class
Java Program:
class ClassName{
declarations; --> data
public static void main(){ --> function
Statements;
}
}
package programs;
import java.util.Scanner;
}
}
package programs;
import java.util.Scanner;
}
}
package programs;
import java.util.Scanner;
}
}
package programs;
import java.util.Scanner;
if(distance<=5) {
fare = 100;
}if(distance<=15) { //12
fare=fare +(distance-5)*10;//12-5 =>7*10+100
}if(distance<=25) {
fare=fare +(distance-15)*8;//
}if(distance>25){
fare=fare +(distance-25)*5;
}
System.out.println("Taxi Number : 2589");
System.out.println("Distance Covered : "+distance);
System.out.println("Amount "+fare);
}
}
________________________________________________________________________________________________________