Saturday 24 September 2016

BANK ACCOUNT PROGRAM IN JAVA

import java.io.*;

class Curr_acct      //CURRENT ACCOUNT CLASS
{
         final int max_limit=20;
         final  int min_limit=1;
         final double min_bal=500;
         private  String name[]=new String[20];
         private int accNo[]=new int[20];
         private  String accType[]=new String[20];
         private double balAmt[]=new double[20];
         static int totRec=0;
       
        //Intializing Methodpublicvoid initialize()
        {
             for(int i=0;i<max_limit;i++)
             {
                name[i]="";
                accNo[i]=0;
                accType[i]="";
                balAmt[i]=0.0;
            }
        }

               
        //TO  ADD NEW RECORD
      public void newEntry()
        {
               String str;
               int acno;
               double amt;
               boolean permit;
                permit=true;

               if (totRec>max_limit)
               {
                    System.out.println("\n\n\nSorry we cannot admit you in our bank...\n\n\n");
                    permit=false;
               }

               if(permit = true)   //Allows to create new entry
               {
               totRec++;         // Incrementing Total Record              
               System.out.println("\n\n\n=====RECORDING NEW ENTRY=====");
               try{
                          accNo[totRec]=totRec;    //Created  AutoNumber  to accNo so no invalid id occurs
                        System.out.println("Account Number :  "+accNo[totRec]);
                       
                     BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
                     System.out.print("Enter Name :  ");
                     System.out.flush();
                     name[totRec]=obj.readLine();

                     accType[totRec]="Current Account";
                     System.out.println("Account Type : "+accType[totRec]);                    

                    do{
                           System.out.print("Enter Initial  Amount to be deposited : ");
                           System.out.flush();
                           str=obj.readLine();
                           balAmt[totRec]=Double.parseDouble(str);
                         }while(balAmt[totRec]<min_bal);      //Validation that minimun amount must be 500

                  System.out.println("\n\n\n");
                    }
                catch(Exception e)
                {}
            }
        }

       
       
        //TO DISPLAY DETAILS OF RECORD
public void display()
        {
              String str;
              int acno=0;
              boolean valid=true;
                         
              System.out.println("\n\n=====DISPLAYING DETAILS OF CUSTOMER=====\n");
              try{
                 BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
                 System.out.print("Enter Account number : ");
                 System.out.flush();
                 str=obj.readLine();
                 acno=Integer.parseInt(str);
                  if (acno<min_limit  || acno>totRec)   //To check whether accNo is valid or Not
                     {
                          System.out.println("\n\n\nInvalid Account Number \n\n");
                          valid=false;
                     }

                    if (valid==true)
                      {    
                        System.out.println("\n\nAccount Number : "+accNo[acno]);
                        System.out.println("Name : "+name[acno]);
                        System.out.println("Account Type : "+accType[acno]);
                        System.out.println("Balance Amount : "+balAmt[acno]+"\n\n\n");
                      }
                 }
            catch(Exception e)
            {}
        }



          //TO DEPOSIT AN AMOUNT
public void deposit()
        {
              String str;
              double amt;
              int acno;
              boolean valid=true;
              System.out.println("\n\n\n=====DEPOSIT AMOUNT=====");
             
              try{
                   //Reading deposit value
                   BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));

                        System.out.print("Enter Account No : ");
                        System.out.flush();
                        str=obj.readLine();
                        acno=Integer.parseInt(str);
                         if (acno<min_limit || acno>totRec)   //To check whether accNo is valid or Not
                         {
                              System.out.println("\n\n\nInvalid Account Number \n\n");
                              valid=false;
                         }
         
                        if (valid==true)
                       {
                            System.out.print("Enter Amount you want to Deposit  : ");
                            System.out.flush();
                            str=obj.readLine();
                            amt=Double.parseDouble(str);

                            balAmt[acno]=balAmt[acno]+amt;

                            //Displaying Depsit Details
                            System.out.println("\nAfter Updation...");
                            System.out.println("Account Number :  "+acno);
                            System.out.println("Balance Amount :  "+balAmt[acno]+"\n\n\n");
                        }
                 }
            catch(Exception e)
            {}
       }




     //TO WITHDRAW BALANCE
public void withdraw()
        {
              String str;
              double amt,checkamt,penalty;
              int acno;
              boolean valid=true;
              System.out.println("\n\n\n=====WITHDRAW AMOUNT=====");
             
              try{
                   //Reading deposit value
                   BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
                 
                        System.out.print("Enter Account No : ");
                        System.out.flush();
                        str=obj.readLine();
                        acno=Integer.parseInt(str);

                          if (acno<min_limit || acno>totRec)   //To check whether accNo is valid or Not
                             {
                                System.out.println("\n\n\nInvalid Account Number \n\n");
                                valid=false;
                            }

                        if (valid==true)
                        {
                                System.out.println("Balance is : "+balAmt[acno]);
                                System.out.print("Enter Amount you want to withdraw  : ");
                                System.out.flush();
                                str=obj.readLine();
                                amt=Double.parseDouble(str);

                                checkamt=balAmt[acno]-amt;

                                if(checkamt >= min_bal)
                                 {
                                    balAmt[acno]=checkamt;
                                    //Displaying Depsit Details
                                    System.out.println("\nAfter Updation...");
                                    System.out.println("Account Number :  "+acno);
                                    System.out.println("Balance Amount :  "+balAmt[acno]+"\n\n\n");
                                }
                                else
                                 {
                                    System.out.println("\n\nYour Balance has gone down and so penalty is calculated");
            //Bank policy is to charge 20% on total difference of balAmt and min_bal to be maintain


                                    penalty=((min_bal - checkamt)*20)/100;
                                    balAmt[acno]=balAmt[acno]-(amt+penalty);
                                    System.out.println("Now your balance revels : "+balAmt[acno]+"\n\n\n");
                                }
                        }
                 }
            catch(Exception e)
            {}
       }

}



class Sav_acct        //SAVING ACCOUNT CLASS
{
         final int max_limit=20;
         final  int min_limit=1;
         final double min_bal=500;
         private  String name[]=new String[20];
         private int accNo[]=new int[20];
         private  String accType[]=new String[20];
         private double balAmt[]=new double[20];
         static int totRec=0;
       
        //Intializing Methodpublicvoid initialize()
        {
             for(int i=0;i<max_limit;i++)
             {
                name[i]="";
                accNo[i]=0;
                accType[i]="";
                balAmt[i]=0.0;
            }
        }

       
       
        //TO  ADD NEW RECORD
public void newEntry()
        {
               String str;
               int acno;
               double amt;
               boolean permit;
                permit=true;

               if (totRec>max_limit)
               {
                    System.out.println("\n\n\nSorry we cannot admit you in our bank...\n\n\n");
                    permit=false;
               }

               if(permit = true)   //Allows to create new entry
               {
               totRec++;         // Incrementing Total Record              
               System.out.println("\n\n\n=====RECORDING NEW ENTRY=====");
               try{
                          accNo[totRec]=totRec;    //Created  AutoNumber  to accNo so no invalid id occurs
                        System.out.println("Account Number :  "+accNo[totRec]);
                       
                     BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
                     System.out.print("Enter Name :  ");
                     System.out.flush();
                     name[totRec]=obj.readLine();

                     accType[totRec]="Saving Account";  
                     System.out.println("Account Type : "+accType[totRec]);

                    do{
                           System.out.print("Enter Initial  Amount to be deposited : ");
                           System.out.flush();
                           str=obj.readLine();
                           balAmt[totRec]=Double.parseDouble(str);
                         }while(balAmt[totRec]<min_bal);      //Validation that minimun amount must be 500

                  System.out.println("\n\n\n");
                    }
                catch(Exception e)
                {}
            }
        }

       
       
        //TO DISPLAY DETAILS OF RECORD
public void display()
        {
              String str;
              int acno=0;
              boolean valid=true;
                         
              System.out.println("\n\n=====DISPLAYING DETAILS OF CUSTOMER=====\n");
              try{
                 BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
                 System.out.print("Enter Account number : ");
                 System.out.flush();
                 str=obj.readLine();
                 acno=Integer.parseInt(str);
                  if (acno<min_limit  || acno>totRec)   //To check whether accNo is valid or Not
                     {
                          System.out.println("\n\n\nInvalid Account Number \n\n");
                          valid=false;
                     }

                    if (valid==true)
                      {    
                        System.out.println("\n\nAccount Number : "+accNo[acno]);
                        System.out.println("Name : "+name[acno]);
                        System.out.println("Account Type : "+accType[acno]);
                       
                        //Bank policy is to give 10% interest on Net balance amt
                        balAmt[acno]=balAmt[acno]+(balAmt[acno]/10);
                        System.out.println("Balance Amount : "+balAmt[acno]+"\n\n\n");
                      }
                 }
            catch(Exception e)
            {}
        }



          //TO DEPOSIT AN AMOUNT
public void deposit()
        {
              String str;
              double amt;
              int acno;
              boolean valid=true;
              System.out.println("\n\n\n=====DEPOSIT AMOUNT=====");
             
              try{
                   //Reading deposit value
                   BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));

                        System.out.print("Enter Account No : ");
                        System.out.flush();
                        str=obj.readLine();
                        acno=Integer.parseInt(str);
                         if (acno<min_limit || acno>totRec)   //To check whether accNo is valid or Not
                         {
                              System.out.println("\n\n\nInvalid Account Number \n\n");
                              valid=false;
                         }
         
                        if (valid==true)
                       {
                            System.out.print("Enter Amount you want to Deposit  : ");
                            System.out.flush();
                            str=obj.readLine();
                            amt=Double.parseDouble(str);

                            balAmt[acno]=balAmt[acno]+amt;

                            //Displaying Depsit Details
                            System.out.println("\nAfter Updation...");
                            System.out.println("Account Number :  "+acno);
                            System.out.println("Balance Amount :  "+balAmt[acno]+"\n\n\n");
                        }
                 }
            catch(Exception e)
            {}
       }



     //TO WITHDRAW BALANCE
public void withdraw()
        {
              String str;
              double amt,checkamt;
              int acno;
              boolean valid=true;
              System.out.println("\n\n\n=====WITHDRAW AMOUNT=====");
             
              try{
                   //Reading deposit value
                   BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
                 
                        System.out.print("Enter Account No : ");
                        System.out.flush();
                        str=obj.readLine();
                        acno=Integer.parseInt(str);

                          if (acno<min_limit || acno>totRec)   //To check whether accNo is valid or Not
                             {
                                System.out.println("\n\n\nInvalid Account Number \n\n");
                                valid=false;
                            }

                        if (valid==true)
                        {
                                System.out.println("Balance is : "+balAmt[acno]);
                                System.out.print("Enter Amount you want to withdraw  : ");
                                System.out.flush();
                                str=obj.readLine();
                                amt=Double.parseDouble(str);

                                checkamt=balAmt[acno]-amt;

                                if(checkamt >= min_bal)
                                 {
                                    balAmt[acno]=checkamt;
                                    //Displaying Depsit Details
                                    System.out.println("\nAfter Updation...");
                                    System.out.println("Account Number :  "+acno);
                                    System.out.println("Balance Amount :  "+balAmt[acno]+"\n\n\n");
                                }
                                else
                                 {
     System.out.println("\n\nAs per Bank Rule you should maintain minimum balance of Rs 500\n\n\n");
                                }
                        }
                 }
            catch(Exception e)
            {}
       }
}



class  Bank_improved
{
    public static void main(String args[])
    {
        String str;
        int choice,check_acct=1,quit=0;
        choice=0;

         Curr_acct curr_obj = new Curr_acct();
         Sav_acct sav_obj = new Sav_acct();

         System.out.println("\n=====WELLCOME TO BANK DEMO PROJECT=====\n");


while( quit!=1)
{
        try{
           BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
           System.out.print("Type 1 for Current Account and Any no for Saving Account : ");
           System.out.flush();
           str=obj.readLine();
           check_acct=Integer.parseInt(str);
           }
           catch(Exception e) {}

    if(check_acct==1)
     {
        do//For Current Account
        {
        System.out.println("\n\nChoose Your Choices ...");
        System.out.println("1) New Record Entry ");
        System.out.println("2) Display Record Details ");
        System.out.println("3) Deposit...");
        System.out.println("4) Withdraw...");
        System.out.println("5) Quit");
         System.out.print("Enter your choice :  ");
        System.out.flush();
              try{
                   BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
                   str=obj.readLine();
                   choice=Integer.parseInt(str);

                          switch(choice)
                           {
                            case 1 :  //New Record Entry
                                            curr_obj.newEntry();
                                           break;
                            case 2 :  //Displaying Record Details
                                           curr_obj.display();
                                           break;
                            case 3 : //Deposit...
                                            curr_obj.deposit();
                                           break;
                            case 4 : //Withdraw...
                                            curr_obj.withdraw();
                                            break;
                            case 5  :  System.out.println("\n\n.....Closing Current Account.....");
                                            break;
                            default : System.out.println("\nInvalid Choice \n\n");
                          }
                    }
                catch(Exception e)
                {}
            }while(choice!=5);
    }
else
  {
      do//For Saving Account
        {
        System.out.println("Choose Your Choices ...");
        System.out.println("1) New Record Entry ");
        System.out.println("2) Display Record Details ");
        System.out.println("3) Deposit...");
        System.out.println("4) Withdraw...");
        System.out.println("5) Quit");
         System.out.print("Enter your choice :  ");
        System.out.flush();
              try{
                   BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
                   str=obj.readLine();
                   choice=Integer.parseInt(str);

                          switch(choice)
                           {
                            case 1 :  //New Record Entry
                                            sav_obj.newEntry();
                                           break;
                            case 2 :  //Displaying Record Details
                                           sav_obj.display();
                                           break;
                            case 3 : //Deposit...
                                            sav_obj.deposit();
                                           break;
                            case 4 : //Withdraw...
                                            sav_obj.withdraw();
                                            break;
                            case 5  :  System.out.println("\n\n.....Closing Saving Account.....");
                                            break;
                            default : System.out.println("\nInvalid Choice \n\n");
                          }
                    }
                catch(Exception e)
                {}
            }while(choice!=5);
      }

try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter 1 for Exit : ");
System.out.flush();
str=obj.readLine();
quit=Integer.parseInt(str);
}catch (Exception e){}
}
  }
}

OUTPUT:-

Horse Race GAME in java

import java.util.*;

class Horse{
public static void main(String[] args) throws InterruptedException {
    Scanner input = new Scanner(System.in);

    int[] tracks = new int[70];

    int bet;

    System.out.println("==============");
    System.out.println("||HORSE RACE||");
    System.out.println("==============");
    System.out.println("WHO'S GONNA WIN IN THIS EPIC RACE?");
    System.out.println("ENTER HOW MANY HORSES WOULD YOU LIKE TO JOIN:"
            + "\n 2-10 HORSES are allowed to join!");
    int horses;
    do {
        horses = input.nextInt();
    } while (horses < 2 || horses > 10);
    int[] move = new int[horses];
    double[] betHorse = new double[horses];

    System.out.println("Enter how many person will bet?");
    int number = input.nextInt();
    for (int i = 1; i <= number; i++) {
        do {
            for (int j = 1; j <= horses; j++) {
                System.out.println("[" + j + "]" + " for HORSE " + j);
            }
            System.out.println("Person no." + i + ": Enter the number of horse:");
            bet = input.nextInt();
        } while (bet < 1 || bet > horses);
        for (int p = 1; p <= horses; p++) {
            if (bet == p) {
                System.out.println("Enter the amount of your bet?");
                betHorse[bet - 1] += input.nextDouble();
            }
        }
        for (int j = 1; j <= horses; j++) {
            System.out.println("Bet for HORSE " + j + ":P" + betHorse[j - 1]);
        }
    }

    System.out.println("OKAY THAT'S SETTLED");
    System.out.println("Race begins in:");
    int num3 = 3;
    for (int i = 1; i <= num3; num3--) {
        System.out.println(num3);
        Thread.sleep(1000);
    }
    do {
        Thread.sleep(100);

        int[] numbers = new int[horses];
        for (int i = 0; i < horses; i++) {
            numbers[i] = 1 + (int) (Math.random() * 6);
        }
        for (int i = 0; i < horses; i++) {
            if (numbers[i] >= 1 && numbers[i] <= 3) {
                move[i]++;
            } else if (numbers[i] == 4 && numbers[i] == 5) {
                move[i] = move[i] + 3;
            } else if (numbers[i] == 6) {
                move[i] = move[i] + 5;
            }
        }
        System.out.println("\n\n\n");
        for (int i = 1; i <= horses; i++){
            System.out.println("Horse " + i +" position:" + move[i-1]);
        }
        for (int i = 1; i <= horses; i++) {
            for (int j = 0; j < move[i - 1]; j++) {
                System.out.print("--");
            }
            System.out.println(i + "H" + move[i - 1]);

        }

    } while (move[horses-1] < tracks.length );

    for (int i = 1; i <= horses; i++) {
        if (move[i - 1] > tracks.length) {
            System.out.println("HORSE " + i + " finished the track! One who bets for HORSE " + i + " won P" + betHorse[i - 1] * 2);
        }
    }

}
}

OUTPUT:-

Friday 9 September 2016

Set Multiple Selection Mode For AWT List Example

  1. /*
  2.         Set Multiple Selection Mode For AWT List Example
  3.         This java example shows how to set multiple selection mode for a list
  4.         using Java AWT List class.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Graphics;
  9. import java.awt.List;
  10.  
  11. /*
  12. <applet code="SetMultiSelectModeExample" width=200 height=200>
  13. </applet>
  14. */
  15. public class SetMultiSelectModeExample extends Applet{
  16.  
  17.         List list = null;
  18.        
  19.         public void init(){
  20.                
  21.                 //create a single selection list
  22.                 list = new List(5);
  23.                
  24.  
  25.                 //add items
  26.                 list.add("One");
  27.                 list.add("Two");
  28.                 list.add("Three");
  29.                 list.add("Four");
  30.                 list.add("Five");
  31.                 list.add("Six");
  32.                 list.add("Seven");
  33.                
  34.                 //add list
  35.                 add(list);
  36.                
  37.                 /*
  38.                  * To set multiple selection mode for a list, use
  39.                  * void setMultipleMode(boolean mode)
  40.                  * method.
  41.                  */
  42.                
  43.                 list.setMultipleMode(true);
  44.         }
  45.        
  46.         public void paint(Graphics g){
  47.                 g.drawString("Is Multiple Selection Allowed? " + list.isMultipleMode(), 10, 120);
  48.         }
  49. }

Get Selected Item Of AWT Choice Or Combobox Example

  1. /*
  2.         Get Selected Item Of AWT Choice Or Combobox Example
  3.         This java example shows how to get selected item of a choice or combobox
  4.         using Java AWT Choice class.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Choice;
  9. import java.awt.Graphics;
  10. import java.awt.event.ItemEvent;
  11. import java.awt.event.ItemListener;
  12.  
  13. /*
  14. <applet code="GetSelectedItemExample" width=200 height=200>
  15. </applet>
  16. */
  17.  
  18. public class GetSelectedItemExample extends Applet implements ItemListener{
  19.  
  20.         Choice language = null;
  21.        
  22.         public void init(){
  23.                
  24.                 //create choice or combobox
  25.                 language = new Choice();
  26.                
  27.                 //add items to the choice
  28.                 language.add("Java");
  29.                 language.add("C++");
  30.                 language.add("VB");
  31.                 language.add("Perl");
  32.                
  33.                 //add choice or combobox
  34.                 add(language);
  35.                
  36.                 //add item listener
  37.                 language.addItemListener(this);
  38.                
  39.         }
  40.        
  41.         public void paint(Graphics g){
  42.                 /*
  43.                  * To get selected item, use
  44.                  * String getSelectedItem()
  45.                  * method of AWT Choice class.
  46.                  */
  47.                 g.drawString(language.getSelectedItem(),10, 70);
  48.         }
  49.  
  50.         public void itemStateChanged(ItemEvent arg0) {
  51.                 repaint();             
  52.         }
  53. }

Set Action Command For AWT Button Example

  1. /*
  2.         Set Action Command For AWT Button Example
  3.         This java example shows how to set custom action command for AWT button using
  4.         setActionCommand method of Java AWT Button class.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Button;
  9. import java.awt.Graphics;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12.  
  13. /*
  14. <applet code="ButtonSetActionCommandExample" width=200 height=200>
  15. </applet>
  16. */
  17.  
  18. public class ButtonSetActionCommandExample extends Applet implements ActionListener{
  19.  
  20.         String actionMessage="";
  21.        
  22.         public void init(){
  23.                 //create Button
  24.                 Button Button1 = new Button("I agree with the terms and conditions");
  25.                                
  26.                 //add Button
  27.                 add(Button1);
  28.                                
  29.                 //set action listeners for buttons
  30.                 Button1.addActionListener(this);
  31.                
  32.                 /*
  33.                  * By default, button's action command is it's label. But in
  34.                  * some cases, labels are too long and is not appropriate to use
  35.                  * it as an action command. In such situation you would want to
  36.                  * define custom short action command for a button.
  37.                  *
  38.                  * To set custom action command for a button, use
  39.                  * void setActionCommand(String command)
  40.                  * method of AWT Button class.
  41.                  */
  42.                
  43.                 Button1.setActionCommand("Agree");
  44.         }
  45.        
  46.         public void paint(Graphics g){
  47.                 g.drawString(actionMessage,10,50);
  48.         }
  49.        
  50.         public void actionPerformed(ActionEvent ae){
  51.                
  52.                 /*
  53.                  * Get the action command using
  54.                  * String getActionCommand() method.
  55.                  */
  56.                
  57.                 String action = ae.getActionCommand();
  58.                 actionMessage = action + " button pressed!";
  59.        
  60.                 repaint();
  61.         }
  62. }

Handle Action Events for AWT Button Example

  1. /*
  2.         Handle Action Events for AWT Button Example
  3.         This java example shows how to handle action event of AWT Button by implementing
  4.         ActionListener interface.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Button;
  9. import java.awt.Graphics;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12.  
  13. /*
  14. <applet code="HandleActionEventExample" width=200 height=200>
  15. </applet>
  16. */
  17.  
  18. public class HandleActionEventExample extends Applet implements ActionListener{
  19.  
  20.         String actionMessage="";
  21.        
  22.         public void init(){
  23.                 //create Buttons
  24.                 Button Button1 = new Button("Ok");
  25.                 Button Button2 = new Button("Cancel");
  26.                
  27.                 //add Buttons
  28.                 add(Button1);
  29.                 add(Button2);
  30.                
  31.                 //set action listeners for buttons
  32.                 Button1.addActionListener(this);
  33.                 Button2.addActionListener(this);
  34.         }
  35.        
  36.         public void paint(Graphics g){
  37.                 g.drawString(actionMessage,10,50);
  38.         }
  39.        
  40.         public void actionPerformed(ActionEvent ae){
  41.                
  42.                 /*
  43.                  * Get the action command using
  44.                  * String getActionCommand() method.
  45.                  */
  46.                
  47.                 String action = ae.getActionCommand();
  48.                
  49.                 if(action.equals("Ok"))
  50.                         actionMessage = "Ok Button Pressed";
  51.                 else if(action.equals("Cancel"))
  52.                         actionMessage = "Cancel Button Pressed";
  53.                
  54.                 repaint();
  55.         }
  56. }
     

Get Selected Radio Button Example

  1. /*
  2.         Get Selected Radio Button Example
  3.         This java example shows how to get selected radio button using
  4.         Java AWT CheckboxGroup class.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Checkbox;
  9. import java.awt.CheckboxGroup;
  10. import java.awt.Graphics;
  11. import java.awt.event.ItemEvent;
  12. import java.awt.event.ItemListener;
  13.  
  14.  
  15. /*
  16. <applet code="GetSelectedRadioButtonExample" width=200 height=200>
  17. </applet>
  18. */
  19.  
  20.  
  21. public class GetSelectedRadioButtonExample extends Applet implementsItemListener{
  22.        
  23.         CheckboxGroup lngGrp = null;
  24.        
  25.         public void init(){
  26.                
  27.                 //create group
  28.                 lngGrp = new CheckboxGroup();
  29.                
  30.                 //create checkboxes and add to group
  31.                 Checkbox java = new Checkbox("Java", lngGrp, true);
  32.                 Checkbox cpp = new Checkbox("C++", lngGrp, false);
  33.                 Checkbox vb = new Checkbox("VB", lngGrp, false);
  34.                
  35.                 //add radio buttons
  36.                 add(java);
  37.                 add(cpp);
  38.                 add(vb);
  39.                
  40.                 //add listeners
  41.                 java.addItemListener(this);
  42.                 cpp.addItemListener(this);
  43.                 vb.addItemListener(this);
  44.         }
  45.        
  46.        
  47.         public void itemStateChanged(ItemEvent ie) {
  48.                 repaint();
  49.         }
  50.        
  51.         public void paint(Graphics g){
  52.                
  53.                 /*
  54.                  * To get selected radio button, use
  55.                  * Checkbox getSelectedCheckbox()
  56.                  * method of CheckboxGroup class.
  57.                  */
  58.                
  59.                 Checkbox chk = lngGrp.getSelectedCheckbox();
  60.                
  61.                 g.drawString(chk.getLabel() + " is selected"10 ,70);
  62.         }
  63. }