Friday, 13 September 2013

Chapter 6 More About Classes and Libraries

CHAPTER-6
MORE ABOUT CLASSES AND LIBRARIES
Brief Summary of the Chapter:
In this chapter the way access of members of a class i.e. about access specifier will be discuss. Java include predefined classes in the form of packages which are also called Java class Library. Some of the used packages are: java.lang. java.util, java.io, java.swing. java.awt, java.applet etc.
Key points:
·   The public member of object are accessed through .(dot) operator.
·   The private members are accessible only inside their own class.
·   The protected members are accessible inside their own class, sub class and packages.
·   The default members are accessible inside their own class as well to classes in the same
     
package.
·   Related classes and interfaces are grouped together in the form of package. ·   Packages and class are imported through import command.
SOLVED QUESTIONS
1. Which keyword can protect a class in a package from accessibility by the classes outside the package?
(a)private (b)protected (c) final (d) None of these Ans: (d) None of these.
2. We would like to make a member of a class visible in all subclasses regardless of what package they are in. Which one of the following keywords would achieve this?
(a)private (b)protected (c) final   (d) public (e) None of these
Ans: (b) protected.
3. Which of the following keywords are used to control access to a class member?
(a) default (b) abstract (c)protected (d) interface (e) public.
Ans: (c) and (e) public
4. The public members of objects are accessed through which operator.
(a) arrow (b) dot   (c) this (d) none of these
Ans: (b) dot
5. The private members are accessible only inside their _______class.
(a) own (b) sub (c) super (d) none of these
Ans: (a) own
6. Which command is used to import packages and their classes?
(a) include (b) import (c) public (d) inline
Ans: (b) import
7.Which statement is used to create a package in Java?
(a) Class (b) super (c) this (d) package


                 Ans:(d) package
8. In Java, all strings are objects?
(a) True (b) False (c) don’t say
Ans: (a) True

9. What do you understand by Package in Java?
Ans: A group of classes is called package 
14.What are the access specifiers in Java? Expalin.
Ans: The Access Specifiers control access to members of class from / within Java Program. Java supports various Access Specifiers to control the accessibility of class members.
   
Private : A variable or method declared as private, may not be accessed outside of the class. Only
class member can access them, since they are private to others.
Protected: Protected members can be accessed by the class members and subclasses (derived
classes) and current package, but they are not accessible from beyond package or outside.
Public: Class members declared as public, are accessible to any other class i.e. everywhere , since they are public.
Package (default): If no any specifier is mentioned, default or friendly access is assumed. Class member may be accessed by any other Class members available in the same package, but not accessible by the other classes outside the package, even subclasses. 

Tuesday, 10 September 2013

Chapter 5 Revision Tour 3

CHAPTER-5
JAVA GUI PROGRAMMING REVISION TOUR - III [Methods etc.]
Brief Summary of the Chapter:
In this chapter concept related with Class, Objects, Constructors and methods are discussed. In Java method or function   is a sequence of some declaration and executable statements.
In Java, which is strictly Object-oriented, any action can take place through methods and methods have to be exist as a part of the class.
Key points:
·   Methods is a sequence of statements that carry out specific tasks. ·   Methods returns a value through return statement.
·   Class is a blue print for creating objects of a certain charactersticks. ·   Class contains fields and methods.
·   Classes created through keyword class.
·   Object is instance of a class created through new operator.
·     Constructor is method with the same name as of that class it is used to initialized object of
     
class.
·   Constructor can either be parameterized or non-parameterized. ·   The “this” keyword is used to refer to current object.

SOLVED QUESTIONS
1. In java, methods reside in __________.
(a) Function (b) Library (c) Classes (d) Object Ans: (c) Classes
2. The number and type of arguments of a method are known as _____________.
(a) Parameter list (b) Calling (c) Definition (d)None to these.
Ans: (a) Parameter list
3. The first line of method definition that tells about the type of return value along with number and type of arguments is called_____________.




(a) Class (b) Object (c) Prototype (d) Datatype Ans: (c) Prototype
4. A member method having the same name as that of its class is called______method.
(a) Destructor (b) Constructor (c) Object (d) Variable
Ans: (b) Constructor
5. A constructor method has__________return type.
(a) float (b) void (c) no (d) int
Ans: (c) no
6.   A_________constructor takes no arguments.
(a) Copy constructor (b) Non-Parameterized constructor (c) Parameterized constructor Ans: (b) Non-Parameterized constructor
7. A_________constructor creates objects through values passed to it.
(a) Copy constructor (b) Default constructor (c) Parameterized constructor Ans: (c) Parameterized constructor
8. The keyword_________refers to current object.
(a) void (b) goto (c) this (d) null
Ans: (c) this

9. Define a method. What is method prototype and signature?
Ans:A message to an object is a call to the object’s method requesting that it performs some specified action.
int absval(int a) {
return(a<0?-a:a);
}
The first line of the method definition is the prototype of the method i.e. the prototypes of method defined above is:
int absval(int a)
10. How are following passed in Java: (i) primitive types (ii) reference types? Ans: (i) By Value (ii) By reference
11. The String objects being reference types are passed by reference but changes, if any, are not reflected back to them. Why?
Ans: The String objects are immutable in Java, which   means once they are created, the cannot
changed. That is why, even though Strings are passed by reference, they cannot be changed.
12.At what time is the constructor method automatically invoked?
Ans: Every time an object is created, the constructor method is automatically invoked.
13. What are Composite and user defined data types?
Ans: The data types that are based on fundamental or primitive data types, are known as Composite Datatypes. Since these data types are created by users, these are also known as User Defined
Datatypes.
14. Can you refer to a class as a composite type/ user-defined type?
Ans: Yes, class is referred to as   a composite type/user defined type.
15.How is a constructor invoked?
Ans: A constructor is automatically called with a new operator in order to create a new object.
16. Which method of a class is invoked just once for an object? When? Ans: The constructor method.
It is invoked for initializing values of the object at the time of its creation.
17. Passing the address means call by value or call by reference? Ans: Call by reference.
18. What’s wrong with the following constructor definition for the class PlayInfo?
           
public void PlayInfo( int sticks)
{




nsticks = sticks;
}
Ans: A constructor cannot have a return type, not even void.
19. How many values can be returned from a method?
Ans: Only one value can be returned from a   method though a method can have multiple return statements but only one gets executed which is reached first and thus returns the value.
20. What do you understand by Class and Object?

Ans: The basic unit of OOP is the Class. It can be described as a blue print of Objects. In other words,
an Object is an instance of a class. A JAVA program may have various class definitions.
An Object is an entity having a unique Identity, characteristics (Properties) and Behavior (Methods).

22. What is the difference between instance and static variable?
Ans: Instance Variable- These data member are created for every object of the class i.e. replicated with objects.
Class variable (static)- These data members that is declared once for each class and all objects share these members. Only a single copy is maintained in
the memory. These are declared with static keyword.
23. What do you understand by constructor in OOP?
Ans: A Constructor is a member method of a class, used to initialize an Object, when it is created (instantiated).
24.What are the properties of Constructor?
Ans:   There are some properties of constructor:
·   A Constructor must have the same name as the class name and provides initial values to its
     
data members.
·   A constructor have no return type not even void.
·   JAVA automatically creates a constructor method, if it is not defined with default values.
25.What do you understand by methods? What are the advantages of methods?
Ans: Definition: A Method or function is sequence of statement which are written to perform a
specific job in the application. In Object Oriented Programming, Method represents the behavior of the object. A message can be thought as a call to an object’s method.
The following three advantages/reasons describes that why we use methods.
To cope with complexity:



When programs become more complex and big in size, it is best technique to fol
conquer” i.e. a complex problem is broken in to smaller and easier task, so that we can make it manageable. Some times it is also called Modularization.
Hiding Details:
Once a method is defined, it works like a Black-box and can be used when required, without concerning that “How it Works?”
Reusability of code:
Once a method is implemented, it can be invoked or called from anywhere in the program when needed i.e. Method can be reused. Even a packaged method may be used in
multiple applications. This saves our time and effort. Most of the method like Math.sqrt() is available as ready to use which can be used anywhere in the application.
26. How to define a method?
Ans: A method must be defined before its use. The method always exist in a class. A Java Program must contain a main() method from where program execution starts. The general form of defining method is as-

[Access specifier]<return_type> <method_name>(<parameter(s)>) {……………. ;
body of the method i.e. statement (s);
}
Access Specifier:
It specified the access type and may be public or protected or private.
Return Type:
Specifies the return data type like int, float etc. Void is used when nothing is to be returned.
Method Name:
Specified the name of method and must be a valid Java identifier.
Parameters List:
It is list of variable(s), also called Formal Parameter or Argument, which are used to catch the values when method is invoked. Also a method may have no parameters.
27.What are the way to pass values to methods in Java?
Ans: You can pass arguments (Actual parameters) to method (Formal Parameters) using valid data
types like int, float, byte, char, double, boolean etc. or Reference data type like Object and
Arrays.
A method can called in two ways -
Call by Value: In this method, the values of Actual parameters are copied to Formal parameters, so
any changes made with Formal parameters in Method’s body, will not reflected back in the calling
function.
The original value of Actual parameters is unchanged because the changes are made on copied value.
Call by Reference:
In Reference method, the changes made on the formal parameters are reflected back in the Actual
parameters of calling function because instead of values, a Reference (Address of Memory location) is passed.
In general, all primitive data types are passed by Value and all
Reference types (Object, Array) are passed by Reference..
28. Differentiate between constructor and method.
Ans: Though Constructor are member method of the class like other methods, but they are different from other method members-
Constructor creates (initializes) an Object where a method is a group of statements which are packaged to perform a specific job.
Constructor has no return type, even void also. Whereas method may have any return type including
void.
The Constructor has the same name as Class, but method may have any name except Class name.
It is called at the time of object creation, but a method can be called any time when required.



29. What is “this” keyword?
Ans: As you are aware that static data and method members of a class is kept in the memory in a
single copy only. All the object are created by their instance variables but shares the class variables (static) and member methods. 

Chapter 4 Revision Tour 2

CHAPTER-4
JAVA GUI PROGRAMMING REVISION TOUR - II [Swing Controls]
Brief Summary of the Chapter:
In this chapter we shall be revising the JAVA GUI programming concepts using Swing API through NetBeans IDE.Java GUI applications are created through RAD tools with   Classes, Object and
methods etc.
Key Points:
·   Swing API includes graphical components for building GUIs.
·   Swing components can be either container or non container component. ·   Swing provide seven different Layout manager.
·   Frame is a top level window with a title and a border, created through jFrame component of
     
Swing.
·     Common properties of buttons are: background, border, font, foreground, enabled, Horizontal
     
Alignment, Vertical Alignment.
·   Label control displays text, that the user can not changed directly. ·   Label is created through jLabel class component.
·   TextField is created through jTextField class component.
·   Password field takes input without showing it on the screen, created through jPasswordField
     
class component.
·   TextArea is multiline component to display or enter text, created through jTextArea class
     
component.
·   Checkbox is a rectangular area that can be chacked or unchecked created
     
class component.
SOLVED QUESTIONS
1.What does getPassword() on a password field return?
(a) a string (b) an integer (c) a character array. Ans: (c) a character array
2. Which of the following component is the best suited to accept the country of the user?
A.  List   B. Combo box C. Radio button   D. Check box
Ans: B. Combo box
3. What command do you need to write in actionPerformed() event handler of a button, in order to make it exit button?
a. System.out.println(); b. System.exit(0);   c. System.out.print() Ans: b. System.exit(0);
4.What method would you use, in order to simulate a button’s(namely Okbtn) click event, without any mouse activity from user’s side?
a. Okbtn.setText() b.Okbtn.getText()   c. Okbtn.doClick() Ans: Okbtn.doClick()
5. What would be the name of the event handler method in the ListSelection listener interface for a list namely CheckList to handle its item selections?
a. CheckListValueChanged()   b. getSelectedValue() c. clearSelection() Ans: a. CheckListValueChanged()
6. Which control displays text that the user cannot directly change or edit?
a.TextField   b. Checkbox   c. Combobox d. Label
Ans: d.Label

7.Which control provides basic text editing facility?
a.TextField   b. Checkbox   c. Combobox d. Label Ans: a. TexfField
8. Occurrence of an activity is called:
a. Function   b. Class   c. Object   d. Event
Ans: d.Event.
9. Which property is used to set the text of the Label?
a. font b.text c.name d. icon
Ans: b.text
10. The object containing the data to be exhibited by the combo box by which property.
a. editable b. model c.selectedIndex d.selectedItem
Ans: b. model
11.   What is GUI programming?
Ans: A GUI(Graphical User Interface) is an interface that uses pictures and other graphic entities along with text, to interact with user.
12. How is swing related to GUI programming?
Ans: We can create a GUI application on Java platform using Swing API (Appli Interface), which is part of Java Foundation Classes(JFC).
13. What is an event? What is event handler?
Ans: An event is occurrence of some activities either initiated by user or by the system. In order to react, you need to implement some Event handling system in your Application. Three things are important in Even Handling-
Event Source: It is the GUI component that generates the event, e.g. Button.
Event Handler or Event Listener: It is implemented as in the form of code. It receives and handles events through Listener Interface.
Event Object or Message: It is created when event occurs. It contains all the information about the event which includes Source of event and type of
event etc.
14. What is the default name of action event handler of a button namely TestBtn?
Ans: private void TestBtnActionPerfomed(java.awt.action.ActionEvent evt){   }.
15. What property would you set to assign access key to a button?
Ans: mnemonic property is used to assign access key or shortcut (Alt + Key).
16. Which method can programmatically performs the click action of a push button?
Ans: private void TestBtnActionPerfomed(java.awt.action.ActionEvent evt){   }.
17. Which property would you set the setting the password character as ‘$’? Ans:echoChar
18. Which method returns the password entered in a password field? Ans: char [] getPassword().
19. Which list property do you set for specifying the items for the list. Ans: model
20. Which method would you use to determine the index of selected item in a list? Ans: int getSelectedIndex().
21. Which method would you use to insert an item at specified index, in the list? Ans:   void setSelectedIndex( int index).
22. How you can determine whether 5th item in a list is selected or not? Ans: isSelectedIndex(4).
23. Which method you would use to insert ‘Hello’ at 10th position in the Text Area control. Ans:void insert(“Hello”, 9).
24. Which method you would like to use to insert an Icon (picture) on a Push Button. Ans: void setIcon(Icon).
25. Which property would you like to set to make a Combo box editable? Ans: editable.
26. What is Layout Manager? Name the layout managers offered by NetBeans?
Ans: Layout managers enable you to control the way in which visual components are arranged in GUI forms by determining the size and position of components within containers.




There are seven types of layout are available-
     
·   Flow Layout
          Grid Layout
          Card Layout
          Spring Layout
          Border Layout
          GridBag Layout
          Box Layout

27. Name three commonly used properties and methods of the following controls.
(a) text field (b) text area (c) Check Box
Ans: (a) Properties: text, font, editable. Methods:   void setText(), String getText(), void setEditable(boolean).
(b) Properties: enabled, editable, wrapStyleWord Methods: setText(), getText(), isEditable()
(c) Properties:font, text, selected . Methods: getText(), isEnabled(), isSelected().
28. What is dispose() used for ?
Ans: dispose() is used for hide and dispose of the frame when the user closes it. This removes the frame from the screen and frees up any resources used by it.
29. What is the difference between-
(a) Text field & Text area
(b) Text field &  password field
(c) Radio Button & Check Box
Ans: (a) A text field’s text property can hold single line of text unless it is an HTML text. While a text area’s text can hold any number of lines of text depending upon its rows property.
(b) Though a text field   and a password field can obtain a single line of text from the user, yet these are different.
A password field displays the obtained text in encrypted form on screen while text field displays the obtained text in unencrypted form.
(c) Radio Button: A jRadioButton control belongs to JRadioButton class of Swing controls. It is used to get choices from the user. It is grouped control, so that only one can be selected at a time among them. Radio Button works in group, so that they must be kept in a ButtonGroup container control like so that only one can be selected at the same time.
Some features of jRadioButton control are-
It can be used to input choices typed input to the application. Only one Radio button can be selected at a time.
They must be kept in a Button Group container control to form a group.
Check box: A jCheckBox control belongs to JCheckBox class of Swing controls. It indicates whether a particular condition is on or off. You can use Check boxes to give users true/false or yes/no options. Check Boxes may works independently to each other, so that any number of check boxes can be
selected at the same time.
Some features of jCheckBox control are-
It can be used to input True/False or Yes/No typed input to the application. Multiple check boxes can be selected at the same time.

30. What is the significance of following properties of a text area ?
(a) lineWrap (b) wrapStyleword
Ans: (a) Defines Wrapping featureenable/disable (b) Determines where line wrapping occurs. If true, the component attempts to wrap only at word boundaries. This property is ignored unless linewrap is set to true.
31. What is the significance of a button group? How do you create a button group?
Ans:   We must add a ButtonGroup control to the frame to group the check boxes by using Button Group property of the check box. By dragging buttongroup control from palette window.
32. What do you understand by focus?
Ans: A Focus is the ability to receive user input/response through Mouse or Keyboard. When object or control has focus, it can receive input from user.
An object or control can receive focus only if its enabled and visible property are set to true.
Most of the controls provides FOCUS_GAINED() and FOCUS_LOST() method in FocusEvent by the FocusListener. FOCUS_LOST() is generally used for validation of data.
    You can give focus to an object at run time by invoking the requestFocus() method in the code.
Ex. jTextBox2.requestFocus();
33. What is meant by scope of a variable?
Ans: In Java, a variable can be declared any where in the program but before using them.

The area of program within which a variable is accessible, known as its scope. A variable can be accessed within the block where it is declared.