24
According to my knowledge all d programs are executing friends ~unzip the “java programs” ZIPfile ~You will find two folders . ~copy all the files from folder “copy all files to bin folder” .. nd paste into java\bin that is Where you have installed yor java ~ steps to run applet program(for example to run animation program) Javac ball.java Edit ball.html (optional if u want to edit) Javac ball.html ~to see applet code right click on html file nd open with notepad (ex code) <applet code=key width=300 height=400></applet> ENJOY!!!!!!!!!!!!!! 1> Write a program to find factorial of list of number reading input as command line arguments. class fact { public static void main(String a[]) { int i,j; int m=Integer.parseInt(a[0]); int n=Integer.parseInt(a[1]); System.out.println("=========================="); System.out.println("factorial series between "+m+ " & " +n); System.out.println("==========================="); for(i=m;i<n;i++) { int fact=1; for(j=1;j<=i;j++) fact=fact*j; System.out.print("factorial of " +i+"="+fact+"\n");

Java Programmes

Embed Size (px)

Citation preview

Page 1: Java Programmes

According to my knowledge all d programs are executing friends

~unzip the “java programs” ZIPfile

~You will find two folders .

~copy all the files from folder “copy all files to bin folder” .. nd paste into java\bin that is Where you have installed yor java

~ steps to run applet program(for example to run animation program) Javac ball.java Edit ball.html (optional if u want to edit) Javac ball.html

~to see applet code right click on html file nd open with notepad (ex code)

<applet code=key width=300 height=400></applet>

ENJOY!!!!!!!!!!!!!!1> Write a program to find factorial of list of number reading input as command line arguments.

class fact{ public static void main(String a[]) { int i,j; int m=Integer.parseInt(a[0]); int n=Integer.parseInt(a[1]); System.out.println("=========================="); System.out.println("factorial series between "+m+ " & " +n); System.out.println("==========================="); for(i=m;i<n;i++) { int fact=1; for(j=1;j<=i;j++) fact=fact*j; System.out.print("factorial of " +i+"="+fact+"\n"); } }}

OUT PUT

Page 2: Java Programmes

2> Write a program to find prime series reading N as command line argument

class primeseries{ public static void main(String a[]) { int i,j; String m=a[0]; int n=Integer.parseInt(m); System.out.println("==========================="); System.out.println("prime series between 1 and "+n); System.out.println("========================="); for(i=1;i<=n;i++) { int counter=0; for(j=2;j<i;j++) { if(i%j==0) { counter=1; break; } }

Page 3: Java Programmes

if(counter==0) System.out.println(" "+i); } System.out.println("==========================="); }}}

3> Write a program to sort list of elements in ascending and decending order and show the exception handling.import java.io.*;class sort{ public static void main(String a[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter n values"); int n=Integer.parseInt(br.readLine()); int x[]=new int[n],i,j,temp; String s[]=new String[10]; try { System.out.println("enter the elements"); for(i=0;i<n;i++) { s[i]=br.readLine(); x[i]=Integer.parseInt(s[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++)

Page 4: Java Programmes

{ if(x[i]>x[j]) { temp=x[i]; x[i]=x[j]; x[j]=temp; } } } System.out.println("ascending order"); for(i=0;i<n;i++) System.out.println(x[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(x[i]<x[j]) { temp=x[i]; x[i]=x[j]; x[j]=temp; } } } System.out.println("descending order"); for(i=0;i<n;i++) System.out.println(x[i]); } catch(NumberFormatException e) { System.out.println("enter only integer elements"); } }}OUTPUT

Page 5: Java Programmes

4> Write a program to implement rhombus pattern reading the limit from user.import java.io.*;class rhombus{ public static void main(String a[])throws IOException { int i,j,n,k; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter the input number"); n=Integer.parseInt(br.readLine()); for(i=0;i<=n;i++) { for(j=0;j<n-i;j++) System.out.print(" "); for(k=0;k<i;k++) System.out.print(" @"); System.out.print("\n"); } for(i=0;i<n-1;i++) { for(j=0;j<=i;j++) System.out.print(" "); for(k=n-1-i;k>0;k--) System.out.print(" @"); System.out.print("\n"); } }}

OUTPUT

Page 6: Java Programmes

5> Write a program to implement all string operation.

class string{ public static void main(String a[]) { String s1,s2; s1="Manjunath"; s2="Vijay"; System.out.println("======================================"); System.out.println("given strings are = "+s1+" and "+s2); System.out.println("======================================"); System.out.println(); System.out.println("string length"); System.out.println("**************************************"); int len=s1.length(); System.out.println("length of string s1 is "+len); System.out.println(); System.out.println("string concatination"); System.out.println("-----------------------"); System.out.println(s1+" is stronger than "+s2); System.out.println(); System.out.println("Char extraction"); System.out.println("-----------------------"); System.out.println("first char of "+s1+" is "+s1.charAt(4)); System.out.println(); System.out.println("comparison of s1 and s2"); System.out.println("-----------------------");

Page 7: Java Programmes

System.out.println(s1.equals(s2)); System.out.println(); System.out.println("searchng"); System.out.println("-----------------------"); System.out.println("t occurs at position "+s1.indexOf("t")+" in "+s1); System.out.println(); System.out.println("substring of s1"); System.out.println("-----------------------"); System.out.println(s1.substring(0,5)); System.out.println(); System.out.println("replacing 'M' by 'H' in s1"); System.out.println("-----------------------"); System.out.println(s1.replace('M','H')); System.out.println(); System.out.println("changing s1 to Uppercase"); System.out.println(s1.toUpperCase()); System.out.println(); }}OUTPUT

6> Write a program to find a area of geometrical figures using methods.class are{ void circle(float pi,int r) { float ar=pi*r*r; System.out.println("Area of circle= "+ar+"sq.units\n"); } void triangle(float base,float ht) { double ar=0.5*(base*ht);

Page 8: Java Programmes

System.out.println("area of tringle= "+ar+"sq.units\n"); } void square(float side) { double ar=side*side; System.out.println("area of square= "+ar+"sq.units\n"); }}class fig{ public static void main(String b[]) { are a = new are(); System.out.println("\n\n Area of Geometrical Figures \n"); System.out.println("===================================="); a.circle(3.14f,3); a.square(1.5f); a.triangle(4.2f,1.2f); System.out.println("===================================="); }}OUTPUT

7> Write a program to implement constructor overloading by passing different number of parameter of different types.class tr{ int x,y; float z; tr(int i) {

Page 9: Java Programmes

x=y=i; z=i; } tr(int i,int j) { x=i; y=i; z=j; } tr(int i,int j,float k) { x=i; y=j; z=k; } float peri() { return(x+y+z); }}class tri{ public static void main(String args[]) { float val; tr m=new tr(5); tr m1=new tr(5,3); tr m2=new tr(2,3,22.5f); val=m.peri(); System.out.println("peri of equilateral triangle="+val+"sq.units\n"); val=m1.peri(); System.out.println("peri of isoceles triangle="+val+"sq.units\n"); val=m2.peri(); System.out.println("peri of scalene triangle="+val+"sq.units"); }}OUTPUT

Page 10: Java Programmes

8> Write a program to create a student report by reading the input using textboxes and display the output using buttons.import java.applet.*;import java.awt.*;import java.awt.event.*;public class stud extends Applet implements ActionListener{ TextField trno,tname,tcourse,tres; Label ltitle,lrno,lname,lcourse,lres; Button creport; String rno="",course="",nam="",res=""; public void init() { ltitle=new Label("enter students Details"); lrno=new Label("register no ",Label.RIGHT); lname=new Label("student name ",Label.RIGHT); lcourse=new Label("course ",Label.RIGHT); lres=new Label("result ",Label.RIGHT); trno=new TextField(20); tname=new TextField(20); tcourse=new TextField(20); tres=new TextField(20); creport=new Button("report"); add(ltitle); add(lrno); add(trno); add(lname); add(tname); add(lcourse);

Page 11: Java Programmes

add(tcourse); add(lres); add(tres); add(creport); add(ltitle); add(ltitle); trno.addActionListener(this); tname.addActionListener(this); tcourse.addActionListener(this); tres.addActionListener(this); creport.addActionListener(this); } public void actionPerformed(ActionEvent a) { if(a.getSource()==creport) { rno=trno.getText(); course=tcourse.getText(); nam=tname.getText(); res=tres.getText(); rno="register number :"+rno; nam="name :"+nam; course="course :"+course; res="result :"+res; repaint(); } } public void paint(Graphics g) { g.drawString(rno,30,180); g.drawString(nam,30,200); g.drawString(course,30,220); g.drawString(res,30,240); }}

OUTPUT

Page 12: Java Programmes

9> Write a program to calculate bonus for different departments using method overriding.class lilly{ float basic,da,it; lilly(float b,float d,float i) { basic=b; da=d;

Page 13: Java Programmes

it=i; } double bonus() { System.out.print("reasearch dept:"); return(basic * 0.2); }}class Subclass extends lilly{ float hra,pt; Subclass(float b,float d,float i,float p,float h) { super(b,d,i); hra=h; pt=p; } double bonus() { System.out.print("administration dept:"); return(basic * 0.4); }}class bonu{ public static void main(String a[]) { Subclass Sc=new Subclass(2000,12000,6000,1000,500); System.out.println("bonus is rs."+Sc.bonus()+"\n"); }}OUTPUT

Page 14: Java Programmes

10> Write a program to implement thread prioritiesclass ca implements Runnable{ int cl=0; Thread t; private volatile boolean running=true; public ca(int p) { t=new Thread(this); t.setPriority(p); } public void run() { while(running) { cl++; } } public void stop() { running=false; } public void start() { t.start(); }}class thread{ public static void main(String a[])

Page 15: Java Programmes

{ Thread.currentThread().setPriority(Thread.MAX_PRIORITY); ca h=new ca(Thread.NORM_PRIORITY+2); ca l=new ca(Thread.NORM_PRIORITY-2); l.start(); h.start(); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("error"); } l.stop(); h.stop(); try { h.t.join(); l.t.join(); } catch(InterruptedException e) { System.out.println("error"); } System.out.println("Low priordr "+l.cl); System.out.println("high priordr "+h.cl); }}

OUTPUT

Page 16: Java Programmes

11> Write a program to implement thread , applet and graphics by implementing animation of ball moving.import java.applet.*;import java.awt.*;public class ball extends Applet implements Runnable{ int xp=30; int yp=100; int xs=1; int rad=20; int appx=300; int appy=300; Thread th=null; boolean flag; private Image i; private Graphics gr; public void init() { setBackground(Color.black); } public void start() { Thread th=new Thread(this); flag=false; th.start(); } public void stop() { flag=true;

Page 17: Java Programmes

th=null; } public void destroy() { } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while(true) { if(xp>appx-rad) { xs=-1; } else if(xp<rad) { xs=+1; } xp+=xs; repaint(); try { Thread.sleep(20); if(flag) break; } catch(InterruptedException e) { } } } public void update(Graphics g) { if(i==null) { i=createImage(this.getSize().width,this.getSize().height); gr=i.getGraphics(); } gr.setColor(getBackground()); gr.fillRect(0,0,this.getSize().width,this.getSize().height); gr.setColor(getForeground()); paint(gr); g.drawImage(i,0,0,this); } public void paint(Graphics g) {

Page 18: Java Programmes

g.setColor(Color.white); g.fillOval(xp-rad,yp-rad,2*rad,2*rad); }}OUTPUT

12> Write a program to implement mouse events .import java.awt.*;import java.awt.event.*;import java.applet.*;public class mouse extends Applet implements MouseListener,MouseMotionListener{ String s; int mx=0,my=0; public void init()

Page 19: Java Programmes

{ addMouseListener(this); addMouseMotionListener(this); } public void mouseClicked(MouseEvent m) { mx=0; my=10; s="mouse clicked"; repaint(); } public void mouseEntered(MouseEvent m) { mx=0; my=10; s="mouse entered"; repaint(); } public void mouseExited(MouseEvent m) { mx=0; my=10; s="mouse clicked"; repaint(); } public void mousePressed(MouseEvent m) { mx=m.getX(); my=m.getY(); s="mouse pressed"; repaint(); } public void mouseReleased(MouseEvent m) { mx=m.getX(); my=m.getY(); s="up"; repaint(); } public void mouseDragged(MouseEvent m) { mx=m.getX(); my=m.getY(); s="*"; showStatus("dragging mouse at "+mx+" , "+my); repaint();

Page 20: Java Programmes

} public void mouseMoved(MouseEvent m) { showStatus("moving mouse at "+m.getX()+" , "+m.getY()); } public void paint(Graphics g) { g.drawString(s,mx,my); }}OUTPUT

13> Write a program to implement keyboard events.import java.awt.*;import java.awt.event.*;import java.applet.*;public class key extends Applet implements KeyListener{

Page 21: Java Programmes

String s; int mx=10,my=20; public void init() { addKeyListener(this); requestFocus(); } public void keyPressed(KeyEvent k) { showStatus("key down"); int key=k.getKeyCode(); switch(key) { case KeyEvent.VK_F1: s+="<f1>"; break; case KeyEvent.VK_F2: s+="<f2>"; break; case KeyEvent.VK_F3: s+="<f3>"; break; case KeyEvent.VK_PAGE_UP: s+="<PGUP>"; break; case KeyEvent.VK_PAGE_DOWN: s+="<PGDN>"; break; case KeyEvent.VK_RIGHT: s+="<ARROW RIGHT>"; break; case KeyEvent.VK_LEFT: s+="<ARROW LEFT>"; break; } repaint(); } public void keyReleased(KeyEvent k) { showStatus("key up"); } public void keyTyped(KeyEvent k) { s+=k.getKeyChar(); repaint(); }

Page 22: Java Programmes

public void paint(Graphics g) { g.drawString(s,mx,my); }}OUTPUT