Thursday, 7 May 2015

Unit 2 Java GUI Programming

INFORMATICS PRACTICES
CLASS XII
UNIT II
PROGRAMMING (JAVA GUI PROGRAMMING)

Q1 What is RAD?
A1. Rapid Application Development describes a method of developing software through the use of pre-programmed tools or wizard. The pre-programmed tools or controls are simply dropped on the screen to visually design the interface of application.

Q2 What is event driven programming?
A2. This programming style responds to the user events and is driven by the occurrence of user-events.

Q3 What is object oriented programming?
A3. This programming style emphasizes upon objects; an object is an identifiable entity with some characteristics and behavior.

Q4. What are event, message, properties and what is the relationship between them?
A4. Events are activities that take place either due to user interaction or due to some internal changes. E.g. User clicks upon a command button or text of a textbox changes owing to a calculation.
A message is the information / request sent to an application about the occurrence of an event.
Properties are characteristics of an object that controls its appearance and behavior.
Relationship – properties can be thought of as an object attributes methods as its actions and events as its responses.

Q5. What are containers? Give e.g.?
A5. Containers are those controls that can hold other controls inside them e.g. frame (JFrame), panel (JPanel), label (JLabel) e.t.c are containers.

Q6. Name the character set supported by java.
A6. Unicode

Q7. What are keywords can keywords be used as identifiers?
A7. Keywords are the words that convey special meaning to a language compiler.
These are reserved for special purposes and must not be used as normal identifiers names.

Q8. What is an identifier?
A8. Identifiers are fundamental building blocks of a program and are used as the general terminology for the names given to different parts of the program viz.variables, objects, classes, functions, arrays e.t.c

Q9 what are literals? How many types of integer literals are available in java?
A9. Literals (often referred as constants) are data items that are fixed data values. Java allows several kinds of literals:
  • integer-literals
  • floating-literals
  • Boolean  literals
  • Character-literals
  • String-literal
  • The null literal

Q10.How many types of integer’s constants are allowed in java: how are they written?
A10.Java allows three types of integer laterals:
1)decimal(base10).these are the integer literals that begin with a non – zero digit.eg,1234.
2)octal (base8)these are the integer literals that begin with a zero eg,to write octal no. 24 ,you need to write :024.
3)hexadecimal  (base 16).these are the integer literal that begin with 0x or 0X eg,to write hexadecimal no. 2AF9,you need to write:0x2AF9.

Q11.what will be the result of a = 5/3 if a is 1)float    2)int?
A11.1)1.0     2)1

Q12.The expression 8%3 evaluates to___.
A12.  2

Q13.What will be the value of j = -- k + 2*k + l++
            If k is 20 and l is 10 initially?
A13. 67, because
            j=19+2*19+10
             = 19+38+10=67
Q14.Name the eight primitive Java types.
A14. The eight primitive types are:
1)byte        2)char
3)short       4)int
5)long        6)float
7)double    8)Boolean
Q15. Which package is always imported by default?
A15. The java.lang package is always imported by deafault.

Q16. To what value is a variable of a string type automatically initialized?
A16. The default value of the string type is NULL.

Q17. To what value is a variable of the bollean type automatically initialized?
A17. The default value of the boolean type is FALSE.

Q18. What is casting? When do we needed?
A18. The casting is a form of conversion which uses the cast operator to specify the type name in parenthesis and is placed    in front of the value to be converted.
Example: Result= (Float) total/count:
They are  helpful in situations where we temporarily need to treat the value as another type.

Q19. What is a literal?
A19. A literal is a sequence of characters used in a program to represent a constant value.
For example: A is a  literal that represents the value A , of type char and 17L is a literal that represents a number 17 as a value of type long. A literal is a way of writing a value.

Q20. What is the significance of the break statement in  a switch statement?
A20. In a switch  statement after matching case’s code is executed, it stops only when it     
         encounters break or closing brace(}) of switch.
So break statement acts as terminating statement for match case in a switch statement.

Q21. What is the purpose of break statement in a loop?
A21. In a loop the break statement terminates the loop when it gets executed.

Q22. What is the effect of absence of break in a switch statement?
A22. If we do not give break after each case then java will start executing  the statement for matching case and keep on executing statements for following cases as well, until either a break statement is found or switch statements end is encounter.

Q23. What is the significance of default clause in a switch statement ?
Or
        What is the purpose of default clause in a switch statement?
A23. The default statement gets executed when no match is found.

Q24. What is meant by an entry controlled loop? Which java loops are entry controlled?
A24. Loop which evaluates its test expression at the beginning of the loop before executing its loop body statements is called entry controlled loop. While loop is an entry controlled loop.

Q25. How many times will each of the following loop will execute? Which one of these is an entry controlled loop and which one is an exit controlled loop?
A25.
Loop1:
Loop2 :
int sum 0, i = 5 ;
int sum 0 , i  5 ;
do
While ( i<5) ;
{ sum i ; i ; ) while (1<5);
{ sum i; i }

A25. Loop 1 will execute once and Loop 2 will execute 0 times.
Loop1 is exit controlled loop and Loop2 is entry controlled loop.

Q26. Write a for loop that  displays the number from 51 to 60.
A26. int i =0;
                        for ( i=51;i<=60; i++)
                        {
                        system.out.print(i +” “);
                        }         

Q27. Write a for loop that displays a number from 10 to 1 i.e., 10,9,8……..3,2,1
A27. int i ;
            for( i=10; i>=1 ; i--)
            {
                        system.out.print(i + “  “);
            }

Q28. How is swing related to GUI programming?
A28. Swing API includes a rich set of graphical components for building GUIs and 
         adding interactivity to Java Applications. So, whenever GUI programming is done  
         in Java it is done through swing API of Java.

Q29. Name two containers for each of the following categories:
i)                    top level
ii)                  middle level
iii)                component level
A29.i) Top level containers
                        JFrame, JDialog
        ii) Middle level containers
                        JPanel, JTabbedPane
        iii)Component level containers
                        JButton,JLabel

Q30. What is an event? What is an event handler?
A30. An event is occurrence of some activity either initiated by user or internally by   
         system.
Event handler of Event Listener. It is implemented in form of code. It receives and handles events, eg, when a button gets clicked, some value is computed and displayed for user’s reference.

Q31. What is a layout manager?
A31.  Layout managers enable to control the way in which visual components are
arranged in GUI forms by determining the size and position of components within containers.

Q32. What is the default name of action event handler of a button namely TestBtn?
A32. TestBtnActionPerformed is the default name of action event handler of a button “TestBtn”.

Q33. What is a label used for?
A33. A label control (created through JLabel class type components) displays text and /or image that user can not directly change or edit.

Q34. What command do you need to write in action performed () event handler of a button in order to make it exit button.
A34. Syatem.exit(0);

Q35. What methods would you use in order to simulate buttons (namely OKBtn) click event, without any mouse activity from users side?
A35. OKBtn. Doclick();

Q36. A label by default allows only one line of text to be displayed . can you displayed multiple
lines of text in a label? If so how ?
A36. Yes, we can display multiple lines of text with in a label by making use of HTML text. For this we will have to start the text properties value with <HTML> and then we can use <BR> or  <P> tags to force the line breaks.

Q37. You have assigned a foreground color and a background color through foreground and background properties of a label. But the label is not showing any backgroubd colour what could be the reason?
A37. The label is filled with background color only if the property opaque is set to TRUE.The default settings for opaque is false. If the label is not showing an
Y background color , the reason might be that its opaque property is set to false.

Q38. What is the difference between text properties of text field and a text area?
A38. A text fields a text property can hold single line of text unless it is an HTML text. A text areas text property can hold any number of line of text depending upon its rows property.

Q39.What is the difference between a text field and a text area?
A39. A text field allows a user to enter a single line of text, where as a text area is editing an area for blocks or multiple lines of text. It allows user to enter multi – lines text. Text area can be constructed from JTextArea  components class and text field can be constructed from JTextField component class.

Q40. What is the difference between a getText( ) and getPassword( ) methods?
A40. The getPassword() method of password field returns the text displayed by the field.
                        The getText() method returns the text disaplayed in the text field , label, button
and other components.

Q41. How can you displayed multiple lines of uneditable text?
A41. Through a text area we can display multiple lines of text and to make it uneditable we can set its editable property to FALSE.

Q42.Which event get fired when :
i)                    User presses enter in a text field.
ii)                  User presses enter in a password field
iii)                A check box is clicked
iv)                A radio button has clicked.
v)                  A key is typed from keyboard
A42.    i)          Action event
            ii)         Action event
iii)                Item event
iv)                Item Event
v)                  Key Typed Event

Q43 If a user presses enter key, while still being with in a JTextField object what happens?
A43. Action event generated.

Q44. Which controls allow text entry in them?
A44. Controls that allows text entry in them are : textfield, passwordfield and text area.

Q45. Which property would you set for setting the password character as “$” ?
A45. To set password character as $ , we will set the echoChar property of the password field to $.

Q46. Ms Sangeeta has developed  the java application through which the students of her school can view their marks by entering their admission number. The marks are displayed in various text fields. What should she do so that the students are able to view  but not change their marks in text field.?
A46. She should make the text field showing marks not editable
or
       She should deselect the editable property of corresponding text boxes.

Q47. Name the swing API classes that create
i)                    A list
ii)                   A combo Box
A47. i)  List     - JList
         ii) Combo Box               - JComboBox

Q48. Which list property do you set for specifying the contents of the list?
A48. The model property of the list is used to specify the contents of the list.

Q49. Which method would you use to determine which index has been selected in the list?
A49 int getselectedindex ( )

Q50. How would you determine whether 7th item of a list namely mylist is selected   or not?
A50. If method
            mylist.isselectedindex( 6) returns TRUE, then the 7th item is selected otherwise not.

Q51. You want to clear the selection in the list namely chklist. How would you do this?
A51. By using method
            chklist.clearselection( ) ;

Q52. What would be the name of the event handler that would handle the list selection event of a list namely worklist?
A52. worklistvaluechanged() 

Q53. How would you ensure that in a list
i)                    only a single item get selected
ii)                  only a single range of items get selected
iii)                multiple ranges of item get selected
A53.  i) setselectionmode property to SINGLE
         ii) setselectionmode property to SINGLE _INTERVAL
         iii) setselectionmode propertyto MULTIPLE_INTERVAL

Q54. While working in net beans, Rjmeeta included a list box in the font. Now she wants the list of her friends names to be displayed in it. Which property of list box control should she used to do this?
A54. Model

Q55. What is a default list model?
A55. List model

Q56. Is a combo box by default editable? If not, then how would you make it editable.
A56. By default, the text field of a combo box  is uneditable , but you can change it to be editable by setting its editable property to TRUE . Eg. Checked

Q57. How would you obtain selected item from a combo box?
A57. By using method
             object   getselecteditem()

Q58. How would you determine whether a combo box is editable or not?
A58 Method Boolean is editable () , indicates whether the combo box is text field is editable or not.

Q59. What event does a Jlist fired when user select an item? What event does a JList use?
A59. A Jlist files list selection event when user makes a selection. The event listener is used by a Jlist is listselectionlistener
 
Q60. What would be the name of event handler method in the list selection listener interface for  a list namely check list to handle its item selection?
A60. Checklistvaluechanged ()

Q61. A list namely my list has selection mode property to set to single_interval. How would you obtain
i)                    the indices of selected value?
ii)                  The selected values?
A61.    i)          By using method
mylist.getselectedindices()
ii)         By using method
mylist.getselectedvalues()

Q62. By default a combo box does not offer editing features. How would you make  a combo box editable?
A62. By setting the editable property of a combo box we can make it editable.

Q63. What method controls the vertical position of the text related to an image in a label?
A63. Setverticaltextposition()

Q64. Which swing component provides a pop up list?
A64. jcombobox component

Q65. Which property ensures that the combo box is editable?
A65. Editable property when set to true( i.e. ischecked) ensures that the combo box is editable.

Q66. How are following passed in java:
i)                    primitive type
ii)                  reference type
A66     i)          by value
            ii)         by reference

Q67. How is a constructor invoke?
A67. A constructor is invoked automatically when a new object is being created
                        In other words a constructor is automatically  called with a new operator in order
to create a new object.

Q68. Which method of a class is invoked just once for an object ?when?
A68. The constructor method.
                        It is invoked for initializing values of the object at the of its creation.

Q69. Whats wrong with the following constructor definition for the class playinfo?
            public void playinfo(int sticks)
            {
                        nsticks= sticks;
            }
A69. A constructor can not have  a return type not even void.

Q70. What will be the scope of
i)                    a public member?
ii)                  a protected member?
iii)                a default member?
iv)                a private member?
A70.    i)          a public specifier donates a variable or method as being directly assessable
from all other classes.
ii)         a protected specifier donates a variable or methods as being public to sub
classes of this class but private to all other classes outside the current package.
iii)                Members with package access are available to all classes in the same package,  but are not available to any other classes but not even sub classes
iv)                The private access specifier donates a variable or method as being private to the class and may not be access outside the class . Accessing will be done by calling one of the public class method.

Q71. Name the package you need to import for performing data base connectivity.
A71. java.sql

Q72. Which commands creates package in java?
A72. Package in java is created by package command i.e., package name, on top of the source file.

Q73. Define inheritance. What is the inheritance mechanism in java?
A73. Inheritance is the capability of one class to derive properties from another class.
                        Inheritance in java is implemented by enabling a class to derive properties from another class by using keyword “extends” at the time of class definition.

Q74. Name various forms of inheritance.
A74.    i)          Single Inheritance
            ii)         Multiple Inheritance
iii)                Multilevel Inheritance
iv)                Hierarchical Inheritance
v)                  Hybrid Inheritance

Q75. What type of inheritance does java have?
A75. Java supports only these inheritance types:
            i)          Single Inheritance
            ii)         Multiple Inheritance
iii)        Hybrid Inheritance

Q76.  How do you prevent a sub class from having access to a member of super class?
 A76. To prevent a sub class from having access to a super class member, declare that member as private.

Q77. What is an abstract class?
A77. An abstract class is the one that simply represents a concept and whose objects can not be created.

Q78. How do you prevent a method from being overridden? How do you prevent a class from being inherited?
A78. To prevent a method from being overridden, declare it is final. To prevent a class from being inherited, declare it as final.   

Q79. Assume a class Derv derived from a base class Base. Both classes contain a member function func( ) that takes no arguments.Write the definition for a member function of Derv that calls both the func()s.
A79.void call ( )
            { func( ) ;
                        // call to member function of derv super.func( );
                        // it will call Base’s member function
            }

Q80. What is dialog?
A80. A dialog is a small separate window that appears to either provide or request information to/ from the user.

Q81. What all classes can you use to create dialogs in Java? Name them.
A81. I) JDialog. Main dialg class ( general purpose dialog)

Q82. Rewrite the following statement using Switch:
                        if(ch==’E’)
                                    eastern++;
if(ch==’W’)
                                    western++;
if(ch==’N’)
                                    northern++;
if(ch==’S’)
                                    southern++;
                        else
                                    unknown++;
A82. switch(ch){
                        case ‘E’ : eastern++;
                                          Break;
                        case ‘W’ : western++;
                                          Break;
case ‘N’ : northern++;
                                          Break;
case ‘S’ : southern++;
                                          Break;
                        default: unknown++;
                        }
Q83. How many times are the following loops executed?
a)      x=5 ; y=50;                        b)         int m=10,n=7;
while(x<=y) {                               while(m%n>=0) {
x=y/x;                                            m=m+1;
}                                                    n=n+2;
                                                      }
A83. a) Infinite number of times since y/x will always be less than y.
        b) Infinite number of times since mod operation always gives a number >=0.

Q84. Given the following code fragment:
            i= 2;
            do {
                        system.out.println(i);
                        i +=2;
                  }
            while(i < 51);
                        JOptionPane.showMessageDialog(null, “Thank You”);
            Rewrite the above code using while loop.
A84. i=2;
            while(i<51) {
                        system.out.println(i);
                        i+2=2;     
}
            JOptionPane.showMessageDialog(null, “Thank You”);

Q85. Given the following code fragment:
            i= 100;
            while(i>0)
                        system.out.println(i--);
                        JOptionPane.showMessageDialog(null, “Thank You”);
            Rewrite the above code using do.while loop.
A85. i=100;
            do {
                        system.out.println(i--);
                  } while(i>0);
            JOptionPane.showMessageDialog(null, “Thank You”);

Q86. Rewrite the following while loop into a for loop:
            int stripes=0;
            while (stripes<=13)
{
                        if(stripes % 2 ==2)
                                    {          system.out.println(“Colors code RED”);
                        }
                        else {
                                    system.out.println(“Colors code BLUE”);
                        } system.out.println(“NEW STRIPE”);
                        stripes=stripes + 1;
            }
A86. for  (int stripes =0;stripes <=13;stripes++)
            {
                        if(stripes % 2 ==2)
                        {
                        system.out.println(“Colors code RED”);
                        }
else
                        {          system.out.println(“Colors code BLUE”);
                        }
            system.out.println(“NEW STRIPE”);
            }

Q87. In the code fragment given below, what happens when choice equals 5?
            num= 0 , num2 =0;
            if(choice ==1) {
                        num=11;
                        num2=110;
            }
            Else if (choice==2){
                        num=20;
                        num2=220;
            }
JOptionPane.showmessageDialog(null,”num=”+num+”,num2=” +num2);
A87 The message dialog box will show the value as :
            Num=0 , num2= 0

Q88. What value is assigned to num  in the following code fragment when choice is equal to 2?
            switch(choice){
            case1 : num =1;
                        break;
case2 : num =2;
            case3 : num =3;
                        break;
default: num =0;
            }         
A88 Value is 3
Q89. What will be the value produced by following code fragment:
            float x=9;
            float y=5;
            int z= (int) (x/y);
            switch(z) {
                        case 1 : x = x+2;
                        case 2 : x = x+3;
                        case 3 : x = x+1;
            }
            system.out.println(“value of x= “  + x);
A 89. value of x =15

Q90. What is explicit and implicit type conversion?
A90. An implicit type conversion is performed by the compiler without programmer’s intervention. It is applied generally whenever differing data types are intermixed in an expression, so as not to lose information.
            An explicit conversion is a user defined that forces an expression to be of specific type. For eg. , to make sure that the expression (x +y )/2 evaluates to type float, write it as:  (float) (x+y) /2
Q91. What is type casting and type cast operator?
A91. The explicit conversion of an operand of a specific type is called type casting.
Syntax   :     (type) expression
            Where type is a valid java data type and is called type cast operator.
            For eg.
                        float x=3.1;
                        int i = (int) x;

Q92. Write the output of the following code fragments:
a)      int i , j , n  ;
n=0,i=1;
do
{          n++; i++;
} while (i<=5;
 Output : No Output
b)       int i=1 , j=0 , n=0  ;
while (i<4)
{          for(j=1;j<=i;j++)
            {          n+=1;
            }i =i +1;
}system.out.println(n);
Output : 6
c)      int j=1 ,s=0  ;
while (j<10)
{          system.out.println(j + “ + “);
            s=s+j;
            j= j + j %3;
}
system.out.println(“=” +s);
Output : 1 + 2+4 +5 +7 + 8 + = 27
d)     int i=3 , n=0  ;
while (i<4)
{n++ , i-- ;
}system.out.println(n);
Output : infinite loop ; no output

Q93. Find out the errors in the following, if any :
a)      m=1;
n=0;
for(; m+n <19 ; ++n)
system.out.println(“hello\n”);
m=m+10;
Output: No Error
b)      while (ctr != 10); {
ctr= 1;
      sum = sum +a;
      ctr= ctr +1;
      }
Output : No syntactical error as such. But the test expression of while loop is followed by semicolon (;). Hence it is an empty loop.
c)      for  ( a=1,a>10;a=a+1)
{ ………………
}
Output : Initialization and test expression are separated by comma(,) . This should be replaced by a semicolon (;) i.e. as :
for  ( a=1;a>10;a=a+1)
{ ………………
}

      Q94. Identify the errors in the following code that is written in action event handler of a
               button namely OKBtn.
  double d = nameTF.getText();
  string age = ageTF.getText();
 double marks = Double.parseDouble (marksTF.getText());
       A94. In a line 1 name TF.getText () will return a string, which has been assigned to double    
               d. String obtained has to be parsed to double before assignment.
   Thus the first code should be:
            double d = Doublr.parseDouble(nameTF.getText());
       Q95. What are these methods used for :
i)                    isEditable() à it returns the setting(true/false) whether the user can edit the text in the text area.
ii)                  setEditable () à Sets whether the user can edit the text in the text area.
iii)                getEchoChar() à It returns the echo character i.e. the character that will replace the text entered in the password field.
       Q96. What are events? What major events are associated with the following :
i)                    Text Field        à Action event
ii)                  Password Field  à Action Event
iii)                Check Box  à Item Event
iv)                Radio Button   à Item Event
      A96. An event is an object that gets generated when user does something such as mouse
              click, dragging, pressing a key on the keyboard etc.
    
      Q97. Write code of item event handler of a check box( namely incCB) that increments a
               variable total and displays in a label (namely count) if the check box is selected.
      A97. incCBItemStateChanged method
            if (incCB.isSelected())
                        { total=total +1 ;
                        count.setText( “ “ + total);
                        }
    
      Q98.Write code for the event handler of a radio button so that when it is selected/ unselected,
             its text changes to “ I am selected “ or “I am unselected “.
      A98. RDItemStateChanged Method 
                        if (RD.isSelected())
                                    RD.setText(“ I am selected”);
                        Else
                                    RD.setText(“ I am unselected”);
     Q99. Compare and contrast a list box and a combo box.
     A99. A list is graphical control that displays a list of items in a box where from the     
   user can make selections.It is different from combo box in many ways:
i)                    A list box doesn’t have a text field to do editing, whereas the combo box have.
ii)                  In a list the user must select the items directly from the list , whereas in a combo box user can do editing if requires.
    
    Q100 Define a method and its prototype.
    A100. A method/ function is a sequence of statements that carry out specific task(s). The first
              line of method definition is the prototype of the method.
  Example:
            int absval( int a)
            and static int maximum ( int a , int b)

     Q101. What are actual and formal parameters of a method?
     A101. The parameters that appear in method definition are called formal parameters and the
                parameters that appear in the method call statement are called actual parameters.
    For example:
            int mult( int x, int y)
            {
                        return x * y ;
            }
            void test {
                        int length =10;
                        int width =5;
                        int area= mult (length, width); }
In above example x,y are formal parameters and length, width are actual parameters.
       Q102.Write the difference between call by value and call by reference.
       A102. In call by value, the called method creates its own work copy for the passed  
                  parameters and copies the passed values in it.Any changes that take place remain in  
                  the work copy and the original data remains intact.
            In the call by reference, the called method receives the reference to the passed    
      parameters and through this reference, it accesses the original data. Any changes that    
      take place are reflected in the original data.
       Q103. What is constructor and how it get invoked?
       A103. Job of initializing an object with some legal values is carried out by the constructor  
                  of the class the object belongs to. It is automatically invoked every time when an  
                  object is created.
      For example:
            Sample obj1= new Sample();
          Q104. How are parameterized constructors different from non-parameterized   
                     constructors?
          A104. A constructor taking no arguments is a non parameterized default constructor.
         A constructor receiving arguments is known as parameterized constructor.
         With non- parameterized constructor, objects are created just the same way as   
         variables of other data types are created.
         For example:
            X O1 =new X();                      // NON- PARAMETERIZED CONSTRUCTOR
            Y O1= new Y (5);                  // PARAMETERIZED CONSTRUCTOR
           Q105. How are private members different from public members of the class?
           A105. Private members are private to the class and cannot be accessed outside the class.  
                      But it can be called by the public members of the class.
                  Where as public members can be accessed directly by all other classes. They
          are used to access the private members of the class. 
Q106. How protected members are are different from private and public members of the
           class?
A106. Protected members can be directly accessed by all the classes in the same
           package, as that of the class I which the member is and sub classes of other
           package. Whereas private members cannot be accessed outside the class, even in
           sub classes of the class and public members can be directly accessed by all other
           classes.
 Q107.How does class enforce information hiding?
A107. Class enforces information hiding by using various access specifiers. Access
           specifiers control access to members of a class from within a java program. The
           access  levels supported by java are : private, public, protected and default.
           Depending upon the access level of a member of a class, access to it is denied or
           allowed.
Q108. What is package? How do we tell java that we want to use a particular package in  
            a file?
A108. When a largish program is to be written, it is often advantageous to divide the
           program up into chunks(modules) and compile the parts separately. This option is
           particularly desirable when several programmers are jointly developing a large
           program. Such modules of code are called packages in java.
                     To use all the classes and interfaces from a package, for instance, the entire
           graphics package, use the import statement with the * wild card character.
Q109. How do we design a package?
A109. Package can be created by using the package statement as shown in following
           example:
           
Package graphics;     //graphics is a name of package
                                    class circle {   
………                       // circle and rectangle are members of graphics package
}
                                    class rectangle {
……..             }
Q110. How the visibility mode does controls the access of members in the derived class?
           Explain with examples.
A110. Visibility modes are:
i)                    Private : These are accessible only inside the class
ii)                  Public : These are accessible in all the classes
iii)                Protected: These are accessible in any class in the same package.
iv)                Default : These are declared without any access specifiers. These are accessible only inside classes that are in same package.
v)                  Private Protected : These are accessible only from sub classes (whether in same package or any other package)
Q111. A class One with data members int a and char b inherits from another class Two
with data members float f and int x. Write definitions for One and Two for the following situations:
i)                    Objects of both the classes are able to access all data members of both the classes.
ii)                  Only the members of class one can access all the data members of both the classes.
A 111. i) public class Two
                        {
                        public float f ;
                        public int x;
                        }
                public class One extends Two
                        {
                        public int a ;
                        public char b;
                        }
ii) public class Two
                        {
                        protected float f ;
                        protected int x;
                        }
                public class One extends Two
                        {
                        private  int a ;
                        private char b;
                        }

            Q112.Write code fragment to obtain a number from user and display whether it is odd or  
                      even. Make use of dialogs only.
            A112. int  num=0;
                        num= Integer.parseInt(JOptionPane.showInputDialog(“Enter the number:”));
                        if (num % 2==0)
                                    JOptionPane.showInputDialog(null,” Entered number is even”);
                        else

                                    JOptionPane.showInputDialog(null,“Entered number is odd“);

No comments:

Post a Comment