Vous êtes sur la page 1sur 82

ABSTRACT CLASS

EX:NO: 1.a DATE :

A SIMPLE DEMONSTRATION OF ABSTRACT


AIM:

ALGORITHM:

PROGRAM:
abstract class interest { abstract void sol(); } class s1 extends interest { int p=25000; int n=5; int r=5; double s; void sol() { s=(p*n*r)/100; System.out.println("SIMPLE INTEREST " +s); } } class c1 extends interest { int p=25000; int r=5; int n=5; double c;

void sol() { c=(p*(math.Pow(1+(r/100),n)); System.out.println("COMPOUND INTEREST " +c); } } class abs { public static void main(String args[]) { interest ob=new s1(); ob.sol(); interest oc=new c1(); oc.sol(); } }

OUTPUT:

RESULT:

EX:NO: 1.b DATE :

USING ABSTRACT METHODS AND CLASSES


AIM:

ALGORITHM:

PROGRAM:
import java.io.*; import java.lang.*; abstract class emp { abstract void salary(); } class salemp extends emp { void salary() { System.out.println("Buy the salary of rs."+5000); } } class houremp extends emp { int n,sal; void salary() { sal=n*200; System.out.println("Buy the salary of rs."+sal); } }

class com extends emp { int ps; void salary() { System.out.println("Buy the salary for % of sale as rs."+(ps*0.1*5000)); } } class comm extends emp { double p,sal=5000; void salary() { sal=sal+(p*0.1*5000); System.out.println("Buy salary of rs."+sal); } } class abstractsalarycalc { public static void main(String args[])throws Exception { int ch; String name;

salemp s=new salemp(); houremp h=new houremp(); com c=new com(); comm cm=new comm(); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("\n\n***DETAILS OF EMPLOYEE'S SALARY***\n\n1.Weekely salary\n2.Hourly Salary\n3.Commisioned Salary\n4.Salarycomm employee\n\nEnter ur choice"); ch=Integer.parseInt(br.readLine()); System.out.println("Enter Name of the employee"); name=br.readLine(); System.out.println("Name of the employee is :"+name); switch(ch) { case 1: s.salary(); break; case 2: System.out.println("No of hours worked"); h.n=Integer.parseInt(br.readLine()); h.salary();

break; case 3: System.out.println("Enter the Amount of percentage of sale"); c.ps=Integer.parseInt(br.readLine()); c.salary(); break; case 4: System.out.println("Enter the Amount of percentage of sale"); cm.p=Integer.parseInt(br.readLine()); cm.salary(); break; } } catch(Exception e1) { } } }

OUTPUT:

RESULT:

INHERITANCE EX:NO:2.a DATE: SINGLE INHERITANCE AIM:

ALGORITHM:

PROGRAM:
import java.io.*; import java.lang.*; class mean { int n=10,i,a,s=0; static int m; int x[]={21,22,23,24,25,26,27,28,29,30}; void calmean() { for(i=0;i<n;i++) { s=s+x[i]; } m=s/n; System.out.println("Mean="+m); } } class stand extends mean { int sd; void sn() {

for(i=0;i<n;i++) { sd=sd+(x[i]-m); } System.out.println("Standard deviation="+Math.sqrt((sd*sd)/n)); } } class inheritancesd { public static void main(String a[]) { mean m=new mean(); stand s=new stand(); m.calmean(); s.sn(); } }

OUTPUT:

RESULT:

EX:NO:2.b DATE: MULTILEVEL INHERITANCE AIM:

ALGORITHM:

PROGRAM:
import java.io.*; class paint { int cost; void show() { System.out.println("Cost of painting is Rs. "+cost); } } class house extends paint { int l,b,sq; void sqft() { sq=l*b; } } class distemper extends house { int cpsq=200; void calc() {

super.cost=sq*cpsq; } } class emulsion extends house { int cpsq=400; void calc() { super.cost=sq*cpsq; } } class commercial extends paint { int l,b,sq; void sqft() { sq=l*b; } } class mat extends commercial { int cpsq=500; void calc()

{ super.cost=sq*cpsq; } } class color { public static void main(String args[]) { distemper d=new distemper(); emulsion e=new emulsion(); mat m=new mat(); int ch,ch1; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("\n\t***Painting***"); System.out.println("1.House"); System.out.println("2.Commercial\n"); System.out.println("Enter ur choice"); ch=Integer.parseInt(br.readLine()); switch(ch) { case 1:

System.out.println("\n\n1.Distemper"); System.out.println("2.Emulsion"); System.out.println("\nEnter ur choice"); ch1=Integer.parseInt(br.readLine()); switch(ch1) { case 1: d.l=20; d.b=20; d.sqft(); d.calc(); d.show(); break; case 2: e.l=20; e.b=20; e.sqft(); e.calc(); e.show(); break; } break; case 2:

m.l=50; m.b=50; m.sqft(); m.calc(); m.show(); break; } } catch(Exception e1) { } } }

OUTPUT:

RESULT:

EX:NO:2.c DATE: MULTILEVEL HIERARCHY AIM:

ALGORITHM:

PROGRAM:
import java.lang.*; import java.io.*; class staff { String cname; void st() { System.out.println("\nStaff Name ="+cname); } } class teacher extends staff { String subject; String pub; void otae() { System.out.println("\nSTAFF \n"); System.out.println("Subject ="+subject); System.out.println("Publication ="+pub); }

} class typist extends staff { int speed; void ty() { System.out.println("Speed ="+speed); } } class reg extends typist { void reg() { System.out.println("\nREGULAR"); } } class cas extends typist { int wag; void cas() { System.out.println("\nCASUAL"); System.out.println("wages ="+wag);

} } class off extends staff { String gr; void off() { System.out.println("Grade ="+gr); } } class sdb { public static void main(String args[]) { teacher t=new teacher(); reg r=new reg(); cas c=new cas(); off o=new off(); int y,p; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("\n1.Staff\n2.Typist\n3.Officer\n\nEnter ur choice");

p=Integer.parseInt(br.readLine()); switch(p) { case 1: System.out.println("Enter Name"); t.cname=br.readLine(); System.out.println("Enter Subject"); t.subject=br.readLine(); System.out.println("Enter Publication"); t.pub=br.readLine(); t.st(); t.otae(); break; case 2: System.out.println("\n1.Regular\n2.casual\n\nur choice"); y=Integer.parseInt(br.readLine()); if(y==1) { System.out.println("Enter Name"); r.cname=br.readLine(); System.out.println("Enter Speed"); r.speed=Integer.parseInt(br.readLine()); r.reg();

r.st(); r.ty(); } else if(y==2) { System.out.println("Enter Name"); c.cname=br.readLine(); System.out.println("Enter Speed"); c.speed=Integer.parseInt(br.readLine()); System.out.println("Enter Wages"); c.wag=Integer.parseInt(br.readLine()); c.st(); c.cas(); c.ty(); } break; case 3: System.out.println("Enter Name"); o.cname=br.readLine(); System.out.println("Enter Greade"); o.gr=br.readLine(); o.st(); o.off();

break; } } catch(Exception e) { } } }

OUTPUT:

RESULT:

INTERFACES Ex:No:3.a DATE:


IMPLEMENTATION OF GROWABLE STACK

AIM:

ALGORITHM:

PROGRAM:
interface intstack { void push(int item); int pop(); } class fixedstack implements intstack { private int stck[]; private int tos; fixedstack(int size) { stck=new int[size]; tos=-1; } public void push(int item) { if(tos==stck.length-1) System.out.println("Stack is full."); else stck[++tos]=item; } public int pop()

{ if(tos<0) { System.out.println("Stack underflow"); return 0; } else return stck[tos--]; } } class iftest { public static void main(String args[]) { fixedstack mystack1=new fixedstack(5); fixedstack mystack2=new fixedstack(8); for(int i=0;i<5;i++) mystack1.push(i); for(int i=0;i<8;i++) mystack2.push(i); System.out.println("Stack in mystack1:"); for(int i=0;i<5;i++) System.out.println(mystack1.pop()); System.out.println("Stack in mystack2:"); for(int i=0;i<8;i++)

System.out.println(mystack2.pop()); } }

OUTPUT:

RESULT:

Ex:No:3.b DATE: INTERFACE USING INHERITANCE AIM:

ALGORITHM:

PROGRAM:
interface A { void meth1(); void meth2(); } interface B extends A { void meth3(); } class myclass implements B { public void meth1() { System.out.println("Implement meth1"); } public void meth2() { System.out.println("Implement meth2"); } public void meth3() { System.out.println("Implement meth3");

} } class ifextend { public static void main(String args[]) { myclass ob=new myclass(); ob.meth1(); ob.meth2(); ob.meth3(); } }

OUTPUT:

RESULT:

Ex:No:3.c DATE: VARIABLES IN INTERFACE AIM:

ALGORITHM:

PROGRAM:
import java.util.Random; interface sharedconstants { int NO=0; int YES=1; int MAYBE=2; int LATER=3; int SOON=4; int NEVER=5; } class question implements sharedconstants { Random rand=new Random(); int ask() { int prob=(int)(100*rand.nextDouble()); if(prob<30) return NO; else if(prob<60) return YES; else if(prob<75) return LATER;

else if(prob<98) return SOON; else return NEVER; } } class askme implements sharedconstants { static void answer(int result) { switch(result) { case NO: System.out.println("NO"); break; case YES: System.out.println("YES"); break; case MAYBE: System.out.println("MAYBE"); break; case LATER: System.out.println("LATER");

break; case SOON: System.out.println("SOON"); break; case NEVER: System.out.println("NEVER"); break; } } public static void main(String args[]) { question q=new question(); answer(q.ask()); answer(q.ask()); answer(q.ask()); answer(q.ask()); } }

OUTPUT:

RESULT:

THREADS Ex:No:4.a DATE: IMPLEMENTING RUNNABLE AIM:

ALGORITHM:

PROGRAM:
class newthread implements Runnable { Thread t; newthread() { t=new Thread(this,"Demo Thread"); System.out.println("Child Thread:"+t); t.start(); } public void run() { try { for(int i=5;i>0;i--) { System.out.println("Child Thread:"+i); Thread.sleep(500); } } catch(InterruptedException e) { System.out.println("Child Interrupted");

} System.out.println("Exiting Child thread"); } } class threadDemo { public static void main (String args[]) { new newthread(); try { for(int i=5;i>0;i--) { System.out.println("Main Thread:"+i); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println("Main Interrupted"); } System.out.println("Exiting Main thread"); }

OUTPUT:

RESULT:

Ex:No:4.b DATE: EXTENDING THREAD AIM:

ALGORITHM:

PROGRAM:
class newthread extends Thread { newthread() { super("Demo Thread"); System.out.println("Child Thread:"+this); start(); } public void run() { try { for(int i=5;i>0;i--) { System.out.println("Child Thread:"+i); Thread.sleep(500); } } catch(InterruptedException e) { System.out.println("Child Interrupted"); }

System.out.println("Exiting Child thread"); } } class extendthread { public static void main (String args[]) { new newthread(); try { for(int i=5;i>0;i--) { System.out.println("Main Thread:"+i); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println("Main Interrupted"); } System.out.println("Exiting Main thread"); } }

OUTPUT:

RESULT:

Ex:No:4.c DATE: MULTIPLE THREADS AIM:

ALGORITHM:

PROGRAM:
class newthread implements Runnable { String name; Thread t; newthread(String threadname) { name=threadname; t=new Thread(this,name); System.out.println("New Thread:"+t); t.start(); } public void run() { try { for(int i=5;i>0;i--) { System.out.println(name+":"+i); Thread.sleep(1000); } } catch(InterruptedException e)

{ System.out.println(name+" Interrupted"); } System.out.println(name+" Exiting"); } } class multithreadDemo { public static void main (String args[]) { new newthread("one"); new newthread("two"); new newthread("three"); try { Thread.sleep(10000); } catch(InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Main thread Exiting"); }

OUTPUT:

RESULT:

FILE HANDLING & I/O HANDLING Ex:No:5.a DATE: FILE WRITER AIM:

ALGORITHM:

PROGRAM:
import java.io.*; class filewriterdemo { public static void main (String args[])throws Exception { String source="Now is the time for all good men\n"+"to come to the aid of their countory\n"+"and pay their due taxes"; char buffer[]=new char[source.length()]; source.getChars(0,source.length(),buffer,0); FileWriter f0=new FileWriter("file1.txt"); for (int i=0;i<buffer.length;i+=2) { f0.write(buffer[i]); } f0.close(); FileWriter f1=new FileWriter("file2.txt"); f1.write(buffer); f1.close(); FileWriter f2=new FileWriter("file3.txt"); f2.write(buffer,buffer.length-buffer.length/4,buffer.length/4); f2.close(); }

OUTPUT:

RESULT:

Ex:No:5.b DATE: FILE READER AIM:

ALGORITHM:

PROGRAM:
import java.io.*; class filereaderdemo { public static void main(String args[])throws Exception { FileReader fr=new FileReader("file3.txt"); BufferedReader br=new BufferedReader(fr); String s; while((s=br.readLine())!=null) { System.out.println(s); } fr.close(); } }

OUTPUT:

RESULT:

EVENT HANDLING USING APPLETS Ex:No:6.a DATE: HANDLING MOUSE EVENTS AIM:

ALGORITHM:

PROGRAM:
import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="MouseEvents" width=400 height=100> </applet> */ public class MouseEvents extends Applet implements MouseListener,MouseMotionListener { String msg=""; int mouseX=0,mouseY=0; public void init() { addMouseListener(this); addMouseMotionListener(this); } public void mouseClicked(MouseEvent me) { mouseX=0;

mouseY=10; msg="Mouse Clicked."; repaint(); } public void mouseEntered(MouseEvent me) { mouseX=0; mouseY=10; msg="Mouse Entered."; repaint(); } public void mouseExited(MouseEvent me) { mouseX=0; mouseY=10; msg="Mouse Exited"; repaint(); } public void mousePressed(MouseEvent me) { mouseX=me.getX(); mouseY=me.getY(); msg="Down";

repaint(); } public void mouseReleased(MouseEvent me) { mouseX=me.getX(); mouseY=me.getY(); msg="Up"; repaint(); } public void mouseDragged(MouseEvent me) { mouseX=me.getX(); mouseY=me.getY(); msg="***"; showStatus("Dragging mouse at "+ mouseX +", "+mouseY); repaint(); } public void mouseMoved(MouseEvent me) { showStatus("Moving mouse at "+me.getX()+" , "+me.getY()); } public void paint(Graphics g) {

g.drawString(msg,mouseX,mouseY); } }

OUTPUT:

RESULT:

Ex:No:6.b DATE: HANDLING KEYBOARD EVENTS AIM:

ALGORITHM:

PROGRAM:
import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="KeyEvents" width=300 height=100> </applet> */ public class KeyEvents extends Applet implements KeyListener { String msg = ""; int X = 10, Y = 20; // output coordinates public void init() { addKeyListener(this); requestFocus(); // request input focus }

public void keyPressed(KeyEvent ke) {

showStatus("Key Down"); int key = ke.getKeyCode(); switch(key) { case KeyEvent.VK_F1: msg += "<F1>"; break; case KeyEvent.VK_F2: msg += "<F2>"; break; case KeyEvent.VK_F3: msg += "<F3>"; break; case KeyEvent.VK_F4: msg += "<F4>"; break; case KeyEvent.VK_PAGE_DOWN: msg += "<PgDn>"; break; case KeyEvent.VK_PAGE_UP: msg += "<PgUp>"; break; case KeyEvent.VK_LEFT:

msg += "<Left Arrow>"; break; case KeyEvent.VK_RIGHT: msg += "<Right Arrow>"; break; } repaint(); } public void keyReleased(KeyEvent ke) { showStatus("Key Up"); } public void keyTyped(KeyEvent ke) { msg += ke.getKeyChar(); repaint(); }

public void paint(Graphics g) { g.drawString(msg, X, Y); } }

OUTPUT:

RESULT:

Ex:No:7 DATE: SWINGS AIM:

ALGORITHM:

PROGRAM:
import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; /* <applet code="JTabbedPaneDemo" width=400 height=100> </applet> */ public class JTabbedPaneDemo extends JApplet { public void init() { JTabbedPane jtp = new JTabbedPane(); jtp.addTab("Cities", new CitiesPanel()); jtp.addTab("Colors", new ColorsPanel()); jtp.addTab("Flavors", new FlavorsPanel()); getContentPane().add(jtp); } }

class CitiesPanel extends JPanel { public CitiesPanel() { JButton b1 = new JButton("New York"); add(b1); JButton b2 = new JButton("London"); add(b2); JButton b3 = new JButton("Hong Kong"); add(b3); JButton b4 = new JButton("Tokyo"); add(b4); } } class ColorsPanel extends JPanel { public ColorsPanel() { JCheckBox cb1 = new JCheckBox("Red"); add(cb1); JCheckBox cb2 = new JCheckBox("Green"); add(cb2); JCheckBox cb3 = new JCheckBox("Blue");

add(cb3); } } class FlavorsPanel extends JPanel { public FlavorsPanel() { JComboBox jcb = new JComboBox(); jcb.addItem("Vanilla"); jcb.addItem("Chocolate"); jcb.addItem("Strawberry"); add(jcb); } }

OUTPUT:

RESULT:

DATABASE APPLICATIONS(JDBC) EX. NO: 8 DATE: CREATE A JAVA PROGRAM TO IMPLEMENT JDBC AIM:

ALGORITHM:

PROGRAM: import java.sql.*; import java.io.*; import java.lang.*; public class jdbc { static Statement stmt; public static void main(String args[])throws SQLException, IOException { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:odbc:my"); float tot,avg,mm1,mm2,mm3; String result,grade; grade =""; stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery("SELECT * FROM marks"); while (rset.next()) { mm1= Integer.parseInt(rset.getString(3)); mm2= Integer.parseInt(rset.getString(4)); mm3= Integer.parseInt(rset.getString(5)); tot = mm1+mm2+mm3;

avg = tot/3; if (mm1 >=50 && mm2>=50 && mm3>=50) { result= "Pass"; if(avg > 75.0 ) { grade ="First class with Dist."; } if(avg >=60 && avg <75 ) { } if(avg >=50 && avg <60 ) { } } else result="Fail"; System.out.println("Roll No System.out.println("Name : " +rset.getString(1) + "\t"); : " +rset.getString(2) + "\t"); grade ="Second class"; grade ="First class";

System.out.println("Web Technology : " +mm1); System.out.println("Unix : " +mm2);

System.out.println("VC++: " +mm3); System.out.println("Total : "+ tot);

System.out.println("Result System.out.println("Percentage if(!result.equals("Fail")) System.out.println("Grade

: "+ result); : "+ avg);

: "+ grade);

System.out.println("----------------------------------------------\n\n\n"); } stmt.close(); conn.close(); System.out.println("Your JDBC installation is correct."); } catch(Exception e) { System.out.println("ERROR"); }} }

OUTPUT: C:\j2sdk1.4.2_05\bin>javac jdbc.java C:\ j2sdk1.4.2_05\bin>java jdbc Roll No Name :1 : java

Web Technology : 80.0 Unix VC++ Total Result Percentage Grade : 90.0 : 90.0 : 260.0 : Pass : 86.666664 : First class with Dist.

---------------------------------------------Roll No Name :2 : program

Web Technology : 78.0 Unix VC++ Total Result Percentage Grade : 90.0 : 50.0 : 218.0 : Pass : 72.666664 : First class

----------------------------------------------

Your JDBC installation is correct.

RESULT:

Vous aimerez peut-être aussi