Anda di halaman 1dari 52

www.studentrocz.co.

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
"

public static void main#$tring arg%&'


"

int a,b,c a()nteger.parse)nt#arg%*&' b()nteger.parse)nt#arg%+&' c()nteger.parse)nt#arg%,&' int delta(#b*b'-#.*a*c' if#delta/*'


"

$ystem.out.println#0roots are imaginary,no real values0' 1 else


"

double 2+(#-b34ath.sqrt#delta''5#,*a' double 2,(#-b-4ath.sqrt#delta''5#,*a' $ystem.out.println#2+30 032,'


1 1

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
"

public static void main#$tring arg% &'


"

int f,f+(*,f,(+,n n()nteger.parse)nt#arg %*&' $ystem.out.println#f+' $ystem.out.println#f,' for#int i(* i/n-+ i33'
"

f(f+3f, f+(f, f,(f $ystem.out.println#f'


1

$ystem.out.println#06ibonacci series using recursion0' for#int i(* i/n i33' $ystem.out.println#fib#i''


1

static int fib#int n'


"

if#n((*77n((+' return + else return fib#n-+'3fib#n-,'


1 1

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
"

public static void main#$tring arg%&'


"

int n,c,i,j n()nteger.parse)nt#arg%*&' $ystem.out.println#0prime numbers are0' for#i(+ i/(n i33'


"

c(* for#j(+ j/(i j33'


"

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
"

public static void main#$tring arg%&'throws ):;2ception


"

<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

if#c+A(r,' $ystem.out.println#04ultiplication )s Bot 8ossible0' else


"

for#i(* i/r+ i33'


"

for#j(* j/c+ j33'


"

c%i&%j&(* for#k(* k/r, k33'


"

www.studentrocz.co.cc www.studentrocz.blogspot.com

c%i&%j&(c%i&%j&3a%i&%k&*b%k&%j&
1 1 1

for#i(* i/r+ i33'


"

for#j(* j/c, j33'


"

$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
"

public static void main#$tring arg%&'


"

int sum(* $tring=okenizer sto(new $tring=okenizer#arg%*&,0:0' $ystem.out.println#sto.count=okens#'' while#sto.has4ore=okens#''


"

int a()nteger.parse)nt#sto.ne2t=oken#'' sum(sum3a $ystem.out.println#a'


1

$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
"

public static void main#$tring arg% &' throws ):;2ception


"

<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

left33 right-- 1 if#flag((+' $ystem.out.println#0palindrome0' else $ystem.out.println#0not palindrome0'


1 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
"

public static void main#$tring arg% &' throws ):;2ception


"

<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

for#int i(* i/n i33'


"

for#int j(* j/n-+ j33'


"

if#array%j&.compare=o#array%j3i&'D*'
"

$tring temp(array%j& array %j&(array%j3+& array%j3+&(temp


1 1 1

$ystem.out.println#0the sorted strings are0' for#int i(* i/n i33'

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
"

public static void main#$tring% & args' throws ):;2ception


"

<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

for#int 2(+ 2/n 233'


"

for#int y(* y/n-2 y33'


"

if#a%y&.compare=o#a%y3+&'D*'
"

temp(a%y& a%y&(a%y3+& a%y3+&(temp


1 1

$ystem.out.println#0the sorted string are:0' for#i(* i/n i33'


"

$ystem.out.println#a%i&30 0'
1

$ystem.out.println#' for#i(* i/n i33' "count(+ if#i/n-+'


"

while#a%i&.compare=o#a%i3+&'((*'
"

count33 i33 if#iD(n-+'


"

$ystem.out.println#a%i&30 03count' $ystem.e2it#*'


1 1 1

$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
"

public static void main#$tring arg% &'throws ):;2ception


"

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

$ystem.out.println#f.canWrite#'G0 0 :0 0' $ystem.out.println#f.length#'30bytes0'


1 1

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
"

public static void main#$tring arg% &'throws ):;2ception


"

int linenum(* $tring line try" 6ile f(new 6ile#0E:5f.t2t 0' if#f.e2ists#''
"

$canner infile(new $canner#f'

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
"

public static void main#$tring arg%&'


"

int ccount(*,lcount(*,wcount(* try


"

6ile)nput$tream f(new 6ile)nput$tream#0E:5week F58alindrome.java0' int i do" i(f.read#' if#iA(-+'


"

www.studentrocz.co.cc

www.studentrocz.blogspot.com

ccount33 if#i((HInH' lcount33 if##char'i((H H77#char'i((HInH' wcount33


1

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

class $tackimp implements 4ystack


"

www.studentrocz.co.cc

www.studentrocz.blogspot.com

int stack%&(new int%n& int top(-+ public void push#'


"

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

1 public void pop#'


"

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/*'
"

$ystem.out.println#0underflow0' return: 1 else


"

www.studentrocz.co.cc www.studentrocz.blogspot.com

int popper(stack%top& $ystem.out.println#0popped element0 3popper'


1

1 public void display#' " if#top/*'


"

$ystem.out.println#0empty0' return 1 else


"

$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
"

public static void main#$tring arg%&'throws ):;2ception


"

$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#'

for#int pos(* pos/e2p.length#' pos33'


"

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

Week-Ja'iii' Cim:Write a java program that evaluates the postfi2 e2pression

import java.lang.* import java.io.* import java.util.* class ;valuation


"

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

public static void main#$tring arg%&'throws ):;2ception


"

$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#'

int result(dooperation#fo,so,ch' stk.push#new )nteger#result'' break


1

1 $ystem.out.println#0result is0 3stk.pop#''


1
1

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
"

public void paint#Rraphics g'


"

g.draw$tring#0=his is )= ))nd Sear ))nd sem0,J*,L*' set6oreground#@olor.blue'


1 1

Output:

Week-6b) Aim:<evelop an applet that displays a simple message Code:

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
"

=e2t6ield tf+ =e2t6ield tf, Tutton b ?abel l public void init#'


"

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

public void action8erformed#Cction;vent ae'


"

$tring str(ae.getCction@ommand#' $tring str+ int fact(+ if#str((0press me0'


"

int n()nteger.parse)nt#tf+.get=e2t#'' for#int i(+ i/(n i33' fact(fact*i str+(003fact tf,.set=e2t#str+'


1 1 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

public void action8erformed#Cction;vent ae'


"

a(ae.getCction@ommand#' if#a((0*077a((0+077a((0,077a((0F077a((0.077a((0J077a((0L077a((0M077a((0N077 a((0O0'


"

t+.set=e2t#t+.get=e2t#'3a'
1

if#a((03077a((0-077a((0*077a((05077a((090'
"

first()nteger.parse)nt#t+.get=e2t#'' oper(a t+.set=e2t#00'

www.studentrocz.co.cc

www.studentrocz.blogspot.com

if#a((0(0'
"

if#oper((030' result(first3)nteger.parse)nt#t+.get=e2t#'' if#oper((0-0' result(first-)nteger.parse)nt#t+.get=e2t#'' if#oper((0*0' result(first*)nteger.parse)nt#t+.get=e2t#'' if#oper((050'


result(first5)nteger.parse)nt#t+.get=e2t#''

if#oper((090' result(first9)nteger.parse)nt#t+.get=e2t#'' t+.set=e2t#result300'


1

if#a((0c0' t+.set=e2t#00'
1 1

5*/applet code(@alculator width(,** height(,**D/5appletD*5

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
"

$tring t2t(00 int 2(+*,y(F* public void init#'


"

add4ouse?istener#this' add4ouse4otion?istener#this'
1

public void mouse@licked#4ouse;vent me'


"

t2t(04ouse @licked0 set6oreground#@olor.pink' repaint#'


1

public void mouse;ntered#4ouse;vent me'


"

t2t(04ouse ;ntered0 repaint#'


1

public void mouse;2ited#4ouse;vent me'


"

t2t(04ouse ;2ited0 set6oreground#@olor.blue' repaint#'


1

public void mouse8ressed#4ouse;vent me'


"

t2t(04ouse 8ressed0 set6oreground#@olor.blue' repaint#'


1

public void mouse4oved#4ouse;vent me'


"

t2t(04ouse 4oved0 set6oreground#@olor.red' repaint#'


1

www.studentrocz.co.cc www.studentrocz.blogspot.com

public void mouse<ragged#4ouse;vent me'


"

t2t(04ouse <ragged0 set6oreground#@olor.green' repaint#'


1

public void mouse>eleased#4ouse;vent me'


"

t2t(04ouse >eleased0 set6oreground#@olor.yellow' repaint#'


1

public void paint#Rraphics g'


"

g.draw$tring#t2t,F*,.*' show$tatus#04ouse events Wandling0'


1 1

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#'
"

t(new =hread#this' $ystem.out.println#0Rood 4orning0' t.start#'


1

public void run#'


"

for#int i(* i/+* i33'


"

$ystem.out.println#0Rood 4orning03i' try" t.sleep#+***' 1 catch#;2ception e' " $ystem.out.println#e' 1


1 1 1

class sec implements >unnable


"

=hread t sec#'
"

t(new =hread#this' $ystem.out.println#0hello0' t.start#'


1

public void run#'


"

for#int i(* i/+* i33'


"

$ystem.out.println#0hello03i' try" t.sleep#,***' 1

www.studentrocz.co.cc www.studentrocz.blogspot.com

catch#;2ception e' " $ystem.out.println#e' 1


1

1
1

class third implements >unnable


"

=hread t third#'
"

t(new =hread#this' $ystem.out.println#0welcome0' t.start#'


1

public void run#'


"

for#int i(* i/+* i33'


"

$ystem.out.println#0welcome03i' try" t.sleep#F***'


1

catch#;2ception e' " $ystem.out.println#e' 1 1


1 1

public class 4ultithread


"

public static void main#$tring arg%&'


"

new 6rst#' new sec#' new third#' 1


1

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 !
"

int n boolean value$et(false synchronized int get#'


"

if#Avalue$et' try" wait#'


1

catch#)nterrupted;2ception e'
"

$ystem.out.println#0)nterrupted;2ception catch0'
1

$ystem.out.println#0got:03n' value$et(false notify#' return n


1

synchronized void put#int n'


"

if#value$et' try" wait#'


1

catch#)nterrupted;2ception e'
"

$ystem.out.println#0)nterrupted;2ception catch0'
1

this.n(n value$et(true $ystem.out.println#0put:03n' notify#'


1 1

class 8roducer implements >unnable


"

!q 8roducer#! q'
"

www.studentrocz.co.cc www.studentrocz.blogspot.com

this.q(q new =hread#this,0producer0'.start#'


1

public void run#'


"

int i(* while#true'


"

q.put#i33'
1 1 1

class @onsumer implements >unnable


"

!q @onsumer#! q'
"

this.q(q new =hread#this,0consumer0'.start#'


1

public void run#'


"

while#true'
"

q.get#'
1 1 1

class 8@6i2ed
"

public static void main#$tring args%&'


"

! q(new !#' new 8roducer#q' new @onsumer#q'


1 1

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

public void action8erformed#Cction;vent ae'


"

if#ae.getCction@ommand#'((0<ivide0'
"

try" int n+()nteger.parse)nt#tf+.get=e2t#'' int n,()nteger.parse)nt#tf,.get=e2t#'' int n(n+5n, tfF.set=e2t#003n'


1

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
"

public static void main#$tring ar%&'throws ;2ception


"

$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

import java.io.* import java.net.* class $erver


"

public static void main#$tring ar%&'throws ;2ception


"

$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
"

$tring msg(00 @heckbo2 >ed,Rreen,Sellow @heckbo2Rroup cbg public void init#'


"

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

public void item$tate@hanged#)tem;vent ie'


"

repaint#'
1

public void paint#Rraphics g'


"

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
"

public void paint#Rraphics g'


"

g.set@olor#@olor.T?V;' g.draw?ine#F,.,+*,,F' g.draw:val#+OJ,+**,O*,JJ' g.draw>ect#+**,.*,O*,J' g.draw>ound>ect#+.*,F*,O*,O*,L*,F*'


1 1

5*/applet code(0<raw.class0 width(F** height(F**D/5appletD*5 Output:

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
"

abstract void number:f$ides#'


1

class =raingle e2tends $hape


"

public void number:f$ides#'


"

$ystem.out.println#0three0'
1 1

class =rapezoid e2tends $hape


"

public void number:f$ides#'


"

$ystem.out.println#0four0'
1 1

class We2agon e2tends $hape


"

public void number:f$ides#'


"

$ystem.out.println#0si20'
1 1

public class $ides


"

public static void main#$tring arg%&'


"

=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( *'
"

int k ( * $tring=okenizer st+ ( new $tring=okenizer#init<ata%i&,0,0' while #st+.has4ore=okens#''


"

data%i - +&%k33& ( st+.ne2t=oken#'


1 1

else

www.studentrocz.co.cc www.studentrocz.blogspot.com

"

int j ( * while #st.has4ore=okens#''


"

columnBames%j33& ( st.ne2t=oken#'
1
1

fis.close#' bis.close#'
dis.close#'
1

catch #6ileBot6ound;2ception e'


"

e.print$tack=race#'
1

catch #):;2ception e'


"

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

private static void createCnd$howRV)#'


"

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

public static void main#$tring%& args'


"

java2.swing.$wingVtilities.invoke?ater#new >unnable#'
"

public void run#'


"

createCnd$howRV)#'
1

1'

www.studentrocz.co.cc

www.studentrocz.blogspot.com

1 1

Output:

Anda mungkin juga menyukai