Object Oriented Programming Labmanual Jntu

Embed Size (px)

Citation preview

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    1/32

    OOP Through JAVA Department of Computer Science and Engineering

    Department

    Of

    Computer Science & Engineering

    Object Oriented Programming Through JAVA

    Lab manual

    ALAJI INSTITUTE OF

    TECHNOLOGY AND SCIENCE

    Department of computer cience & engineering

    !n"charge #OD Principal

    Prepared b$% Approed &

    'eie(ed b$%

    !ued b$% ()e)f Date%

    Balaji Institute of Technolog and Science !

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    2/32

    OOP Through JAVA Department of Computer Science and Engineering

    ALAJI INSTITUTE OF TECHNOLOGY AND

    SCIENCE

    Laknepally(V), Narsampet, Warangal

    DEPA'T*E+T O, CO*P-TE' SC!E+CE &

    E+.!+EE'!+.

    Lab Manual for the Academic Year !""#"

    ($n accordance %ith &N' syllabus)

    *&+' - ../s 'hrough &AVA LA*

    '0+AM - +

    12.23

    1 OBJECT ORIENTED PROGRAMMING LAB

    Balaji Institute of Technolog and Science "

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    3/32

    OOP Through JAVA Department of Computer Science and Engineering

    Week1 :a) Write a Java program that prints all real solutions to the quadraticequation ax2 + bx + c = 0 !ead in a" b" c and use the quadratic #ormula $#the discriminant b2%&ac is negative" displa' a message stating that thereare

    no real solutionsimport java.io.*;import java.io.*;cla root!p"#lic tatic voi$ mai%&'tri%( ar()+t,ro- IOEc/ptio%!B"0/r/$R/a$/r #r%/- B"0/r/$R/a$/r&%/-I%p"t'tr/amR/a$/r&'2t/m.i%++;i%t aI%t/(/r.par/I%tr.r/a$Li%/&++;i%t #I%t/(/r.par/I%tr.r/a$Li%/&++;

    i%t cI%t/(/r.par/I%tr.r/a$Li%/&++;i%t $&*#+3&4*a*c++;i5&$67+!'2t/m.o"t.pri%t&8root ar/ r/al a%$ $iti%ct8+;9i5&$7+!'2t/m.o"t.pri%t&8root ar/ r/al8+;9i5&$:7+

    !'2t/m.o"t.pri%t&8t,/r/ i %o ol"tio%8+;999b) Write a Java program that uses both recursive and non recursive#unctions to print the nth value in the(ibonacci sequenceimport java.io.*;import java.la%(.*;cla #o%acci

    !p"#lic tatic voi$ mai%&'tri%( ar()+ t,ro- IOEc/ptio%!i%t aI%t/(/r.par/I%t&ar()7+;i%t #I%t/(/r.par/I%t&ar()1+;i%t %I%t/(/r.par/I%t&ar()

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    4/32

    OOP Through JAVA Department of Computer Science and Engineering

    !ca=#;'2t/m.o"t.pri%t&c+;a#;#c;

    999

    Week 2 :a) Write a Java program that prompts the user #or an integer and thenprints out all prime numbers up to thatintegerimport java.lang.*;

    import java.io.*;class prime{public static void main(String args[]){int n=Integer.parseInt(args[0]);or(int r=0;r!n;r""){int t=0;or(int i=#;i!r;i""){

    i (r$i==0){t"";%%i(t!&){S'stem.out.print(r);S'stem.out.print( );%%

    %%

    b) Write a Java program to multipl' to given matricesimport java.lang.*;import java.io.*;class arra'mul

    Balaji Institute of Technolog and Science $

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    5/32

    OOP Through JAVA Department of Computer Science and Engineering

    {public static void main(String args[])tros I+,-ception{uered/eader br=ne uered/eader(ne InputStream/eader(S'stem.in));S'stem.out.print(enter no o ros);

    int m=Integer.parseInt(br.readine());S'stem.out.print(enter no o columns);int n=Integer.parseInt(br.readine());int a[][]=ne int[m][n];int i1j12;{or( i=0;i!m;i""){or(j=0;j!n;j""){a[i][j]=Integer.parseInt(br.readine());

    %%S'stem.out.print(enter no o ros);int p=Integer.parseInt(br.readine());S'stem.out.print(enter no o columns);int 3=Integer.parseInt(br.readine());int b[][]=ne int[p][3];{or( i=0;i!p;i""){or( j=0;j!3;j"")

    {b[i][j]=Integer.parseInt(br.readine());%%%int c[][]=ne int[m][i];i(n==p){or( i=0;i!m;i""){or( j=0;j!3;j""){c[i][j]=0;or( 2=0;2!p;2""){c[i][j]=c[i][j]"(a[i][2]*b[2][j]);%%%

    Balaji Institute of Technolog and Science %

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    6/32

    OOP Through JAVA Department of Computer Science and Engineering

    or(i=0;i!m;i""){or(j=0;j!3;j""){S'stem.out.println(c[i][j]);

    %%%%% % .

    c) Write a Java *rogram that reads a line o# integers" and then displa'seach integer" and the sum o# all theintegers ,se -tring.okeni/er class o# avautil)import java.io.*;

    class sum{public static void main(String args[])tros I+,-ception{uered/eader br=ne uered/eader(ne InputStream/eader(S'stem.in));int i1n1sum=0;S'stem.out.print(enter te no o elements);n=Integer.parseInt(br.readine());S'stem.out.print(te elements are);or(i=0;i!n;i""){

    int ni=Integer.parseInt(br.readine());sum=sum"ni;%S'stem.out.print(te sum is"sum);%

    %

    Week :a) Write a Java program that checks hether a given string is a palindromeor notcla Pali%$rom/!p"#lic tatic voi$ mai%&'tri%( ar()+!'tri%( ar()7;i%t l/%>i7>%>c7>p;

    Balaji Institute of Technolog and Science &

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    7/32

    OOP Through JAVA Department of Computer Science and Engineering

    l/%.l/%(t,&+;%l/%?

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    8/32

    OOP Through JAVA Department of Computer Science and Engineering

    {s=-[i];-[i]=-[j];-[j]=s;%

    %%S'stem.out.print(string in alpebetical order are);or(int i=0;i!n;i""){S'stem.out.println(-[i]);%%%

    c) Write a Java program to make #requenc' count o# ords in a given textcla Co"%t!p"#lic tatic voi$ mai%&'tri%( ar()+! i%t i>c7;5or&i7;i:ar(.l/%(t,;i==+! '2t/m.o"t.pri%tl%&ar()i+;c==;9'2t/m.o"t.pri%tl%&8%"m#/r o5 -or$8=c+;

    99

    Week & :a) Write a Java program that reads a #ile name #rom the user" then displa'sin#ormation about hether the#ile exists" hether the #ile is readable" hether the #ile is ritable" the t'peo# #ile and the length o# the #ile inb'tes

    import java.io.*;cla @il/D/mo!p"#lic tatic voi$ mai%&'tri%( ar()+!@il/ 51%/- @il/&8?java?cop2ri(,t8>8B,arat,.java8+;'2t/m.o"t.pri%tl%&8l/ %am/8=51.(/tNam/&++;'2t/m.o"t.pri%tl%&8pat,8=51.(/tPat,&++;

    Balaji Institute of Technolog and Science (

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    9/32

    OOP Through JAVA Department of Computer Science and Engineering

    '2t/m.o"t.pri%tl%&8par/%t8=51.(/tPar/%t&++;'2t/m.o"t.pri%tl%&51./it&++;'2t/m.o"t.pri%tl%&51.ca%R/a$&++;'2t/m.o"t.pri%tl%&51.ca%rit/&++;'2t/m.o"t.pri%tl%&51.iDir/ctor2&++;

    '2t/m.o"t.pri%tl%&51.i@il/&++;'2t/m.o"t.pri%tl%&51.latMo$i/$&++;'2t/m.o"t.pri%tl%&51.l/%(t,&++;'2t/m.o"t.pri%tl%&51.$/l/t/&++;99

    b) Write a Java program that reads a #ile and displa's the #ile on the screen"ith a line number be#ore each

    lineimport java.io.*;cla @il/D/mo1!p"#lic tatic voi$ mai%&'tri%( ar()+t,ro- Ec/ptio%!i%t c7;'tri%( 8i= % i % java % pro(ram %8;c,ar #"0/r)%/- c,ar).l/%(t,&+;.(/tC,ar&7>.l/%(t,&+>#"0/r>7+;@il/rit/r 51%/- @il/rit/r&8c?i%$/.tt8+;

    51.-rit/"0/r+;51.clo/&+;@il/R/a$/r 5r%/- @il/R/a$/r&8c?i%$/.tt8+;B"0/r/$R/a$/r #r%/- B"0/r/$R/a$/r&5r+;'tri%( t;-,il/&&t#r.r/a$Li%/&++%"ll+!c==;'2t/m.o"t.pri%tl%&c=t+;95r.clo/&+;

    99

    c) Write a Java program that displa's the number o# characters" lines andords in a text #ileimport java-.sing.*;import java.io.*;

    Balaji Institute of Technolog and Science )

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    10/32

    OOP Through JAVA Department of Computer Science and Engineering

    import java.util.*;public class 6ount{public static void main(String args[]){

    tr'{String s=7+ption8ane.soInput9ialog(,nter te ile name : );ileInputStream =ne ileInputStream(s);9ataInputStream d=ne 9ataInputStream();String data;String4o2enin "cars);S'stem.out.println(total lines >n "i );.close();%catc(,-ception)S'stem.out.println(err"e);

    %%%

    Week :Write a Java program that:i) $mplements stack 34.import java.io.*;class stac2{int stac2[]=ne int[#0];int tos;stac2(){tos=?#;%void pus(int item)

    Balaji Institute of Technolog and Science !*

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    11/32

    OOP Through JAVA Department of Computer Science and Engineering

    {i(tos==@)S'stem.out.println(stac2 is ull);elsestac2[""tos]=item;

    %int pop(){i(tos!0){S'stem.out.println(stac2 underlo);return 0;%elsereturn stac2[tos??];%

    %class teststac2{public static void main(String args[]){stac2 m'stac2#=ne stac2();stac2 m'stac2&=ne stac2();or(int i=0;i!#0;i"")m'stac2#.pus(i);or(int i=#0;i!&0;i"")m'stac2&.pus(i);

    S'stem.out.println(stac2 in m' stac2#:);or(int i=0;i!#0;i"")S'stem.out.println(m'stac2#.pop());S'stem.out.println(stac2 in m' stac2&:);or(int i=0;i!#0;i"")S'stem.out.println(m'stac2&.pop());%%

    ii) 5onverts in#ix expression into *ost#ix #orm import java.io.*;class stac2{car stac2#[]=ne car[&0];int top;void pus(car c){

    Balaji Institute of Technolog and Science !!

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    12/32

    OOP Through JAVA Department of Computer Science and Engineering

    top"";stac2#[top]=c;%car pop(){

    car c;c=stac2#[top];top??;return c;%int pre(car c){sitc(c){case A?A:return #;case A"A:return #;

    case A*A:return &;case ABA:return &;%return 0;%boolean operator(car c){i(c==ABACCc==A*ACCc==A"ACCc==A?A)return true;elsereturn alse;

    %boolean isDlpa(car c){i(cE=AaAFFc!=A

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    13/32

    OOP Through JAVA Department of Computer Science and Engineering

    %else i(isDlpa(c)){output[p""]=c;%

    else i(operator(c)){i(stac2#[top]==0CC(pre(c)Epre (stac2#[top]))CCstac2#[top]==A(A){pus(c);%%else i(pre(c)!=pre(stac2#[top])){output[p""]=pop();

    pus(c);%else i(c==A(A){ile((c=pop())=A(A){output[p""]=c;%%%ile(top=0)

    {output[p""]=pop();%or(int j=0;j!str.lengt();j""){S'stem.out.print(output[j]);%%%class In4o8ost{public static void main(String[] args)tros ,-ception{String s;uered/eader br=neuered/eader(ne InputStream/eader(S'stem.in));stac2 b=ne stac2();S'stem.out.println(,nter input string);s=br.readine();

    Balaji Institute of Technolog and Science !#

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    14/32

    OOP Through JAVA Department of Computer Science and Engineering

    S'stem.out.println(Input String:"s);S'stem.out.println(+utput String:);b.posti-(s);%%

    iii) 6valuates the post#ix expressionimport java.io.*;import java.util.*;class Stac29emo{static int inde-1pos;int 4;loat st2[];

    Stac29emo(){st2=ne loat[#0];4=?#;inde-=0;pos=0;%void pus(loat s){i(4E=#@){

    S'stem.out.println(Stac2 overlo);S'stem.e-it(0);%else{4=4"#;st2[4]=s;%%loat pop(){loat num;i(4==?#){S'stem.out.println(Stac2 is empt');return(0);%else{num=st2[4];4??;

    Balaji Institute of Technolog and Science !$

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    15/32

    OOP Through JAVA Department of Computer Science and Engineering

    %return(num);%loat ,-p,val(car si-[]1loat data[]){

    int j=0;loat op#1op&1s;car c;ile(si-[j]=A>0A){c=si-[j];i(si-[j]E=AaACCsi-[j]E=ADAFFsi-[j]!=A

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    16/32

    OOP Through JAVA Department of Computer Science and Engineering

    int j=0;tr'{uered/eader br=neuered/eader(neInputStream/eader(S'stem.in));

    S'stem.out.println(,nter a posti- e-pression:);str=br.readine();str.get6ars(01str.lengt()1posti-10);ile(posti-[j]=A>0A){i(posti-[j]E=AaACCposti-[j]E=ADAFFposti-[j]!=An /ead:"e);%Stac29emo s#=ne Stac29emo();S'stem.out.println(4e result is "s#.,-p,val(posti-1number));%%Week 7 :a) 4evelop an applet that displa's a simple messageBB!applet code=Dpplet9emo.class eigt=00 idt=00 E !BappletEimport java.applet.*;import java.at.*;public class Dpplet9emo e-tends Dpplet{public void paint(Jrapics g){g.set6olor(6olor.red);g.draString(DK9D/I S/IKILDS IKS4I4M4, + 4,6NK++JO 1P01P0);%%b) 4evelop an applet that receives an integer in one text #ield" andcomputes its #actorial 8alue and returns itin another text #ield" hen the button named 95ompute is clickedBB!applet code=actorialDpplet.class idt=00 eigt=H00 E !BappletEimport java-.sing.*;import java.at.*;

    Balaji Institute of Technolog and Science !&

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    17/32

    OOP Through JAVA Department of Computer Science and Engineering

    import java.at.event.*;public class actorialDpplet e-tends 7Dpplet implements Dctionistener{78anel p#1p&;7abel label#1label&;

    74e-tield input1result;7utton compute;public void init(){6ontainer con=get6ontent8ane();con.seta'out(ne ordera'out());label#=ne 7abel(,nter te number : );label&=ne 7abel(actorial is : );input= ne 74e-tield(H);result= ne 74e-tield(H);compute =ne 7utton(6ompute);

    compute.addDctionistener(tis);p#=ne 78anel();p&=ne 78anel();p#.setac2ground(6olor.pin2);p&.setac2ground(6olor.green);p#.add(label#);p#.add(input);p#.add(label&);p#.add(result);p&.add(compute);con.add(p#1ordera'out.K+/4N);

    con.add(p&1ordera'out.6,K4,/);%public void action8erormed(Dction,vent ae){int act=#;int number=Integer.parseInt(input.get4e-t());i (ae.getSource()==compute){or (int i=#;i!=number;i""){act=act*i;%result.set4e-t("act);%%%Week ; :Write a Java program that orks as a simple calculator ,se a grid la'out toarrange buttons #or the digits

    Balaji Institute of Technolog and Science !'

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    18/32

    OOP Through JAVA Department of Computer Science and Engineering

    and #or the +" %"

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    19/32

    OOP Through JAVA Department of Computer Science and Engineering

    t&.setac2ground(6olor.'ello);t.setac2ground(6olor.'ello);%public void action8erormed(Dction,vent ae){

    tr'{i (ae.getSource()==b#){int -=Integer.parseInt(t#.get4e-t());int '=Integer.parseInt(t&.get4e-t());int sum=-"';t.set4e-t( "sum);%i (ae.getSource()==b&){

    int -=Integer.parseInt(t#.get4e-t());int '=Integer.parseInt(t&.get4e-t());int sub=-?';t.set4e-t( "sub);%i (ae.getSource()==b){int -=Integer.parseInt(t#.get4e-t());int '=Integer.parseInt(t&.get4e-t());int mul=-*';t.set4e-t( "mul);

    %i (ae.getSource()==bQ){int -=Integer.parseInt(t#.get4e-t());int '=Integer.parseInt(t&.get4e-t());int div=-B';t.set4e-t( "div);%%catc (,-ception e){7+ption8ane.soRessage9ialog(null1e);%%%Week > :a) Write a Java program #or handling mouse eventsimport java.at.*;import java.applet.Dpplet;

    Balaji Institute of Technolog and Science !)

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    20/32

    OOP Through JAVA Department of Computer Science and Engineering

    import java.at.event.*;B*!applet code=Rouseeventsidt=&00 eigt=#00E!BappletE*Bpublic class Rouseevents e-tends Dpplet implementsRouseistener1RouseRotionistener

    {String msg=;int -=01'=0;public void init(){addRouseistener(tis);addRouseRotionistener(tis);%public void mouse6lic2ed(Rouse,vent me){-=#0;

    '=&0;msg=mouse clic2ed;repaint();%public void mouse,ntered(Rouse,vent me){-=#0;'=&0;msg=mouse entered;repaint();%

    public void mouse,-ited(Rouse,vent me){-=#0;'=&0;msg=mouse e-ited;repaint();%public void mouse8ressed(Rouse,vent me){-=me.get();'=me.getO();msg=don;repaint();%public void mouse/eleased(Rouse,vent me){-=me.get();'=me.getO();msg=up;

    Balaji Institute of Technolog and Science "*

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    21/32

    OOP Through JAVA Department of Computer Science and Engineering

    repaint();%public void mouse9ragged(Rouse,vent me){-=me.get();

    '=me.getO();msg=*;soStatus(dragging mouse at"-"1"');repaint();%public void mouseRoved(Rouse,vent me){soStatus(moving mouse at"me.get()"1"me.getO());%public void paint(Jrapics g){

    g.draString(msg1-1');%%Week ? :a) Write a Java program that creates three threads (irst thread displa's9@ood Aorning ever' one second"the second thread displa's 9Bello ever' to seconds and the third threaddispla's 9Welcome ever' threesecondscla A /t/%$ T,r/a$!

    2%c,ro%i/$ p"#lic voi$ r"%&+!tr2!-,il/&tr"/+!l//p&17+;'2t/m.o"t.pri%tl%&8(oo$ mor%i%(8+;99catc,&Ec/ptio% /+

    ! 999cla B /t/%$ T,r/a$!2%c,ro%i/$ p"#lic voi$ r"%&+!tr2

    Balaji Institute of Technolog and Science "!

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    22/32

    OOP Through JAVA Department of Computer Science and Engineering

    !-,il/&tr"/+!l//p&

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    23/32

    OOP Through JAVA Department of Computer Science and Engineering

    2%c,ro%i/$ i%t (/t&+!i5&val"/'/t+tr2!

    -ait&+;9catc,&I%t/rr"pt/$Ec/ptio% /+!'2t/m.o"t.pri%tl%&8Ec/ptio% i8=/+;9'2t/m.o"t.pri%tl%&8(ot8=%+;%oti52&+;r/t"r% %;92%c,ro%i/$ voi$ p"t&i%t %+

    !i5&val"/'/t+tr2!-ait&+;9catc,&I%t/rr"pt/$Ec/ptio% /+!'2t/m.o"t.pri%tl%&8% Ec/ptio% i% p"t8=/+;9

    t,i.%%;val"/'/ttr"/;'2t/m.o"t.pri%tl%&8%p"t8=%+;%oti52&+;99cla Pro$"c/r impl/m/%t R"%%a#l/! H;Pro$"c/r& H+!

    t,i.HH;%/- T,r/a$&t,i>8Pro$"c/r8+.tart&+;9p"#lic voi$ r"%&+!i%t i7;-,il/&tr"/+H.p"t&i==+;

    Balaji Institute of Technolog and Science "#

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    24/32

    OOP Through JAVA Department of Computer Science and Engineering

    99cla Co%"m/r impl/m/%t R"%%a#l/! H;

    Co%"m/r& H+!t,i.HH;%/- T,r/a$&t,i>8Co%"m/r8+.tart&+;9p"#lic voi$ r"%&+!-,il/&tr"/+H.(/t&+;99

    cla Pro$Co%D/mo!p"#lic tatic voi$ mai%&'tri%( ar()+! H%/- &+;%/- Pro$"c/r&H+;%/- Co%"m/r&H+;'2t/m.o"t.pri%tl%&8% pr/ ctrl=c to top8+;99Week 10 :

    Write a program that creates a user inter#ace to per#orm integer divisions.he user enters to numbers inthe text#ields" Cum1 and Cum2 .he division o# Cum1 and Cum2 isdispla'ed in the !esult #ield hen the4ivide button is clicked $# Cum1 or Cum2 ere not an integer" the programould thro aCumber(ormat6xception $# Cum2 ere Dero" the program ould thro an3rithmetic6xception 4ispla' theexception in a message dialog boximport java.a-t.*;import java.a-t./v/%t.*;

    import java.appl/t.*;?*:appl/t co$/8Div8-i$t,

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    25/32

    OOP Through JAVA Department of Computer Science and Engineering

    p"#lic voi$ i%it&+!l1%/- La#/l&8N"m#/r 18+;l

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    26/32

    OOP Through JAVA Department of Computer Science and Engineering

    catc,&Ec/ptio% /+!'2t/m.o"t.pri%tl%&8N"m#/r@ormat8=/+;9m(8N"m#/r@ormat8;

    r/pai%t&+;9/l/!i%t %"mF%"m1?%"mF7>7+;99Week 11 :Write a Java program that implements a simple clientEserver application.he client sends data to a server.he server receives the data" uses it to produce a result" and then sendsthe result back to the client .heclient displa's the result on the console (or ex: .he data sent #rom theclient is the radius o# a circle" andthe result produced b' the server is the area o# the circle ,se avanet)

    server.javaimport java.%/t.*;import java.io.*;p"#lic cla /rv/r!p"#lic tatic voi$ mai%&'tri%( ar()+ t,ro- Ec/ptio%!'/rv/r'oc/t %/- '/rv/r'oc/t&ar/a;'tri%( r/"lt;ra$Do"#l/.par/Do"#l/r.r/a$Li%/&++;'2t/m.o"t.pri%tl%&8@rom Cli/%t 8=ra$+;ar/aMat,.PI*ra$*ra$;r/"lt8Ar/a i 8=ar/a;Pri%t'tr/am p%/- Pri%t'tr/am&.(/tO"tp"t'tr/am&++;p.pri%tl%&r/"lt+;

    Balaji Institute of Technolog and Science "&

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    27/32

    OOP Through JAVA Department of Computer Science and Engineering

    #r.clo/&+;p.clo/&+;.clo/&+;.clo/&+;9

    9Client.javaimport java.%/t.*;import java.io.*;p"#lic cla cli/%t!p"#lic tatic voi$ mai%&'tri%( ar()+ t,ro- Ec/ptio%!'oc/t %/- 'oc/t&8local,ot8>

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    28/32

    OOP Through JAVA Department of Computer Science and Engineering

    private 7/adioutton r#1r&1r;private uttonJroup bg;private 6ontainer c;public 4raicigt(){

    seta'out(ne loa'out());c=get6ontent8ane();label=ne 7abel( 4raic igt);displa' =ne 74e-tield(#0);r#=ne 7/adioutton(/,9);r&=ne 7/adioutton(J/,,K);r=ne 7/adioutton(O,+T);bg=ne uttonJroup();c.add(label);c.add(r#);c.add(r&);

    c.add(r);c.add(displa');bg.add(r#);bg.add(r&);bg.add(r);r#.addDctionistener(tis);r&.addDctionistener(tis);r.addDctionistener(tis);setSi

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    29/32

    OOP Through JAVA Department of Computer Science and Engineering

    %public static void main(String args[]){4raicigt ligt=ne 4raicigt();ligt.set9eault6lose+peration(7rame.,I4U+KU6+S,);

    %%b) Write a Java program that allos the user to dra lines" rectangles andovalsBB!applet code=ines/ects+vals.class eigt=&H0 idt=Q00E !BappletEimport java.applet.*;import java.at.*;import java-.sing.*;public class ines/ects+vals e-tends 7Dpplet{public void paint(Jrapics g)

    {super.paint(g);g.set6olor(6olor.red);g.draine(H101H010);g.set6olor(6olor.blue);g.dra/ect(H1Q01@01HH);g.ill/ect(#001Q01@01HH);g.set6olor(6olor.c'an);g.ill/ound/ect(#@H1Q01@01HH1H01H0);g.dra/ound/ect(&@01Q01@01HH1&01&0);g.set6olor(6olor.'ello);

    g.dra9/ect(H1#001@01HH1true);g.ill9/ect(#001#001@01HH1alse);g.set6olor(6olor.magenta);g.dra+val(#@H1#001@01HH);g.ill+val(&@01#001@01HH);%%Week 1 :a) Write a ava program to create an abstract class named -hape thatcontains an empt' method namednumberF#-ides )*rovide three classes named .rape/oid" .riangle andBexagon such that each one o# theclasses extends the class -hape 6ach one o# the classes contains onl' themethod numberF#-ides ) thatshos the number o# sides in the given geometrical #iguresimport java.-i%(.*;a#tract cla ',ap/!p"#lic a#tract voi$ %"m#/rO5'i$/&+;

    Balaji Institute of Technolog and Science ")

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    30/32

    OOP Through JAVA Department of Computer Science and Engineering

    9cla Trap/oi$ /t/%$ ',ap/!p"#lic voi$ %"m#/rO5'i$/&+!

    JOptio%Pa%/.,o-M/a(/Dialo(&%"ll>8TRAPEOID 33 N"m#/r o5 i$/i% trap/oi$ i 4 &O5 -,ic, t-oar/ parall/l a%$ -it, %o a%(l/+8+;99cla Tria%(l/ /t/%$ ',ap/!p"#lic voi$ %"m#/rO5'i$/&+!JOptio%Pa%/.,o-M/a(/Dialo(&%"ll>8TRIANGLE 33 N"m#/r o5 i$/ i%Tria%(l/ i F 8+;

    99cla /a(o% /t/%$ ',ap/!p"#lic voi$ %"m#/rO5'i$/&+!JOptio%Pa%/.,o-M/a(/Dialo(&%"ll>8EAGON 33 N"m#/r o5 i$/ i%/a(o% i Q 8+;99cla ',ap/D/mo

    !p"#lic tatic voi$ mai%&'tri%() ar(+!JOptio%Pa%/.,o-M/a(/Dialo(&%"ll>8'om/ o5 t,/ G/om/trical ("r/ar/ a 5ollo- 8 +;Trap/oi$ t%/- Trap/oi$&+;Tria%(l/ t(%/- Tria%(l/&+;/a(o% ,%/- /a(o%&+;t.%"m#/rO5'i$/&+;t(.%"m#/rO5'i$/&+;,.%"m#/rO5'i$/&+;

    99b) -uppose that a table named .abletxt is stored in a text #ile .he #irst linein the #ile is the header" and theremaining lines correspond to ros in the table .he elements are&eparated b' commas Write a avaprogram to displa' the table using Jtable componentimport java.at.*;

    Balaji Institute of Technolog and Science #*

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    31/32

    OOP Through JAVA Department of Computer Science and Engineering

    import java.at.event.*;import java-.sing.*;import java.util.*;import java.io.*;public class 4able# e-tends 7rame

    {int i=0;int j=012=0;+bject data[][]=ne +bject[H][Q];+bject list[][]=ne +bject[H][Q];7utton save;74able table#;ileInputStream is;9ataInputStream dis;public 4able#(){

    String d= ;6ontainer con=get6ontent8ane();con.seta'out(ne ordera'out());inal String[] colNeads={Kame1/oll Kumber19epartment18ercentage%;tr'{String s=7+ption8ane.soInput9ialog(,nter te ile name present in tecurrent director');ileInputStream is=ne ileInputStream(s);9ataInputStream dis = ne 9ataInputStream(is);ile ((d=dis.readine())=null)

    {String4o2eni

  • 7/25/2019 Object Oriented Programming Labmanual Jntu

    32/32

    OOP Through JAVA Department of Computer Science and Engineering

    int v=Scroll8ane6onstants.L,/4I6DUS6/+D/UDSUK,,9,9;int =Scroll8ane6onstants.N+/IG+K4DUS6/+D/UDSUK,,9,9;7Scroll8ane scroll=ne 7Scroll8ane(table#1v1);con.add(scroll1ordera'out.6,K4,/);%

    public static void main(String args[]){4able# t=ne 4able#();t.setac2ground(6olor.green);t.set4itle(9ispla' 9ata);t.setSi