Skip to main content

Posts

Showing posts with the label java

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

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

Evaluates the Postfix expression

import java.io.*; import java.util.*; class Stac { int st_arr[]=new int[100]; int t=-1; void push(int ele) { if(t==99) { System.out.println("STACK is Full.\n"); return; } st_arr[++t]=ele; } int pop() { if(t==-1) { return -99; } return(st_arr[t--]); } } class Post { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Stac s=new Stac(); System.out.println("Enter an postfix expression"); String s1=br.readLine(); StringTokenizer s2=new StringTokenizer(s1); String a[]=new String[s2.countTokens()]; int j=0; while(s2.hasMoreTokens()) a[j++]=s2.nextToken(); for(int i=0;i<a.length;i++) { if(a[i].equals("+")) { int a1=s.pop(); int a2=s.pop(); s.push(a1+a2); } else if(a[i].equals("-")) { int a1=s.pop(); int a2=s.pop(); s.push(a1-a2); } else if(a[i].equals("*")) { ...

Converts Infix expression into Postfix form

import java.io.*; class Stac { char st_arr[]=new char[100]; int t=-1; char top() { return(st_arr[t]); } void push(char ele) { if(t==99) { System.out.println("STACK is Full.\n"); return; } st_arr[++t]=ele; } char pop() { if(t==-1) { return '#'; } return(st_arr[t--]); } void display() { if(t==-1) System.out.println("Stack is empty\n"); else { System.out.println("\tElements present in the stack are:"); for(int k=0;k<=t;k++) System.out.print("\t"+st_arr[k]); System.out.println(); } } } class In_post { char infix[]=new char[25]; char postfix[]; int prec(char c) { int p=0; switch(c) { case '(': p=0; break; case '+': case '-': p=1; break; case '*': case '/': case '%': p=2; break; default:p=-1; } return p; } void convert(String st) { infix=st.toCharArray(); ...

Implements Stack ADT

import java.io.*; class Stack { int s[]=new int[7]; int t=-1; void push(int x) { if(t==6) { System.out.println("stack is full"); return; } s[++t]=x; } void pop() { if(t==-1) { System.out.println("stack is empty"); } else { System.out.println("element deleted from stack is"+s[t--]); } } void display() { if(t==-1) { System.out.println("stack is empty"); } else { for(int i=0;i<=t;i++) System.out.println(s[i]); } } } class Stack1 { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Stack y=new Stack(); System.out.println("Menu"); System.out.println("1.push"); System.out.println("2.pop"); System.out.println("3.display"); System.out.println("4.exit"); out:while(true) { System.out.println("enter your choice...

Displays The Number Of Characters, Lines and Words in a Text File

import java.io.*; class File2 { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a file name"); String s=br.readLine(); int s1,ch=0,ln=0,wd=0; FileInputStream fin=new FileInputStream(s); do { s1= fin.read(); if(s1 !=-1) { ch++; if((char)s1==' '||(char)s1=='\n') wd++; if((char)s1=='\n') ln++; } } while(s1!= -1); System.out.println("The number of characters is "+ch); System.out.println("The number of words is "+wd); System.out.println("The number of lines is "+ln); fin.close(); } }

Reads a File And Displays The File On The Screen

import java.io.*; class File2 { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a file name"); String s=br.readLine(); int s1,i=1; FileInputStream fin=new FileInputStream(s); System.out.println(i+++” “); do { s1= fin.read(); if(s1 !=-1) System.out.println((char)s1); if((char)s1=='\n') System.out.println(i+++” “); } } while(s1!= -1); fin.close(); } }

Checking File Existance

import java.io.*; class File1 { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a file name"); String s=new String(br.readLine()); File f=new File(s); if(f.exists()) System.out.println("File exists"); if(f.canRead()) System.out.println("File is readable"); if(f.canWrite()) System.out.println("File is writable"); if(f.isFile()) System.out.println("It is a file"); else if(f.isDirectory()) System.out.println("It is a directory"); System.out.println("The length of the file is "+f.length()+" bytes"); } }

Frequency Count Of Words in a Given Text

import java.io.*; import java.util.*; class Fre_count { public static void main(String[] args) throws IOException { BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter a string"); String a=new String(b.readLine()); System.out.println("enter the word you want to find the count"); String c=new String(b.readLine()); StringTokenizer s=new StringTokenizer(a); int i=s.countTokens(); int k=0,j=0; String tok[]=new String[i]; while(s.hasMoreTokens()) { tok[j++]=s.nextToken(); } for(j=0;j<tok.length;j++) { if(c.equals(tok[j])) k++; } System.out.println("frequency count of "+c+" is "+k); } }

Sorting a Given List Of Names in Ascending Order

import java.io.*; class Ascending { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter the number of strings"); int n=Integer.parseInt(br.readLine()); String s[]=new String[n]; for(int i=0;i<n;i++) { System.out.println("enter the string"); s[i]=br.readLine(); } System.out.println("the strings in ascending order are"); for(int i=0;i<n-1;i++) { for(int j=0;j<(n-(i+1));j++) { if(s[j].compareTo(s[j+1])>0) { String s1=new String(); s1=s[j]; s[j]=s[j+1]; s[j+1]=s1; } } } for(int i=0;i<n;i++) System.out.println(s[i]); } }