CBSE Class 12 Informatics Practices Java GUI Programming Revision Tour
CBSE Class 12 Informatics Practices Java GUI Programming Revision Tour
COM
10.
11.
12.
m
o
.c
CHAPTER-4
y
a
d
o
t
s
In this chapter we shall be revising the JAVA GUI programming concepts using Swing API through
e
i
d
NetBeans IDE.Java GUI applications are created through RAD tools with Classes, Object and
u
t
s
.
methods etc.
Key Points:
w
w
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.
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 through jCheckBox
class component.
SOLVED QUESTIONS
m
o
.c
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()
o
t
s
y
a
d
e
i
d
6. Which control displays text that the user cannot directly change or edit?
a.TextField b. Checkbox c. Combobox d. Label
Ans: d.Label
u
t
s
.
w
w
Ans: We can create a GUI application on Java platform using Swing API (Application Programming
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 HandlingEvent 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).
m
o
.c
16. Which method can programmatically performs the click action of a push button?
Ans: private void TestBtnActionPerfomed(java.awt.action.ActionEvent evt){ }.
y
a
d
17. Which property would you set the setting the password character as $?
o
t
s
Ans:echoChar
e
i
d
u
t
s
.
19. Which list property do you set for specifying the items for the list.
w
w
Ans: model
20. Which method would you use to determine the index of selected item in a list?
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.
Downloaded from WWW.STUDIESTODAY.COM
m
o
.c
frame from the screen and frees up any resources used by it.
29. What is the difference between-
y
a
d
o
t
s
e
i
d
Ans: (a) A text fields text property can hold single line of text unless it is an HTML text. While a text
u
t
s
.
areas 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.
w
w
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 ?
Downloaded from WWW.STUDIESTODAY.COM
m
o
.c
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.
{
int x=10;
if (a>b)
{ int y=5;
Scope of x and y
}
else
{ int z=3;
.
Scope of z
}
.
}
o
t
s
e
i
d
u
t
s
.
y
a
d
w
w
34. Create a Java Desktop Application to find the incentive (%) of Sales for a Sales Person on the
basis of following feedbacks:
Feedback
Incentive (%)
Maximum Sales
10
Excellent Customer Feedback
8
Maximum Count Customer
5
Note: that the sales entry should not be space.Calculate the total incentive as :Sales amount* Incentive.
The feedback will be implemented in JCheckBox controls.Using a JButtons (Compute Incentive)
click event handler,display the total incentives in a JTextField control. Assume the nomenclature of
the swing components of your own.
Note that the JFrame from IDE window will be shown as given:
u
t
s
.
y
a
d
o
t
s
e
i
d
m
o
.c
w
w
35. Assume the following interface built using Netbeans used for bill calculation of a ice-cream
parlor. The parlor offers three verities of ice-cream vanilla, strawberry, chocolate. Vanilla icecream
costs Rs. 30, Strawberry Rs. 35 and Chocolate Rs. 50. A customer can chose one or
more ice-creams, with quantities more than one for each of the variety chosen. To calculate the
bill parlor manager selects the appropriate check boxes according to the verities of ice-cream
chosen by the customer and enter their respective quantities.
Write Java code for the following:
a. On the click event of the button Calculate, the application finds and displays the total bill of the
customer. It first displays the rate of various ice-creams in the respective text fields. If a user doesnt
select a check box, the respective ice-cream rate must become zero. The bill is calculated by
multiplying the various quantities with their respective rate and later adding them all.
b. On the Click event of the clear button all the text fields and the check boxes get cleared.
c. On the click event of the close button the application gets closed.
Ans: (a)
private void jBtncalculateActionPerformed(java.awt.event.ActionEvent evt)
{
if(jchkStrawberry.isSelected()==true)
jTxtPriceStrawberry.setText("35");
else
{
jTxtPriceStrawberry.setText("0");
jTxtQtyStrawberry.setText("0");
}
if(jChkChocolate.isSelected()==true)
jTxtPriceChocolate.setText("50");
else
{
jTxtPriceChocolate.setText("0");
jTxtQtyChocolate.setText("0");
}
if(jChkVinella.isSelected()==true)
jtxtPriceVinella.setText("30");
else
{
jtxtPriceVinella.setText("0");
jTxtQtyVinella.setText("0");
}
int r1,r2,r3,q1,q2,q3,a1,a2,a3,gt;
r1=Integer.parseInt(jTxtPriceStrawberry.getText());
r2=Integer.parseInt(jTxtPriceChocolate.getText());
r3=Integer.parseInt(jtxtPriceVinella.getText());
q1=Integer.parseInt(jTxtQtyStrawberry.getText());
q2=Integer.parseInt(jTxtQtyChocolate.getText());
q3=Integer.parseInt(jTxtQtyVinella.getText());
a1=r1*q1;
jTxtAmtStrawberry.setText(""+a1);
a2=r2*q2;
jTxtAmtChocolate.setText(""+a2);
a3=r3*q3;
jTxtAmtVinella.setText(""+a3);
gt=a1+a2+a3;
jTxtTotalAmt.setText(""+gt);
}
u
t
s
.
y
a
d
o
t
s
e
i
d
m
o
.c
w
w
Ans.(b)
private void jBtnClearActionPerformed(java.awt.event.ActionEvent evt)
{
jTxtPriceStrawberry.setText("");
jTxtPriceChocolate.setText("");
jtxtPriceVinella.setText("");
jTxtQtyStrawberry.setText("");
jTxtQtyChocolate.setText("");
jTxtQtyVinella.setText("");
jTxtAmtStrawberry.setText("");
jTxtAmtChocolate.setText("");
jTxtAmtVinella.setText("");
jchkStrawberry.setSelected(false);
jChkChocolate.setSelected(false);
jChkVinella.setSelected(false);
}
Ans: (c)
private void jBtncloseActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
m
o
.c
y
a
d
36. Read the following case study and answer the questions that follow.
o
t
s
The School is having two categories of employees : Teaching and Non Teaching. The Teaching
e
i
d
employees are further categorized into PGTs, TGTs and PRTs having different Basic salary.
u
t
s
.
The School gives addition pay of 3000 for employees who are working for more than 10 years.
w
w
Employee Type
Basic
DA (% of Basic Sal)
Salary
Deductions
(% of Basic sal)
Non Teaching
12500
31
30
12
PGT
14500
30
30
12
TGT
12500
21
30
12
PRT
11500
20
25
12
(a) Write the code to calculate the Basic salary, deductions, gross salary and net salary based on the
given specification. Add 3000 to net salary if employee is working for more than 10 years.
u
t
s
.
y
a
d
o
t
s
e
i
d
m
o
.c
w
w
Ans:(b)
System.exit(0);
Ans:(c)
tfgross.setEditable(false);
tfded.setEditable(false);
tfnet.setEditable(false);
Downloaded from WWW.STUDIESTODAY.COM
37. ABC School uses the following interface built in java to check the eligibility of a student for a
particular stream from science, commerce and humanities. The user first enters the total percentage
and selects the desired stream by selecting the appropriate option button. An
additional 5% is marks is given to students of NCC. Write Java Code for the following
a. On Action event of the button Calc Percentage Net percentage of the student is
calculated and displayed in the appropriate text filed. Net percentage is same as that of
the actual percentage if the student doesnt opts for NCC otherwise 5% is added to actual
percentage.
b. On Action event of the button Result, the application checks the eligibility of the
students. And display result in the appropriate text field. Minimum percentage for science
is 70, 60 for commerce and 40 for humanities.
c. On the Click event of the clear button all the text fields and the check boxes get cleared.
d. On the click event of the close button the application gets closed.
u
t
s
.
y
a
d
o
t
s
e
i
d
m
o
.c
Ans:
a.
private void jBtnCalcPerActionPerformed(java.awt.event.ActionEvent evt)
{
int p;
p=Integer.parseInt(jTextField2.getText());
if (jCheckBox1.isSelected())
p=p+5;
jTextField3.setText(Integer.toString(p));
}
w
w
b.
private void jBtnResultActionPerformed(java.awt.event.ActionEvent evt)
{
int p;
p=Integer.parseInt(jTextField3.getText());
if( jRadioButton1.isSelected())
{
if ( p>=70)
jTextField4.setText(Eligible for all subject);
else
jTextfield4.setText(Not Eligible for science);
}
else if( jRadioButton2.isSelected())
{
Downloaded from WWW.STUDIESTODAY.COM
if ( p>=60 )
jTextField4.setText(Eligible for Commerce and Humanities);
else
jTextfield4.setText(Not Eligible for Science and Commerce);
}
else
{
if ( p>=40 )
jTextField4.setText(Eligible for Humanities);
else
jTextfield4.setText(Not Eligible for any subject );
}
}
c.
private void jBtnClearActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText( ) OR jTextField1.setText(null)
jTextField1.setText( ) OR jTextField1.setText(null)
jTextField1.setText( ) OR jTextField1.setText(null)
jTextField1.setText( ) OR jTextField1.setText(null)
jCheckbox1.setSelected(false);
}
m
o
.c
y
a
d
d.
private void jBtnCloseActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
o
t
s
e
i
d
u
t
s
.
Unsolved Questions:
1. Describe the relationship between properties, methods and events.
2. What is container tag?
3. What does a getPassword() method of a password field returns?
4. What will be the contents of jTextArea1 after executing the following statement: 1
5. jTextArea1.setText("Object\nOriented\tProgramming");
6. What is difference between jRadioButton and jCheckBox?
7. What does a JList fire when a user selects an item?
8. What is Layout Manager? Discuss briefly about layout managers offered by NetBeans?
9. Name three commonly used properties and methods of the following controls.
10. (a) text field (b) text area (c) label (d) Check Box (e) button.
11. What is dispose() used for ?
12. What is the difference between13. (a) Text field & Text area
14. (b) List & Combo
15. (c) Radio Button & Check Box
16. What is the significance of following properties of a text area ?
17. (a) lineWrap (b) wrapStyleword
18. What is the significance of a button group ? How do you create a button group ?
19. Discuss about some commonly used properties of lists and a combo boxes.
20. What methods obtains the current selection of a combo box ? Give a code example.
21. The FOR U SHOP has computerized its billing. A new bill is generated for each customer. The
w
w
shop allows three different payment modes. The discount is given based on the payment mode.
Downloaded from WWW.STUDIESTODAY.COM
Cheque
Credit Card
< 10000
20 %
>= 10000
25 %
< 15000
10 %
>= 15000
15 %
< 10000
10 %
>= 10000
12%
a) Write the code for the CmdClear Button to clear all the Text Fields.
b) Write the code for the CmdCalc Button to display the Discount Amount and Net Price in
the TxtDisc and the TxtNet Text Fields respectively.
CHAPTER-5
JAVA GUI PROGRAMMING REVISION TOUR III [Methods etc.]
m
o
.c
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.
y
a
d
In Java, which is strictly Object-oriented, any action can take place through methods and methods
o
t
s
e
i
d
Key points:
Constructor is method with the same name as of that class it is used to initialized object of
u
t
s
.
w
w
class.
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_____________.
Downloaded from WWW.STUDIESTODAY.COM
m
o
.c
Ans:A message to an object is a call to the objects method requesting that it performs some specified
action.
y
a
d
int absval(int a) {
return(a<0?-a:a);
}
o
t
s
e
i
d
The first line of the method definition is the prototype of the method i.e. the prototypes of method
u
t
s
.
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. Whats wrong with the following constructor definition for the class PlayInfo?
public void PlayInfo( int sticks)
{
w
w
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).
21. How to declare a class in Java?
Ans: In Java a Class is declared/definedby using class keyword followed bya class name.
For example:
public class Student
{
String Name;
int RollNo;
String FName;
String DOB;
void getAdmission()
{
}
oid getTransfer()
{
..
}
oid feeDeposit()
{ ..
..
}
}
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.
u
t
s
.
y
a
d
o
t
s
e
i
d
m
o
.c
w
w
When programs become more complex and big in size, it is best technique to follow Divide and
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 Methods 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 membersConstructor 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.
u
t
s
.
y
a
d
o
t
s
e
i
d
m
o
.c
w
w
u
t
s
.
y
a
d
o
t
s
e
i
d
m
o
.c
w
w
UNSOLVED QUESTIONS