Skip to main content

Posts

create an Abstract Class Named Shape That Contains an empty method named NumberOfSides ( )

abstract class SHape { abstract void noofsize(); } class Triangle extends SHape { void noofsize() { System.out.println("The size of triangle is 3"); } } class Trapezoid extends SHape { void noofsize() { System.out.println("The size of trapezoid is 4"); } } class Hexagon extends SHape { void noofsize() { System.out.println("The size of hexagon is 6"); } } class Sha1 { public static void main(String[] args) { Triangle t=new Triangle(); Trapezoid u=new Trapezoid(); Hexagon v=new Hexagon(); t.noofsize(); u.noofsize(); v.noofsize(); } }

Allowing the User To Draw Lines, Rectangles and Ovals

import java.applet.*; import java.awt.event.*; import java.awt.*; /*<applet code="Appq12.class" height=310 width=400> </applet>*/ public class Appq12 extends Applet implements ActionListener { Button b[]=new Button[10]; int in; public void init() { b[0]=new Button("Line"); b[1]=new Button("Rectangle"); b[2]=new Button("FilledRectangle"); b[3]=new Button("RoundedRectangle"); b[4]=new Button("FilledRoundedRectangle"); b[5]=new Button("Oval"); b[6]=new Button("FilledOval"); b[7]=new Button("Arc"); b[8]=new Button("FilledArc"); b[9]=new Button("Polygon"); for(int i=0;i<10;i++) { add(b[i]); b[i].addActionListener(this); } } public void actionPerformed(ActionEvent ae) { for(int j=0;j<10;j++) { if(ae.getSource()==b[j]) { in=j+1; break; } } repaint(); } public void paint(Graphics g) { if(in==1) g....

Simulates a Traffic Light

import java.awt.*; import java.applet.*; import java.awt.event.*; /* <applet code="traffic.class" width=500 height=500> </applet> */ public class traffic extends Applet implements ActionListener { int i=0; Button R,O,G; public void init() { setBackground(Color.white); setForeground(Color.black); R=new Button("red"); O=new Button("orange"); G=new Button("green"); add(R); add(O); add(G); R.addActionListener(this); O.addActionListener(this); G.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String s=ae.getActionCommand(); if(s.equals("red")) i=1; if(s.equals("orange")) i=2; if(s.equals("green")) i=3; repaint(); } public void paint(Graphics g) { g.setColor(Color.yellow); g.drawRect(50,50,100,200); g.fillRect(50,50,100,200); g.setColor(Color.black); g.drawOval(80,70,30,30); g.drawOval(80,130,30,30); g.drawOval(80,190,30,3...

Implementing a Simple Client/Server Application

import java.net.*; import java.io.*; public class server { public static void main(String args[]) throws Exception { ServerSocket ss=new ServerSocket(2000); Socket s=ss.accept(); BufferedReader br=new BufferedReader(newInputStreamReader(s.getInputStream())); double rad,area; String result; rad=Double.parseDouble(br.readLine()); System.out.println("From Client : "+rad); area=Math.PI*rad*rad; result="Area is "+area; PrintStream ps=new PrintStream(s.getOutputStream()); ps.println(result); br.close(); ps.close(); s.close(); ss.close(); } } public class client { public static void main(String args[]) throws Exception { Socket s=new Socket("192.168.0.19",2000); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String rad; System.out.println("Enter radius of the circle "); rad=br.readLine(); PrintStream ps=new PrintStream(s.getOutputStream()); ps.println(rad); BufferedReader fs=n...

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(); } }