• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Java practical file
 

Java practical file

on

  • 6,598 views

 

Statistics

Views

Total Views
6,598
Views on SlideShare
6,572
Embed Views
26

Actions

Likes
10
Downloads
0
Comments
2

3 Embeds 26

http://pratikjain1212.wordpress.com 14
http://localhost 11
https://www.facebook.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • please mail this useful doc to me , at maheshmalknoor@gmail.com
    Are you sure you want to
    Your message goes here
    Processing…
  • good enough
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Java practical file Java practical file Document Transcript

    • A program in Java to generate Fibonacci seriesimport java.io.*;importjava.util.*;public class Fibonacci{ public static void main(String[] args) { DataInputStream reader = new DataInputStream((System.in)); int x = 0, y = 0, z =1; try { System.out.print("Enter value of n: "); String st = reader.readLine(); intnum = Integer.parseInt(st); for(int i=1;i<=num;i++) { System.out.println("tt"+z+"tnt"); x = y; y = z; z = x + y; } } catch(Exception e) { System.out.println("wrong input"); } }}Pratik Jain(94972346366) Page 1
    • OUTPUTPratik Jain(94972346366) Page 2
    • A program in Java to demonstrate the use of an array class Student { private String name; public Student(){} public String setName(String name) { this.name = name; return (name); } } classStudentMain { public static void main (String[] args) { Student[] obj = new Student[2]; obj[0] = new Student(); System.out.println (obj[0].setName ("Tafadzwa")); obj[1] = new Student(); System.out.println (obj[1].setName ("Kombe")); } } OUTPUTPratik Jain(94972346366) Page 3
    • A program in Java to take two numbers as input from command line interfaceand display their sumclass Sum{ public void add(int a,int b) { return (a+b); }}classSumMain{ public static void main(String arg[]) { Sum obj1=new Sum(); int x =0 ,y = 0 ; String s1=arg[0]; String s2=arg[1]; x =Integer.parseInt(s1); y=Integer.parseInt(s2); System.out.prinln (obj1.add(x,y)); }}OUTPUTPratik Jain(94972346366) Page 4
    • A program in Java to create a class customer having three attributes name,bill and id. Include appropriate methods for taking input from customer anddisplaying its valuesimportjava.io.DataInputStream;class Customer{ publicstatic void main(String arg[]) { DataInputStreammyinput=new DataInputStream(System.in); String name; int bill=0,id=0; try { System.out.println("enter name of customer"); name=myinput.readLine(); System.out.println("enter bill"); bill=Integer.parseInt(myinput.readLine()); System.out.println("enter id"); id=Integer.parseInt(myinput.readLine()); System.out.println("name of customer is"+name); System.out.println("bill of customer"+bill); System.out.println("id of customer"+id); } catch(Exception e) { System.out.println("wrong input error!!!"); } }}Pratik Jain(94972346366) Page 5
    • OUTPUTPratik Jain(94972346366) Page 6
    • A program in Java to show the concept of method overloadingclass Add{ public int add(int a,int b) { return (a+b); } public float add(float a,float b) { return (a+b); }}classAddMain{ public static void main(String arg[]) { Addobj=new Add(); System.out.println(obj.add(20,30)); System.out.println(obj.add(100.44f,20.54f)); }} OUTPUTPratik Jain(94972346366) Page 7
    • A program in Java to count no. of object created of a classclass Demo{ private static int count=0; public Demo() { count++; System.out.println("object "+count+" created"); }}classDemoMain{ public static void main(String args[]) { Demo obj1=new Demo(); Demo obj2=new Demo(); Demo obj3=new Demo(); }} OUTPUTPratik Jain(94972346366) Page 8
    • A program in Java to show concept of multilevel inheritanceimport static java.lang.Math.*;class A{ private int num1,num2,sum; public void set(int x,int y) { num1=x; num2=y; sum=num1+num2; } public int get1() { return(sum); }}class B extends A{ public void display() { System.out.println("sum of two numbers is"+get1()); }}class C extends B{ private double sqr; public void sqrs() { System.out.println("square root of sum is"+sqrt(get1())); }}Pratik Jain(94972346366) Page 9
    • classABCMain{ public static void main(String args[]) { C obj1=new C(); obj1.set(100,200); System.out.println("first number is 100"); System.out.println("second number is 200"); obj1.display(); obj1.sqrs(); }}OUTPUTPratik Jain(94972346366) Page 10
    • A program in Java to demonstrate the concept of method overridingclass Mytd1 implements Runnable{public void run () {for (int i =1; i<=5; i++) {System.out.println("Mytd1 is executing " + i); }System.out.println ("Mytd1 finished executing"); }}classMytdMain{public static void main (String[] args) { Mytd1 obj1 = new Mytd1(); Thread t1 = new Thread(obj1);t1.start();for (int i = 1; i<=5; i++) {System.out.println("Main executes " + i); }System.out.println ("Main finished executing"); }}OUTPUTPratik Jain(94972346366) Page 11
    • A program in Java create a class that will at least import two packages anduse the method defined in the classes of those packages.importjava.awt.*;importjava.applet.*;public class MyApplet extends Applet{ public void paint(Graphics g) { g.drawString ("This is my first java applet", 200,100); }}<applet code="MyApplet.class" height="600"width="800"</applet> OUTPUTPratik Jain(94972346366) Page 12
    • A program in Java to create thread by extending thread classclass T1 extends Thread{ public void run() { for(int i=0;i<5;i++) { System.out.println("thread 1 created"); } }}classTMain{ public static void main(String arg[]) { T1 obj1=new T1(); obj1.start(); }} OUTPUTPratik Jain(94972346366) Page 13
    • A program in Java to create thread by implementing runnable interfaceclass T1 implements Runnable{ public void run() { for(int i=0;i<5;i++) { System.out.println("thread 1 created"); } }}class TMain1{ public static void main(String arg[]) { T1 obj1=new T1(); Thread t1=new Thread(obj1); t1.start(); }} OUTPUTPratik Jain(94972346366) Page 14
    • A program in Java to create user defined exceptionclassInvalidRollno extends Exception{ String msg; publicInvalidRollno(){} publicInvalidRollno(String m) { msg=m; } public String toString() { return(msg); }}class Student{ private int rollno; public void setStudent(int r) throws InvalidRollno { rollno=r; if(r<1) { throw new InvalidRollno("invalid rollno"); } }}classStudentMain{ public static void main(String[]args) { Student obj1=new Student(); try { obj1.setStudent(-11); }Pratik Jain(94972346366) Page 15
    • Catch(InvalidRollno e) { System.out.println(e); } }} OUTPUTPratik Jain(94972346366) Page 16
    • A program in Java to demonstrate sleep method in Multithreadingimportjava.awt.Graphics;importjava.applet.Applet;public class MyApplet extends Applet implements Runnable{ String msg; Boolean stopanimation; Thread t1; public void init() { msg = "I am Pctean"; stopanimation = false; t1 = new Thread(this); } public void start() { t1.start(); } public void run() { char c; for ( ; ; ) { repaint(); try { Thread.sleep(250); c = msg.charAt(0); msg = msg.substring(1,msg.length()); msg = msg + c; } catch (InterruptedException e) { System.out.println("Exception Caught"); }Pratik Jain(94972346366) Page 17
    • } } public void paint(Graphics g) { g.drawString(msg,150,300); } public void stop() { stopanimation = true; t1 = null; }}<applet code = "MyApplet.class" height = "800" width = "700"></applet>Pratik Jain(94972346366) Page 18
    • OUTPUTPratik Jain(94972346366) Page 19
    • A program in Java to illustrate a basic appletimportjava.awt.*;importjava.applet.Applet;public class MyBanner extends Applet{ public void paint(Graphics g) { g.drawString("HELLO WORLD",150,300); }}<applet code ="MyBanner.class" height = "700" width ="400"></applet>Pratik Jain(94972346366) Page 20
    • OUTPUTPratik Jain(94972346366) Page 21
    • A program in Java to draw various shapes on an appletimportjava.applet.Applet;importjava.awt.*;public class poly extends Applet{ public void paint (Graphics g) { int Cursor; int[] XArray = {20, 160, 120, 160, 20, 60}; int[] YArray = {20, 20, 90, 160, 160, 90}; g.drawPolygon (XArray, YArray, 6); Cursor = 0; while (Cursor < 6) { XArray [Cursor] = XArray [Cursor] + 200; Cursor = Cursor + 1;} g.fillPolygon (XArray, YArray, 6); }}<applet code="poly.class" width=700 height=700></applet>OUTPUTPratik Jain(94972346366) Page 22
    • A program in Java for filling various graphical objects with colorimportjava.applet.*;public class cirm extends Applet{ public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(10,10,50,50); g.drawOval(10,10,50,50); g.setColor(Color.black); g.fillOval(60,60,50,50); g.drawOval(60,60,50,50); g.setColor(Color.yellow); g.fillOval(110,110,50,50); g.drawOval(110,110,50,50); g.setColor(Color.green); g.fillOval(160,160,50,50); g.drawOval(160,160,50,50); }}<applet code="cirm.class" width=400 height=400></applet>Pratik Jain(94972346366) Page 23
    • OUTPUTPratik Jain(94972346366) Page 24
    • A program in java to implement the mouse motion listener interfaceimportjava.awt.*;importjava.applet.Applet;importjava.awt.event.*;public class MyApplet1 extends Applet implements MouseMotionListener{ String msg; intx,y; Thread t; public void init() { addMouseMotionListener(this); msg="i am from PCTE"; } public void mouseMoved(MouseEvent me) { x=me.getX(); y=me.getY(); msg="Mouse Moved"; showStatus("x="+x+"y="+y); repaint(); } public void mouseDragged(MouseEvent me) { x=me.getX(); y=me.getY(); msg="Mouse Dragged"; repaint(); } public void paint(Graphics g) { g.drawString(msg,x,y); }}Pratik Jain(94972346366) Page 25
    • OUTPUTPratik Jain(94972346366) Page 26
    • A program in Java to demonstrate the key listener interfaceimportjava.awt.*;importjava.applet.Applet;importjava.awt.event.*;public class MyApplet2 extends Applet implements KeyListener{ String msg; intx,y; Thread t; public void init() { addKeyListener(this); msg="i am from PCTE"; } public void keyTyped(KeyEvent me) { msg="key typed"; repaint(); } public void keyPressed(KeyEvent me) { msg="key pressed"; repaint(); } public void keyReleased(KeyEvent me) { msg="key released"; repaint(); } public void paint(Graphics g) { g.drawString(msg,200,200); }}Pratik Jain(94972346366) Page 27
    • OUTPUTPratik Jain(94972346366) Page 28
    • A program in Java to implement Inet address classimport java.net.*;classMynet{ public static void main (String[] arg) { try { InetAddressil = InetAddress.getLocalHost(); System.out.println("Information: " + il); } catch (Exception e) { System.out.println("Exception caught"); } }}OUTPUTPratik Jain(94972346366) Page 29
    • A program in Java to demonstrate the for each statementclass State{ String name[]; Int code [];}classForEach{ public static void main(String arg[]) { State obj=new State(); obj.name=new String[6]; obj.code=new int[6]; obj.name[0]="Texas"; obj.acode [0]=19; obj.name[1]="Alaska"; obj.acode [1]=19; obj.name[2]="Atlanta"; obj. code [2]=19; obj.name[3]="California"; obj. code [3]=19; obj.name[4]="Oregon"; obj. code [4]=19; obj.name[5]="Mississippi"; obj. code [5]=19; for(String i:obj.name) System.out.println(i); for(int j:obj .code) System.out.println(j); }}Pratik Jain(94972346366) Page 30
    • OUTPUTPratik Jain(94972346366) Page 31
    • WAP to Design An Applet For Recording The Student Informationimportjava.awt.*;importjava.awt.event.*;importjava.applet.*;public class forma extends Applet implements ActionListener{ String msg = ""; TextFieldtxtroll, txtname, txtFName, txtSearchRoll, txtSearchName; Button b1, b2; Label roll, name, Fname, gend, qualification, searchroll, searchname; Checkbox male, female; CheckboxGroupcbg; List quali; public void init() { txtroll = new TextField(20); txtname = new TextField(20); txtFName = new TextField(20); roll = new Label("Roll Number: ");Pratik Jain(94972346366) Page 32
    • name = new Label("Name: "); Fname = new Label("Fathers Name: "); gend = new Label("Gender: "); cbg = new CheckboxGroup(); male = new Checkbox("Male", cbg, false); female = new Checkbox("Female", cbg, false); b1 = new Button("Submit"); b2 = new Button("Search"); qualification = new Label("Qualification: "); quali = new List (3, false); quali.addItem("Undergraduate"); quali.addItem("Graduate"); quali.addItem("Postgraduate"); searchroll = new Label("RollNo to Search: "); txtSearchRoll = new TextField(20); txtSearchName = new TextField(20); searchname = new Label("Searched Name"); add(roll); add(txtroll);Pratik Jain(94972346366) Page 33
    • add(name); add(txtname); add(Fname); add(txtFName); add(gend); add(male); add(female); add(qualification); add(quali); add(b1); add(searchroll); add(txtSearchRoll); add(b2); add(searchname); add(txtSearchName); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEventae) { if (ae.getSource() == b2) { System.out.print("Hello");Pratik Jain(94972346366) Page 34
    • } }}/*<applet code="forma.class" width=300 height=200></applet>/*OUTPUTPratik Jain(94972346366) Page 35
    • WAP to illustrate the use of methods of StringBuffer classclasssttbu{ public static void main(String a[]) { StringBuffer s = new StringBuffer("PRATIK JAIN"); System.out.println("n Original String:"+s +"n"); System.out.println("Length of String:"+s.length()); for(int i=0;i<s.length();i++) { int p = i+1; System.out.println("Character at position"+ p + "is" + s.charAt(i)); }}}Pratik Jain(94972346366) Page 36
    • OUTPUTPratik Jain(94972346366) Page 37
    • Program to design an applet to draw a polygonimportjava.awt.*;importjava.applet.*;public class polygon extends Applet{ public void paint(Graphics g) { int x[]={100,150,200,160,300};int y[]={100,100,150,150,130};g.drawPolygon(x,y,5); }}<html><body><applet code="polygon.class" height=650 width=650></applet></body></html>Pratik Jain(94972346366) Page 38
    • OUTPUTPratik Jain(94972346366) Page 39
    • Program to display tables from 1 to 10 using 2d arrayclass table10{public static void main(String a[]) {inti,j;int t[][]=new int[10][10];for(i=1;i<=10;i++) {for(j=1;j<=10;j++) {t[i-1][j-1]=i*j;System.out.print(t[i-1][j-1]+"t"); }System.out.print("n"); } }}Pratik Jain(94972346366) Page 40
    • OUTPUTPratik Jain(94972346366) Page 41