Sie sind auf Seite 1von 23

1)// program to find the Harmonic series up to a given range import java.io.

DataInputStream; class Hseries { public static void main(String args[]) { int n,sum=0; DataInputStream ins=new DataInputStream(System.in); System.out.println("Enter a range"); try { n=Integer.parseInt(ins.readLine()); } catch(Exception e){} while(n>0) { sum=sum+(1/n); } System.out.println("The Hseries of "+n+" is="+sum); } } 2) // Program to perfrom String operations import java.io.DataInputStream; class StringManip { public void stringsort() { String name[]={"Madras","Delhi","Ahmedabad","Kolkatta","Mumbai"}; String temp=null; int i,j,size=name.length; for(i=0;i<size;i++) { for(j=i+1;j<size;j++) { if(name[j].compareTo(name[i])<0) { temp=name[i]; name[i]=name[j]; name[j]=temp; } }

} System.out.println("the sorted strings are:"); for(i=0;i<size;i++) System.out.println(name[i]); } public void substring() { String s=null,s1=null; DataInputStream ins=new DataInputStream(System.in); try { System.out.println("enter a String:"); s = ins.readLine(); System.out.println("Enter the sub string:"); s1 = ins.readLine(); } catch(Exception e){ } int i = s.indexOf(s1); if (i!= -1) System.out.println("The string contains the substring " + s1); else System.out.println("The string does not contain the substring " + s1); } public void upper() { DataInputStream ins=new DataInputStream(System.in); String s=null; System.out.println("Enter the string"); try { s=ins.readLine(); } catch(Exception e){} System.out.println(""); System.out.println("the given string is"); System.out.println(s); System.out.println(""); System.out.println("the upper case string is"); System.out.println(s.toUpperCase()); } }

class StringOper { public static void main(String ags[]) { DataInputStream ins=new DataInputStream(System.in); int ch=0; StringManip sm=new StringManip(); System.out.println("the string operations are:"); System.out.println("1.to sort the strings"); System.out.println("2.to find the substring"); System.out.println("3.to convert the string into upper case"); System.out.println("Enter ur Choice:"); try { ch=Integer.parseInt(ins.readLine()); } catch(Exception e){} switch(ch) { case 1: sm.stringsort(); break; case 2: sm.substring(); break; case 3: sm.upper(); break; } } } 3)//online shopping import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; import java.lang.*; public class MyForm1 extends Applet implements ActionListener { String str="",str1="",str2=""; Object x[]; Label n,a,i,lbl,b; TextField name; TextArea addr,delvr; List lst; Button b1,b2; Container c;

public void init() { Frame jf=new Frame(); c=jf.getContentPane(); c.setBackground(Color.yellow); c.setLayout(null); jf.setSize(500,400); jf.setTitle("myForm"); jf.setVisible(true); Font f=new Font("Dialog",Font.BOLD,26); lbl=new Label(); lbl.setFont(f); lbl.setForeground(Color.red); lbl.setText("IIyr online shopping"); lbl.setBounds(200,10,500,50); c.add(lbl); n=new Label("Name",JLabel.LEFT); name=new TextField(30); n.setBounds(50,100,100,30); name.setBounds(200,100,200,30); c.add(n); c.add(name); a=new Label("Address:",JLabel.LEFT); b=new Label("purchased list",JLabel.RIGHT); delvr=new TextArea(5,50); addr=new TextArea(5,50); a.setBounds(50,150,100,30); b.setBounds(450,100,200,80); addr.setBounds(200,150,200,100); delvr.setBounds(550,150,230,100); c.add(a); c.add(b); c.add(addr); c.add(delvr); i=new Label("Select items:",JLabel.LEFT); String data[]={"tvs","washing machine","DVD players","refrigerators"}; lst=new List(data); i.setBounds(50,270,100,30); lst.setBounds(200,270,200,100);

c.add(i); c.add(lst); b1=new Button("ok"); b2=new Button("cancel"); b1.setBounds(200,400,100,30); b2.setBounds(350,400,100,30); c.add(b1); c.add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent ae) { str=ae.getActionCommand(); if(str.equals("ok")) { str1=name.getText()+"\n"; str1+=addr.getText()+"\n"; x=lst.getSelectedValues(); for(int i=0;i<x.length;i++) str2+=(String)x[i]+"\n"; delvr.setText(str1+str2); str1=""; str2=""; } else { name.setText(""); addr.setText(""); delvr.setText(""); lst.clearSelection(); } } } <HTML> <BODY> <APPLET CODE ="MyForm1.class" WIDTH="800" HEIGHT="500"> </APPLET> </BODY> </HTML>

4) //find a duplicate value in vector import java.util.*; class DupValue { public static void main(String a[]) { Vector v=new Vector(); v.add("mumbai"); v.add("chennai"); v.add("hyd"); // v.add("mumbai"); // v.add("chennai"); for(int i=0;i<v.size();i++) { for(int j=i+1;j<v.size();j++) { if(v.elementAt(i)==v.elementAt(j)) { System.out.println("The value "+v.elementAt(i)+" is found at "+i+" and "+j+" locations"); } else System.out.println("There are no duplicate values"); } } }} 5) /Create two threads such that one of the thread print even nos and another prints odd nos up to a given range */ import java.io.DataInputStream; class Odd extends Thread { int n; public void run() { for(int i=1;i<n;i=i+2) { System.out.println("ODD:"+i); } }

} class Even extends Thread { int m; public void run() { for(int j=2;j<m;j=j+2) { System.out.println("EVEN:"+j); } } } class EvenOdd { public static void main(String a[]) { DataInputStream ins=new DataInputStream(System.in); Odd o=new Odd(); Even e=new Even(); o.setPriority(8); System.out.println("Enter the range:"); try { o.n=e.m=Integer.parseInt(ins.readLine()); } catch(Exception ex){ } System.out.println("The numbers are:"); o.start(); e.start(); } } /* 6. Define an exception called Marks Out Of Bound Exception, that is thrown if the entered marks are greater than 100. */ import java.io.DataInputStream; import java.lang.Exception; class MarksOutOfBoundException extends Exception { MarksOutOfBoundException(String msg) { super(msg); }

} class Marks { public static void main(String s[]) { DataInputStream ins=new DataInputStream(System.in); int m1=0,m2=0,m3=0,total; double avg; try { System.out.println("Enter 3 subject marks"); m1=Integer.parseInt(ins.readLine()); m2=Integer.parseInt(ins.readLine()); m3=Integer.parseInt(ins.readLine()); } catch(Exception e){ } try { if( m1>100 || m2>100 || m3>100) throw new MarksOutOfBoundException("Marks must be less than 100"); else { total=m1+m2+m3; avg=total/3; System.out.println("total marks="+total); System.out.println("Average ="+avg); } } catch(MarksOutOfBoundException me) { System.out.println(me.getMessage()); } } } 7)//shuffle list import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.io.DataInputStream; public class ShufflingList

{ public static void main(String[] args) { int n=0; DataInputStream in = new DataInputStream(System.in); System.out.print("How many elements you want to add to the list: "); try{ n = Integer.parseInt(in.readLine()); } catch(Exception e){} System.out.print("Enter " + n + " values to sort: "); List list = new ArrayList(); for(int i = 0; i < n; i++){ try{ list.add(in.readLine()); } catch(Exception e){} } Collections.shuffle(list); System.out.println("List shuffled :"+ list); Collections.sort(list); System.out.println("List sorting :"+ list); } } 8) //creating package package Arithmetic; public class Calc { public void add(int a, int b) { System.out.println("the sum is:"+(a+b)); } public void sub(int a, int b) { System.out.println("the difference is:"+(a-b)); } public void multi(int a, int b) { System.out.println("the product is:"+(a*b)); } public void div(int a, int b)

{ if(b!=0) System.out.println("the division is:"+(a/b)); else System.out.println("The division is not possible"); } } import java.io.DataInputStream; import Arithmetic.Calc; class ArithOper { public static void main(String ag[]) { DataInputStream ins=new DataInputStream(System.in); int a=0,b=0; System.out.println("Enter a and b values"); try { a=Integer.parseInt(ins.readLine()); b=Integer.parseInt(ins.readLine()); } catch(Exception e){} Calc c=new Calc(); c.add(a,b); c.sub(a,b); c.multi(a,b); c.div(a,b); } } 9) //calculator import java.io.DataInputStream; import java.applet.* import java.awt.*; import java.awt.event.*; public class Calc extends Applet implements ActionListener { String cmd={"+","-","*","/","=","c"}; int pv=0; String op=" "; Button b[]=new Button[16]; TextField tf=new TextField(10); public void init()

{ setLayout(new BorderLayout()); add(tf,"North"); tf=setText("0"); Panel p=new Panel(); p.setLayout(new GridLayout(4,4)); for(int i=0;i<16;i++) { if(i<10) b[i]=new Button(String.valueOf(i)); else b[i]=new Button(cmd[i%10]); b[i].setFont(new Font("Arial",Font.BOLD,25)); p.add(b[i]); add(p,"center"); b[i].addActionListener(this); } } public void actionPerformed(ActionEvent ae) { int res=0; String cap=ae.getActionCommand(); int cv=Integer.parseInt(tf.getText()); if(cap.equals("c")) { tf.setText("0"); pv=0; cv=0; res=0; op=" "; } end if(cap.equals("=")) { res=0; if(op=="+") res=pv+cv; else if(op=="-") res=pv-cv; else if(op=="*") res=pv*cv; else if(op=="/") res=pv/cv; tf.setText(String.valueOf(res)); }

else if(cap.equals("+") || cap.equals("-"||cap.equals("*") ||cap.equals("/")) { pv=cv; op=cp; tf.setText("0"); } else { int r=cv*10+integer.parseInt(cap); tf.setText(String.valueOf(v)); } } } <HTML> <BODY> <APPLET CODE ="Calc" WIDTH="800" HEIGHT="500"> </APPLET> </BODY> </HTML> 10)//text occurences import java.io.DataInputStream; class Text { public static void main(String a[]) { DataInputStream ins=new DataInputStream(System.in); int i=0,cnt=0; String text="", s=""; try { System.out.println("Enter the string"); s=ins.readLine(); while(s.length()!=0) { text+=s; try{ s=ins.readLine(); } catch(Exception e){} } System.out.println("Enter the search word"); s=ins.readLine(); } catch(Exception e){}

while(true) { i=text.indexOf(s,i); if(i==-1) break; System.out.println("Word found at position:"+i); cnt++; i+=s.length(); } System.out.println("No.of occurences is:"+cnt); } } 11)//events import java.applet.*; import java.awt.event.*; import java.awt.*; public class EventListeners extends Applet implements ActionListener { TextArea txtArea; String Add, Subtract,Multiply,Divide; int i = 10, j = 20, sum =0,Sub=0,Mul = 0,Div = 0; public void init() { txtArea = new TextArea(10,20); txtArea.setEditable(false); add(txtArea,"center"); Button b = new Button("Add"); Button c = new Button("Subtract"); Button d = new Button("Multiply"); Button e = new Button("Divide"); b.addActionListener(this); c.addActionListener(this); d.addActionListener(this); e.addActionListener(this); add(b); add(c); add(d); add(e); }

public void actionPerformed(ActionEvent e) { sum = i + j; txtArea.setText(""); txtArea.append("i = "+ i + "\t" + "j = " + j + "\n"); Button source = (Button)e.getSource(); if(source.getLabel() == "Add") { txtArea.append("Sum : " + sum + "\n"); } if(i >j) { Sub = i - j; } else { Sub = j - i; } if(source.getLabel() == "Subtract") { txtArea.append("Sub : " + Sub + "\n"); } Mul = i*j; if(source.getLabel() == "Multiply") { txtArea.append("Mul = " + Mul + "\n"); } if(i > j) { Div = i / j; } else { Div = j / i; } if(source.getLabel() == "Divide") { txtArea.append("Divide = " + Div); } }

} <HTML> <BODY> <APPLET CODE ="EventListeners" WIDTH="800" HEIGHT="500"> </APPLET> </BODY> </HTML> 12)// program to demonstrate method overriding class Base { public void method(int a,int b) { int c=a+b; System.out.println("The value of c is:"+c); } } class Derived extends Base { public void method(int a,int b) { int c=a-b; System.out.println("The value of c is:"+c); } } class Override { public static void main(String a[]) { Derived d=new Derived(); d.method(24,12); } } 13) /* Write a program to fill elements into a list. Also, copy them in reverse order into another list */ import java.util.*; class List { public static void main(String ag[])

{ Vector v1=new Vector(); String s[]={"a","b","c","d","e"}; for(int i=0;i<s.length;i++) v1.addElement(s[i]); System.out.println("The elements in the First vector are:"); for(int i=0;i<v1.size();i++) System.out.println(v1.elementAt(i)); Vector v2=new Vector(); int n=v1.size(); for(int i=n-1;i>=0;i--) v2.addElement(v1.elementAt(i)); System.out.println("The elements in Second vector are:"); for(int i=0;i<n;i++) System.out.println(v2.elementAt(i)); } } 14)//invalid name exception import java.lang.Exception; import java.io.DataInputStream; class InvalidNameException extends Exception { InvalidNameException(String s) { super(s); } } class Name { public static void main(String ag[]) { DataInputStream ins=new DataInputStream(System.in); String name=" "; char c; int n; try { System.out.println("Enter a name"); name=ins.readLine(); } catch(Exception e){} n=name.length();

for(int i=0;i<n;i++) { c=name.charAt(i); switch(c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': try { throw new InvalidNameException("invalidName"); } catch(InvalidNameException ie) { System.out.println(ie.getMessage()); System.exit(1); } } } System.out.println("The given name is a valid name"); } } 15)//inserting text at specified position import java.applet.*; import java.awt.*; public class PrintText extends Applet { public void init() { setBackground(Color.pink); } public void paint(Graphics g) { String s="WELCOME TO APPLET"; g.drawString(s,30,250); }

} <html> <body> <applet code=PrintText.class width=350 height=450 align=top > </applet> </body> </html> 16)//profit and loss import java.applet.*; import java.awt.*; import java.awt.event.*; public class ProfitLoss extends Applet implements ActionListener { TextField t1,t2; TextArea ta; Button b; Label l1,l2; public void init() { t1=new TextField(8); t2=new TextField(8); ta=new TextArea(); b=new Button("Find"); l1=new Label("cost price"); l2=new Label("selling price"); add(l1); add(t1); add(l2); add(t2); add(b); add(ta); // add(l1); // add(l2); b.addActionListener(this); t1.setText("0"); t2.setText("0"); setBackground(Color.red); } public void actionPerformed(ActionEvent ae)

{ String s1,s2,s; int cp,sp,p,l; try { s1=t1.getText(); s2=t2.getText(); cp=Integer.parseInt(s1); sp=Integer.parseInt(s2); if(cp>sp) { l=(cp-sp)*100/cp; s="Loss%= "+String.valueOf(l); ta.setText(""); ta.append(s); } else { p=(sp-cp)*100/cp; s="profit%= "+String.valueOf(p); ta.setText(""); ta.append(s); } } catch(Exception e) {} } } <html> <body> <applet code=ProfitLoss.class width=300 height=400> </applet> </body> </html> 17)//anonymous array public class AnnonArray { public static void main(String ar[]) { System.out.println("length of array is="+findLength(new int[]{1,2,2,3})); } public static int findLength(int a[])

{ return a.length; } } 18)//font animation import java.awt.*; import java.lang.*; import java.applet.*; import java.awt.event.*; import java.util.*; public class Animation extends Applet implements ActionListener { Button b1=new Button("Change color"); int r=0; int b=0; int g=0; public void init() { add(b1); b1.addActionListener(this); setFont(new Font("Arial",Font.BOLD,30)); } public void actionPerformed(ActionEvent a) { r=(int) (Math.random()*255); g=(int) (Math.random()*255); b=(int) (Math.random()*255); repaint(); } public void paint(Graphics gg) { gg.setColor(new Color(r,g,b)); gg.drawString("VNDC",20,200); } } <HTML> <BODY> <APPLET CODE ="Animation" WIDTH="800" HEIGHT="500"> </APPLET> </BODY> </HTML>

19)wish of day import java.io.*; import java.lang.*; class Wish { public static void main(String a[]) { int hour=0; DataInputStream ins=new DataInputStream(System.in); System.out.println("ENTER THE TIME hours IN 24 HRS patern:"); try { hour=Integer.parseInt(ins.readLine()); } catch(Exception e){} if(hour<=23 && hour>=19) System.out.println("Hai GOOD NIGHT"); else if(hour<19 && hour>=16) System.out.println("Hai GOOD EVENING"); else if(hour<16 && hour>=12) System.out.println("Hai GOOD AFTERNOON"); else if(hour<12 && hour>=5) System.out.println("Hai GOOD MORNING"); else if(hour<5 && hour>=0) System.out.println("DON'T DISTURB"); else { System.out.println("Enter correct time & Try again"); } } }

20)//library import java.io.*; class Library { public static void main(String arg[]) { int i=0,x=0,m; String con=""; int a[]=new int[10]; String name[]=new String[10]; String s[]={"java","VB","ORACLE","C"}; DataInputStream ins=new DataInputStream(System.in); do { System.out.println("Select the book"); System.out.println("1. java"); System.out.println("2. vb"); System.out.println("3.oracle"); System.out.println("4. c"); System.out.println("Select the number"); try{ x=Integer.parseInt(ins.readLine()); } catch(Exception e){} if(x<1 || x>4) continue; a[i]=--x; System.out.println("Enter ur name"); try { name[i]=ins.readLine(); } catch(Exception e){} i++; System.out.println("Continue y/n"); try { con=ins.readLine(); } catch(Exception e){} }while(con.equals("y")); m=i; System.out.println("Books borrowed are:"); for(i=0;i<m;i++) System.out.println(name[i]+"\t"+s[a[i]]); } }

Das könnte Ihnen auch gefallen