1.
1 data types
//default values of instance(object) variables
class dtype
{ int a;
float b;
double c;
char d;
long e;
String f;
boolean g;
public static void main(String[] args)
{
dtype t=new dtype();
[Link](t.a+"\n"+t.b+"\n"+t.c+"\n"+t.d+"\n"+t.e+"\n"
+t.f+"\n"+t.g);
}
}
—--------------
1.2 local(function) variables must be initialized
class dtype
{
public static void main(String[] args)
{
int a=10;
float b=6.7f;
double c=8.9;
char d='q';
long e=234567;
String f="hi";
boolean g=true;
[Link](a+"\n"+b+"\n"+c+"\n"+d+"\n"+e+"\n"+f+"\n"+g)
;
}
}
—------------
1.3 reading values from keyboard
import [Link];
class dtype
{
public static void main(String args[])
{
String name;
int age;
float height;
boolean indian;
Scanner cse=new Scanner([Link]);
[Link]("enter ur name:");
name=[Link]();
[Link]("enter ur age:");
age=[Link]();
Page 1
[Link]("enter ur height:");
height=[Link]();
[Link]("you are indian?(true/false):");
indian=[Link]();
[Link]("Details u have entered
are.....\n");
[Link](name+" "+age+" "+height+" "+indian);
}
}
-------------------------------—-------------------------------
2 Bank customer object creation with methods
class bankcus
{
int ano;
double bal;
public static void main(String money[])
{
bankcus raj=new bankcus();
bankcus vani=new bankcus();
[Link]=222;
[Link]=20.5;
[Link]=333;
[Link]=10.5;
[Link](20.5);
[Link](10.5);
[Link]();
[Link]();
}
void balance()
{
[Link]("Balance of account no "+ano+"
is:"+bal);
}
void credit(double b)
{
bal=bal+b;
}
}
—-------------------------------------------------
3 prime num generator with given range
import [Link];
class prime
{
public static void main(String p[])
{
int s,e,divcount;
Scanner sc=new Scanner([Link]);
[Link]("enter start num:");
s=[Link]();
[Link]("enter end num:");
e=[Link]();
Page 2
[Link]("prime numbers within("+s+","+e+") range
are:");
for(int i=s;i<=e;i++)
{
divcount=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
divcount=divcount+1;
}
else
{
continue;
}
}
if(divcount==2)
{
[Link](i+" ");
}
}
}
}
—---------—---------—---------—---------
4 rectangle class
class rectangle
{
double length;
double width;
public static void main(String[] args)
{
rectangle r1=new rectangle();
rectangle r2=new rectangle();
r1.find_perimeter(10,5);
r1.find_area(10,5);
r2.find_perimeter(5,8);
r2.find_area(5,8);
[Link]("bye...");
}
void find_perimeter(double l,double w)
{
length=l;
width=w;
[Link](2*(length+width));
}
void find_area(double l,double w)
{
length=l;
width=w;
[Link](length*width);
}
}
—---------—---------—---—---------—----
Page 3
5.1 no-argument constructor
class svstudent
{ String cadd;
String cpri;
svstudent()
{
cadd="tpt";
cpri="[Link] sir";
}
public static void main(String[] args)
{
svstudent raj=new svstudent();
svstudent bob=new svstudent();
[Link]([Link]+" "+[Link]);
}
}
—--------------------
5.2 parameterized constructor
class student
{
int roll;
String college;
student(String c,int r)
{
college=c;
roll=r;
}
void show()
{
[Link](college+" "+roll);
}
}
class study
{
public static void main(String[] pen)
{
student siri=new student("svce",111);
student vani=new student("aits",222);
[Link]();
[Link]();
[Link]("bye...");
}
}
—---------—---------—---—---------—
6 Method overloading
Page 4
class area
{
double find_area(int r)
{
return (3.14*r*r);
}
double find_area(int l,int w)
{
return l*w;
}
double find_area(double sd)
{
return sd*sd;
}
public static void main(String[] args)
{
double ca,cr,cs;
area circle=new area();
area rect=new area();
area square=new area();
ca=circle.find_area(2);
cr=rect.find_area(3,4);
cs=square.find_area(3.0);
[Link](ca+" "+cr+" "+cs);
}
}
—---------—---------—---------—---------—---------—---------
7 String palindrome
import [Link];
class palin
{
public static void main(String[] args)
{
String n;
int end,beg=0;
boolean is_palin=true;
Scanner sc=new Scanner([Link]);
[Link]("enter a string :");
n=[Link]();
end=[Link]()-1;
while(beg<end)
{
if([Link](beg)==[Link](end))
{
beg++;
end--;
}
else
{
is_palin=false;
break;
}
}
Page 5
if(is_palin==false)
{
[Link](n+" :is not palindrome");
}
else
{
[Link](n+" :is palindrome");
}
}
}
—---------—---------—---------—---------—---------—---------
8 finding grades
import [Link];
class grades
{
public static void main(String[] marks)
{
int tel,math,eng,otot,tot=300;
char grade;
float per;
Scanner sc=new Scanner([Link]);
[Link]("\nenter telugu marks:");
tel=[Link]();
[Link]("\nenter maths marks:");
math=[Link]();
[Link]("\nenter eng marks:");
eng=[Link]();
otot=tel+math+eng;
per=(float)otot/tot*100;
[Link]("UR PERCENTAGE IS: "+per);
if(per>=75 && per<=100)
{
[Link]("A grade....");
}
else if(per>=60 && per<75)
{
[Link]("B grade....");
}
else if(per>=35 && per<60)
{
[Link]("C grade....");
}
else
{
[Link]("D grade....");
}
}
}
—---------—---------—-----—----
9 Check Armstrong number or not
import [Link];
class armstrong
Page 6
{
public static void main(String am[])
{
long n,sum=0,tn,rem,slen;
Scanner sc=new Scanner([Link]);
[Link]("enter a num:");
n=[Link]();
//to convert int to string value
String str=[Link](n);
slen=[Link]();
tn=n;
while(n>0)
{
rem=n%10;
sum=sum+(long)[Link](rem,slen);//type conversion
n=n/10;
}
if(sum==tn)
{
[Link]("num: "+tn+" is armstrong");
}
else
{
[Link]("num: "+tn+" is not armstrong");
}
}
}
—---------—---------—-----—----—---------—---------—-----—----
10.1 Bubble sort on numbers
class bubble
{
public static void main(String am[])
{
int a[]={999,88,7,66,55,4,22,11,0,-1,-2,0};
int temp;
for( int i=0;i<=[Link]-2;i++)
{
for(int j=i+1;j<=[Link]-1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
[Link]("After bubble sort......\n");
for(int i=0;i<=[Link]-1;i++)
{
[Link](a[i]+" ");
Page 7
}
}
}
10.2 Bubble sort on array of strings
class strbubble
{
public static void main(String am[])
{
String a[]={"sai","cse","svce","svec","pen","canteen",
"juice","read","pay"};
String temp;
for( int i=0;i<=[Link]-2;i++)
{
for(int j=i+1;j<=[Link]-1;j++)
{
if(a[i].compareTo(a[j])>0)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
[Link]("After bubble sort on array of
strings......\n");
for(int i=0;i<=[Link]-1;i++)
{
[Link](a[i]);
}
}
}
—---------—---------—-----——---------—---------—-----—
11 abstract class
11.1
abstract class great_grand_father
{
abstract void work();
}
class grand_father extends great_grand_father
{
void work()
{
[Link]("i work as doctor..");
}
}
Page 8
class father extends great_grand_father
{
void work()
{
[Link]("i work as teacher..");
}
}
class child extends great_grand_father
{
void work()
{
[Link]("i work as software eng..");
}
}
class family
{
public static void main(String am[])
{
father f=new father();
[Link]();
child c=new child();
[Link]();
}
}
11.2
abstract class math
{
abstract void cal(int i,int j);
}
class sub extends math
{
void cal(int i,int j)
{
[Link](i-j);
}
}
class mul extends math
{
void cal(int i,int j)
{
[Link](i*j);
}
}
class study
{
public static void main(String[] args)
{
sub s=new sub();
Page 9
[Link](2,3);
mul m=new mul();
[Link](2,3);
}
}
—---------—---------—----—---------—---------—----
12.1 inheritance
12.2 final keyword
12.3 method overriding
Refer your lecture note book
—---------—---------—----—---------—---------—----
13 .package implementation
Refer your lecture note book
-----—----—---------—---------—---------—----—-----
14. multiple inheritance with interface
interface ggf//great grand father
{
String caste="oc";
String reli="hindu";
String ill="diabetis";
void eat();
}
interface gf//grand father
{
double pro_val=8000000;
void sleep();
}
interface father extends ggf,gf
{
String job="doc";
}
class child implements father
{
public static void main(String[] args)
{
child b=new child();
[Link]([Link]+ ", "+[Link]+", "+[Link]);
[Link]();
[Link]();
}
public void sleep()
{
[Link]("we sleep for 6 hours");
}
public void eat()
{
Page 10
[Link]("we eat only veg");
}
-----—---------—---------—----—----------—---------
15. Write a java program that inputs 5 numbers, each between 10 and 100
inclusive. As each number is read, display it only if it‘s not a
duplicate of any number already read.
import [Link];
class nodup
{
public static void main(String y[])
{
int kiwi[]={0,0,0,0,0},n,found,k,j,position=0;
Scanner take=new Scanner([Link]);
for(int i=0;i<=4;i++)
{ found=0;
[Link]("\n\nEnter a number between 10 and
100: ");
n=[Link]();
[Link]("\nHere are unique values u
entered: ");
for(j=0;j<position;j++)
{
if(n==kiwi[j])
{
found=1;
break;
}
}
if(found==1)
{
for(k=0;k<position;k++)
{
[Link](kiwi[k]+" ");
}
}
else
{
kiwi[position]=n;
position=position+1;
for(k=0;k<position;k++)
{
[Link](kiwi[k]+" ");
}
}
}
Page 11
[Link]("\n");
}
}
-----—---------—---------—----—-------—---------—---------—----—--
16 creating your own exception
Eg 1
/* this program raises "IHateOddNum" exception when you enter
odd number
*/
import [Link];
class IHateOddNum extends Exception
{
static String mess;
IHateOddNum(String m)
{
mess=m;
}
public static void main(String[] args)
{
Scanner s=new Scanner([Link]);
int n;
[Link]("enter an integer:");
n=[Link]();
try
{
if(n%2!=0)
{
IHateOddNum sachin=new IHateOddNum("dont enter odd numbers");
throw sachin;
}
[Link]("i like even numbers:"+n);
}
catch(IHateOddNum tendulkar)
{
[Link](tendulkar+":"+mess);
}
[Link]("bye...keep loving even numbers.");
}
}
-----—---------—---------—----—--------—---------—---------—----—-
--
//raises “IHAAException” when u enter a word starting with ‘a’ or
‘A’
Eg 2
Page 12
import [Link];
class IHAAException extends Exception
{
static char l;
IHAAException(char c)
{
l=c;
}
public static void main(String[] args)
{
Scanner s=new Scanner([Link]);
String n;
[Link]("enter a word:");
n=[Link]();
if([Link](0)=='a' || [Link](0)=='A')
{
try
{
IHAAException alpha=new
IHAAException([Link](0));
throw alpha;
}
catch(IHAAException e)
{
[Link](e+":"+l);
}
}
[Link](n+":bye..");
}
}
-------—---------—---------—----—--
17 “NumberFormatException”
17.1
class nformat
{
public static void main(String[] args)
{
String a="129";
try
{
byte b=[Link](a);
[Link](b);
}
catch(Exception e)
{
Page 13
[Link](e);
}
[Link]("bye...");
}
}
-------—----
17.2
class nformat
{
public static void main(String[] args)
{
String a="12.7";
try
{
int b=[Link](a);
[Link](b);
}
catch(Exception e)
{
[Link](e);
}
[Link]("bye...");
}
}
-------—----
class nformat
{
public static void main(String[] args)
{
String a="sai";
try
{
float b=[Link](a);
[Link](b);
}
catch(Exception e)
{
[Link](e);
}
[Link]("bye...");
}
}
-------—-----------—-----------—----
18. Using ‘throws’ keyword
class mistake
{
public static void main(String ex[])
{
for(int i=-1;i<6;i++)
Page 14
{
try
{
task(i);
}
catch(Exception e)
{
[Link](e);
}
}
[Link]("bye...");
}
static void task(int j)throws
ArithmeticException,ArrayIndexOutOfBoundsException
{
int a[]={2,0,4,0,5},n=20;
int r=n/a[j];
[Link](r);
}
}
-------—-----------—--------—-----------—--------—-----------—-
19.1 “synchronizing joint account “
class sbi extends Thread
{
int bal=0;
public void run()
{
synchronized(this)
{
for(int i=1;i<=100;i++)
{
bal=bal+1;
try
{
[Link](100);
}
catch(Exception e)
{
}
}
}
}
public static void main(String a[])throws Exception
{
sbi jaccount=new sbi();
Thread sai=new Thread(jaccount);
Thread ram=new Thread(jaccount);
[Link]();
[Link]();
[Link]();
Page 15
[Link]();
[Link]("current balance of joint account
is:"+[Link]);
}
-------—-----------—----------—-----------—---
19.2 Interthread communication using wait() and notify()
class fsum extends Thread
{
static int arr[]={1,2,3,4,5};
int sum=0,b[];
fsum(int a[])
{
b=a;
}
public void run()
{
[Link]("Entered into child thread.....");
synchronized(this)
{
for(int i=0;i<[Link];i++)
{
sum=sum+b[i];
}
try
{
[Link](3000);
}
catch(Exception e)
{
}
[Link]();
}
[Link]("exited from child thread.....");
}
public static void main(String a[])throws Exception
{
[Link]("Entered into parent thread.....");
fsum s=new fsum(arr);
[Link]();
synchronized(s)
{
[Link]();
[Link]("waiting for child thread to
find sum of array.....");
[Link]("i have been notified.....");
[Link]("The sum of array is:"+[Link]);
}
[Link]("exited from parent thread.....");
}
Page 16
}
-------—-----------—----------—-----------—---
20. using “join()” in thread
class fsum extends Thread
{
int sum=0,b[];
fsum(int a[])
{
b=a;
}
public void run()
{
for(int i=0;i<[Link];i++)
{
sum=sum+b[i];
}
}
public static void main(String a[])throws Exception
{
int arr[]={1,2,3,4,5};
fsum s=new fsum(arr);
[Link]();
[Link]();
[Link]([Link]);
}
-------—-----------—----------—-----------—---
20. File concepts
20.1 Reading file contents and display
import [Link];
import [Link];
import [Link];
class app
{
public static void main(String oy[])
{
int linenum=1;
try
{
Page 17
File f=new File("D:\\sai\\java\\[Link]");
Scanner s=new Scanner(f);
while([Link]())
{
String line=[Link]();
[Link](linenum+"."+line);
linenum++;
}
}
catch(FileNotFoundException fn)
{
[Link](fn);
}
}
}
----------------------------------------------------------------------
20.2 copy from one file to another
import [Link];
import [Link].*;
class app
{
public static void main(String oy[])throws IOException
{
File f;
Scanner s;
FileWriter fw=null;
try
{
f=new File("D:\\sai\\java\\[Link]");
s=new Scanner(f);
fw=new FileWriter("D:\\sai\\java\\[Link]");
while([Link]())
{
String line=[Link]();
[Link](line);
[Link](line);
[Link]("\n");
}
}
catch(FileNotFoundException fn)
{
Page 18
[Link](fn);
}
[Link]();//closing [Link] file
}
}
----------------------------------------------------------------------
Page 19