cc
www.studentrocz.blogspot.com
Week-1a) Aim: Write a java program that print all roots of equation of quadratic a,b,c will be supplied through keyboard Code: import java.lang.* import java.math.* class !uad
"
Output:
Week-1b) Aim: Write a java program that uses both recursive and non recursive functions to print the nth value in 6ibonacci series. Code: import java.lang.* class 6ibo
"
int f,f+(*,f,(+,n n()nteger.parse)nt#arg %*&' $ystem.out.println#f+' $ystem.out.println#f,' for#int i(* i/n-+ i33'
"
www.studentrocz.co.cc
www.studentrocz.blogspot.com
Output:
www.studentrocz.co.cc www.studentrocz.blogspot.com
Week-2a) Aim: Write a java program that prompts the user for an integer and then prints out all prime numbers upto the integer. Code: import java.lang.* class 8rime
"
if#i9j((*' c33
1
if#c((,' $ystem.out.println#0
1
03i'
1
1
Output:
www.studentrocz.co.cc
www.studentrocz.blogspot.com
Week-2b) Aim: Write a java program to multiply two given matrices. Code: import java.lang.* import java.io.* class 4atri2
"
<ata)nput$tream dis(new <ata)nput$tream#$ystem.in' $ystem.out.println#0;nter =he >ow $ize :f =he 6irst 4atri20' int r+()nteger.parse)nt#dis.read?ine#'' $ystem.out.println#0;nter =he @olumn $ize :f =he 6irst 4atri20' int c+()nteger.parse)nt#dis.read?ine#'' $ystem.out.println#0;nter =he >ow $ize :f =he $econd 4atri20' int r,()nteger.parse)nt#dis.read?ine#'' $ystem.out.println#0;nter =he @olumn $ize :f =he $econd 4atri20' int c,()nteger.parse)nt#dis.read?ine#'' int a%&%&(new int%r+&%c+& int b%&%&(new int%r,&%c,& int c%&%&(new int%r+&%c,& int i,j,k $ystem.out.println#0;nter =he ;lements )nto =he 6irst 4atri20' for#i(* i/r+ i33' for#j(* j/c+ j33'
"
a%i&%j&()nteger.parse)nt#dis.read?ine#''
1
$ystem.out.println#0;nter =he ;lements )nto =he $econd 4atri20' for#i(* i/r, i33' for#j(* j/c, j33'
"
b%i&%j&()nteger.parse)nt#dis.read?ine#''
1
www.studentrocz.co.cc www.studentrocz.blogspot.com
c%i&%j&(c%i&%j&3a%i&%k&*b%k&%j&
1 1 1
$ystem.out.println#c%i&%j&30
1
0'
$ystem.out.println#0
1 1 1 1
0'
Output:
www.studentrocz.co.cc
www.studentrocz.blogspot.com
Week-2c) Aim: Write a program that display each integer and sum of all integers using stringtokenizer. Code: import java.util.* class $tringtoken
"
$ystem.out.println#0sum( 03sum'
1 1
Output:
www.studentrocz.co.cc
www.studentrocz.blogspot.com
Week-3a) Aim: Write a java program that checks whether a given string is a palindrome or not.;2:4C<C4 Code: import java.lang.* import java.io.* import java.util.* class 8alindrome
"
<ata)nput$tream dis(new <ata)nput$tream#$ystem.in' $tring word(dis.read?ine# ' int flag(+ int left(* int right(word.length# '-+ while#left/right'
"
if#word.charCt#left'A(word.charCt#right''
"
flag(* 55break
1
Output:
www.studentrocz.co.cc www.studentrocz.blogspot.com
Week-3b) Aim:Write a java program for sorting a given list of name in ascending order. Code: import java.lang.* import java.io.* import java.util.* class $ort
"
<ata)nput$tream dis(new <ata)nput$tream#$ystem.in' $ystem.out.println#0enter the size0' int n()nteger.parse)nt#dis.read?ine#'' $tring array% &(new $tring%n& $ystem.out.println#0enter names0' for#int i(* i/n i33'
"
array%i&(dis.read?ine#'
1
if#array%j&.compare=o#array%j3i&'D*'
"
www.studentrocz.co.cc
www.studentrocz.blogspot.com
$ystem.out.println#array%i&'
1 1
Output:
www.studentrocz.co.cc
www.studentrocz.blogspot.com
Week-3c) Aim: Write a java program to make frequency count of words in a given te2t. Code: import java.util.* import java.lang.* import java.io.* class 6re
"
<ata)nput$tream dis(new <ata)nput$tream#$ystem.in' $tring str(dis.read?ine#' $tring temp $tring=okenizer st(new $tring=okenizer#str' int n(st.count=okens# ' $tring a% &(new $tring%n& int i(*,count(* while#st.has4ore=okens#''
"
a%i&(st.ne2t=oken#' i33
1
if#a%y&.compare=o#a%y3+&'D*'
"
$ystem.out.println#a%i&30 0'
1
while#a%i&.compare=o#a%i3+&'((*'
"
$ystem.out.println#a%i&30 03count'
1 1 1
Output:
www.studentrocz.co.cc
www.studentrocz.blogspot.com
Week-4a) Aim: Write a java program that reads a filename from user then displays information from user then displays information about whether the file e2ists,whether file is readable, whether file is writable the type of file and length of file in bytes. Code: import java.lang.* import java.io.* import java.util.* class 6ile:p
"
6ile f(new 6ile#0E:5week F58alindrome.java0' $ystem.out.println#f.getBame#'' $ystem.out.println#f.get8ath#'' $ystem.out.println#f.getBame#'' $ystem.out.println#f.can>ead#'G0>eadable0:0Bot >eadable0' $ystem.out.println#f.e2ists#'G0;2ist0:0Bon ;2ist0'
www.studentrocz.co.cc www.studentrocz.blogspot.com
Output:
Week-4b) Aim: Write a java program that reads a file and displays the file on the screen with a line number before each line. Code: import java.util.* import java.io.* import java.lang.* class 6ileBo
"
int linenum(* $tring line try" 6ile f(new 6ile#0E:5f.t2t 0' if#f.e2ists#''
"
www.studentrocz.co.cc
www.studentrocz.blogspot.com
while#infile.hasBe2t#''
"
line(infile.ne2t?ine#' $ystem.out.println#33linenum30
1
03line'
infile.close#'
1 1
catch#;2ception e'
"
$ystem.out.println#e'
1 1 1
Output:
Week-4c) Aim: Write a java program that displays the number of characters,lines and words in ate2t file import java.lang.* import java.io.* class 6ile@ount
"
www.studentrocz.co.cc
www.studentrocz.blogspot.com
1while#iA(-+' f.close#' $ystem.out.println#0line count is 03lcount' $ystem.out.println#0@haracter count is 03ccount' $ystem.out.println#0word count is 03wcount' 1 catch#;2ception e'
"
$ystem.out.println#e'
1 1 1
Output:
Week-5a)i) Aim: Write a java program that )mplements stack C<=. Code: import java.lang.* import java.io.* import java.util.* interface 4ystack
"
int n(+* public void pop#' public void push#' public void peek#' public void display#'
1
www.studentrocz.co.cc
www.studentrocz.blogspot.com
try" <ata)nput$tream dis(new <ata)nput$tream#$ystem.in' if#top((#n-+'' " $ystem.out.println#0overflow0' return 1 else " $ystem.out.println#0enter element0' int ele()nteger.parse)nt#dis.read?ine#'' stack%33top&(ele 1
1
catch#;2ception e'
"
$ystem.out.println#0e0'
1
if#top/*' " $ystem.out.println#0underflow0' return 1 else " int popper(stack%top& top-$ystem.out.println#0popped element0 3popper' 1 1 public void peek#' " if#top/*'
"
www.studentrocz.co.cc www.studentrocz.blogspot.com
$tring str(0 0 for#int i(* i/(top i33' str(str30 03stack%i& $ystem.out.println#0elements are03str'
1
1 1 class $tackadt
"
public static void main#$tring arg%&'throws ):;2ception " <ata)nput$tream dis(new <ata)nput$tream#$ystem.in' $tackimp stk(new $tackimp#' int ch(* do" $ystem.out.println#0enter ur choice for +.push ,.pop F.peek ..peek J.displayK' ch()nteger.parse)nt#dis.read?ine#'' switch#ch'" case +:stk.push#' break case ,:stk.pop#' break case F:stk.peek#' break case .:stk.display#' break case J:$ystem.e2it#*'
1
1while#ch/(J' 1
1
Output:
www.studentrocz.co.cc www.studentrocz.blogspot.com
Week-5a)ii) Aim: Write a java program that evaluates the postfi2 e2pression Code: import java.lang.* import java.io.* import java.util.* class )nto8o
"
$tack stk(new $tack#' <ata)nput$tream dis(new <ata)nput$tream#$ystem.in' char input $tring output(0 0 $ystem.out.println#0enter e2presion0' $tring e2p(dis.read?ine#'
input(e2p.charCt#pos' switch#input'
"
case H3H : case H-H : case H*H : case H5H : stk.push#input' break case H'H : char ch(##@haracter'stk.pop#'' output(output3ch break case H#H : break default : output(output3input break
1 1
1
1
$ystem.out.println#0result is03output'
Output:
www.studentrocz.co.cc
www.studentrocz.blogspot.com
www.studentrocz.co.cc
www.studentrocz.blogspot.com
static int dooperation#int fo,int so,char op' " int val(* switch#op'" case H3H : val(fo3so break case H-H : val(fo-so break case H*H : val(fo*so break case H5H : val(fo5so break 1 return val
1
$tack stk(new $tack#' <ata)nput$tream dis(new <ata)nput$tream#$ystem.in' $tring str(dis.read?ine#' char ch for#int pos(* pos/str.length#' pos33' " ch(str.charCt#pos' switch#ch'" case H*H : case H+H : case H,H : case HFH : case H.H : case HJH : case HLH : case HMH : case HNH : case HOH : stk.push#new )nteger##@haracter.digit#ch,+*'''' break case H3H: case H-H: case H5H: case H*H:
www.studentrocz.co.cc www.studentrocz.blogspot.com
int fo(##)nteger'stk.pop#''.intPalue#'
int so(##)nteger'stk.pop#''.intPalue#'
Week-6a) Aim:<evelop an applet that receives an integer in one te2t field, and computes as factorial value and returns it in another te2t field,when the button named Q@omputeK is clicked
www.studentrocz.co.cc
www.studentrocz.blogspot.com
import java.io.* import java.awt.* import java.applet.Cpplet 5*/applet code(0applet=est.class0 height(+** width(J**D/5appletD*5 public class applet=est e2tends Cpplet
"
Output:
www.studentrocz.co.cc
www.studentrocz.blogspot.com
import java.awt.* import java.awt.event.* import java.applet.Cpplet 5*/applet code(0Cdd;vent.class0 height(+** width(J**D/5appletD*5 public class Cdd;vent e2tends Cpplet implements Cction?istener
"
l(new ?abel#0;nter the number U press the button0' tf+(new =e2t6ield#00,J' tf,(new =e2t6ield#00,+*' b(new Tutton#0press me0' add#l' add#tf+' add#tf,' add#b' b.addCction?istener#this'
1
Output:
www.studentrocz.co.cc www.studentrocz.blogspot.com
Week-7)
www.studentrocz.co.cc www.studentrocz.blogspot.com
Aim: Write a java program that works as a simple calculator.Vse a grid layout to arrange buttons for the digits 3,-,*,5,9 operations.Cdda te2t field to display the result. import java.awt.* import java.applet.* import java.awt.event.* public class @alculator e2tends Cpplet implements Cction?istener
"
=e2t6ield t+ $tring a(00,b $tring oper(00,s(00,p(00 int first(*,second(*,result(* 8anel p+ Tutton b*,b+,b,,bF,b.,bJ,bL,bM,bN,bO Tutton add,sub,mul,div,mod,res,space public void init#'
"
8anel p,,pF p+(new 8anel#' p,(new 8anel#' pF(new 8anel#' t+(new =e2t6ield#a,,*' p+.set?ayout#new Torder?ayout#'' p,.add#t+' b*(new Tutton#0*0' b+(new Tutton#0+0' b,(new Tutton#0,0' bF(new Tutton#0F0' b.(new Tutton#0.0' bJ(new Tutton#0J0' bL(new Tutton#0L0' bM(new Tutton#0M0' bN(new Tutton#0N0' bO(new Tutton#0O0' add(new Tutton#030' sub(new Tutton#0-0' mul(new Tutton#0*0' div(new Tutton#050' mod(new Tutton#090' res(new Tutton#0(0' space(new Tutton#0c0' pF.set?ayout#new Rrid?ayout#.,.'' b*.addCction?istener#this' b+.addCction?istener#this' b,.addCction?istener#this' bF.addCction?istener#this' b..addCction?istener#this'
www.studentrocz.co.cc www.studentrocz.blogspot.com
bJ.addCction?istener#this'
bL.addCction?istener#this' bM.addCction?istener#this' bN.addCction?istener#this' bO.addCction?istener#this'
add.addCction?istener#this' sub.addCction?istener#this' mul.addCction?istener#this' div.addCction?istener#this' mod.addCction?istener#this' res.addCction?istener#this' space.addCction?istener#this' pF.add#b*' pF.add#b+' pF.add#b,' pF.add#bF' pF.add#b.' pF.add#bJ' pF.add#bL' pF.add#bM' pF.add#bN' pF.add#bO' pF.add#add' pF.add#sub' pF.add#mul' pF.add#div' pF.add#mod' pF.add#res' pF.add#space' p+.add#p,,Torder?ayout.B:>=W' p+.add#pF,Torder?ayout.@;B=;>' add#p+'
1
t+.set=e2t#t+.get=e2t#'3a'
1
if#a((03077a((0-077a((0*077a((05077a((090'
"
www.studentrocz.co.cc
www.studentrocz.blogspot.com
if#a((0(0'
"
if#a((0c0' t+.set=e2t#00'
1 1
Output:
www.studentrocz.co.cc
www.studentrocz.blogspot.com
Week-8 Aim: Write a java program for handling mouse events. Code: import java.io.* import java.applet.Cpplet import java.awt.* import java.awt.event.* 5*/applet code(4ouse height(.** width(.**D/5appletD*5 public class 4ouse e2tends Cpplet implements 4ouse?istener,4ouse4otion?istener
"
add4ouse?istener#this' add4ouse4otion?istener#this'
1
www.studentrocz.co.cc www.studentrocz.blogspot.com
Output:
www.studentrocz.co.cc www.studentrocz.blogspot.com
Week-9a) Aim: Write a java program that creates three threads.6irst thread displaysKRood 4orningK every one second,the second thread displays QWelloK every two seconds and the third thread displays QWelcomeK every three seconds. Code: class 6rst implements >unnable
"
=hread t 6rst#'
"
=hread t sec#'
"
www.studentrocz.co.cc www.studentrocz.blogspot.com
1
1
=hread t third#'
"
www.studentrocz.co.cc www.studentrocz.blogspot.com
Output:
www.studentrocz.co.cc www.studentrocz.blogspot.com
Week-9b)
Aim: Write a java program that correctly implements producer consumer problem using the concept of inter thread communication. Code: import java.lang.* import java.lang.=hread class !
"
catch#)nterrupted;2ception e'
"
$ystem.out.println#0)nterrupted;2ception catch0'
1
catch#)nterrupted;2ception e'
"
$ystem.out.println#0)nterrupted;2ception catch0'
1
!q 8roducer#! q'
"
www.studentrocz.co.cc www.studentrocz.blogspot.com
q.put#i33'
1 1 1
!q @onsumer#! q'
"
while#true'
"
q.get#'
1 1 1
class 8@6i2ed
"
Output:
www.studentrocz.co.cc
www.studentrocz.blogspot.com
www.studentrocz.co.cc www.studentrocz.blogspot.com
Week-10 ) Aim: Write a program that creates a user interface to perform integer divisions.=he user enters two numbers int the te2tfields,Bum+ and Bum,.=he divison of Bum+ and Bum, is displayed in the >esult field when the <ivide button is clicked.)f Bum+ or Bum, were not an integer,the program would throw a Bumber6ormat;2ception.)f Bum, were Eero,the program would throw an Crithmetic;2ception <isplay the e2ception in a message dialog bo2. Code: import java.awt.* import java.awt.event.* import java.applet.Cpplet import java2.swing.* 5*/applet code(0Cdd;vent.class0 height(+** width(J**D/5appletD*5 public class Cdd;vent e2tends Cpplet implements Cction?istener
"
=e2t6ield tf+ =e2t6ield tf, Tutton b =e2t6ield tfF ?abel l public void init#'
"
l(new ?abel#0enter the numbers and press divide button0' tf+(new =e2t6ield#00,J' tf,(new =e2t6ield#00,J' tfF(new =e2t6ield#00,J' b(new Tutton#0<ivide0' add#l' add#tf+' add#tf,' add#b' add#tfF' b.addCction?istener#this'
1
if#ae.getCction@ommand#'((0<ivide0'
"
catch#Crithmetic;2ception e+'
"
X:ption8ane.show4essage<ialog#null,0Crthimetic ;2ception0'
www.studentrocz.co.cc www.studentrocz.blogspot.com
catch#Bumber6ormat;2ception e,'
"
X:ption8ane.show4essage<ialog#null,0Bumber6ormat;2ception0'
1 1 1 1
Output:
Week-11)
www.studentrocz.co.cc
www.studentrocz.blogspot.com
Aim: Write a java program that implements a simple client5server application. =he client sends a data to a server. =he server receives the data uses it to produce a result, and then sends the result back to the client. =he client displays the result on the console .6or ;2: =he data sent from the client is the radius of a circle ,and the result produced by the server is the area of the circle,#Vse java.net' Code: import java.io.* import java.net.* class @lient
"
$ocket s(new $ocket#)netCddress.get?ocalWost#',+****' $ystem.out.println#0enter the radius of the circle 0' <ata)nput$tream dis(new <ata)nput$tream#$ystem.in' int n()nteger.parse)nt#dis.read?ine#'' 8rint$tream ps(new 8rint$tream#s.get:utput$tream#'' ps.println#n' <ata)nput$tream dis+(new <ata)nput$tream#s.get)nput$tream#'' $ystem.out.println#0area of the circle from server:03dis+.read?ine#''
1 1
$erver$ocket ss(new $erver$ocket#+****' $ocket s(ss.accept#' <ata)nput$tream dis(new <ata)nput$tream#s.get)nput$tream#'' int n()nteger.parse)nt#dis.read?ine#'' 8rint$tream ps(new 8rint$tream#s.get:utput$tream#'' ps.println#F.+.*n*n'
1 1
Output:
www.studentrocz.co.cc www.studentrocz.blogspot.com
www.studentrocz.co.cc www.studentrocz.blogspot.com
Week-12a)
Aim: Write a java program that stimulates a traffic light. =he program lets the user select one of three lights: red, yellow, or green. When a radio button is selected, the light is turned on, and only one light can be on at a time Bo light is on when the program starts. Code: import java.awt.* import java.awt.event.* import java.applet.* 5*/applet code(0@TRroup0 width(,J* height(,**D/5appletD*5 public class @TRroup e2tends Cpplet implements )tem?istener
"
cbg(new @heckbo2Rroup#' >ed(new @heckbo2#0>;<0,cbg,false' Rreen(new @heckbo2#0R>;;B0,cbg,false' Sellow(new @heckbo2#0S;??:W0,cbg,false' add#>ed' add#Sellow' add#Rreen' >ed.add)tem?istener#this' Rreen.add)tem?istener#this' Sellow.add)tem?istener#this'
1
repaint#'
1
55g.draw:val#+*,+*,J*,J*' if#cbg.get$elected@heckbo2#'.get?abel#'((0>;<0'
"
g.set@olor#@olor.red' g.fill:val#+*,+*,J*,J*'
1
if#cbg.get$elected@heckbo2#'.get?abel#'((0S;??:W0'
"
g.set@olor#@olor.yellow' g.fill:val#+*,+*,J*,J*'
1
if#cbg.get$elected@heckbo2#'.get?abel#'((0R>;;B0'
"
www.studentrocz.co.cc www.studentrocz.blogspot.com
g.set@olor#@olor.green' g.fill:val#+*,+*,J*,J*'
1 1 1
Output:
Week-12b)
www.studentrocz.co.cc
www.studentrocz.blogspot.com
Aim: Write a java program that allows the user to draw lines,rectangles and ovals. Code: import java.awt.* import java.applet.* public class <raw e2tends Cpplet
"
Date: +M-*F-,**O
www.studentrocz.co.cc www.studentrocz.blogspot.com
Week-13a) Aim: Write a java program to create an abstract class named $hape that contains an empty method name number:f$ides#'.8rovide three classes named =rapezoid,=raingle and We2agon such that each of the classes e2tends the class $hape. ;ach one of the classes contains only the method number:f$ides#' that shows the number of sides in the given geometrical figures. Code: import java.lang.* abstract class $hape
"
$ystem.out.println#0three0'
1 1
$ystem.out.println#0four0'
1 1
$ystem.out.println#0si20'
1 1
=raingle =(new =raingle#' =rapezoid =a(new =rapezoid#' We2agon W(new We2agon#' =.number:f$ides#' =a.number:f$ides#' W.number:f$ides#'
www.studentrocz.co.cc www.studentrocz.blogspot.com
Output:
Week-13b) Aim: $uppose that a table named =able.t2t is stored in a te2t file. =he first line in the file is the header, and the remaining lines corresponding to rows in the table. =he elements are separated by commas. Write a java program to display the tabe using X=able @omponent. Code: import java2.swing.* import java.awt.* import java.io.* import java.util.* public class X=able;2 e2tends X8anel
"
public X=able;2#'
"
super#new Rrid?ayout#+, *'' 6ile file ( new 6ile#0):5=able.t2t0' 6ile)nput$tream fis ( null Tuffered)nput$tream bis ( null <ata)nput$tream dis ( null $tring init<ata%& ( new $tring%+**& $tring%& columnBames ( null :bject%&%& data ( null int rows ( * try
"
fis ( new 6ile)nput$tream#file' bis ( new Tuffered)nput$tream#fis' dis ( new <ata)nput$tream#bis' while #dis.available#' A( *'
"
init<ata%rows33& ( dis.read?ine#'
1
$tring=okenizer st ( new $tring=okenizer#init<ata%*&,0,0' columnBames ( new $tring%st.count=okens#'& data ( new :bject%rows - +&%st.count=okens#'& for #int i ( * i / rows i33'
"
if #i A( *'
"
else
www.studentrocz.co.cc www.studentrocz.blogspot.com
"
columnBames%j33& ( st.ne2t=oken#'
1
1
fis.close#' bis.close#'
dis.close#'
1
e.print$tack=race#'
1
e.print$tack=race#'
1
final X=able table ( new X=able#data, columnBames' table.set8referred$crollablePiewport$ize#new <imension#J**, +**'' 55table.set6illsPiewportWeight#true' X$croll8ane scroll8ane ( new X$croll8ane#table' add#scroll8ane'
1
X6rame frame ( new X6rame#0$imple=able<emo0' frame.set<efault@lose:peration#X6rame.;Y)=Z:BZ@?:$;' X=able;2 new@ontent8ane ( new X=able;2#' new@ontent8ane.set:paque#true' frame.set@ontent8ane#new@ontent8ane' frame.pack#' frame.setPisible#true'
1
java2.swing.$wingVtilities.invoke?ater#new >unnable#'
"
createCnd$howRV)#'
1
1'
www.studentrocz.co.cc
www.studentrocz.blogspot.com
1 1
Output: