package conditionalstmts;
import java.util.Scanner;
public class IfExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
// int y=sc.nextInt();
// int z=sc.nextInt();
// if(x>y && x>z)
// System.out.println(x+" is greater");
// else if(y>z)
// System.out.println(y+" is greater");
// else
// System.out.println(z+" is greater");
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;
// Print values between 1 - 10
public class LoopExample {
public static void main(String[] args) {
int i=1;
// while(i<=10){
// System.out.println("i value is "+i);
// i++; //i=i+1 2,3,4....9,10,11
// }
// do{
// System.out.println("i value is "+i);
// i++; //i=i+1 2,3,4....9,10,11
// }while(i<=10);
// 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
}
}
switch : multiple selections/options
syntax:
switch(condition)
{
case value1: statements;
case value2 : statments;
:
:
case valuen: statements;
default: statements;
}
control break statements:
break : terminates the current block
continue : skip the current block
Looping/Iterative Statements:
initial value updation condition
1 increment 10
10 decrement 1
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:
start updation end
initial value increment/decrement condition/bool expression
1 increment 10
10 decrement 1
Types of Looping statments:
1. while : infinite loop, if the condition is true then the set of statements will get executed
syntax:
initial value;
while(boolexp){
statements;
updation;
}
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
Robust --> Strong
|
Memory Management --> Dynamic memory management --> Runtime memory allocation -->
Allocation and Deallocation is automatic
Exception Handling --> Handle the errors at runtime
ASCII codes
Java Code
.java code --> source code
| compiler
.class code/byte code
JDK --> library
|
packages
|
classes,interfaces and their methods/functions
Packages:
java.io
java.lang
java.math
java.util
java.awt
java.net
java.text
Use packages in program:
import packagename;
ex:
import java.io.*; // import all the classes and its methods
import java.util.Scanner; // import only scanner class
Command Line Arguments:
Expression: collection of operands and operators
Arithmetic Expression--> return an numeric value
x+y => x,y are operands and + is operator
x++ => x is operand and ++ is operator
Boolean Expression --> boolean value (True/ False)
x>y => x,y are operands and > is operator
x>y && x<y --> x,y operands , >,&&,< --> operators
Conditional statements: Boolean expression
1. If statements : it will only have true situation
2. if else statement
3. else if statement
4. nested if statement
5. switch statement
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;
public class CheckGrade {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
float avg=sc.nextFloat();
if(avg>=80){
System.out.println("Distinction");
}
else if(avg>=60 && avg<80){
System.out.println("First division");
}else if(avg>=45 && avg<60){
System.out.println("Second division");
}else if(avg>=40 && avg<45){
System.out.println("Pass");
}else {
System.out.println("Promotion not granted");
}
}
}
package programs;
import java.util.Scanner;
public class CheckGrade {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Marks for Physics, Biology and Chemistry subjects");
int physics=sc.nextInt();
int biology=sc.nextInt();
int chemistry=sc.nextInt();
int total=physics+biology+chemistry;
double avg=(double) total/3;
if(avg>=80){
System.out.println("Distinction");
}
else if(avg>=60 && avg<80){
System.out.println("First division");
}else if(avg>=45 && avg<60){
System.out.println("Second division");
}else if(avg>=40 && avg<45){
System.out.println("Pass");
}else {
System.out.println("Promotion not granted");
}
}
}
IPO --> Input --> Process --> Output
OutPut:
syntax:
System.out.printn("Message");
Introduction to Object Oriented Programming:
Concepts:
--> class :
collection of objects
--> Blue print of real world entity
class: Dog
|
properties/data --> color,breed,structure
behaviours/functions/methods -->
Object : Pomerian,lab,dalmation,....
--> object : instance of the class
--> abstraction : data hiding
--> encapsulation : data binding (wrapping of data and its functions/methods)
--> inheritance : define a new class from already exisiting class --> reusability
--> polymorphism : Poly --> many, morph --> forms
Process of using a function for carrying multiple operations
calculator : +, * , /, -
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;
public class CheckGrade {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
float avg=sc.nextFloat();
if(avg>=80){
System.out.println("Distinction");
}
else if(avg>=60 && avg<80){
System.out.println("First division");
}else if(avg>=45 && avg<60){
System.out.println("Second division");
}else if(avg>=40 && avg<45){
System.out.println("Pass");
}else {
System.out.println("Promotion not granted");
}
}
}
package programs;
import java.util.Scanner;
public class CheckGrade {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Marks for Physics, Biology and Chemistry subjects");
int physics=sc.nextInt();
int biology=sc.nextInt();
int chemistry=sc.nextInt();
int total=physics+biology+chemistry;
double avg=(double) total/3;
if(avg>=80){
System.out.println("Distinction");
}
else if(avg>=60 && avg<80){
System.out.println("First division");
}else if(avg>=45 && avg<60){
System.out.println("Second division");
}else if(avg>=40 && avg<45){
System.out.println("Pass");
}else {
System.out.println("Promotion not granted");
}
}
}
package programs;
import java.util.Scanner;
public class CheckDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number=sc.nextInt();
int number1=number;
int count=0;
while(number>0){
count++;
number=number/10;
}
if(count==3)
{
double cuberoot=Math.cbrt(number1);
System.out.println("Cuberoot "+cuberoot);
}else if(count==2){
double squareRoot=Math.sqrt(number1);
System.out.println("Square Root "+squareRoot);
}else if(count==1){
double square=number1*number1;
System.out.println("Square "+square);
}else {
System.out.println("The number entered is more than three digits");
}
}
}
package programs;
import java.util.Scanner;
public class DistanceCovered {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double distance = sc.nextDouble();
double fare=0;
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);
}
}
________________________________________________________________________________________________________