NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Showing posts with label Java program. Show all posts
Showing posts with label Java program. Show all posts

Sunday, October 21, 2012

Java program to implement multiple inheritance


import java.io.*;
interface bookinfo
{          public void getbookdata()throws IOException;
            public void putbookdata();
}
interface basicinfo
{          public void getbasicdata()throws IOException;
            public void putbasicdata();
}
interface otherinfo
{          public void getotherdata()throws IOException;
            public void putotherdata();
}

class book implements bookinfo,basicinfo,otherinfo
{          String Bname,Bauthor, Blanguage,Bpublisher;
            int Bprice, Bcopies,Byear;
            BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

public void getbookdata()throws IOException
{          System.out.println("Enter the Book name:");
            Bname=br.readLine();
            System.out.println("Enter the author's name:");
            Bauthor=br.readLine();
}

public void putbookdata()
{ System.out.print(Bname+"\t"+Bauthor+"\t");
}

public void getbasicdata()throws IOException
{          System.out.println("Enter the language:");
            Blanguage=br.readLine();
           
            System.out.println("Enter the published year:");
            Byear=Integer.parseInt(br.readLine());
            System.out.println("Enter the price");
            Bprice=Integer.parseInt(br.readLine());
}

public void putbasicdata()
{          System.out.print(Blanguage+"\t\t"+Byear+"\t"+Bprice+"\t");
}

public void getotherdata()throws IOException
{          System.out.println("Enter the number of copies:");
            Bcopies=Integer.parseInt(br.readLine());
            System.out.println("Enter the publisher:");
            Bpublisher=br.readLine();
}


public void putotherdata()
{          System.out.print(Bcopies+"\t"+Bpublisher+"\t\t");
}
}

class interfaceEg
{
public static void main(String s[])throws IOException
            {
              book it[];
               BufferedReader br=new BufferedReader(new
               InputStreamReader(System.in));
               System.out.println("How much entries..?");
               int k=Integer.parseInt(br.readLine());
               it=new book[k];
               for(int i=0;i<k;i++)
               { it[i]=new book();
               }
             
                for(int i=0;i<k;i++)
               {
               int j=i+1;
               System.out.println("ENTER DETAILS OF THE BOOK :   "+j);
               it[i].getbookdata();
               it[i].getbasicdata();
               it[i].getotherdata();
               }
             
             System.out.println("\n\n***********-----------------Book
              details-----------------************\n");
              System.out.println("Book\tAuthor\tLanguage\tYear\tPrice\tCopies
             \tPublisher");
             System.out.println("............................................................................");
             for(int i=0;i<k;i++)
               {
                
               it[i].putbookdata();
               it[i].putbasicdata();
               it[i].putotherdata();
               }}}   

Java program to animate ball or moving ball using frame


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class movball extends Frame implements Runnable
{ int x,y;
  Thread t;
  boolean flag;
  public movball()
  { x=0;
    y=250;
    addWindowListener(new mwa(this));
    t=new Thread(this);
    t.start();
    flag=false;
  }
  public void run()
  { for(;;)
    { try
      { repaint();
        x++;
        Thread.sleep(100);
      }
      catch(InterruptedException e)
      {
      }
      if(flag)
        break;
    }
  }
  public void paint(Graphics g)
  { g.drawOval(x,y,30,30);
  }
  public static void main(String s[])
  { movball b=new movball();
    b.setSize(new Dimension(1000,600));
    b.setTitle("Running Ball");
    b.setVisible(true);
  }
}
class mwa extends WindowAdapter
{ movball a;
  public mwa(movball f)
  { a=f;
  }
  public void windowClosing(WindowEvent e)
  { a.flag=true;
    a.t=null;
    System.exit(0);
  }
}
Output

Java program to create user defined exception handling


import java.io.*;
class myException extends Exception
{ myException()
  { System.out.println("Error:Password too short");
  }
  myException(int n)
  { System.out.println("Error:Only adults can join");
  }
}
class user_exception
{ public static void main(String s[])throws IOException,myException
  { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    try
    { System.out.print("Enter user name : ");
      String n=br.readLine();
      System.out.print("Enter your password : ");
      String m=br.readLine();
      if(m.length() <6)
        throw new myException();
      System.out.print("Enter your age : ");
      int o=Integer.parseInt(br.readLine());
      if(o<18)
        throw new myException(o);
    }
    catch(Exception e)
    {    }
  }
}   
Output

Java program to illustrate swing concept


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swing extends JFrame implements ActionListener,ItemListener
{JComboBox combo=new JComboBox();
  JButton btnExit=new JButton("Exit");
  Container cp=getContentPane();
  Color c=cp.getBackground();
  public swing()
  {        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(null);
            combo.addItem("Reset to Default Color");
            combo.addItem("Set background color to red");
            combo.addItem("Set background color to green");
            combo.addItem("Set background color to blue");
            cp.add(combo);
            cp.add(btnExit);
            btnExit.setBounds(85,120,70,30);
            combo.setBounds(20,10,200,30);
            combo.addItemListener(this);
            btnExit.addActionListener(this);
  }
public void itemStateChanged(ItemEvent e)
  {        String s=(String)e.getItem();
            if(s.compareTo("Reset to Default Color")==0)
            cp.setBackground(c);
            if(s.compareTo("Set background color to red")==0)
            cp.setBackground(Color.red);
            if(s.compareTo("Set background color to green")==0)
            cp.setBackground(Color.green);
            if(s.compareTo("Set background color to blue")==0)
            cp.setBackground(Color.blue);
  }
public void actionPerformed(ActionEvent a)
  {        String s=a.getActionCommand();
            if(s.equals("Exit"))
            System.exit(0);
  }
public static void main(String[]args)
{
            swing cd=new swing();
            cd.setSize(new Dimension(250,200));
            cd.setTitle("A swing application");
            cd.setVisible(true);
   }
 }
Output

Java program to create student registration form


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class student extends Frame implements ActionListener
{String msg;
  Button b1=new Button("save");
  Label l11=new Label("Student details",Label.CENTER);
  Label l1=new Label("Name:",Label.LEFT);
  Label l2=new Label("age:",Label.LEFT);
  Label l3=new Label("Sex(M/F):",Label.LEFT);
  Label l4=new Label("Address:",Label.LEFT);
  Label l5=new Label("Course:",Label.LEFT);
  Label l6=new Label("Semester:",Label.LEFT);
  Label l7=new Label("",Label.RIGHT);
  TextField t1=new TextField();
  Choice c1=new Choice();
  CheckboxGroup cbg=new CheckboxGroup();
  Checkbox ck1=new Checkbox("Male",false,cbg);
  Checkbox ck2=new Checkbox("Female",false,cbg);
  TextArea t2=new TextArea("",180,90,TextArea.SCROLLBARS_VERTICAL_ONLY);
  Choice course=new Choice();
  Choice sem=new Choice();
  Choice age=new Choice();
public student()
 {addWindowListener(new myWindowAdapter());
  setBackground(Color.cyan);
  setForeground(Color.black);
  setLayout(null);
  add(l11);
  add(l1);
  add(l2);
  add(l3);
  add(l4);
  add(l5);
  add(l6);
  add(l7);
  add(t1);
  add(t2);
  add(ck1);
  add(ck2);
  add(course);
  add(sem);
  add(age);
  add(b1);
  b1.addActionListener(this);
  add(b1);
  course.add("BSc c.s");
  course.add("BSc maths");
  course.add("BSc physics");
  course.add("BA English");
  course.add("BCOM");
  sem.add("1");
  sem.add("2");
  sem.add("3");
  sem.add("4");
  sem.add("5");
  sem.add("6");
  age.add("17");
  age.add("18");
  age.add("19");
  age.add("20");
  age.add("21");
  l1.setBounds(25,65,90,20);
  l2.setBounds(25,90,90,20);
  l3.setBounds(25,120,90,20);
  l4.setBounds(25,185,90,20);
  l5.setBounds(25,260,90,20);
  l6.setBounds(25,290,90,20);
  l7.setBounds(25,260,90,20);
  l11.setBounds(10,40,280,20);
  t1.setBounds(120,65,170,20);
  t2.setBounds(120,185,170,60);
  ck1.setBounds(120,120,50,20);
  ck2.setBounds(170,120,60,20);
  course.setBounds(120,260,100,20);
  sem.setBounds(120,290,50,20);
  age.setBounds(120,90,50,20);
  b1.setBounds(120,350,50,30);
}
public void paint(Graphics g)
{g.drawString(msg,200,450);}
public void actionPerformed(ActionEvent ae)
{if(ae.getActionCommand().equals("save"))
  {msg="Student details saved!";
   setForeground(Color.red); }
}
public static void main(String g[])
{student stu=new student();
 stu.setSize(new Dimension(500,500));
 stu.setTitle("student registration");
 stu.setVisible(true);
}
}
 class myWindowAdapter extends WindowAdapter
{public void windowClosing(WindowEvent we)
 {
  System.exit(0);
 }
}
Output

Java program to implement single inheritance


import java.io.*;
  class employee
  {
    private String empname;
    private  int empcode;

  void read(String s,int p)
  {
    empname= s;
    empcode=p;
  }
 void display()
  {
    System.out.println("The employee name is :" +empname);
    System.out.println("The employee code is :"+empcode);
  }
 }

 class labourer extends employee
 
  { private String labourtype;
    private int laboursalary;

  void read1()throws IOException
 
    {
       BufferedReader br= new BufferedReader( new InputStreamReader (System.in));
       System.out.println("Enter the employee name:");
       String a=br.readLine();
       System.out.println("Enter the employee code :");
       int p= Integer.parseInt(br.readLine());
       read(a,p);
       System.out.println("Enter the labourtype :");
       labourtype = br.readLine();
       System.out.println("Enter the labour salary :");
       laboursalary=Integer.parseInt(br.readLine());
    }

   void display1()
 
    {
    
      display();
      System.out.println(" The labour type is : "+ labourtype);
      System.out.println(" The labour salary is :"+laboursalary);   }
 }
class single   
    { public static void main (String s[])throws IOException
      {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the number of employees");
        int n = Integer.parseInt(br.readLine());
        labourer l[]= new labourer[n];
        for(int i=0; i<n; i++)
        l[i]= new labourer();
        for(int i=0; i<n; i++)
        l[i] .read1();
        for(int i=0; i<n; i++)
      l[i].display1(); }
    }
Output

Java program to animated text or moving text


import java.applet.*;
import java.awt.*;
/*<applet code="runtext" width=450 height=400>
</applet>*/

public class runtext extends Applet implements Runnable
 {
   Thread t;
   int x,y;
  boolean stopflag;
            public void init()
             {
               setBackground(Color.black);
               setForeground(Color.white);
               x=120;y=130;
              
             }

            public void start()
              {
                stopflag=false;
                t=new Thread(this,"one");
                t.start();
                       
              }

            public void paint(Graphics g)
              {showStatus("name of college");
                g.drawString("SS College",x,y);
              }

            public void stop()
              {
                stopflag=true;
                t=null;
              }

            public void run()
              {
                for(; ;)
                        {
                          try
                            {
                                    y++;x++;
                                    repaint();
                                    Thread.sleep(50);
                                    if(stopflag) break;
                            }
                         catch(InterruptedException e)
                          {
                                    System.out.println(e);
                          }
                        }
             }
}
Output