Vous êtes sur la page 1sur 66

Practical 1 Write a program to implement concept of class, constructor & Inheritance.

import java.io.*; class Bank { int Acc_No; String Cust_Name,Password,Acc_Type; double Bal_Amount; public Bank(){} public Bank(int no, String name, String pass, String type, double amt) { Acc_No = no; Cust_Name = name; Password = pass; Acc_Type = type; Bal_Amount = amt; } public void Moneydeposit() throws Exception {} public void getWithdraw() throws Exception {} public void BalanceEnquire() throws Exception {} } public class XXXBank extends Bank { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Bank acc[] = new Bank[10]; int No_of_acc; public XXXBank() { acc[0] = new Bank(1001,"User1","123","saving",12000); acc[1] = new Bank(1002,"User2","456","saving",10000); acc[2] = new Bank(1003,"User3","789","current",9500); No_of_acc = 3; } public void Operation() throws Exception { do { System.out.println("\n--------------------------------"); System.out.println("\t Welcome to XXXBank"); System.out.println("--------------------------------"); System.out.println(" 1. New Register Customer");

System.out.println(" 2. Withdraw"); System.out.println(" 3. Deposit"); System.out.println(" 4. Balace Enquiry"); System.out.println(" 5. Exit"); System.out.println("--------------------------------"); System.out.println("Enter a Choice :"); int choice = Integer.parseInt(br.readLine()); switch(choice) { case 1: New_Customer(); break; case 2: getWithdraw(); break; case 3: Moneydeposit(); break; case 4: BalanceEnquire(); break; case 5: System.out.println("...Thank you...!!!"); System.exit(0); } }while(true); } public void getWithdraw() throws Exception { Bank ac = getCustomer(); System.out.println("Enter Amount to Withdraw :"); double amt = Double.parseDouble(br.readLine()); if(amt<0 || amt > ac.Bal_Amount) { System.out.println("\nAmount Enter is Inefficient\nPlease repeat the step!"); } else { ac.Bal_Amount = ac.Bal_Amount - amt; System.out.println("\nAmount Withdraw Successfully...!"); } } public void Moneydeposit() throws Exception { Bank ac = getCustomer();

System.out.println("Enter Amount for Deposit :"); double amt = Double.parseDouble(br.readLine()); ac.Bal_Amount = ac.Bal_Amount + amt; System.out.println("\nAmount Deposit Successfully...!"); } public void BalanceEnquire() throws Exception { Bank ac = getCustomer(); System.out.println("\nTotal Balance : "+ ac.Bal_Amount); } public Bank getCustomer() throws Exception { Console cs=System.console(); System.out.println("Enter AccountNumber :"); int accno = Integer.parseInt(br.readLine()); System.out.println("Enter Password :"); char[] ch=cs.readPassword(); String pass = ""; for(int i=0;i<ch.length;i++) pass=pass+ch[i]; for(int i=0; i<No_of_acc; i++) { if(accno == acc[i].Acc_No && pass.equals(acc[i].Password)) { return acc[i]; } } return null; } public void New_Customer() throws Exception { System.out.println("Enter AccountNumber :"); int accno = Integer.parseInt(br.readLine()); System.out.println("Enter Name : "); String name = br.readLine(); System.out.println("Enter Password :"); String pass = br.readLine(); System.out.println("Enter Account Type :"); String type = br.readLine(); System.out.println("Enter Balance Amount :"); double amt = Double.parseDouble(br.readLine()); acc[No_of_acc] = new Bank(accno, name, pass, type, amt); No_of_acc = No_of_acc + 1; System.out.println("\nAccount Successfully Registered!"); } public static void main(String args[])

{ try { XXXBank ob1 = new XXXBank(); ob1.Operation(); } catch(Exception e) { System.out.println("Error : "+e); } } }

Output:

Pratical2 Aim:- Java code to display MIB node information using

com.adventnet.snmp.mibs package

import com.adventnet.snmp.snmp2.*; import com.adventnet.snmp.mibs.*; import java.io.*; public class MibNodeInfo1 { public static void main(String args[]) throws IOException{ // Take care of getting OID and the MIB file name String mibfile ="RFC1213-MIB.mib"; String OID; BufferedReader br1=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter OID : "); OID=br1.readLine(); // load the MIB file MibOperations mibops = new MibOperations(); try { mibops.loadMibModules(mibfile); } catch (Exception ex){ System.err.println("Error loading MIBs: "+ex.getMessage()); } // add OIDs MibNode node = mibops.getMibNode(OID); if(node == null) { //System.out.println("Invalid OID / the node " + oid + " is not available"); } else { System.out.println("Syntax:"+node.getSyntax()+"\n"+ "Access:"+node.printAccess()+"\n"+ "Status:"+node.getStatus()+"\n"+ "Reference:"+node.getReference()+"\n"+ "OID:"+node.getNumberedOIDString()+"\n"+ "Node:"+node.getOIDString()+"\n"+ "Description:"+node.getDescription()+"\n"); } } }

Output

Practical 3

Applet application
1. Write an applet application which displays the name in a serif font in plain , bold, italic and bold italic. Name should be displayed on the top of each other. import java.applet.Applet; import java.awt.*; import java.awt.event.*; /*<applet code="MainFont" width=200 height=200></applet>*/ public class MainFont extends Applet implements ItemListener { CheckboxGroup lngGrp = null; Label l1; TextField tf1; Font font; Checkbox plain,bold,italic,BoldItalic; public void init() { l1=new Label("Enter the text"); tf1=new TextField(20); lngGrp = new CheckboxGroup(); plain = new Checkbox("Plain", lngGrp, true); bold = new Checkbox("Bold", lngGrp, false); italic = new Checkbox("Italic", lngGrp, false); BoldItalic = new Checkbox("BoldItalic", lngGrp, false); add(l1); add(tf1); add(plain); add(bold); add(italic); add(BoldItalic); plain.addItemListener(this); bold.addItemListener(this); italic.addItemListener(this); BoldItalic.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { if(plain.getState()) { font=new Font("Microsoft Sans Serif",Font.PLAIN,20); repaint(); } if(bold.getState()) { font=new Font("Microsoft Sans Serif",Font.BOLD,20);

repaint(); } if(italic.getState()) { font=new Font("Microsoft Sans Serif",Font.ITALIC,20); repaint(); } if(BoldItalic.getState()) { font=new Font("Microsoft Sans Serif",Font.BOLD | Font.ITALIC ,20); repaint(); } } public void paint(Graphics g) { String str=tf1.getText(); g.setColor(Color.BLUE); g.setFont(font); g.drawString(str,10,70); } } Output:

2. Write a program to draw different shapes using graphics 2D & shape Interface as per the user co-ordinates & also paint them using colors & give an effect of Transparency, Cyclic Colouring & Different Color Patterns also mention the line Stroke of the Shape as Dotted etc. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.*; import java.util.*; //<applet code= "Java2D.class" width=600 height=350></applet> public class Java2D extends JApplet implements ActionListener,ItemListener { ButtonGroup bg=null; JRadioButton trans,fill,linestroke,cyclic;

JComboBox transPer; JButton submit; Vector vTrans; JButton rect,circle,line,arc; JPanel jp1,jp2; boolean blnTrans,blnFill,blnlinestroke; boolean blnRect,blnCircle,blnArc,blnLine; float fl[] = { 12.0f }; public Java2D() { bg=new ButtonGroup(); trans=new JRadioButton("Transperency"); fill=new JRadioButton("Fill"); linestroke=new JRadioButton("Line Stroke"); cyclic=new JRadioButton("Cyclic Coloring"); trans.addItemListener(this); fill.addItemListener(this); linestroke.addItemListener(this); cyclic.addItemListener(this); vTrans=new Vector(); vTrans.add("Full Transperent"); vTrans.add("Semi Transperent"); vTrans.add("No Transperent"); transPer=new JComboBox(vTrans); submit=new JButton("Submit"); submit.addActionListener(this); bg.add(trans); bg.add(fill); bg.add(cyclic); bg.add(linestroke); setLayout(new BorderLayout()); jp2=new JPanel(); jp2.add(trans); jp2.add(cyclic); jp2.add(fill); jp2.add(linestroke); jp2.add(transPer); transPer.setEnabled(false); this.add(jp2,BorderLayout.NORTH); rect=new JButton("Rectangle");

circle=new JButton("Circle"); arc=new JButton("Arc"); line=new JButton("Line"); rect.addActionListener(this); circle.addActionListener(this); arc.addActionListener(this); line.addActionListener(this); jp1=new JPanel(); jp1.add(rect); jp1.add(circle); jp1.add(arc); jp1.add(line); this.add(jp1,BorderLayout.SOUTH); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==rect) { blnRect=true; blnArc=false; blnLine=false; blnCircle=false; repaint(); } if(ae.getSource()==circle) { blnRect=false; blnArc=false; blnLine=false; blnCircle=true; repaint(); } if(ae.getSource()==arc) { blnLine=false; blnCircle=false; blnRect=false; blnArc=true; repaint(); }

if(ae.getSource()==line) { blnRect=false; blnCircle=false; blnArc=false; blnLine=true; repaint(); } } public void itemStateChanged(ItemEvent ie) { if(trans.isSelected()) transPer.setEnabled(true); else transPer.setEnabled(false); } public void paint(Graphics g) { Graphics2D g2=(Graphics2D)g; if(blnRect) { if(trans.isSelected()) { if(transPer.getSelectedIndex()==0) { AlphaComposite ac =AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.10f); g2.setComposite(ac); g2.setColor(Color.magenta); g2.fillRect(150,150,100,100); } if(transPer.getSelectedIndex()==1) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.50f); g2.setComposite(ac); g2.setColor(Color.magenta); g2.fillRect(150,150,100,100); } if(transPer.getSelectedIndex()==2) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f);

g2.setComposite(ac); g2.setColor(Color.magenta); g2.fillRect(150,150,100,100); } } else if(fill.isSelected()) { g.setColor(Color.pink); g.fillRect(150,150,100,100); } else if(cyclic.isSelected()) { GradientPaint gp=new GradientPaint(150,150,new Color(255,0,0),150,200,new Color(0,0,255),true); g2.setPaint(gp); Shape s=new Rectangle2D.Double(150.0,150.0,100.0,100.0) ; g2.fill(s); blnRect=false; } else if(linestroke.isSelected()) { g.setColor(Color.orange); BasicStroke basicStroke = new BasicStroke(2.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 12.0f,fl, 0.0f); g2.setStroke(basicStroke); Shape s=new Rectangle2D.Double(150.0,150.0,100.0,100.0) ; g2.draw(s); blnRect=false; } } if(blnCircle) { if(trans.isSelected()) { if(transPer.getSelectedIndex()==0) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.10f); g2.setComposite(ac); g2.setColor(Color.magenta); g2.fillOval(35, 50, 125, 180);

} if(transPer.getSelectedIndex()==1) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.50f); g2.setComposite(ac); g2.setColor(Color.magenta); g2.fillOval(35, 50, 125, 180); } if(transPer.getSelectedIndex()==2) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f); g2.setComposite(ac); g2.setColor(Color.magenta); g2.fillOval(35, 50, 125, 180); } } if(fill.isSelected()) { g.setColor(Color.pink); g.fillOval(35, 50, 125, 180); } if(cyclic.isSelected()) { GradientPaint gp=new GradientPaint(150,150,new Color(255,0,0),150,200,new Color(0,0,255),true); g2.setPaint(gp); Shape s=new Ellipse2D.Double(35.0, 50.0, 125.0, 180.0); g2.fill(s); blnCircle=false; } if(linestroke.isSelected()) { g.setColor(Color.orange); BasicStroke basicStroke = new BasicStroke(2.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 12.0f,fl, 0.0f); g2.setStroke(basicStroke); Shape s=new Ellipse2D.Double(35.0, 50.0, 125.0, 180.0); g2.draw(s); blnCircle=false; }

} if(blnArc) { if(trans.isSelected()) { if(transPer.getSelectedIndex()==0) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.10f); g2.setComposite(ac); g2.setColor(Color.magenta); g2.fill(new Arc2D.Double(150,150, 150, 150, 80, 130, Arc2D.OPEN)); } if(transPer.getSelectedIndex()==1) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.50f); g2.setComposite(ac); g2.setColor(Color.magenta); g2.fill(new Arc2D.Double(150,150, 150, 150, 80, 130,Arc2D.OPEN)); } if(transPer.getSelectedIndex()==2) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f); g2.setComposite(ac); g2.setColor(Color.magenta); g2.fill(new Arc2D.Double(150,150, 150, 150, 80, 130, Arc2D.OPEN)); } } else if(fill.isSelected()) { g.setColor(Color.pink); g.fillArc(150,150, 150, 150, 90, 135); } else if(cyclic.isSelected()) { GradientPaint gp=new GradientPaint(150,150,new Color(255,0,0),150,200,new Color(0,0,255),true); g2.setPaint(gp); Shape s=new Arc2D.Double(150,150, 150, 150, 80, 130, Arc2D.OPEN); g2.fill(s); blnArc=false;

} else if(linestroke.isSelected()) { g.setColor(Color.orange); BasicStroke basicStroke = new BasicStroke(2.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 12.0f,fl, 0.0f); g2.setStroke(basicStroke); Shape s=new Arc2D.Double(150,150, 150, 150,80, 130, Arc2D.OPEN); g2.draw(s); blnArc=false; } } if(blnLine) { if(trans.isSelected()) { if(transPer.getSelectedIndex()==0) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.10f); g2.setComposite(ac); g2.setColor(Color.magenta); g2.draw(new Line2D.Double(350, 50, 160, 230)); } if(transPer.getSelectedIndex()==1) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.50f); g2.setComposite(ac); g2.setColor(Color.magenta); g2.fill(new Line2D.Double(350, 50, 160, 230)); } if(transPer.getSelectedIndex()==2) { AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f); g2.setComposite(ac); g2.setColor(Color.magenta); g2.fill(new Line2D.Double(350, 50, 160, 230)); } } if(fill.isSelected()) {

g.setColor(Color.pink); g.drawLine(350, 50, 160, 230); } if(cyclic.isSelected()) { GradientPaint gp=new GradientPaint(200,200,new Color(255,0,0),250,250,new Color(0,0,255),true); g2.setPaint(gp); Shape s=new Line2D.Double(350, 50, 160, 230); g2.draw(s); blnLine=false; } if(linestroke.isSelected()) { g.setColor(Color.orange); BasicStroke basicStroke = new BasicStroke(2.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 12.0f,fl, 0.0f); g2.setStroke(basicStroke); Shape s=new Line2D.Double(350, 50, 160, 230); g2.draw(s); blnLine=false; } } } } Output:

Practical 4

Write a program using Multithreading for bouncing balls with randomly changing colors. For each ball separate thread is assign. import java.awt.*; import java.awt.event.*; import java.applet.*; //<applet code =MainBounceColl.class width = 300 height = 200></applet> public class MainBounceColl extends Applet implements Runnable { Thread t1; int dx1=0,dy1=0,dx2=300,dy2=0; int radius=50; int win_w=200,win_h=150; boolean flgdx1=false; boolean flgdy1=false; boolean flgdx2=false; boolean flgdy2=false; public void init() { t1=new Thread(this); } public void start() { t1.start(); } public void run() { while(true) { repaint(); } } public void paint(Graphics g) {try { g.setColor(Color.magenta); g.fillOval(dx1,dy1,radius,radius); if(dy1>=win_h)

{ flgdy1=true; } if(dx1>=win_w) { flgdx1=true; } if(dy1<=0) { flgdy1=false; } if(dx1<=0) { flgdx1=false; } if(flgdx1==false) { dx1++; } else { dx1--; } if(flgdy1==false) { dy1++; } else { dy1--; } } catch (Exception ar){} try { g.setColor(Color.yellow); g.fillOval(dx2,dy2,radius,radius); if(dy2>=win_h) { flgdy2=true;

} if(dx2>=win_w) { flgdx2=true; } if(dy2<=0) { flgdy2=false; } if(dx2<=0) { flgdx2=false; } if(flgdx2==false) { dx2++; } else { dx2--; } if(flgdy2==false) { dy2++; } else { dy2--; } Thread.sleep(20); } catch (Exception ar){} if(dx1==dx2 && dy1==dy2) System.out.println("Collision"); } }

Output:

Practical 5

Write a Program for file handling to implant the ceaser cipher an application of Cryptography.
import java.io.*; public class Cipherex { public static void main(String arg[]) { try { File f1=new File("a.txt"); File f2=new File("b.txt"); FileInputStream fis=new FileInputStream(f1); FileOutputStream fos=new FileOutputStream(f2); int l; int asciiValue; while((l=fis.read())!=-1) { asciiValue=l+3; if(l>=65 && l<=90) { if(asciiValue>90) { asciiValue=64+(asciiValue-90); } } else if(l>=97 && l<=122) { if(asciiValue>122) { asciiValue=96+(asciiValue-122); } } fos.write(asciiValue); } } catch (Exception e) { System.err.println("Error: " + e.getMessage()); }}}

Output:

File before encryption

File after encryption

Practical no 6

Define a user define bean name as thermometer which is use to check the temperature also define the temperatureListener & TemperatureEvent Class for it.
TemperatureListener.java import java.util.*; interface TemperatureListener extends EventListener { public void tempChanged(TemperatureEvent evt); } TemperatureEvent.java import java.util.*; public class TemperatureEvent extends EventObject { double currTemp; TemperatureEvent(Object source,double temp1) { super(source); currTemp=temp1; } public double getTemperature() { return currTemp; } } Thermometer.java import java.util.*; import javax.swing.*; import javax.swing.event.*; public class Thermometer extends JSlider implements ChangeListener { Vector v; double curtemp=37; TemperatureListener current=null; public Thermometer() { this.setValue((int)curtemp); v=new Vector(); this.addChangeListener(this); } public void stateChanged(ChangeEvent ce)

{ System.out.println("Changed State\t"+ this.getValue()); setTemp(); notifyTempChange(); } public synchronized void addTemperatureListener(TemperatureListener tl) { if(v.contains(tl)) { System.out.println("already exists"); } else { v.add(tl); current=tl; } } public void setTemp() { curtemp=this.getValue(); this.setValue((int)curtemp); } public synchronized void removeTemperatureListener(TemperatureListener tl) { if(v.contains(tl)) v.removeElement(tl); } public synchronized void notifyTempChange() { System.out.println("Current temp\t"+ curtemp); TemperatureEvent ob1=new TemperatureEvent(this,curtemp); current.tempChanged(ob1); } public double getTemp() { curtemp=this.getValue(); this.setValue((int)curtemp); return curtemp; } } TestFrame.java import javax.swing.*; import java.awt.*; public class TestFrame extends JFrame implements TemperatureListener {

Thermometer th; JLabel jl1; String str; TestFrame() { th=new Thermometer(); th.addTemperatureListener(this); this.add(th,BorderLayout.CENTER); jl1=new JLabel(" "); this.add(jl1,BorderLayout.SOUTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setSize(200,200); } public static void main(String args[]) { new TestFrame(); } public void tempChanged(TemperatureEvent evt) { str=evt.getTemperature()+""; jl1.setText("\tCurrent temperature is \t" +str); } } Output:

Practical 7

Write a Swing application to design an editor like word where we can type a text or draw picture & save it. import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.io.*; import java.util.*; import java.util.regex.Pattern; public class Pract7 extends JFrame implements ActionListener { JMenuBar mb; JMenu file,edit,shape; JMenuItem New,Open,Save,Exit,Cut,Copy,Paste,Rect,Circle; JTextArea txt; JScrollBar jsb; JFileChooser jf; File f; JPanel panel; static int i; String s=""; public Pract7() { txt=new JTextArea(); mb=new JMenuBar(); file=new JMenu("File"); edit=new JMenu("Edit"); shape=new JMenu("Shape"); New= new JMenuItem("New"); Open=new JMenuItem("Open"); Save=new JMenuItem("Save"); Exit=new JMenuItem("Exit"); Cut=new JMenuItem("Cut"); Copy=new JMenuItem("Copy");

Paste=new JMenuItem("Paste"); Rect=new JMenuItem("Rect"); Circle=new JMenuItem("Circle"); //Adding to menubar mb.add(file); mb.add(edit); mb.add(shape); // on file menu file.add(New); file.add(Open); file.add(Save); file.add(Exit); // on edit menu edit.add(Cut); edit.add(Copy); edit.add(Paste); shape.add(Rect); shape.add(Circle); //addig action listener New.addActionListener(this); Open.addActionListener(this); Save.addActionListener(this); Exit.addActionListener(this); Cut.addActionListener(this); Copy.addActionListener(this); Paste.addActionListener(this); Rect.addActionListener(this); Circle.addActionListener(this); this.add(mb,BorderLayout.NORTH); this.add(txt,BorderLayout.CENTER); //this.getContentPane().add(panel."South"); this.setSize(400,400); this.setTitle("Document"+i); i++;

this.setVisible(true); } public void actionPerformed(ActionEvent ae) { if(ae.getSource().equals(New)) { new Pract7(); System.out.println("Practical 7"); } else if(ae.getSource().equals(Open)) { try{ jf=new JFileChooser(); jf.setDialogTitle("Choose a file"); jf.showOpenDialog(this); f=jf.getSelectedFile(); String fname=f.getName(); this.setTitle(fname); FileInputStream fis=new FileInputStream(f); int a; while((a=fis.read())!=-1) { s=s+(char)a; } txt.append(s); } catch(Exception e){} } else if (ae.getSource().equals(Save)) { try { jf=new JFileChooser();

jf.setDialogTitle("Save a file"); jf.showSaveDialog(this); f=jf.getSelectedFile(); this.setTitle(f.getName()); FileOutputStream fos=new FileOutputStream(f); byte b[]=new byte[1024]; b=txt.getText().getBytes(); fos.write(b); System.out.println("File Written"); } catch(Exception e){} } else if (ae.getSource().equals(Cut)) { try { s=txt.getSelectedText(); txt.replaceSelection(""); } catch(Exception e){} } else if (ae.getSource().equals(Copy)) { try{ s=txt.getSelectedText(); System.out.println(s); } catch(Exception e){} } else if(ae.getSource().equals(Paste)) { try{ txt.insert(s,txt.getCaretPosition());

System.out.println("PASTED"); } catch(Exception e){} } else if(ae.getSource().equals(Rect)) { try{ new DrawFrame("rect"); } catch(Exception e){} } else if(ae.getSource().equals(Circle)) { try{ new DrawFrame("circle"); } catch(Exception e){} } } public static void main(String[] args) { new Pract7(); } } class DrawFrame extends JFrame { public DrawFrame(String shp) { this.add(new ShapeDemo(shp)); this.setSize(500,500); this.setVisible(true); } }

class ShapeDemo extends Canvas { String shape; public ShapeDemo(String shp) { shape=shp; } public void paint(Graphics g) { if(shape.equals("rect")) { int a,b,c,d; a=Integer.parseInt(JOptionPane.showInputDialog("enter x coordinate")); b=Integer.parseInt(JOptionPane.showInputDialog("enter y point coordinate")); c=Integer.parseInt(JOptionPane.showInputDialog("enter width coordinate")); d=Integer.parseInt(JOptionPane.showInputDialog("enter heigth coordinate")); g.drawRect(a,b,c,d); } if(shape.equals("circle")) { int a,b,c,d; a=Integer.parseInt(JOptionPane.showInputDialog("enter x coordinate")); b=Integer.parseInt(JOptionPane.showInputDialog("enter y point coordinate")); c=Integer.parseInt(JOptionPane.showInputDialog("enter width coordinate")); d=Integer.parseInt(JOptionPane.showInputDialog("enter heigth coordinate")); g.drawOval(a, b, c,d); } }

}
Output:

(100,100,250,250)

(100,100,250,230)

Practical no 8 AIM: JDBC 1. Write a Java code to create a table named Student & insert the records in it. 2. Also alter the table by adding new column named Photo where you can Add student photo. 3. Also to retrieve the records from the Database. Student.java import java.sql.*; import java.io.*; class StudentCon { ResultSet rs; Statement st; Connection con; public StudentCon() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:dsn1"); st=con.createStatement(); } catch (Exception e) { e.printStackTrace(); } } public void Create_StudentTable() { try { boolean b=st.execute("create table student1(rollno Integer,name varchar(10))"); System.out.println("Table Created Successfully"); } catch (Exception e){ System.out.println("Table already exist");} } public void Alter_StudentTable() { try { String query="alter table student1 add Photo image";

int i=st.executeUpdate(query); System.out.println("Table Altered Successfully"); } catch (Exception e) { e.printStackTrace(); } } public void Insert_StudentTable() { try { String query="insert into student1 values(?,?,?)"; PreparedStatement pst=con.prepareStatement(query); pst.setInt(1,10); pst.setString(2,"arnav"); File file = new File("D:/W1.jpg"); FileInputStream fis = new FileInputStream(file); pst.setBinaryStream(3,fis,(int)file.length()); pst.executeUpdate(); System.out.println("Data Inserted Successfully"); st.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } public void Display_StudentTable() { try { String query="select * from student1"; st=con.createStatement(); rs=st.executeQuery(query); byte[] img = new byte[1024]; while(rs.next()) { String name= rs.getString(2); int roll= rs.getInt(1); InputStream is=rs.getBinaryStream(3);

System.out.println("Student name:" + name); System.out.println("Roll No:" + roll); OutputStream out=new FileOutputStream(new File("arnav.jpg")); int c=0; while((c=is.read(img))!=-1) { out.write(img); } is.close(); out.flush(); out.close(); } } catch (Exception e){e.printStackTrace();} } } public class Student { public static void main(String a[]) throws Exception { char ans; String s; do { System.out.println("Enter Your Choice: \n 1:Create Table \n 2.Alter Table \n 3.Insert Data\n 4:Display Information\n 5:Exit\n" ); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); int choice=Integer.parseInt(br.readLine()); switch (choice) { case 1: new StudentCon().Create_StudentTable(); break; case 2: new StudentCon().Alter_StudentTable(); break; case 3: new StudentCon().Insert_StudentTable(); break; case 4:

new StudentCon().Display_StudentTable(); break; case 5: System.exit(0); break; } System.out.println("Do u want to continue(y/n)"); s=br.readLine(); ans=s.charAt(0); }while(ans=='Y' || ans=='y'); } }

Practical no 9

Java Localization (using Local Class, ResourceBundle) to get a concept of Localizing Program as per the locals. 1. Displaying entered name in different languages.
import java.util.*; import java.io.*; public class IDemo { public static void main(String[] args) { try { Locale[] l = { new Locale("fr", "FR"),new Locale("es", "ES"),new Locale("ja","JM")}; ResourceBundle rb; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter text"); String txt=br.readLine(); System.out.println("Enter the choice corresponding to the languageyou want:"); System.out.println("1.Spanish"); System.out.println("2:French"); System.out.println("3:Japanese"); int code=Integer.parseInt(br.readLine()); switch(code) { case 1: rb=ResourceBundle.getBundle("msg",l[0]); System.out.println(rb.getString(txt)); break; case 2: rb=ResourceBundle.getBundle("msg",l[1]); System.out.println(rb.getString(txt)); break; case 3: rb=ResourceBundle.getBundle("msg",l[2]); System.out.println(rb.getString(txt)); break; } } catch (Exception e){} }

} Output:

2. Displaying currency of numbers in different Locals.


import java.text.*; import java.util.*; public class INumber { public static void main(String a[]) { try { Locale l1[]={new Locale("fr","FR"),new Locale("en","US"),new Locale("th", "TH")}; for(int i=0;i<l1.length;i++) { NumberFormat nf=NumberFormat.getCurrencyInstance(l1[i]); System.out.println("Number Format::" +l1[i].getDisplayCountry()); System.out.println(nf.format(578)); System.out.println(); } } catch(Exception e){} } }

Output:

Number

Practical no 10

Write a JSP program to display a bean.


package MyPack; public class Fact { private int count; public void setCount(int x) { count=1; while(x>0) { count=count*x; x=x-1; }

public int getCount() { return count; } }

FBean.jsp <jsp:useBean id="MyBean" class="MyPack.Fact" />

<jsp:setProperty name="MyBean" property="count" value='<%=Integer.parseInt(request.getParameter("no"))%>' /> <h1>The Factorial of number is: <jsp:getProperty name="MyBean" property="count" /> </h1>

Test.html
<form action="http://localhost:8082/FBean.jsp"> <h1>Enter the Number to find Factorial:<input type=text name="no" size="25"/></h1> <br> <h2><input type="SUBMIT"/></h2> </br> </form>

Web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true">

<servlet> <servlet-name>MyBean</servlet-name> <servlet-class>MyPack.Fact</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyBean</servlet-name> <url-pattern>/FBean</url-pattern> </servlet-mapping>

</web-app>

Output

Pratical2 Aim:- Java code to display MIB node information using

com.adventnet.snmp.mibs package

import com.adventnet.snmp.snmp2.*; import com.adventnet.snmp.mibs.*; import java.io.*; public class MibNodeInfo1 { public static void main(String args[]) throws IOException{ // Take care of getting OID and the MIB file name String mibfile ="RFC1213-MIB.mib"; String OID; BufferedReader br1=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter OID : "); OID=br1.readLine(); // load the MIB file MibOperations mibops = new MibOperations(); try { mibops.loadMibModules(mibfile); } catch (Exception ex){ System.err.println("Error loading MIBs: "+ex.getMessage()); } // add OIDs MibNode node = mibops.getMibNode(OID); if(node == null) { //System.out.println("Invalid OID / the node " + oid + " is not available"); } else { System.out.println("Syntax:"+node.getSyntax()+"\n"+ "Access:"+node.printAccess()+"\n"+ "Status:"+node.getStatus()+"\n"+ "Reference:"+node.getReference()+"\n"+ "OID:"+node.getNumberedOIDString()+"\n"+ "Node:"+node.getOIDString()+"\n"+ "Description:"+node.getDescription()+"\n"); } } }

Output

Vous aimerez peut-être aussi