Uploaded by arun kumar

CoreJava for beginners

advertisement
30-05-2023
I.
II.
III.
IV.
V.
History of Java
Difference between JDK,JRE and JVM
Features of Java
Architecture of JVM
Installation of JDK Doubts
History of java
It was invented by James Gosling, Patrik Naughton,Mike Sheridan
Initially they named it as Oak -> a tree symbol of strength
Java -> Island in Indonesia
Where good quality large amount of coffee is produced
Java is just a name and it is not an acronym
Why Java?
Java is a Platform independent programming language and also
highly secured programming language
Quack, Angry Bird ->
It works on the concept of WORA -> Write Once Run Anywhere
In Java compiler converts our code into an intermediate code called
bytecode which can be run anywhere where ever JRE is there
This bytecode is JVM Dependent
Features of java
1) Simple
2) Secured
3) Platform Independent
4) High Performance
5) Object Oriented programming language
6) Interpreted
7) Robust
8) Dynamic
9) Distributed
10)
Architecture
Neutral
11)
Multithreaded
12)
Portable
1) Simple
It has same structure as that of C,C++ Programming language if we
know C,C++ then updating our knowledge in java is easier
It follows same construct as that of C,C++.
Difference between C++ and Java
i.
No goto Statements available in java
ii.
Constructor and destructor are there in C++
Where as in java only constructors are there
iii.
Operator overloading is not there in Java
2) Secured
Encryption, Decryption and Digital Signature are inbuilt in java
In Java we convert our code into bytecode ie .class file which is a
plain text file which can be distributed since no virus can occur in
text file
Impersonation, Eves Dropping, Tampering will happen in internet
which can be avoided by Encryption, Decryption and Digital
Signature
JVM while executing the code will verify the bytecode
3) Platform independent
WORA
4) Object Oriented programming language
It supports the concept of
a) Class and object
b) Encapsulation
c) Inheritance
d) Polymorphism
e) Abstraction
5) Architecture Neutral
Size of data type is fixed irrespective of which machine u r working
6) Interpreted
Interpreted programming language are slower in execution.
Compiler based programming languages are faster in execution.
7) High Performance
JIT Compiler(Just in Time Compiler)
8) Distributed
Java.net through which we can write network applications
9) Dynamic
Which is changing applet programming
10)
Robust
Strong or hard
In Java we have inbuilt exception handling which can handle
runtime errors
How Memory management is done in java?
Garbage Collector
11)
Multithreading
12)
Portable
31-05-2023
What are keywords?
Reserved words
-class,public,private,protected,default,int,short,byte,long,boolean,char,
long,float,double,static,void,if,else,else if,switch,case,for,while,do
while,break,continue,transient,
All keywords will be in lower case
We cannot use it for any other purpose other than for which it is defined
Programming language has these keywords for specific purpose
Java Data Types
Java is strongly typed programming language
1) Primitive Data Type
2) Non-Primitive Data Type -> class,array,string
Primitive Data Type
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
128 64 32 16 8 4 2 1
1) -byte -> 1 byte -> 8 bit -> -2^7 to 2^7-1 -> -128 to 127
2) -short -> 2 byte -> 16 bit -> -2^15 to 2^15-1 -> -32768 to 32767
3) -int -> 4 byte -> 32 bit -> -2^31 to 2^31-1 -> -2147483648 to
2147483647
4) -long ->8 byte -> 64 bit -> -2^63 to 2^63-1
5) -float -> 4 byte -> single precision -> upto 6 frac digits will be
identified
6) -double -> 8 byte -> double precision -> upto 15 fractional digits will
be identified
7) -char -> 2 byte -> UNICODE -> 256 foreign language characters
8) -boolean -> 1 bit -> true or false
class Hello{
public static void main(String[] args){
System.out.println("Hai Welcome to Java Learning");
if(args.length>=2){
System.out.println(args[0]);
System.out.println(args[1]);
}
System.out.println("Integer minimum value is :
"+Integer.MIN_VALUE);
System.out.println("Integer maximum value is :
"+Integer.MAX_VALUE);
}
}
//javac Hello.java
//java Hello
//java Hello Anudip JavaTutorials
//javap java.lang.String
//javadoc
//String is a class in java it is inside the package java.lang
//and java.lang is by default imported by all applications in java
//Integer int wrapper class which represents object of int data type
Java we have what are called as packages, class, interface, methods and
variables
Java is a collection of packages
Collection of classes and interfaces
Collection of methods and data
-java.lang, java.util, java.net,java.io,javax.swing,java.text,
- String, Integer, Collection, Collections
- concat, trim(), lastIndexOf()
We have Naming conventions in java
Convention is u may follow or u may not follow also
Program will run but for other developers to read and understand ur code we
follow naming convention
America, -america, London, -london
This is what is convention
1) Packages will be in lower case – com.arun.mypack
2) Class and Interface will start with upper case letter followed by each and
every word in upper case MyClass,MyInter
3) Methods will start with lowercase and each and every word following it
in uppercase myMethod(),myVar
4) Same for data also like how we write for methods ()
Comments
What is comment and why is it required
Comments are used to increase the readability and understandability of
the program
It is usually ignored by the compiler
It wont occupy any memory in space
So where ever required u can use comments and make the program
understandable
There are different types of comments in java
1) Single line comment -> //this is a single line comment
2) Multi line comment -> /*
Multi
Line
Comment */
3) Documentation comment -> /**
This
Is a
Documentation
Comment
*/ javadoc
This documentation comment is
used create an API file for us
/**This is just a class DocComment
to show the usage of documentation comment*/
public class DocComment{
/** This is the main method called by JVM
while starting the application*/
public static void main(String[] args){//this is a main method
System.out.println("Hello,Welcome to java Learning");
DocComment.display();/*
This is just
to display the
some output*/
}
/** this is just a display method*/
public static void display(){//This is just a display method
System.out.println("Happy Learning");
}
}
//How to extract documentation comment using javadoc tool
//javadoc DocComment.java
01-06-2023
How to use System.out.println
class HelloWorld {
public static void main(String[] args) {
int a = 10;
byte c = 20;
byte e = 30;
byte g = (byte)(c+e);//type casting int->4 byte byte->1 byte
float d = 10.54f;
System.out.println("Hello, World!");
System.out.print("Integer value is "+a);
System.out.println("Byte value is "+c);
System.out.printf("Float value is %f ",d);
System.out.println("Integer value is "+a+" \nFloat value is "+d+" \nByte
value is "+c);
System.out.println(a+d);
System.out.println(c+e);
System.out.println(g);
System.out.println("Addition of byte is "+(c+e));
System.out.println(c+e+" Addition of byte ");
}
}
//For float and double default is double
//10.54 compiler considers it as double
//double 8 byte whereas float is 4 byte
//use f or F for float value
//type casting
//20,20.54,1010.54
class HelloWorld {
public static void main(String[] args) {
int a =-1,b=-3;
System.out.println(a/b);//3 -> division reutrns quotient
System.out.println(a%b);//1 -> modulus returns remainder
a=-3;
b=7;
System.out.println(a/b);//3 -> division reutrns quotient 0
System.out.println(a%b);//1 -> modulus returns remainder -3
int d = -4%3*2+10/2*5%-3;//-1,-2
System.out.println("d = "+d);//d=?
}
}
//Operators
//Binary operator->a/b
//1) Arithmetic operator -> *,/,% -> 1st precedence +,- -> 2nd precedence
//if all occurs in same line then precedence will be from left to right
//-4%3*2+10/2*5%-3
//-1*2+10/2*5%-3
//-2+10/2*5%-3
//-2+5*5%-3
//-2+25%-3
//-2+1
//-1
//2) Relational operator -> >,<,>=,<=,==,!=
//3) Logical operator -> &&,||,!
//4) Assignment operator -> +=,-=,/=,%=,>>=
//5) Bitwise operator -> &,|,^,~
//6) Shift operator -> >>,<<,>>>
//7) Ternary or conditional operator -> ? :
//8) Unary operator -> ++,-//9) instanceof operator is available in java
//3 | 10 ( 3(/)q
// 9
// 1(%) r
//-10 | -3 ( 0(/)q
//
0
//
-3 SC->in modulus numerator symbol is retained in the result
//-3%-1 -> 0
//-3/-1 -> 3
//-1/-3 -> -3 | -1 ( 0(q)
//
0
//
-1 (r) SC->in moduls when num is less than deno reulst is num
//
//-1%-3 ->
02-06-2023
3) Relational Operator -> >,<,>=,<=,==,!=
It returns true or false whenever we test the condition we use
relational operator for it
class Sample{//Sample.main()
public static void main(String... args){
int a = 10,b = 20,c = 15,d=20,e=20;
boolean flag=true;
System.out.println(a<b);
System.out.println(b>c);
System.out.println(b==d);
System.out.println(b!=d);
System.out.println(a<b&&b==d&&b>c);
System.out.println(!flag||a<b);
//I want condition to check whether b,d and e are equal
System.out.println((b==c)||(++a<b));
System.out.println(a);//10,11
}
}
//we go for logical operator
//where two or more relational operators can be used
//logical And(&&) -> short circuited operation
//logical Or(||)
//logical Not(!)
class BitWiseOper{
public static void main(String... args){
int a = 12,b=14,c=23;
System.out.println(26^31);
System.out.println(~15);
System.out.println(~-15);
System.out.println("Before swapping a = "+a+" b = "+b);
a = a^b;
b = a^b;
a = a^b;
System.out.println("After swapping a = "+a+" b = "+b);
System.out.println(a^b^c);//for this we need to know precedence
}
}
//12^14^23
//1st 12^14->d
//2nd d^23
class Sample{//Sample.main()
public static void main(String... args){
int a = 10,b = 20,c = 15,d=20,e=20;
boolean flag=true;
System.out.println(a<b);
System.out.println(b>c);
System.out.println(b==d);
System.out.println(b!=d);
System.out.println(a<b&&b==d&&b>c);
System.out.println(!flag||a<b);
//I want condition to check whether b,d and e are equal
System.out.println((b==c)||(++a<b));
System.out.println(a);//10,11
}
}
//we go for logical operator
//where two or more relational operators can be used
//logical And(&&) -> short circuited operation
//logical Or(||)
//logical Not(!)
class SampleExa1{//Sample.main()
public static void main(String... args){
float bal=5600;
//i want to update the balance with 5% interest
bal+=(5*bal)/100;
System.out.println(bal);
}
}
class UnaryOper{
public static void main(String... args){
int a = 10,b = 20;
System.out.println(a++);//10 a = 11
System.out.println(++a);//12 a = 12
System.out.println(--b);//19 b = 19
System.out.println(b--);//19 b = 18
System.out.println("a = "+a+" b = "+b);
a = 10;
b = 20;
int c = a++ + ++a + a++ + a++;
System.out.println("c = "+c+" a = "+a);
}
}
//a = 10
//c = a++ + ++a + a++ + a++
//a++ -> 10 a->11
//++a -> 12 a->12
//a++ -> 12 a->13
//a++ -> 13 a->14
//
47
class TernaryOper{
public static void main(String... args){
System.out.println(10>20?10:20);
}
}
//if(10>20){return 10;}else{return 20;}
//Ternary operator is also called as conditional operator
class InstanceOfOper{
public static void main(String... args){
Integer in1=20;
boolean flag = in1 instanceof Integer;
System.out.println(flag);
String name="Arun Kumar";
flag = name instanceof String;
System.out.println(flag);
}
}
//Integer wrapper class
//for every primitive data type we have wrapper class
//ie we can represent it as objects
class SampleEx3{
public static void main(String... args){
int a = 10,b = -5,c = 6,d = 2;
c+=a%b*d+c/b+b%a*d;
System.out.println("c = "+c);
}
}
//only at last assignment will happen
//c+=a%b*d+c/b+b%a*d
//a%b -> 10%-5 -> 0
//2*d -> 0*2 -> 0
//c/b -> 6/-5 -> -1
//b%a -> -5%10 -> -5
//-5*d -> -5*2 -> -10
//0-1-10
//-11
//c+=-11 -> c = 6-11 -> c=6-11=-5
//-5 | 6 (-1
// 5
// 1
class SampleEx3{
public static void main(String... args){
int a = 10,b = -5,c = 6,d = 2;
c+=a^d>>d+a++/++b;
System.out.println("c = "+c);
}
}
//1st unary
//2nd arithmetic
//3rd shift operator
//4th bitwise opera
//5th assignment opera
//6
//a++ -> 10 -> a=11
//++b -> -4 -> b=-4
//10/-4 -> -2
//d-2->2-2=0
//d>>0->2>>0->2
//a = 10 -> 1 0 1 0
//d = 2 -> 0 0 1 0
//
-> 1 0 0 0 ->8
//c+=8 -> 14
//boolean f = in1 instanceof Integer -> will return true or false
//to store it we need boolean variable
//we are using command line argument and passing string type of
arguments
//I want to perform addition
class WrapperClass{
public static void main(String... args){
if(args.length>=2){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a+b;
byte d = Byte.parseByte(args[2]);
byte e = Byte.parseByte(args[3]);
byte f = (byte)(d+e);
float g = Float.parseFloat(args[4]);
float h = Float.parseFloat(args[5]);
float m = g+h;
System.out.println("Addition of integer is "+c);
System.out.println("Addition of byte is "+f);
System.out.println("Addition of float is "+m);
}else{
System.out.println("Input 2 or more
parameters");
}
}
}
//for every data type we have wht is called as wrapper class
//boolean f = in1 instanceof Integer -> will return true or false
//to store it we need boolean variable
//we are using command line argument and passing string type of
arguments
//I want to perform addition
class WrapperClass{
public static void main(String... args){
if(args.length>=2){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a+b;
byte d = Byte.parseByte(args[2]);
byte e = Byte.parseByte(args[3]);
byte f = (byte)(d+e);
float g = Float.parseFloat(args[4]);
float h = Float.parseFloat(args[5]);
float m = g+h;
System.out.println("Addition of integer is "+c);
System.out.println("Addition of byte is "+f);
System.out.println("Addition of float is "+m);
System.out.println("Square of "+a+" is
"+Math.pow(a,2));
System.out.println(Math.ceil(Math.PI));
System.out.println(Math.ceil(Math.sin(90)));
}else{
System.out.println("Input 2 or more
parameters");
}
}
}
06-06-2023
1) System.out.println()
2) System.out.print()
3) System.out.printf()
Now how to get input from the user
There are many ways
-we are going to use a package java.util
-Scanner class using this we are going to get input from user
-next(),nextLine(),nextByte(),nextInt,nextShort(),nextLong(),nextBoolean()
import java.util.*;
class Sample{
public static void main(String... args){
int a;float b;byte c;boolean d;
Scanner sc = new Scanner(System.in);
System.out.println("Input a value : ");
a = sc.nextInt();
System.out.println("Input a float value : ");
b = sc.nextFloat();
System.out.println("Input a byte values : ");
c = sc.nextByte();
System.out.println("Input a boolean value : ");
d = sc.nextBoolean();
System.out.println("a = "+a+" b = "+b+" c = "+c+" d = "+d);
}
}
//3 streams 1) System.out 2) System.in 3) System.err
//Input is celsius convert into farenheat and display it
//fh = (1.8f*cel)+32
//Hint use float
import java.util.*;
class FhToCel{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input the degree in celcius : ");
float cel = sc.nextFloat();
float fh = (1.8f*cel)+32;
System.out.println("Degree in farenheat is "+fh);
}
}
//Input is radius of circle find the area of the circle
//use Math.PI
//aoc = Math.PI*rad*rad;
class Aoc{
public static void main(String... args){
Scanner obj = new Scanner(System.in);
System.out.println("Input the radius of the circle : ");
int rad = obj.nextInt();
double aoc = Math.PI*rad*rad;
System.out.println("Area of the circle is "+aoc);
}
}
//Input is coefficients a,b,c find the quadratic equation
//-b+sqrt(b^2-4*a*c)/(2*a) Math.sqrt(),Math.pow(b,2)
class QEqua{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input the coefficients a,b and c");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
double qe = (-b+Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a);
System.out.println("Quadratric equation = "+qe);
}
}
//Basic control statements available in java
//1) Simple if
//2) if else
//3) if else if else if ... else
//4) Nested if
//5) Switch case default
//How to use simple if and write programs
//Input is a number check whether it is positive
//simple if
import java.util.*;
class IfElse{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input a number : ");
int num = sc.nextInt();
if(num>=0){System.out.println("Number is positive");}
else{System.out.println("Number is negative");}
}
}
//Input is age of a person print whether he is eligible to vote or not
//if else
class VoterEligibility{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input the age of the person : ");
int age = sc.nextInt();
if(age>=18){System.out.println("He is eligible to vote");}
else{System.out.println("He is not eligible to vote");}
}
}
//Input is years display whether it is leap year or not
//if year is divisible by 4 then leap year
//if(year%4==0)
class LeapYear{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input the year : ");
int year = sc.nextInt();
if(year%4==0){System.out.println("Year is a leap year ");}
else{System.out.println("Year is not a leap year ");}
}
}
//input is a number print whether it is even or odd
//num%2==0
//num%2!=0 ^
//is there any other way other than num%2==0
class EvenOrOdd{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input a number : ");
int num = sc.nextInt();
if(num%2!=0){System.out.println("It is odd");}
else{System.out.println("It is even");}
if((num&1)==0){System.out.println("It is even");}
else{System.out.println("It is odd ");}
}
}
//Input is three sides of a traingle sa,sb,sc print whether the triangle is
Iscocels,equilateral or scalene
//Equilateral -> all 3 sides are equal
//Isoceles -> any 2 sides equal
//Scalene -> none of the sides are equal
//use if(){s.o.p(equilateral);} else if(){s.o.p(Isoceles);} else{s.o.p(scalene);}
class Traingle{
public static void main(String... args){
Scanner obj = new Scanner(System.in);
System.out.println("Input the 3 sides of the triangle");
int sa = obj.nextInt();
int sb = obj.nextInt();
int sc = obj.nextInt();
if(sa==sb&&sb==sc){System.out.println("Equilateral");}//sa==sb==sc(wro
ng)sa==sb&&sa==sc
else if(sa==sb||sa==sc||sb==sc){System.out.println("Isoceles");}
else{System.out.println("Scalene");}
}
}
//Input is coefficients of the quadratic equation find the determinent and roots
of the qe
//input a,b,c determinent
//b^2-4ac
//
//input is babies age in number of days i want an ouput of babies age in
years,months and days
//500 -> 1 year 4 months 20 days
//Hint : Use Scanner class and operators
//Division Arithmetic operator
//modulus
/*360 | 500 ( 1(/)Q YEAR
360
30 | 140 ( 4(/)Q MONTHS
120
20 (%) R Days*/
//1st step get the input
import java.util.*;
class BaInNumOfDays{
public static void main(String... args){
Scanner ins = new Scanner(System.in);
System.out.println("Input the babies age in number of days : ");
int banod = ins.nextInt();
int yrs = banod/360;//1 year
//int mon = (banod-360)/30;//4 months
int mon = banod%360/30;//4 months
int days = banod%360%30;//20 days
System.out.println("Babies age is "+yrs+" Year "+mon+" months
and "+days+" days");
}
}
07-06-2023
//ATM
//In am ATM machine the user is asked to enter the pin number
//Validate the pin number it should be 4 digit number and range is 1111 to
9999
//if he is authorized then get the amount to be withdrawn
//check whether the amount is in multiples of 100 if it is correct
//then display amount withdrawn else display unable to dispense cash
import java.util.*;
class ATM{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter ATM Pin number : ");
int pinNum = sc.nextInt();
if(pinNum>=1111&&pinNum<=9999){
System.out.println("Authorized");
System.out.println("Enter the amount");
int amount = sc.nextInt();
if(amount%100==0){
System.out.println(amount+" withdrawn");
}else{
System.out.println("Unable to dispense cash");
}
}else{
System.out.println("UnAuthorized");
}
}
}
//There are 3 sides of a triangle check whether they do form a triangle
//Input is 3 sides of a triangle find the largest side and add the other
//two sides if ls is greater than the addition of other two sides
//then display sides form a triangle else display sides do not form a
//triangle
class Traingle{
public static void main(String... args){
Scanner obj = new Scanner(System.in);
System.out.println("Input 3 sides of the triangle :");
int sa = obj.nextInt();
int sb = obj.nextInt();
int sc = obj.nextInt();
int ls = (sa>sb)?(sa>sc?sa:sc):(sb>sc?sb:sc);
//sa>sb if sa is largest then sa or sc should be largest
//sa>sb if sb is largest then sb or sc should be largest
if(ls==sa){
int aots = sb+sc;
if(ls>=aots){
System.out.println("Sides form a triangle");
}else{
System.out.println("Sides do not form a triangle");
}
}else if(ls==sb){
int aots = sa+sc;
if(ls>=aots){
System.out.println("Sides form a triangle");
}else{
System.out.println("Sides do not form a triangle");
}
}else{
int aots = sa+sb;
if(ls>=aots){
System.out.println("Sides form a triangle");
}else{
System.out.println("Sides do not form a triangle");
}
}
}
}
//Switch case
class SwitchCase{
public static void main(String... args){
switch(10){//
case 1:
System.out.println("ONE");
break;
case 2:
System.out.println("TWO");
break;
case 3:
System.out.println("THREE");
break;
case 4:
System.out.println("FOUR");
break;
case 5:
System.out.println("FIVE");
break;
case 3%2+4%-5+1*2-1:
System.out.println("SIX");
break;
case 7:
System.out.println("SEVEN");
break;
case 8:
System.out.println("EIGHT");
break;
case 9:
System.out.println("NINE");
break;
default:
System.out.println("None of the cases matched with
expression");
}
}
}
//5%-4*4/2+5
//1*4/2+5
//4/2+5
//2+5
//7
//3%2+4%-5+1*2-2
//1+4+2-1
//Switch case
import java.util.*;
class SwitchCase{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input a number : ");
int num = sc.nextInt();
switch(num&1){
case 0:
System.out.println("EVEN");
break;
case 1:
System.out.println("ODD");
}
}
}
//input is a number display whether even or odd using switch case
class Switch1{
public static void main(String... args){
switch(10){
case 5:
switch(8){
default:
}
default:
}
}
}
//Input a day in 1-7 and print the day in words
//we will go for switch case
//first of all why we need switch case
class DaysInWords{
public static void main(String... args){
Scanner obj = new Scanner(System.in);
System.out.println("Input days 1-7");
int day=obj.nextInt();
switch(day){
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Input days only 1-7");
}
}
}
//print natural between 1 to 10 using all loops
class LoopsInJava{
public static void main(String... args){
int i=1;
while(i<=10){//i=11<=10(false)loop stops
System.out.print(i+" : ");
i++;
}
//i=11
System.out.println("--------DO WHILE LOOP----------");
i=1;
do{
System.out.print(i+" : ");
i++;
}while(i<=10);
System.out.println("------------FOR LOOP--------------");
for(i=1;i<=10;i++){
System.out.print(i+" : ");
}
}
}
12-06-2023
import java.util.*;
class LoopsInJava{
public static void main(String... args){
int i=1;
while(i<=20){
System.out.println(i++);
i++;
}
}
}
//456987
//fd=4,ld=7
//hint use operators and while loop
//Input is any number of digit display the first digit and last digit
///,%
//456987%10->
class WhileEx1{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input any digit number : ");
int num = sc.nextInt();
int ld = num%10;
while(num>10){
num/=10;
}
int fd = num;
System.out.println("First Digit = "+fd+" Last digit = "+ld);
}
}
//Input is any digit number find the product and sum of digits of the number
//2453 -> product=120 sum=14
class WhileEx2{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input any digit number : ");
int num = sc.nextInt();
int prod=1,sum=0;
while(num!=0){
int rem=num%10;
prod*=rem;//prod=prod*rem
sum+=rem;
num/=10;
}
System.out.println("Sum = "+sum+" Product = "+prod);
}
}
//find reverse of the number
//2576 output 6752
class WhileEx3{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input any digit number : ");
int num = sc.nextInt();
int rev=0;
while(num!=0){
int rem=num%10;
rev = rev*10+rem;
num/=10;
}
System.out.println("Reverse of number : "+rev);
}
}
//arsmtrong number
//input is any digit number check whether it is armstrong or not
//153 (onum)
//step 1 : count of digits of number count=3
//anum = 1^3+5^3+3^3 = 1 + 125 + 27 = 153
//if(anum==onum){Armstrong}else{not an armstrong}
//1634
//count = 4
//anum = 1^4+6^4+3^4+4^4 = 1+1296+81+256=1634
//if(anum==onum){Armstrong}else{not an armstrong}
//9474
class WhileEx4{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input any digit number : ");
int num = sc.nextInt();
int onum,anum=0,ed,count=0;
onum = num;
while(num!=0){
count++;
num/=10;
}
System.out.println("Count of digits of number is : "+count);
//find out armstrong number
num = onum;
while(num!=0){
ed = num%10;
anum+=Math.pow(ed,count);
num/=10;
}
if(anum==onum){System.out.println("Armstrong number");}
else{System.out.println("Not an Armstrong number");}
}
}
//input is any digit number print in words
//24576 -> TWO FOUR FIVE SEVEN SIX 67542
//% / while(num!=0){dig=num%10;switch(num){ case 0: case 1: case
2:};num/=10;}
//step 1:input any digit number
//step 2:reverse of the number
//step 3: while loop switch case
//24576 -> TWOTY FOUR THOUSAND FIVE HUNDRED AND SEVENTY SIX ONLY
class WhileEx5{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input any digit number : ");
int num = sc.nextInt();
int rnum=0,count=0;
while(num!=0){
int dig = num%10;
rnum=rnum*10+dig;
count++;
num/=10;
}
while(rnum!=0){
int dig=rnum%10;
switch(dig){
case 0:
System.out.print("ZERO ");
break;
case 1:
System.out.print("ONE ");
break;
case 2:
System.out.print("TWO ");
break;
case 3:
System.out.print("THREE ");
break;
case 4:
System.out.print("FOUR ");
break;
case 5:
System.out.print("FIVE ");
break;
case 6:
System.out.print("SIX ");
break;
case 7:
System.out.print("SEVEN ");
break;
case 8:
System.out.print("EIGHT ");
break;
case 9:
System.out.print("NINE ");
}
switch(count){
case 2:
case 5:
System.out.print("\bTY ");
break;
case 4:
System.out.print("THOUSAND ");
break;
case 3:
System.out.print("HUNDRED ");
break;
case 1:
System.out.print("ONLY ");
}
count--;
rnum/=10;
}
}
}
//check whether the given number is pronic or not
//what is pronic number
//2 -> 1*2
//6 -> 2*3
//12 -> 3*4
//20 -> 4*5
//30 -> 5*6
//input is any digit number print whether it is pronic or not
//break,continue
class WhileEx6{
public static void main(String... args){
int i=1;
while(i<=30){
if(i>10&&i<=20){i++;continue;}
if(i>25){break;}
System.out.print(i+" : ");
i++;
}
}
}
//swap only fd and ld of a given any digit number
//ex 24768 84762
class WhileEx7{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input any digit number : ");
int num = sc.nextInt();
int i=1;
while(i<=num){
int pnum = i*(i+1);
if(pnum==num){System.out.println("It is a pronic
number");break;}
else if(pnum<num){i++;continue;}
else{System.out.println("It is not a pronice
number");break;}
}
}
}
13-06-2023
Do-while loop
//for loop
//i=24 i>>1 -> 24/2->12
//i=27 i>>3 -> 27/2->13/2->6/2->3
//i=12 i<<1 -> 12*2->24
//i=13 i<<3 -> 13*2->26*2->52*2->104
//so right shift is equivalent to repeatedly dividing by 2 as many number of
shifts
//so left shift is equivalent to repeatedly multiplying by 2 as many number of
shifts
import java.util.*;
class ForLoopEx1{
public static void main(String... args){
int i=1;
for(;i<10;){//infinite loop
System.out.println(i++);
}
}
}
//In for loop all 3 are optional
//Input is a number print its multiplication table
//3
//3 * 1 = 3
//3 * 2 = 6
//3 * 3 = 9
//...
//...
//3 * 10 = 30
class ForLoopEx2{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input a number : ");
int num = sc.nextInt();
for(int i=1;i<=10;i++){
System.out.println(num+" * "+ i +" = " + (num*i));
}
}
}
//factorial of a number
//4 -> 4*3*2*1 = 24
class ForLoopEx3{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input a number : ");
int num = sc.nextInt();
long fact=1;
for(;num>=1;num--){
fact*=num;
}
System.out.println("Factorial is "+fact);
}
}
//Factors of a number
//12 -> 1,2,3,4,6 cof -> 5
//24 -> 1,2,3,4,6,8,12 cof -> 7
//hint use % operator
//check whether the number is prime or not
//prime number factors are 1 and number itself count of factors should be 2
class ForLoopEx4{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input a number : ");
int num = sc.nextInt();
int count=0;
for(int i=1;i<=num;i++){
if(num%i==0){count++;System.out.print(i+"\t");}
}
System.out.println("Count of factors is "+count);
if(count==2){System.out.println("Number is prime");}
else{System.out.println("Number is not prime");}
}
}
//We know how to find factors of a number
//12 -> 1,2,3,4,6,12
//24 -> 1,2,3,4,6,8,12,24
//GCD of two numbers 12
class ForLoopEx5{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input any 2 numbers ");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int gcd=1;//greatest common factors of num1 and num2
for(int i=1;i<=num1&&i<=num2;i++){
if(num1%i==0&&num2%i==0){gcd=i;}
}
System.out.println("GCD of two numbers is "+gcd);
}
}
//print fibonnoci series
class ForLoopEx6{
public static void main(String... args){
int t1=1,t2=1,nt=0;
for(int i=1;i<=10;i++){
System.out.print(t1+"\t");
nt = t1 + t2;
t1 = t2;
t2 = nt;
}
}
}
//Pattern printing
class ForLoopEx7{
public static void main(String... args){
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print("*\t");
}
System.out.println();
}
}
}
//do while loop is an exit controlled loop
//but mostly do while loop is used for menu driven application
//why menu has to be displayed atleast once then the user can exit the
application
import java.util.*;
class Calculator{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
char ch;
do{
System.out.print("\n\t1. Press '+' for addition\n\t2. Press '-'
for subtraction\n\t3. Press '*' for multiplication\n\t4. Press '/' for division\n\t5.
Press 'e' for exit\n");
System.out.println("Input a character");
ch = sc.next().charAt(0);
switch(ch){
case '+':
System.out.println("Input two values");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Addition = "+(a+b));
break;
case '-':
System.out.println("Input two values");
int c = sc.nextInt();
int d = sc.nextInt();
System.out.println("Subtraction = "+(c-d));
break;
case '*':
System.out.println("Input two values");
int e = sc.nextInt();
int f = sc.nextInt();
System.out.println("Multiplication = "+(e*f));
break;
case '/':
System.out.println("Input two values");
int g = sc.nextInt();
int h = sc.nextInt();
System.out.println("Division = "+((float)g/h));
break;
}
}while(ch!='e');
}
}
class DoWhileLoop{
public static void main(String... args){
int i=1024;
do{
System.out.print(i+"\n");
i>>=1;//i=i>>1;1024/2->512/2->256/2->128/2->64/2->32/2>16/2->8/2->4/2->2/2->1/2->0
}while(i!=0);
}
}
//1024 512
14-06-2023
import java.util.*;
class Pattern{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input the number of rows : ");
int rows = sc.nextInt();
int st=0;
for(int i=1;i<=rows;i++,st=0){
for(int sp=1;sp<=rows-i;sp++){
System.out.print(" ");
}
while(st!=2*i-1){
System.out.print("*");
st++;
}
System.out.println();
}
}
}
class PatternEx1{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input the number of rows : ");
int rows = sc.nextInt();
int sp=0,d,i,j;
for(i=rows,d=1;i>=1;i--,sp=0,d++){
for(j=1;j<=2*i;j++){
System.out.print("*");
while(j==i&&sp!=2*d-2){
System.out.print(" ");
sp++;
}
}
System.out.println();
}
for(i=1,sp=0,d=rows;i<=rows;i++,sp=0,d--){
for(j=1;j<=2*i;j++){
System.out.print("*");
while(j==i&&sp!=2*d-2){
System.out.print(" ");
sp++;
}
}
System.out.println();
}
}
}
15-06-2023
import java.util.*;
class PatternNum{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input a number of series to print : ");
int rows = sc.nextInt();
int af=0,nor=rows;
for(int i=1;i<=rows;i++,nor=rows){
for(int j=1,af=0;j<=i;j++,nor--){
if(j==1){af=0;System.out.print(i+af+"\t");}
if(j>1){af+=rows;System.out.print(i+af+"\t");}
}
System.out.println();
}
}
}
import java.util.*;
class ArraysInJava{
public static void main(String... args){
int arr[]=new int[5];
int[] arr1[]=new int[2][];
int arr2[]={10,20,30,40,50};
int arr3[][]={
{10,20,30},
{40,50,60},
{70,80,90}
};
for(int i=0;i<arr2.length;i++){
System.out.print(arr2[i]+"\t");
}
System.out.println();
for(int i=0;i<arr3.length;i++){
for(int j=0;j<arr3[i].length;j++){
System.out.print(arr3[i][j]+"\t");
}
System.out.println();
}
}
}
//Declare an array of any size and intialize it with user input and display it
class ArrayIntia{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input the size of the array : ");
int []arr=new int[sc.nextInt()];
System.out.println("Size of the array is "+arr.length);
System.out.println("------INPUT ARRAY ELEMENTS------------");
for(int i=0;i<arr.length;i++){
arr[i]=sc.nextInt();
}
System.out.println("------DISPLAYING ARRAY ELEMENTS------------");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+"\t");
}
}
}
//find the average and sum of elements of the array
class ArraySumAvg{
public static void main(String... args){
Scanner sc =new Scanner(System.in);
System.out.println("Input the array size : ");
int []arr = new int[sc.nextInt()];
int sum=0;
System.out.println("----------INPUT ARRAY ELEMENTS----------");
for(int i=0;i<arr.length;i++){
arr[i]=sc.nextInt();
}
for(int i=0;i<arr.length;i++){
sum+=arr[i];
}
//sum will have addition of all array elements
float avg=(float)sum/arr.length;
System.out.println("Sum = "+sum+" Average = "+avg);
}
}
//Sort the elements of the array
//Input 12,16,14,11,13,15
//output 11,12,13,14,15,16
//16,15,14,13,12,11
//what are the soring algorithm in array
//Bubble sort
//Insertion sort
//Merge sort
//Quick sort
16-06-2023
//There is a match happening it is a 6 match series you are a player in it
//input is your score in every match find the average and total score
//and display the average and total score in the series
import java.util.*;
class ArraysInJava{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
String name;
System.out.println("Input your name : ");
name = sc.nextLine();
int score[]=new int[6];
for(int i=0;i<score.length;i++){
System.out.println((i+1)+ "Match score ");
score[i]=sc.nextInt();
}
int total=0;
float avg;
for(int i=0;i<score.length;i++){
total+=score[i];
}
avg = (float)total/score.length;
System.out.println(name+" Total Score = " + total+" Average =
"+avg);
}
}
19-06-2023
1D array
class ArrayDemo2{
public static void main(String... args){
int arr[]={14,11,13,16,15,12};
boolean flag = false;
for(int i=0;i<arr.length;i++){
System.out.println((i+1)+" Pass ");
for(int j=0;j<arr.length-1;j++){
if(arr[j]<arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
flag=true;
}
}
if(flag){flag=false;}
else{break;}
}
System.out.println("---------ARRAY AFTER SORTING----------");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+"\t");
}
}
}
//how to search an element in an array
//Linear serach algorithm
class ArrayDemo3{
public static void main(String... args){
int arr[]={13,15,18,17,24,23,21};
Scanner sc = new Scanner(System.in);
System.out.println("Input an element to search in array : ");
int se=sc.nextInt();
int i=0;
for(;i<arr.length;i++){
if(se==arr[i]){System.out.println(se+" Found at location
"+(i+1));break;}
}
if(i==arr.length){System.out.println(se+" Not Found ");}
}
}
//2D array
class ArrayDemo4{
public static void main(String... args){
int[] arr[]=new int[3][4];//no of elements = no of col*no of row
Scanner sc = new Scanner(System.in);
System.out.println("Input the array elements : ");
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j] = sc.nextInt();
}
}
System.out.println("-------DISPLAYING ARRAY ELEMENTS------------");
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
//Jagged Array
class ArrayDemo5{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
int[] arr[]=new int[3][];//arr[0] = new int[4],arr[1] = new int[2]
for(int i=0;i<arr.length;i++){
System.out.println("Input the no of columns for "+(i+1)+"
row : ");
arr[i]=new int[sc.nextInt()];
}
System.out.println("Input the array elements");
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j]=sc.nextInt();
}
}
System.out.println("------------DISPLAYING ARRAY ELEMENTS--------------");
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
//There are 3 students 1st student has 4 subjects 2nd student 2 subjects 3rd
student 3 subjects
//display the total and average marks of each student
class ArrayDemo6{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
int[] arr[] = new int[3][];
for(int i=0;i<arr.length;i++){
System.out.println("Input "+(i+1)+" Students no of subjects
");
arr[i] = new int[sc.nextInt()];
}
for(int i=0;i<arr.length;i++){
System.out.println("Input student "+(i+1)+" subject marks");
for(int j=0;j<arr[i].length;j++){
System.out.print("Subject "+(j+1)+" mark ");
arr[i][j] = sc.nextInt();
}
}
float avg;
int total=0;
for(int i=0;i<arr.length;i++,total=0){
for(int j=0;j<arr[i].length;j++){
total+=arr[i][j];
}
avg = (float)total/arr[i].length;
System.out.println("Student "+(i+1)+" Total = "+total+"
Average = "+avg);
}
}
}
//There are two cities Record the weekly temperature of two cities and display
it
//hint use 2D array and final variables
class ArrayDemo7{
public static void main(String... args){
final int CITY=3;
final int WEEK=7;
int arr[][]=new int[CITY][WEEK];
Scanner sc = new Scanner(System.in);
for(int i=0;i<arr.length;i++){
System.out.println("Input City "+(i+1)+" Weekly
temperature ");
for(int j=0;j<arr[i].length;j++){
arr[i][j] = sc.nextInt();
}
}
for(int i=0;i<arr.length;i++){
System.out.print("\nCity "+(i+1)+" Temperature : ");
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+"\t");
}
}
}
}
//Find the transpose of the matrix
//we have to convert rows into columns and columns into rows
//Addition of two matrices
//multipliction of two matrices
//find the determinant of a matrix
20-06-2023
import java.util.*;
class ArrayDemo1{
public static void main(String... args){
int a[]={1,2,3,4,5};
int b[]=new int[a.length];
b=a;//Shallow cloning
a[0]+=5;
b[2]+=9;
for(int i=0;i<b.length;i++){
System.out.print(b[i]+"\t");
}
}
}
class ArrayDemo2{
public static void main(String... args){
int a[]={1,2,3,4,5};
int b[]=new int[a.length];
b=a.clone();
b[0]+=6;
a[2]+=8;
for(int i=0;i<b.length;i++){
System.out.print(b[i]+"\t");
}
}
}
class ArrayDemo3{
public static void main(String... args){
int a[]={1,2,3,4,5};
int b[]=new int[a.length];
System.arraycopy(a,0,b,0,3);
a[0]+=5;
for(int i=0;i<b.length;i++){
System.out.print(b[i]+"\t");
}
}
}
class ArrayDemo4{
public static void main(String... args){
int a[]=new int[5];
a[0]=1;
a[1]=2;
for(int i=0;i<a.length;i++){
System.out.print(a[i]+"\t");
}
}
}
//3D array
class ArrayDemo5{
public static void main(String... args){
int a[][][]={
{{1,2,3},{4,5,6},{7,8,9}},//a[i].length a[i][j].length
{{10,11,12},{13,14,15},{16,17,18}},
{{16,17,18},{19,20,21},{22,23,24}}//a.length
};
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
for(int k=0;k<a[i][j].length;k++){
System.out.print(a[i][j][k]+"\t");
}
System.out.println();
}
System.out.println();
}
}
}
//Declare a 3D array with 2*3*3 = 18 elements intialize it though user input
and display it
class ArrayDemo6{
public static void main(String... args){
int[][] a[]=new int[2][3][3];
Scanner sc = new Scanner(System.in);
System.out.println("Input array elements : ");
for(int i=0;i<a.length;i++){//2
for(int j=0;j<a[i].length;j++){//3
for(int k=0;k<a[i][j].length;k++){//3
a[i][j][k]=sc.nextInt();
}
}
}
System.out.println("--------DISPLAYING ARRAY ELEMENTS---------");
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
for(int k=0;k<a[i][j].length;k++){
System.out.print(a[i][j][k]+"\t");
}
System.out.println();
}
System.out.println();
}
}
}
//There is a college and there are 2 departments with 3 students with 3 subject
marks display
//the total of each student in each department
class ArrayDemo7{
public static void main(String... args){
int[][][] coll={
{{67,66,68},{98,88,89},{78,77,79}},
{{87,88,86},{76,77,75},{87,88,86}}
};
int total=0;
for(int i=0;i<coll.length;i++){
System.out.print("\nDepartment "+(i+1)+" : ");
for(int j=0;j<coll[i].length;j++,total=0){
System.out.print("\tStudent "+(j+1)+" : ");
for(int k=0;k<coll[i][j].length;k++){
total+=coll[i][j][k];
}
System.out.print("Total "+total+"\n\t");
}
}
}
}
//int a[]={1,2,3,4,5,6};
//we want to insert 3 at position 3
//inserting and deleting an element from an array is difficult
class ArrayDemo8{
public static void main(String... args){
int a[]=new int[8];
a[0]=1;a[1]=2;a[2]=4;a[3]=5;a[4]=6;a[5]=7;
for(int i=0;i<a.length;i++){
System.out.print(a[i]+"\t");
}
Scanner sc = new Scanner(System.in);
System.out.println("Input the element to be inserted : ");
int ie = sc.nextInt();
System.out.println("Input the position where to insert : ");
int pos = sc.nextInt();
for(int i=a.length-2;i>=pos-1;i--){
a[i+1]=a[i];
}
a[pos-1]=ie;
System.out.println("Array after insertion is");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+"\t");
}
}
}
//String is a class in java
//and in java class is also a datatype so we can call String as data type
//it is in java.lang package
//we will how to declare and intialize the string
class StringDemo{
public static void main(String... args){
String str1 = "Hello World";//literals
String str2 = new String("Arun Kumar");//using new keyword
char arr[]={'A','n','u','d','i','p'};
String str3 = new String(arr);//intialize using char array
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
class StringDemo1{
public static void main(String... args){
String str1="Welcome to Java Tutorials";
String str2="
Hello,
";
System.out.println(str2.concat(str1));
System.out.println(str2.length());
System.out.println(str1.length());
System.out.println(str1.toUpperCase());
System.out.println(str2.toLowerCase());
System.out.println(str2.trim());
System.out.println(str1.substring(0,7));
System.out.println(str1.substring(8));
System.out.println(String.valueOf(new Integer(18)));
System.out.println(String.valueOf(20.54));
System.out.println(str1.startsWith("e"));
System.out.println(str1.endsWith("s"));
int i = str1.indexOf('o');
int j = str1.indexOf('o',i+1);
int k = str1.lastIndexOf('o');
System.out.println(i+" : "+j+" : "+k);
System.out.println(str1.charAt(4));
}
}
class StringDemo2{
public static void main(String... args){
String str1 = "Welcome to java Tutorials";
String str2 = "Learn ";
//Learn Java Tutorials
System.out.println(str2.concat(str1.substring(10)));
String str3 = "hai i am good in java";//in this how many vowels are
there
}
}
//Strings are immutable in java,
//Immutable modifications not allowed in String
//String is a final class in java
//final class cannot be inherited
21-Jun-2023
package com.arun.batch10am.anpc5667;
import java.util.*;
public class StringDemo {
public static void main(String[] args) {
String str = "Hello C,Hello c++,Hello Java";
System.out.println(str.replaceAll("Hello", "Hi"));
System.out.println(str.replaceFirst("Hello", "Hi"));
}
}
class StringDemo1{
public static void main(String... args) {
String str = "This is our class on java/Today's topic is
on String/We are exploring";
String str1[]=str.split("/",2);
String str2;
System.out.println("Hai"+str1.length);
for(String x:str1) {
System.out.println(x);
}
System.out.println(str.hashCode());
}
}
class StringDemo2{
public static void main(String... args) {
String str1="Hello";
String str2="Hello";
if(str1.equals(str2)) {System.out.println("Both are
equal");}
else {System.out.println("Both are not equal");}
String str3 = new String("hello");
if(str2.equalsIgnoreCase(str3)) {System.out.println("Both
are equal");}
else {System.out.println("Both are not equal");}
System.out.println(str1.compareToIgnoreCase(str3));
}
}
//Immutability--modifications not allowed
class StringDemo3{
public static void main(String... args) {
String str1 = "Hello ";
String str2 = "World";
System.out.println(str1.hashCode());
str1 = str1.concat(str2);
System.out.println(str1.hashCode());
System.out.println(str1);
}
}
//Now Strings are immutable then how to modify the strings
//for this we have StringBuffer and StringBuilder
//StringBuilder was introduced in java v1.5
//StringBuffer and StringBuilder both the classes are
mutable that is modifications allowed
//it contains methods like
insert(),append(),delete(),reverse()
//Get an input as fname,lname,mname,append the fname and
lname and insert mname in to it and display
//delete the lanme
class StringDemo4{
public static void main(String... args) {
StringBuffer sb=new StringBuffer(50);
Scanner sc = new Scanner(System.in);
System.out.println(sb.capacity());
String fname,lname,mname;
System.out.println("Enter the first name : ");
fname=sc.next();
System.out.println("Enter the last name : ");
lname=sc.next();
System.out.println("Enter the middle name : ");
mname = sc.next();
System.out.println(sb.hashCode());
sb.append(fname);
sb.append(lname);
sb.insert(fname.length(),mname);
System.out.println(sb);
sb.delete(fname.length()+mname.length(),sb.length());
System.out.println(sb);
System.out.println(sb.hashCode());
}
}
//We have to check whether the given string is palindrome
or not
//madam,liril,radar,malayalam
class StringDemo5{
public static void main(String... args) {
Scanner sc = new Scanner(System.in);
System.out.println("Input a string : ");
String str1 = sc.next();
StringBuffer sb = new StringBuffer(str1);
String str2 = sb.reverse().toString();
if(str1.equals(str2)) {System.out.println("Palindrome");}
else {System.out.println("Not a palindrome");}
}
}
//Input is a line of a text print only palindrome words in
the string
//if no palindrome words then print no palindrome in given
string
class StringDemo6{
public static void main(String... args) {
}
}
//There are 2 theaters and 1st theater running 2 movies
and 2nd theater running 3 movies at
//different time display to the user the theater name and
the corresponding movies
//There are 3 types of variables in java
//1. Local variable
//2. Class/Static variable
//3. Instance variable
public class VariablesInJava{
public static float b;//class/static variable it has class level scope
private String str;//instance variable//this is attaced with each object
boolean flag;
public static void main(String... args){
int a=10;//local variable local to the method its visibility is only
within the method
VariablesInJava vij = new VariablesInJava();
System.out.println("b = "+b+" str = "+vij.str+" a = "+a+" flag =
"+vij.flag);
}
}
//Access Modifiers -> public,private,protected,default
//can we use access modifier for local variable
class Demo1{
{
System.out.println("Inside block1");
}
{
System.out.println("Inside block2");
}
static{
System.out.println("Inside static block1");
}
static{
System.out.println("Inside static block2");
}
public static void main(String... args){
System.out.println("Inside main method");
Demo1 d1 = new Demo1();
Demo1 d2 = new Demo1();
}
}
//static block gets executed even before main and in the order in which we
give and only once for the first
//time we execute the application
//Instance block gets executed for each time we create an instance or object
for the class in the order in which we give
class Demo2{
static int i = 10;//static variable
int j = 20;//instance variable
static {
i=12;
}
public static void main(String... args){
Demo2 ins1 = new Demo2();
Demo2 ins2 = new Demo2();
ins1.i++;//10
ins2.i++;//
ins1.j++;
ins2.j++;
System.out.println("ins1.i = "+ins1.i+" ins2.i = "+ins2.i+" ins1.j =
"+ins1.j+" ins2.j = "+ins2.j);
}
}
"Hello my age is 23 and i am born in 2000"
Download