Skip to main content

Posts

To Perform Integer Divisions

import java.applet.*; import java.awt.event.*; import java.awt.*; /*<applet code="App8.class" height=600 width=600> </applet>*/ public class App8 extends Applet implements ActionListener { TextField t1,t2,t3; Button b; Label l1,l2,l3; int res; public void init() { t1=new TextField(5); t2=new TextField(5); t3=new TextField(15); b=new Button("divide"); l1=new Label("Num1"); l2=new Label("Num2"); l3=new Label("Result"); add(l1); add(t1); add(l2); add(t2); add(b); add(l3); add(t3); b.addActionListener(this); } public void actionPerformed(ActionEvent e) { try { int a=Integer.parseInt(t1.getText()); int b=Integer.parseInt(t2.getText()); res=a/b; t3.setText(""+res); } catch (NumberFormatException ne) { t3.setText("Number format exception"); } catch(ArithmeticException ae) { t3.setText("arithmetic exception"); } } }

Inter Thread Communication

class Q { int n; boolean valueSet=false; synchronized int get() { if(!valueSet) try { wait(); } catch(InterruptedException e) { System.out.println("Interrupted Exception caught"); } System.out.println("Got:"+n); valueSet=false; notify(); return n; } synchronized void put(int n) { if(valueSet) try { wait(); } catch(InterruptedException e) { System.out.println("Interrupted Exception caught"); } this.n=n; valueSet=true; System.out.println("Put:"+n); notify(); } } class Producer implements Runnable { Q q; Producer(Q q) { this.q=q; new Thread(this,"Producer").start(); } public void run() { int i=0; while(true) { q.put(i++); } } } class Consumer implements Runnable { Q q; Consumer(Q q) { this.q=q; new Thread(this,"Consumer").start(); } public void run()...

Creating Threads and Each Thread Displaying a Simple Message

class MyThreada extends Thread { public void run() { while(true) { try { System.out.println("Good Morning"); sleep(1000); } catch (InterruptedException e) { } } } } class MyThreadb extends Thread { public void run() { while(true) { try { System.out.println("Hello"); sleep(2000); } catch (InterruptedException e) { } } } } class MyThreadc extends Thread { public void run() { while(true) { try { System.out.println("Welcome"); sleep(3000); } catch (InterruptedException e) { } } } } class MyThreadMain { public static void main(String[] args) { MyThreada a=new MyThreada(); MyThreadb b=new MyThreadb(); MyThreadc c=new MyThreadc(); a.start(); b.start(); c.start(); } }

Handling Mouse events

import java.applet.*; import java.awt.event.*; import java.awt.*; /*<applet code="Appq2.class" height=600 width=600> </applet>*/ public class Appq2 extends Applet implements MouseListener,KeyListener { String msg=""; int x,y; public void init() { addMouseListener(this); addKeyListener(this); requestFocus(); } public void mouseClicked(MouseEvent me) { msg="mouseClicked"; repaint(); } public void mouseEntered(MouseEvent me) { showStatus("mouse entered"); } public void mouseExited(MouseEvent me) { showStatus("mouse exited"); } public void mousePressed(MouseEvent me) { msg="mouse pressed"; repaint(); } public void mouseReleased(MouseEvent me) { msg="mouse released"; repaint(); } public void keyPressed(KeyEvent ke) { showStatus("key pressed"); } public void keyReleased(KeyEvent ke) { showStatus("key released"); } public void keyTyped(KeyE...

Calculator

import java.awt.*; import java.applet.*; import java.awt.event.*; public class Calculator extends Applet implements ActionListener { TextField t1; String a="",b; String oper="",s="",p=""; int first=0,second=0,result=0; Panel p1; Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9; Button add,sub,mul,div,mod,res,space; public void init() { Panel p2,p3; p1=new Panel(); p2=new Panel(); p3=new Panel(); t1=new TextField(a,20); p1.setLayout(new BorderLayout()); p2.add(t1); b0=new Button("0"); b1=new Button("1"); b2=new Button("2"); b3=new Button("3"); b4=new Button("4"); b5=new Button("5"); b6=new Button("6"); b7=new Button("7"); b8=new Button("8"); b9=new Button("9"); add=new Button("+"); sub=new Button("-"); mul=new Button("*"); div=new Button("/"); mod=new Button("%"); res=new Button("="); space=new Button(...

Factorial Evaluation Using Applets

import java.applet.*; import java.awt.event.*; import java.awt.*; /*<applet code="App2.class" height=600 width=600> </applet>*/ public class App2 extends Applet implements ActionListener { TextField t1,t2; Button b; Label l1,l2; int a,fact; public void init() { l1=new Label("Enter a number",Label.LEFT); l2=new Label("Result",Label.RIGHT); t1=new TextField(5); t2=new TextField(10); b=new Button("Compute"); add(l1); add(t1); add(l2); add(t2); add(b); b.addActionListener(this); } public void actionPerformed(ActionEvent ae) { a=Integer.parseInt(t1.getText()); fact=1; if(a<0) { t2.setText("Wrong input"); } else { for(int i=a;i>1;i--) fact*=i; } t2.setText(""+fact); //repaint(); } }

Applet that Displays a Simple Message

import java.applet.*; import java.awt.*; /*<applet code="App1.class" height=400 width=400> </applet>*/ public class App1 extends Applet { String s=""; public void init() { s=s+"My First Applet Program"; } public void paint(Graphics g) { g.drawString(s,100,100); } }