class 11 12 practicals

Upload: subhash-varghese

Post on 05-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Class 11 12 Practicals

    1/14

    1 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    Create a GUI Form to do the following:

    1. Create a GUI to perform addition, multiplication, subtraction and division of two numbers using fourdifferent buttons for each operation and a single text box to display the result.

    2. Create a GUI to perform addition, multiplication, subtraction and division of two numbers using a singlebutton and four different text boxes to store the respective results.

    3. Create a GUI form to find simple interest. Enter Principal amount, Rate of Interest and time in threedifferent text boxes and calculate and display interest amount in a text box.

    4. Find square and cube of a given number.5. Enter a number and display its square root.6. Enter two numbers and display the result of First Number raise to power Second Number.7. Enter radius of a circle and find its area and circumference.8. Enter your name, class, section, address, phone number, city, state and pin in different text fields and

    display all these different values in a textarea control.9. Enter a name and display a message Hello and the name you entered in the textfield in a message

    box.10. Enter a name and create two buttons to convert that to Uppercase and Lowercase. Also create a

    button that will display the Length of the name you entered in a message box.11. Enter a number and reverse the sign of that number i.e. if the number is positive change it to negative

    and it it is a negative than change it to positive.12. Enter two numbers and display which of them is the biggest and the smallest. Also check for their

    equality.13. Enter 5 numbers and display the maximum and minimum of those numbers. Create two buttons, one to

    find maximum and other to find minimum.14. Enter a number between 1-7 and display the corresponding dayname using switch;.15. Enter a number between 1-12 and display the corresponding month name using if.16. Enter a number between 1-100 and display the grade based on the following criteria using if-else if

    17. Enter a number and check (Create 5 different BUTTONS to do the following)a. if it is an even number or an odd number.b. if it is a prime number or not.c. if it is a positive, negative or zero.d. if it is divisible by 5 or not.e. if it is an Armstrong number or not.

    18. Enter a number in a texbox and on clicking a button that number will be appended to a textarea controland text box will be blank. Another Button will show an Input Box and on clicking its OK button thenumber will be appended to the same textarea control.

    19. Input two strings in two text boxes (Create 8 different BUTTONS to do the following)a. and concat them and display the concated string in a third text box.b. and print whether they are same :Ignoring case Case sensitive using equals(),

    equalsIgnoreCase() and compareTo() methods.c. Print a character at position n of first stringd. Reverse the first String using StringBuilder Class.e. Find the length of first string

    20. Using for loop create a GUI Form to: (Create 14 different BUTTONS to do the following)a. Print 1st 10 numbers in a textarea control.

    b. Print 1st 10 numbers in reverse in a textarea control.c. Print 1st 10 even numbers in a textarea control.d. Print 1st 10 even numbers in reverse in a textarea control.e. Print 1st 10 odd numbers in a textarea controlf. Print 1st 10 odd numbers in reverse in a textarea control.g. Enter a number using Input box and print its table upto x10 in a textarea control.h. Enter a message using Input box and print it 10 times in a textarea control.i. Print characters from A to Z in a textarea control.

    j. Print all the armstrong numbers in a textarea control.k. Print 1st 10 numbers and their sum in a textarea control.l. Print 1st 10 even numbers and their sum in a textarea control.m. Print 1st 50 Prime numbers in a textarea control.

    n. Enter a number in a Input box and print its factorial in a textarea control.

    Marks Percentage Grade> = 90 A + +80 90 A +

    75 80 A60 75 B50 60 C40 50 D< 40 Fail

  • 7/31/2019 Class 11 12 Practicals

    2/14

    2 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    21. Using while Loop, enter a number in a textfield and : (Create 15 different BUTTONS to do the following)a. Display total digits it containb. Display sum of its individual digits.c. Reverse that number.d. Display maximum digit.e. Display minimum digit.f. Display second maximum digit.g. Display total number of even and odd digits.h. Display sum of its even digits.i. Display sum of its odd digits.

    j. Display sum of cube of its individual digits.k. Display sum of square of its individual digits.l. Increase each digit of the number by 1 and display it as a new number.m. Display n Fibonacci numbers in a textarea.n. Display Prime numbers upto n.o. Display n Prime Numbers.

    22. Input a string and : (Create 3 different BUTTONS to do the following)a. Count total vowels in it. It should also display the count of all the individual vowels. Make use of a

    method namely isVowel to check if the character is a vowel or not.b. Reverse that string without using StringBuffer or StringBuilder class. Also check if the string is a

    Palindrome or Not a Palindrome.23. Enter a starting value and an end value in two text boxes and (Create 6 different BUTTONS to do the

    following)a. Print a series between those two numbers and their sumb. Print all the even numbers between them as well as the sum of those even numbersc. Print all the odd numbers between them as well as their sum.d. Print all the prime numbers between them as well as their sum.e. Print a Fibonacci series between them and their sum.f. Interchange or swap values of both the textfield.

    In all the above cases the result should be displayed in a textarea control.

    24. Enter 5 numbers and find maximum, minimum and 2nd maximum of those numbers.25. Create a Panel control on the form and add three radio buttons namely Male, Female and Kid in it.

    Perform action so as to make only one radio button selectable in that panel. Now, display which radiobutton has been selected in a message box by:

    a. Creating a buttonb. By creating a common code for the actionPerformed event of those three radio buttons and

    name the method as custType26. Create a combo box and add Jaipur, Kota, Ajmer and Udaipur in it. Now, display the selected value

    and its index position in a message box by:a. Creating a buttonb. By creating a common code for the actionPerformed event of that combo box and name the

    method as CityName

    27. Input a year and display if it is a leap year or not.28. Write a program which on clicking a button will keep on asking for a number in an Input box until user

    enters -999 and then will display the sum and count of those numbers in a message box.29. Write a program to print the following series:

    1 1 *12 22 **123 333 ***1234 4444 ****

    30. Enter a name using Input Box. Now add that name in a textarea control ONLY if users click YES buttonin a Confirm Box. On Adding that name in a textarea control a message Success will appear in aMessage Box. The program will keep on prompting for a name using Input Box till user does not enter aBlank value in that box.

    31. Enter a string in a textbox anda. On clicking a button that string will be added into a combo box.b. On clicking a button, the selected value will be removed from the combo box after confirming it

    from the user using Confirm Box.32. Create a calculator using four radio buttons that will perform all the four basic mathematical operations.

    Write a common code for all the four radio buttons actionPerformed event. 33. Create a calculator using a List Box. Four mathematical operations will be displayed in a list box and on

    clicking an option in a list box, the result will be displayed in a text field. Make use of itsgetSelectedValue() method.

    34. Create a calculator using a Combo Box. Four mathematical operations will be displayed in a combobox and on clicking an option in a combo box, the result will be displayed in a text field. Make use of itsgetSelectedItem() method.

  • 7/31/2019 Class 11 12 Practicals

    3/14

    3 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    35. Design the following Java Form to calculate net payment to be made by a customer.

    There are four mode of payments : Cash,Credit Card, Debit Card and Cheque in a combo box.

    The modes Credit Cards, Debit Cards and Cheque is not applicable for kids. And so selecting thesemodes should disable Kid customer type.

    Ensure that ONLY numeric values are entered in tPr and tQty text fields. No editing should be allowed in gross, discount amount, additional discount and net text fields.

    When the form starts, Male radio button should be selected as the customer type.

    NOTE: The following discount is given for the gross amount greater than 1000 rs. Any bill less than 1000Rs will not get any discount.

    CUSTOMERTYPE

    PAYMENT MODE DISCOUNT %

    MALE CASH 2CREDIT CARD 5DEBIT CARD 10CHEQUE NIL

    FEMALE CASH 3CREDIT CARD 7DEBIT CARD 12CHEQUE NIL

    KID CASH 5CREDIT CARD NOT APPLDEBIT CARD NOT APPL

    CHEQUE NOT APPLThe customer gets additional discount of 2% if he/she is a member of the Mall.

    To make Male radio button selected when the form starts, type the following code just below theinitComponents() code as shown below:

    initComponents();rbM.setSelected(true);

    Now to ensure that ONLYnumeric values are entered in Price and Quantity text fields do the following: o To write a common KeyTyped event code for these two text fields.o Select these two text boxes:

    From the shortcut menu, select Properties. In that dialog box, click on the Events tab. Now click on the ellipsis () button in front of keyTyped option. In the Handler dialog box, click on Add button. Type a name for the Handler say numbers and click OK button. Now click on the drop down box in front of KeyTyped method and select the handler

    name you have just created i.e. numbers in our case. Now open the source window and there you will see the numbers method and then type

    the following piece of code:

    private void numbers(java.awt.event.KeyEvent evt) {char c=evt.getKeyChar();

    if( ! ( (c >= '0' && c

  • 7/31/2019 Class 11 12 Practicals

    4/14

    4 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    Now to disable Kid radio button when payment mode is Credit Card, Debit Card and Cheque, writethe following code for that combo boxs Action Performed event:

    private void cmbPaymentActionPerformed(java.awt.event.ActionEvent evt) {int n=cmbPayment.getSelectedIndex();

    // to disable Credit Card, Debit Card and Cheque if customer is KIDif(n>0) rbK.setEnabled(false);else rbK.setEnabled(true);

    }

    Now to write the code behind Calculate button:

    private void btnCalcActionPerformed(java.awt.event.ActionEvent evt) {double price=Double.parseDouble(tPr.getText());int qty=Integer.parseInt(tQty.getText());double gross=price*qty;double disc=0;double addldisc=0;

    // to get the selected string from the combo boxString modename=cmbPayment.getSelectedItem()+"";

    //or you can even get the index position of the selected modeint modeposition=cmbPayment.getSelectedIndex();

    // if customer is Maleif(rbM.isSelected()){

    if(gross>1000){ // here modename is used for comparisonif(modename.equalsIgnoreCase("cash")){ disc=gross*2/100 ; }if(modename.equalsIgnoreCase("credit card")) { disc=gross*5/100; }if(modename.equalsIgnoreCase("debit card")) { disc=gross*10/100; }

    }// end gross}//end male

    //if customer is Femaleif(rbF.isSelected()){

    if(gross>1000){ // here modeposition is used for comparisonif(modeposition==0){ disc=gross*3/100; }if(modeposition==1){ disc=gross*7/100; }if(modeposition==2){ disc=gross*12/100;}

    }//end gross}//end female

    //if customer is a Kidif(rbK.isSelected()){

    if(gross>1000){ // here modeposition is used for comparisonif(modeposition==0) { disc=gross*5/100; }

    }//end gross}//end kid

    if(chkMember.isSelected()){ addldisc=gross*2/100; }

    double totaldisc=disc+addldisc;double net=gross-totaldisc;

    tGross.setText(gross+"");tDisc.setText(disc+"");tAddlDisc.setText(addldisc+"");tNet.setText(net+"");

    }// end button calc

  • 7/31/2019 Class 11 12 Practicals

    5/14

    5 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    36.Create a form as shown below:

    It contains 2 panels namely panPer and panTemp. When the form loads both these panels are not visible. On clicking Permanent radio button, panPer get visible and other one invisible.

    On clicking Temporary radio button, panTemp get visible and other one invisible.

    The criterion for calculation is as given below :

    For Temporary Employee Salary is Rs. 250 per day.OverTime is Rs. 50 per overtime hour

    For Permanent Employee HRA is 10% of Basic SalaryCCA is Rs. 500

    OverTime allowance is Rs. 75 per overtime hour.

    Total Amount is the sum total of Salary Amount (TxtSalary) and OverTime

    Amount (TxtOverTime).

    private void opPerActionPerformed(java.awt.event.ActionEvent evt) {

    panPer.setVisible(true);

    panTemp.setVisible(false);

    }

    private void opTempActionPerformed(java.awt.event.ActionEvent evt) {

    panPer.setVisible(false);

    panTemp.setVisible(true);

    }

    private void txtBasicFocusLost(java.awt.event.FocusEvent evt) {int basic,hra;

    txtBasic

    txtOver

    txtHr

    txtCCA

    panPer(a panel)

    txtGross txtOvertime

    txtNet

    panTemp(a panel)

    txtGross txtOvertime

    txtNet

    txtWork

    txtOverTmp

  • 7/31/2019 Class 11 12 Practicals

    6/14

    6 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    try{

    basic=Integer.parseInt(txtBasic.getText());

    }catch(Exception e){

    System.out.println("Invalid Basic Amount");

    txtBasic.requestFocus();

    return;

    }

    hra=basic*10/100;

    txtHr.setText(""+hra);

    }

    private void txtOverFocusLost(java.awt.event.FocusEvent evt) {

    int overtime;

    try{

    overtime=Integer.parseInt(txtOver.getText());

    }catch(Exception e){

    System.out.println("Invalid Overtime Hours");

    txtOver.requestFocus();

    return;}

    }

    private void cmdCalculateActionPerformed(java.awt.event.ActionEvent evt) {

    int gross,net;

    int overtimeamt;

    if(panPer.isVisible()==true){

    int basic=Integer.parseInt(txtBasic.getText());

    int overtime=Integer.parseInt(txtOver.getText());

    int hra=Integer.parseInt(txtHr.getText());

    int cca=Integer.parseInt(txtCCA.getText());

    overtimeamt=overtime*75;

    gross=basic+hra+cca;

    net=gross+overtimeamt;

    txtGross.setText(""+gross);

    txtOvertime.setText(""+overtimeamt);

    txtNet.setText(""+net);

    }

    if(panTemp.isVisible()==true){

    int work=Integer.parseInt(txtWork.getText());

    int overtime=Integer.parseInt(txtOverTmp.getText());

    gross=(work*250);

    overtimeamt=overtime*50;

    net=gross+overtimeamt;

    txtGross.setText(""+gross);

    txtOvertime.setText(""+overtimeamt);

    txtNet.setText(""+net);

    }

    }

    private void txtWorkFocusLost(java.awt.event.FocusEvent evt) {int workdays;

    try{

    workdays=Integer.parseInt(txtWork.getText());

    }catch(Exception e){

    System.out.println("Invalid Number....");

    txtWork.requestFocus();

    return;

    }

    }

  • 7/31/2019 Class 11 12 Practicals

    7/14

    7 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    private void txtOverTmpFocusLost(java.awt.event.FocusEvent evt) {

    int overtime;

    try{

    overtime=Integer.parseInt(txtOverTmp.getText());

    }catch(Exception e){

    System.out.println("Invalid Overtime Hours");

    txtOverTmp.requestFocus();return;

    }

    LIST BOX: assuming that its name is jList1a. To add items in a list box at design time do the following:

    i. Select the List Box.

    ii. From its Properties dialog box, click on the ellipsis next to model.

    iii. A model window will appear. There type the values you want to display in the list box.

    b. To add the values that you type in a text box into the List box or say to add the values in

    the List Box at runtime do the following:

    i. Select the List Box.

    ii. From its Properties dialog box, click on the ellipsis next to model.

    iii. A model window will appear.

    iv. Click on the drop down box displayed at the top of the model window and select Custom code.

    v. In the new window a text box will appear. There type the model name as modLstCity. (It can

    be any name you want)

    vi. Now click on the source tab.

    vii. At the topmost line and just before the class definition begins add the line : import

    javax.swing.*;

    viii. At the bottom of the source window just above the variable declaration code type following :

    1. DefaultListModel modLstCity=new DefaultListModel();

    ix. Now on the button click event, type the following code:

    1. modLstCity.addElement(txt1.getText());

    c. To display the index number of the selected item in the list box do the following:

    i. Select the List Box.

    ii. From the shortcut menu select Events -> ListSelection > valueChanged and type the following

    code there:

    1. JOptionPane.showMessageDialog(null,jList1.getSelectedIndex());

    d. To display the selected item in the list box in another text box do the following:

    i. Select the List Box.

    ii. From the shortcut menu select Events -> ListSelection _> valueChanged and type the following

    code there:

    1. txt2.setText(+jList1.getSelectedValue());

    e. To search an entry in a list box do the following:

    i. Add elements in a list box.

    ii. Enter value to search in a text box namely txt.

    String txt=jTextFieldTxt.getText();

    int po=jList1.getNextMatch(txt,0,javax.swing.text.Position.Bias.Forward);

    if (po==-1)

    jLabelPosition.setText("Invalid entry");

    else jLabelPosition.setText("Position : "+po);

    f. To delete an entry from the list box do the following:

    i. Add elements in the list box.

    ii. Create a button Delete Item and add the following code:

    modLstCity.removeElementAt(jList1.getSelectedIndex());

  • 7/31/2019 Class 11 12 Practicals

    8/14

    8 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    g. To copy a selected item of a list box to another list box on clicking a button:

    i. Create a new list box. Name is jList2

    ii. Create its model as described above as modLstCity2

    iii. Add its DefaultListModel code in source window as explained above.

    iv. Create a button copy element and add the following code:

    modLstCity2.addElement(jList1.getSelectedValue());

    h. To insert an item before the selected item in a list box do the following:

    i. Add elements in the list box.

    ii. Create a button Insert element and add the following code:

    int pos=jList1.getSelectedIndex();

    String s=txt1.getText();

    modLstCity.insertElementAt(s, pos);

    2. Combo Boxes : assuming its name is jCombo1

    a. To add items in a combo box at design time do the following:

    i. Select the Combo Box.

    ii. From its Properties dialog box, click on the ellipsis next to model.

    iii. A model window will appear. There type the values you want to display in the combo box.

    b. To add the values that you type in a text box into the Combo box or say to add the values in the

    Combo Box at runtime do the following:

    i. Select the Combo Box.

    ii. From its Properties dialog box, click on the ellipsis next to mode l.

    iii. A model window will appear.

    iv. Click on the drop down box displayed at the top of the model window and select Custom code.

    v. In the new window a text box will appear. There type the model name as mod CmbState. (It

    can be any name you want)

    vi. Now click on the source tab.

    vii. At the topmost line and just before the class definition begins add the line : import

    javax.swing.*;

    viii. At the bottom of the source window just above the variable declaration code type following :

    1. DefaultComboBoxModel modCmbState=new DefaultComboBoxModel();

    ix. Add a button Add and type the following code :

    1. modCmbState.addElement(txt1.getText());

    c. To display the index number of the selected item in the combo box do the following:

    i. Add items in the Combo Box.

    ii. Select the Combo Box.

    iii. From the shortcut menu select Events -> Items > itemStateChanged and type the following

    code there:

    1. JOptionPane.showMessageDialog(null,jCombo1.getSelectedIndex());

    d. To display the selected item in the combo box in another text box do the following:

    i. Select the Combo Box.

    ii. From the shortcut menu select Events -> Items > itemStateChanged and type the following

    code there:

    1. txt2.setText(""+jCombo1.getSelectedItem());

    e. To search an entry in a combo box do the following:

    i. Add elements in a combo box.

    ii. Enter value to search in a text box namely txt2.

    iii. Create a button Search and add the following piece of code in it:

    String s=txt2.getText();

    jCombo1.setSelectedItem(s);

  • 7/31/2019 Class 11 12 Practicals

    9/14

    9 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    f. To delete an entry from the combo box do the following:

    i. Add elements in the combo box.

    ii. Create a button Delete Item and add the following code:

    modCmbState.removeElementAt(jCombo1.getSelectedIndex());

    g. To copy a selected item of a combo box to another combo box on clicking a button:

    i. Create a new combo box. Name is jCombo2

    ii. Create its model as described above as modCmbState2

    iii. Add its DefaultComboModel code in source window as explained above.

    iv. Create a button copy element and add the following code:

    modCmbState2.addElement(jCombo1.getSelectedItem());

    h. To insert an item before the selected item in a combo box do the following:

    i. Add elements in the combo box.

    ii. Create a button Insert element and add the following code:

    int pos=jCombo1.getSelectedIndex();

    String s=txt1.getText();

    modCmbState.insertElementAt(s, pos);

    Check Boxes : it returns only two values; true or false i.e. selected or not selected.

    1. To add a group of check box do the following:

    a. From the palette add 3 checkboxes on to the form

    b. To change their names, select it and from the shortcut menu select change variable name and give a

    name ex. chkInf, chkPhy and chkBio for our program.

    c. To change their displayed text, select it and from the shortcut menu select Edit Text and give a name

    ex. Informatics, Physics and Biology for our program.

    2. To display value of the check box which can be either true or false or say selected or not selected do the

    following:

    a. Select the check box chkInf

    b. From the shortcut menu, select Events -> Action -> actionPerformed and add the following code:

    boolean s=chkInf.isSelected();

    String caption=chkInf.getText();

    txt1.setText(caption + " is "+ s);

    c. Do (a) and (b) steps for other two check boxes namely chkPhy and chkBio and in the code just change

    chkInf to chkPhy and chkBio respectively.

    3. To display all the options of check boxes which are selected in a textarea namely ta on click of a button , add

    the following code to the button:

    ta.setText("The options Selected: \n");

    if(chkInf.isSelected()==true)

    ta.append(chkInf.getText() + " ");

    if(chkPhy.isSelected()==true)

    ta.append(chkPhy.getText()+" ");

    if(chkBio.isSelected()==true)

    ta.append(chkBio.getText()+" ");

  • 7/31/2019 Class 11 12 Practicals

    10/14

    10 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    4. To set all the check boxes value to false or unselect them all, do the following:

    a. Add a button titled Unselect All

    b. Add the following code to the button:

    chkInf.setSelected(false);

    chkPhy.setSelected(false);

    chkBio.setSelected(false);

    txt1.setText("");

    c.

    Radio Buttons/Option Buttons:In radiobutton we can have the only one decision. But checkbox we canhave mulitiple options. Based on the usage we choose these controls.

    for example,RadioButton-----------

    Gender -Male Female here we can select only one option

    CheckBox--------

    your Interested Games?

    cricket football valleyball hockey here you may have more than option.

    1. To add a group of radio buttons to ask for stream selected, Science, Commerce or Arts.

    a. Add three radio buttons on to the form.

    b. Change their names to opSc, opCom and opArts, by selecting Change Variable Name option from the

    shortcut menu.

    c. Change their displayed text to Science, Commerce and Arts by selecting Edit Text option from the

    shortcut menu.

    d. Now to have only one radio button selected out of these three do the following:i. From the palette drag component namely Button Group on the form. This component will not

    be visible on the form but it will be visible in the inspector window under Other Components

    heading. The default name will be buttonGroup1. Select this name , and from the shortcut

    menu select Change variable name option and change the name to grStream.

    ii. Now in the form, select all the three buttons together and open Properties dialog box from the

    shortcut menu.

    iii. Click the drop down next to buttonGroup option and select grStream from the dropdown and

    close the window.

    iv. You will see that all the three buttons are now

    connected together

    v. These steps will ensure that only one of these radio

    buttons is selected or you can say that these three

    buttons belong to a group namely grStream.

    On Clicking Unselect Allbutton

  • 7/31/2019 Class 11 12 Practicals

    11/14

    11 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    2. To display value of the radio button which can be either true or false or say selected or not selected do the

    following:

    a. Select the radio button opSc

    b. From the shortcut menu, select Events -> Action -> actionPerformed and add the following code:

    boolean s=opSc.isSelected();

    String caption=opSc.getText();

    txt1.setText(caption + " is "+ s);

    c. Do (a) and (b) steps for other two radio buttons namely opCom and opArts and in the code just change

    opSc to opCom and opArts respectively.

    3. To set all the radio buttons to false or unselected state, do the following:

    a. Add a button with title Unselect.

    b. Add the code : grStream.clearSelection(); // name of the buttonGroup

    4. Now, add three Panel container on the form. Add a border around each panel. Name them as panSc, panCom

    and panArts and add different labels in each panel as shown below:

    5. Now open the source window and add the

    following code just below the

    initComponents(); line. These line of codes

    set there visible property to false when the

    form loads:-

    initComponents();

    panSc.setVisible(false);

    panCom.setVisible(false);

    panArts.setVisible(false);

    6. Next step is, when we click opSc option button, panSc should be visible and when we click opCom option

    button, panCom should be visible and when we click opArts option button, panArts s hould be visible.

    7. Select opSc button and from the shortcut menu select Events -> Action -> actionPerformed and add the

    following code:

    panSc.setVisible(true);

    panCom.setVisible(false);

    panArts.setVisible(false);

    8. Select opCom button and from the shortcut menu select Events -> Action -> actionPerformed and add the

    following code:

    panSc.setVisible(false);

    panCom.setVisible(true);

    panArts.setVisible(false);

    9. Select opArts button and from the shortcut menu select Events -> Action -> actionPerformed and add the

    following code:

    panSc.setVisible(false);

    panCom.setVisible(false);

    panArts.setVisible(true);

  • 7/31/2019 Class 11 12 Practicals

    12/14

    12 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    10.

    BASIC CONCEPTS TO KNOW

    1. Null statements are also called empty statements. These statements contains only a semicolon andno other statements. Such statements are useful in creating time delay loops. These loops pausethe program for some time. E.g. long wait =0;

    while (++wait b){z=20;

    }else{

    z=10;}

    this code can be written with ?: as z=(a>b)? 20 : 10;But this can be complicated if we have too many nested ifs to check eg.

    if(a>b){If(a>c){

    m=a;} else {

    m=c;}}if(b>c){

    m=b;} else {

    m=c;}}

    with ?: above code will look like : m=(a>b: (a>c? a :c) : (b>c? b : c)); // this is little bit confusing..

    4. Conditional operators are feasible where conditions to check are minimal. In cases where there aretoo many conditions and nested conditions are to be checked, they may prove complicated andconfusing. Also the precedence of conditional operator is less than the other mathematicaloperators.

    Default dangling else matching can be overridden by making proper use of {} braces.

    5. Dangling else problem arises when in a nested if statements, number of ifs is more than thenumber of else clauses. The question then arises, with which if does the additional else clauseproperty match up. In such case , java matches an else with the nearest if clause.

    Eg. pg 236 q4 and q5

  • 7/31/2019 Class 11 12 Practicals

    13/14

    13 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    6. Switch and if-else statements can be used to select one of several alternatives.7. Break statement is used to avoid fall-through problem associated with switch statement. In fall-

    through the control falls to the matching case. When break statement is encountered in switchstatement, program execution jumps to the line of code following the switch statement i.e. outsidethe body of the switch statement.

    8. Switch can only test for equality whereas if can evaluate a relational or logical expression.If-else can handle ranges, whereas switch cannot. Each switch case label must be a single value.Switch case label must be a constant while if-else can be used when 2 or more variables are

    tested.Switch statements are more efficient while testing a value against a set of constants.

    9. In the absence of a break statement in switch, java will start executing the statements frommatching case and keep on executing statements for the following cases as well until either abreak statement is found or switch statement end is encountered. This is called fall-through.

    10. No two case labels in the same switch statement can have identical values.11. The default statement gets executed when no match is found in a switch statement.12. Iteration statements are those statements which repeats a particular set of statements a

    finite/infinite number of times based on a condition . E.g. for-next, while, do-while.13. A loop has 4 elements namely:

    o initialization expression: this is used to assign a starting value to the loop control variable.The initialization expression(s) is executed only once, at the start of the loop.

    o Test expression: this expression decides whether the loop body will be executed or not. If testexpression is true, the control enters the loop and executes the loop body statementsotherwise the control is transferred just after the loop.

    Entry controlled loop or Top-tested loop: here the test expression is evaluated firstbefore entering the loop. Chances are that the loop body may not be executed evenonce. E.g. for and while loop.

    Exit controlled loop or Bottom tested loop: here the test condition is evaluated at the

    end of the loop. This loop will be executed at least once. E.g. do-while loop.o Update expression(s): this will change the value of loop control variable. This is evaluated atthe end after the loop body is executed in the absence of this expression, the loop may beexecuted infinitely.

    o Body of the loop: these contains the statements that will be executed repeatedly until the testexpression evaluates to true.

    14. It is true that you can decrement the loop control variable. But in such a case, the start value of theloop specified as initial value must be greater than the end value specified in test expression.

    15. for(int i=51; i=1;--i) S.o.p(i);17. In a for loop, initialization expression(s), test expression and update expression are optional, i.e.

    you can do away with any or all of these expressions. Even if you skip these expressions, the blank

    semi colons(;) must be given.If a loop does not contain any statement in its loop body, it is said to be an empty loop. An emptyloop is used to run a time delay loop. E.g. for(i=1; i

  • 7/31/2019 Class 11 12 Practicals

    14/14

    14 | P a g e J a v a P r a c t i c a l s s u b h a s h v 1 2 2 4 @ g m a i l . c o m

    19. In the while loop, test expression is first evaluated and then the loop body is executed if thecondition is true. In case of do-while loop, the loop body is first executed and the test expression isevaluated at the end of the loop statement. Thus with do-while loop, the loop body will be executedat least once.

    20. The break statement terminates the smallest enclosing while, do-while, for and switch statement.Execution resumes at the statement immediately following the body of the terminated statement.

    break [label] causes the flow of control to break out of the containing statement which must belabelled [label].e.g outer:

    for(i=0; i