You are on page 1of 17

AREA AND PERIMETER OF A CIRCLE import java.util.Scanner; class Cir { double pi=3.14,ar,pr; void area(int r) { ar=pi*r*r; System.out.

println("AREA OF THE CIRCLE: "+ar); } void peri(int r) { pr=2*pi*r; System.out.println("PERIMETER OF THE CIRCLE: "+pr); } } class prac { public static void main(String args[]) { int r; Cir c = new Cir(); Scanner s = new Scanner(System.in); System.out.println("OUTPUT"); System.out.println("---------------------------------------"); System.out.println("AREA AND PERIMETER OF THE CIRCLE"); System.out.println("________________________________________"); System.out.println("Enter the radius value"); r=s.nextInt(); c.area(r); c.peri(r); System.out.println("---------------------------------------"); } } OUTPUT --------------------------------------AREA AND PERIMETER OF THE CIRCLE ________________________________________ Enter the radius value 8 AREA OF THE CIRCLE: 200.96 PERIMETER OF THE CIRCLE: 50.24

GENRATING RANDOM NUMBERS import java.io.*; import java.util.Random; class Rand { public static void main(String args[])throws IOException { int a; System.out.println("\n Random numbers"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); a=Integer.parseInt(br.readLine()); Random r=new Random(); r.setSeed(a); for(int i=0;i<5;i++) { System.out.println(""+r.nextInt()+"\t"+r.nextDouble()+"\t"+r.nextGaussian()); } } } OUTPUT Random numbers ************************************** -1154715079 -624140595 -60658084 1260042744 -423279216 142959438 17850135 2133836778 -613647601 -330177159

-2.789065654 -0.457884544 0.63836836 -0.63836836 -0.356345355

STRING MANIPULATION SUBSTRIN REMOVAL, STRING REPLACEMENT import java.io.*; import java.lang.String.*; import java.lang.StringBuffer; class St { public static void main(String args[ ])throws IOException { String x; System.out.println("\n Enter the String"); BufferedReader n=new BufferedReader(new InputStreamReader(System.in)); x=n.readLine(); StringBuffer s=new StringBuffer(); System.out.println(x.toLowerCase()); System.out.println(x.toUpperCase()); System.out.println(x.length()); System.out.println(x.substring(0,4)); System.out.println(s.delete(0,4)); } } OUTPUT Enter the String Students Students STUDENTS 8 Stud

DRAWING RECTANGLES,OVALS USING APPLETS import java.awt.*; import java.applet.*; /*<applet code= "Home" width=400 height=500> </applet> */ public class Home extends Applet { public void init( ) { setBackground(Color.black); } public void paint(Graphics g) { Color c1=new Color(30,20,40); Color c2=new Color(10,150,75); g.setColor(c1); g.drawLine(100,100,200,100); g.drawLine(100,100,50,150); g.drawLine(200,100,250,150); g.drawLine(145,145,145,215); g.setColor(c2); g.drawRect(55,145,190,70); g.drawRect(80,170,35,45); g.setColor(c1); g.fillOval(10,20,40,40); } }

USINTG THREAD AND EXCEPTION HANDLING class Child1 extends Thread { public void run() { for(int i=1;i<=8;i++) System.out.println("Child1"); }} class Child2 extends Thread { public void run() { try { for(int i=1;i<=6;i++) { System.out.println("Child2"); Thread.sleep(50); } } catch(InterruptedException e) { } } } class Child3 extends Thread { public void run() { for(int i=1;i<=6;i++) System.out.println("Child3"); } } class Methods { public static void main(String args[ ]) { Child1 c1=new Child1(); Child2 c2=new Child2(); Child3 c3=new Child3(); c1.start(); c1.suspend(); c2.start(); c3.start(); c1.resume(); } }

OUTPUT Child1 Child1 Child1 Child2 Child3 Child3 Child3

APPLICATION USING SYNCHRONIZATION import java.io.*; class Callme { void call(String msg) { System.out.print("["+msg); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Interrupted"); } System.out.println("]"); } } class Caller implements Runnable { String msg; Callme target; Thread t; public Caller(Callme targ,String s) { target=targ; msg=s; t=new Thread(this); t.start(); } public void run() { synchronized(target) { target.call(msg); } } } class Sych { public static void main(String args[]) throws IOException { Callme target=new Callme(); Caller ob1=new Caller(target,"Hello"); Caller ob2=new Caller(target,"Synchronized"); Caller ob3=new Caller(target,"World");

try { ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch(InterruptedException e) { System.out.println("Interrupted"); } } } OUTPUT [Hello] [Syncthronized] [world]

GUI BASED APPLICATION USING SWING COMPONENTS import java.io.*; import java.applet.*; import java.awt.event.*; /* <applet code = "pay" width = 400 height = 500> </applet> */ public class pay extends Applet implements ActionListner,ItemListener { String msg = " "" , msg1 = " "; TextField eno,name,age,address; Button dis; CheckBox ma,fe; CheckBoxGroup cbg; public void init() { Label l1 = Label("ENO",Label.LEFT); Label l2 = Label("ENAME",Label.RIGHT); Label l3 = Label("SEX",Label.LEFT); Label l4 = Label("AGE",Label.LEFT); Label l5 = Label("ADDRESS",Label.LEFT); eno = new TextField(7); name = new TextField(10); age = new TextField(); address = new TextField(50); dis = new Button("DISPLAY"); cbg = new CheckBoxGroup(); ma = new CheckBox("MALE",cbg,false); fe = new CheckBox("FEMALE",cbg,true); add(l1); add(eno); add(l2); add(name); eno.addActionListner(this); name.addActionListner(this); add(l3); add(ma); add(fe); ma.addActionListner(this); fe.addActionListner(this); add(l4); add(age); add(l5); add(address) age.addActionListner(this); address.addActionListner(this);

} publc void actionPerformed(ActionEvent ae) { String str = ae.getActionCommand(); } public void itemStateChanged(ItemEvent ie) { } protected void proccessActionEvent(ActionEvent ae) { repaint(); } public void paint(Graphicd g); { g.drawString("Eno:"+eno.getText(),120,100); g.drawString("name:"+name.getTextField(),120,120); msg = "sex"; msg+=cbg.getSelectedCheckBox().getLabel(); g.drawString("MSG",120,140); g.drawString("AGE:"+age.getTextField(),120,160); g.drawString("ADDRESS"+address.getTextField(),120,180); } }

IMPLEMENTING GUI BASED APPLICATION USING LAYOUT MANAGERS import java.awt.*; import java.applet.*; /* <applet code="Llay" width=300 height=200> </applet> */ public class Llay extends Applet { int n=4; public void init() { setLayout(new GridLayout(n,n)); setFont(new Font("sanscrif",Font.BOLDS,24)); for(int i=0;i<=n;i++) { for(int j=0;j<n;j++) { int k=i*n+1; if(k>0) add(new Button(""+k)); } } } }

MENUS // WORKING WITH MENUS /*applet code="apmm" width=300 height=300> </applet>*/ import java.io.*; import java.awt.*; import java.applet.*; import java.awt.event.*; class Ven extends Frame { String msg=" "; ven(String title) { super(title); MenuBar mbar=new MenuBar(); setMenuBar(mbar); Menu file=new Menu("File"); MenuItem it1,it2; file.add(it1=new MenuItem("OPEN"); file.add(it2=new MenuItem("EXIT"); mbar.add(file); Menu about=new Menu("about"); MenuItem it3; about.add(it3=new MenuItem("HELP"); mbar.add(about); mywind adap=new mywind(this); addWindowListener(adap); } } class Mywind extends WindowAdapter { ven m; public mywind(ven m) { this.m=m; } public void WindowClosing(WindowEvent we) { m.dispose(); } } public class apmn extends Applet { Frame f; public void init() {

f=new ven("MENU DEMO"); f.setSize(200,200); f.setVisible(true); } public void start() { f.setVisible(true); } public void stop() { f.setVisible(false); } }

APPLICATION USING FILESTREAMS(SEQUENTIAL FILE) import java.io.*; import java.util.*; class InputStreamEnumerator implements Enumeration { private Enumeration files; public InputStreamEnumerator(Vector files) { this.files=files.elements(); } public boolean hasMoreElements() { return files.hasMoreElements(); } public Object nextElement() { try { return new FileInputStream(files.nextElement().toString()); } catch(Exception e) { return null; } } } class SequenceInputStreamDemo { public static void main(String args[])throws Exception { int c; Vector files=new Vector(); files.addElement("sss.java"); files.addElement("Lamp.java"); InputStreamEnumerator e=new InputStreamEnumerator(files); InputStream input=new SequenceInputStream(e); while((c=input.read())!=-1) { System.out.print((char)c ); } input.close(); } }

OUTPUT Class java { public static void main(String arg[]) System.out.println("WELCOME TO CS"); } Class Lamp { public static void main(String arg[]) System.out.println("Sequence files");

APPLICATIN USING FILE STREAM (RANDOM FILE) import java.io.*; class RandomIO { public static void main(String args[]) { RandomAccessFile file=null; try { file=new RandomAccessFile("rand.dat","rw"); file.WriteChar('X'); file.WriteInt(555); file.WriteDouble(3.1412); file.seek(0); System.out.println(file.readChar()); System.out.println(file.readInt()); System.out.println(file.readDouble()); file.seek(2); System.out.println(file.readInt()); file.seek(file.length()); file.WriteBoolean(false); file.seek(4); System.out.println(file.readBoolean()); file.close(); } catch(IOException e) { System.out.println(e); } } }

OUTPUT X 555 3.1412 555 true

You might also like