PROGRAM 1 AIM
Write a program to calculate the electricity bill by reading the consumer no and no of units consumed. The charges for different slabs are given below Up to 50 units 50 – 100 units 100 – 00 units 00 – "00 units above "00 units
Rs. 1.5 per unit Rs. per unit Rs. .! per unit Rs. ".5 per unit Rs. #.50 per unit
CODE pac$age bill% import &ava.util.'canner% public class (ill ) static double calcamt*int unit+ ) double bill,mt% if*unit-"00+ ) bill,mt *double+ unit/#.5% else if *unit-00+ ) bill,mt *double+ unit/".5% else if*unit-100+ ) bill,mt *double+ unit/.!% else if*unit-50+ ) bill,mt *double+ unit/%
else ) bill,mt *double+ unit/1.5% return bill,mt% public static void main*'tring2 args+ ) int unit% double bill,mt% 'ystem.out.println*34nter the usage in Units3+% 'canner scannew 'canner*'ystem.in+% unit scan.ne6t7nt*+% bill,mt calcamt*unit+% 'ystem.out.println*38our bill ,mount is 39bill,mt+%
OUTPUT
PROGRAM 2
AIM
Write &ava program to display :ibonacci series up to n. CODE
pac$age fibonacci.series% import &ava.util.'canner%
public class :ibonacci'eries ) public static void main*'tring2 args+ ) int first;o0first;o% ne6t;ofirst;o9ne6t;o% 'ystem.out.println*33+% catch*46ception e+ ) 'ystem.out.println*34rror 3 9 e+%
OUTPUT
PROGRAM 3 AIM
Write &ava program to chec$ whether a given number is perfect< abundant or deficient. CODE
pac$age perabudef% import &ava.util.'canner%
public class ?er,bu@ef ) static int perfectAalc*int num+ ) int i% int temp 0% i *int+ Bath.floor*numC+% while*i D 0+) if*numEi 0+) temp9i% i>>% return temp% public static void main*'tring2 args+ ) int number% int temp% try ) 'ystem.out.println*34nter , ;umber3+%
'canner scannew 'canner*'ystem.in+% number scan.ne6t7nt*+% tempperfectAalc*number+% if*number temp+ ) 'ystem.out.println*3?erfect ;umber 3+% else if*number = temp+) 'ystem.out.println*3,bundant ;umber 3+% else ) 'ystem.out.println*3@eficent ;umber 3+% catch *46ception e+ ) 'ystem.out.println*34rror DDD 3 9 e+%
OUTPUT
PROGRAM 4
AIM
Write &ava program to chec$ whether the given sides can form a triangle. 7f yes< find the type *isosceles< eFuilateral< scalene+ and area of the triangle. CODE
pac$age canformtriangle% import &ava.util.'canner%
public class Aan:ormTriangle ) static boolean triAhec$*float s1s1+/*s>s+/*s>s"++% return area%
public static void main*'tring2 args+ ) float side1< side< side"< area%
try ) 'ystem.out.println*34nter the Hength of " 'ides3+% 'canner scannew 'canner*'ystem.in+% side1scan.ne6t:loat*+% side scan.ne6t:loat*+% side" scan.ne6t:loat*+%
if*triAhec$*side1
else ) 'ystem.out.println*3, Triangle canJt be :ormed3+% catch*46ception e+ ) 'ystem.out.println*34rror DDD 66K66 39 e+%
OUTPUT
PROGRAM 5 AIM
Write a java program to read an array of 10 elements and to nd following: a)the Sum of the elements. b) Average of the element. ) !a"imum of the elements. d) !inimum of the elements. CODE
pa#age array$ import java.util.Sanner$ publi lass Array % publi stati void main&String'( args) % int arr'(new int '*0($ int n+sum+min+ma"$ sum0$ try % Sanner sannew Sanner&System.in)$ System.out.print&,enter the limit:,)$ nsan.ne"t-nt&)$ System.out.println&,enter the elements:,)$ for& int i0$in$i//)% arr'i(san.ne"t-nt&)$ sum/arr'i($ minma"arr'0($ System.out.println&,Sum , / sum)$ System.out.println&,Average , / &oat)sum2n)$ for& int i1$in$i//)% if&arr'i( min) minarr'i($ else ma"arr'i($
System.out.println&,!inimum of elements :, / min)$ System.out.println&,!a"imum of elements :, / ma")$ ath&3"eption e) % System.out.println&,34454 666 , / e)$ System.e"it&0)$
OUTPUT
PROGRAM 6 AIM
Write a java program to nd the sum of the digit and reverse of given number using lass and objet. CODE
pa#age reverse$ import java.util.Sanner$ lass number % int num$ void input&) % Sanner san new Sanner&System.in)$ numsan.ne"t-nt&)$ reverse5p&)$ void reverse5p&) % int dupnum+sum0+d+rev0$ while&dup 6 0) % ddup710$ rev&rev 8 10) / d$ sum/d$ dup210$ System.out.println&,Sum of the digits :,/ sum)$ System.out.println&,4everse number :,/ rev)$ publi lass 4everse % publi stati void main&String'( args) % number 91new number&)$ System.out.print&,3nter the 9umber ,)$ 91.input&)$
OUTPUT
PROGRAM 7 AIM
Write a program in java with lass retangle with the data elds length+ breadth. alulate the area. reate two objet of retangle and ompare their area. CODE
pa#age retarea$ import java.util.Sanner$ lass retangle % oat len+br$ void input&) % Sanner sannew Sanner&System.in)$ lensan.ne"t;loat&)$ brsan.ne"t;loat&)$ double area&) % return&len8br)$ publi lass 4etArea % publi stati void main&String'( args) % retangle 41new retangle&)$ retangle 4* new retangle&)$ System.out.println&,3nter the length and breadth of 1st retangle:,)$ 41.input&)$ System.out.println&,3nter the length and breadth of *nd retangle:,)$ 4*.input&)$ if&41.area&) < 4*.area&)) System.out.println&,Area of 1st retangle is greater than the *nd retangle66,)$ else if&41.area&) 4*.area&))
System.out.println&,Area of 1st retangle is less than the *nd retangle66,)$ else System.out.println&,=oth retangles have same area66,)$ OUTPUT
PROGRAM 8
AIM
Write a java program to nd the volume of ube+ retangular bo" and ylinder using funtion overloading. CODE
pa#age overloading$ import java.util.8$ publi lass 5verloading % void volume&int ") 22for ube % System.out.println&,>olume of ube :,/ "8"8")$ void volume&int "+int y+int ?) 22for retangular bo" % System.out.println&,>olume of retangular bo" :,/"8y8?)$ void volume&int "+int y) 22for ylinder % System.out.println&,>olume of ylinder :,/!ath.@-8"8"8y)$ publi stati void main&String'( args) % int a+b++h$ 5verloading 31new 5verloading&)$ Sanner sannew Sanner&System.in)$ System.out.println&,;ind the area of :n 1.ube n *.4etangular bo" n B.ylinder,)$ System.out.print&,3nter ur hoie :,)$ hsan.ne"t-nt&)$ swith&h) % ase 1: System.out.println&,3nter the side of ube:,)$ asan.ne"t-nt&)$ 31.volume&a)$ brea#$ ase *: System.out.println&,enter the length+breadth and height of the retangular bo":,)$ asan.ne"t-nt&)$ bsan.ne"t-nt&)$ san.ne"t-nt&)$
31.volume&a+b+)$ brea#$ ase B: System.out.println&,3nter the height and radius of the ylinder:,)$ asan.ne"t-nt&)$ 22height of ylinder bsan.ne"t-nt&)$ 22radius of ylinder 31.volume&b+a)$ brea#$ default:System.out.println&,-nvalid entry,)$ OUTPUT
PROGRAM 9 AIM
Write a java program to reate a lass omple". reate two objets and nd the sum of two omple" numbers. 4ead the real and imaginary part using onstrutor. CODE
pa#age omple"num$ import java.util.Sanner$ lass omple" % int real$ int imag$ omple"&) % real0$ imag0$ omple"&int a+int b) % reala$ imagb$ void add&omple" 1+omple" *) % real1.real/*.real$ imag1.imag/*.imag$ void display&) % System.out.println&,4esult :, / real / , / ,/ ,i ,/imag )$ publi lass omple"9um % publi stati void main&String'( args) % int a+b$ Sanner sannew Sanner&System.in)$
System.out.println&,3nter the real and imaginary part of 1st omple" no:,)$ asan.ne"t-nt&)$ bsan.ne"t-nt&)$ omple" 1new omple"&a+b)$ System.out.println&,3nter the real and imaginary part of *st omple" no:,)$ asan.ne"t-nt&)$ bsan.ne"t-nt&)$ omple" *new omple"&a+b)$ omple" Bnew omple"&)$ B.add&1+ *)$ B.display&)$ OUTPUT
PROGRAM 10 AIM
Write a java program to nd the area of sCuare and a retangle using overloaded onstrutors. CODE
pa#age overloadedonst$ import java.util.Sanner$ lass area % int a$ int b$ area&int a) %
System.out.println&,Area of sCuare :,/ a8a)$ area&int a+int b) % System.out.println&,Area of reetangle :,/ a8b)$ publi lass 5verloadedonst % publi stati void main&String'( args) % int a+b+h$ Sanner sannew Sanner&System.in)$ System.out.println&,;ind the area of :n 1.sCuare n *.retangle,)$ System.out.println&,3nter ur hoie :,)$ hsan.ne"t-nt&)$ swith&h) % ase 1:System.out.println&,3nter the side of sCuare :,)$ asan.ne"t-nt&)$ area a1new area&a)$ brea#$ ase *:System.out.println&,3nter the length and breadth of retangle :,)$ asan.ne"t-nt&)$ bsan.ne"t-nt&)$
area a*new area&a+b)$ brea#$ default:System.out.println&,-nvalid entry,)$ OUTPUT
PROGRAM 11 AIM
Write a java program to reate a lass student. Write methods to read and display the student details. reate a derived lass result. Write methods to read mar# of D subjets and display the total and grade. reate objet to read and display the result of a student. CODE
pa#age stud$ import java.util.8$ lass student % String name$ int rollno$ void read&) % System.out.println&,3nter the name of student:,)$ Sanner san new Sanner&System.in)$ name san.ne"tEine&)$ System.out.println&,enter the roll number:,)$ rollno san.ne"t-nt&)$ void display&) % System.out.println&,SFGH39FS H3FA-ESn 88888888888888888888888888888,)$ System.out.println&,9ame: ,/ name /,n, / ,4oll 9o: , / rollno)$ lass result e"tends student % int'( mar# new int'D($ int tot+per$ har grade$ result&) %
tot0$ void read!ar#&) % System.out.println&,3nter the mar#s for D subjets&out of 100),)$ Sanner san new Sanner&System.in)$ for&int i0$ iD $i//) % mar#'i(san.ne"t-nt&)$ tot/mar#'i($ pertot2D$ if&per<I0) gradeJAJ$ else if &per<K0) gradeJ=J$ else if&per<L0) gradeJJ$ else if&per<M0) gradeJHJ$ else if&per<D0) gradeJ3J$ else if& per<0 ) gradeJ;J$ else System.out.println&,-nvalid mar#s66,)$ void disp&) % System.out.println&,Fotal mar#: ,/ tot)$ System.out.println&,Nrade: ,/ grade)$
publi lass Stud % publi stati void main&String'( args) % result r1new result&)$ r1.read&)$ r1.read!ar#&)$ r1.display&)$ r1.disp&)$
OUTPUT
PROGRAM 12
AIM
Write a java program to use this #eyword to prevent namespae ollision. CODE
pa#age this#ey$ publi lass FhisOey % private int "+y$ publi void set>ar&int "+int y) % this.""$ this.yy$ System.out.println&" / , , / y)$ publi stati void main&String'( args) % FhisOey obj new FhisOey&)$ obj.set>ar&10+*0)$ System.out.println&obj." / , , / obj.y)$ OUTPUT
PROGRAM 13
AIM
Write a program to nd the area of irle and sphere using interfae. CODE
pa#age interfaearea$ import java.util.Sanner$ interfae Area % nal stati oat @-B.1Pf$ oat ompute& oat r )$ lass irle implements Area % publi oat ompute& oat r) % return&@-8r8r)$ lass Sphere implements Area % publi oat ompute&oat r) % return&P8@-8r8r)$ publi lass -nterfaeArea % publi stati void main&String'( args) % oat r$ irle ir new irle&)$ Sphere sph new Sphere&)$ Sanner san new Sanner&System.in)$ Area area$ area ir$ System.out.println&,3nter the radius : ,)$ rsan.ne"t;loat&)$ System.out.println&,Area of irle : , / area.ompute&r))$
area sph$ System.out.println&,Area of sphere : , / area.ompute&r))$ OUTPUT
PROGRAM 14
AIM
Write a java program for generating two threads 5ne for odd numbers and one for even numbers. CODE
pa#age oddeventhread$ lass 3ven e"tends Fhread % publi void run&) % System.out.println&,Fhread even start:,)$ for& int i0 $i10 $ i/*) % System.out.println&, even no: , / i)$
System.out.println&,Fhread even stop n,)$
lass 5dd e"tends Fhread % publi void run&) % System.out.println&,Fhread odd start:,)$ for& int i1 $i10$ i/*) % System.out.println&, odd no: , / i)$ System.out.println&,Fhread even stop n,)$ publi lass 5dd3venFhread % publi stati void main&String'( args) % new 3ven&).start&)$ new 5dd&).start&)$
OUTPUT
PROGRAM 15 AIM
Write a java program to enable arithmeti e"eptions using throw #eyword. CODE pa#age throwe"ample$
lass FhrowOeyword % stati void divide&int "+int y) % int ?$ try % if&y0) throw new Arithmeti3"eption&, error : divide by ?ero66,)$ else ?"2y$ ath&3"eption e) % System.out.println&e)$
publi lass Fhrow3"ample % publi stati void main&String'( args) % FhrowOeyword obj new FhrowOeyword&)$ obj.divide&10+0)$
System.out.println&,4est of the ode...,)$
OUTPUT
PROGRAM 16 AIM
Write a java program to override method greatest &) for nding the greatest of * numbers and B numbers. CODE
pa#age overridegreatest$ lass Super % int "$ int y$ Super&int "+ int y) % this.""$ this.yy$ void greatest&) % if &"
else System.out.println&? / , is greater,)$ publi lass 5verrideNreatest % publi stati void main&String'( args) % Sub s1 new Sub&B+L+10)$ s1.greatest&)$ OUTPUT
PROGRAM 17 AIM
Write an applet to display a retangle with speied oordinate and olor passed as parameter from the RF!E le. CODE
4etangle.java import java.awt.8$ import java".swing.te"t.html.StyleSheet$ import java.applet.8$ publi lass 4etangle e"tends Applet % String a+b++d+e$ olor olr$ int "1+y1+"*+y*$ StyleSheet snew StyleSheet&)$ publi void init&) % a get@arameter&,"1,)$ b get@arameter&,y1,)$ get@arameter&,"*,)$ d get@arameter&,y*,)$ e get@arameter&,olor,)$ "1 y1 -nteger.parse-nt&a)$ -nteger.parse-nt&b)$ "* -nteger.parse-nt&)$ y* -nteger.parse-nt&d)$ olr s.stringFoolor&e)$ publi void paint&Nraphis g) % g.setolor&olr)$ g.draw4et&"1+y1+"*+y*)$
4etangle.html html< head< title< 4etangle 2title< 2head< body< applet ode4etangle.lass width M00 heightM00 < param name,"1, value100 < param name,y1, value100 < param name,"*, valueB00 < param name,y*, valueB00 < param name,olor, value,red,< 2applet< 2body< 2html< OUTPUT
PROGRAM 18 AIM
reate an AWF appliation to add+ remove items in a listbo". CODE
import java.applet.8$ import java.awt.8$ publi lass SampleEist e"tends Applet % Fe"t;ield t"t+t"t1$ Eist list$ publi void init&) % list new Eist&)$ t"t new Fe"t;ield&K)$ t"t1 new Fe"t;ield&10)$
list.add&,apple,)$ list.add&,orange,)$ list.add&,mango,)$ list.add&,grapes,)$ add&list)$ 22adding list list.remove&*)$ 22remove element using inde"
OUTPUT
PROGRAM 19 AIM
reate an applet for a displaying smiling fae. CODE
import java.awt.8$ import java.applet.8$ publi lass ;ae e"tends Applet % publi void paint&Nraphis g) % g.draw5val&P0+P0+1*0+1D0)$ g.draw5val&DL+LD+B0+*0)$ g.draw5val&110+LD+B0+*0)$ g.ll5val&MK+K1+10+10)$ g.ll5val&1*1+K1+10+10)$ g.draw5val&KD+100+B0+B0)$ g.llAr&M0+1*D+K0+P0+1K0+1K0)$ g.draw5val&*D+I*+1D+B0)$
g.draw5val&1M0+I*+1D+B0)$ OUTPUT
PROGRAM 20 AIM
Write a program to display the " and y position of the ursor movement using mouse. CODE
import java.awt.8$ import java.awt.event.8$ import java.applet.8$ publi lass !ouseT e"tends Applet implements !ouse!otionEistener % int "+y$ publi void init&) % add!ouse!otionEistener&this)$
publi void mouse!oved&!ouse3vent e) % "e.get&)$ ye.getT&)$ repaint&)$ publi void mouseHragged&!ouse3vent e) % "e.get&)$ ye.getT&)$ repaint&)$ publi void paint&Nraphis g) % showStatus&" / , + , / y)$
OUTPUT