IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Chapter-1 Understand Integrated Development Environment
(NETBEANS)
Integrated Development Environment (IDE)- An Integrated Development Environment (IDE) is a
so ware applica on that provides all the necessary tools required to develop a program at one place. Instead
of using separate tools for wri ng code, compiling, running, and debugging, an IDE combines all these facili es
into a single pla orm.
In Java programming, NetBeans IDE is commonly used. It helps programmers to write Java code, design
Graphical user interfaces (GUI), compile programs, run applica ons, and debug errors easily.
Features of an IDE
Code editor for wri ng programs
Compiler to convert source code into machine code
Debugger to find and correct errors
GUI builder for designing forms
Output window to display results
NetBeans ID- NetBeans IDE is used to create java applica ons very easily using the efficient GUI builder. It
allows us to develop applica ons by dragging and posi oning GUI components from a pale e onto a container.
Different components of the NetBeans IDE Interface-
1. Title Bar
2. Menu Bar with pull down menus
3. Toolbars
4. GUI builder: It is an area to place components on the form visually. There are two views of the GUI builder- the Design
View and the Source View. We can switch over from one view to another by simply clicking on the source and design tabs
directly above the Design Area.
5. Pale e: Pale e contains controls or components used to create GUI applica ons.
6. Inspector Window: This window is used to display a hierarchy of all the components or controls placed on the current
form.
7. Proper es Window: Using this window we can make changes in the proper es of currently selected control on the
form.
8. Code Editor Window: - It is the area where we write code for our java applica on.
Page-1 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Components (Widgets)
Components are the basic graphical elements used to create the user interface of a Java applica on. These are
the controls through which a user interacts with the program, such as entering text, clicking bu ons, or selec ng
op ons. In NetBeans, components are provided by Swing library and are placed on containers like JFrame using
drag-and-drop.
Every component is an object and belongs to a predefined Java class. Each component has its own proper es,
methods, and events which control its appearance and behaviour.
Example: JBu on, JLabel, JTextField, JTextArea etc.
Types of Controls
In Java GUI programming, components are broadly classified into two types based on their role and placement
in a form: Parent (Container) Controls and Child Controls.
(i) Parent / Container Controls
Parent controls act as a base or background for other controls. They are used to hold and organize child controls.
A container can contain mul ple child controls inside it.
Example: JFrame, JPanel
Act as a container for child controls
Dele ng parent deletes all child controls
Moving parent moves all child controls
Used to structure the GUI layout
(ii) Child Controls
Child controls are placed inside container controls and are used for interac on with the user. They cannot exist
independently without a parent container.
Example: JLabel, JTextField, JBu on, JCheckBox
Always placed inside a container
Used to accept input or display output
Represent user interac on elements
Crea ng a New Project in NetBeans
Steps:
1. File → New Project
2. Select Java Applica on
3. Enter project name and loca on. Do not create main class
4. Click Finish
Project, Form and Components Rela onship
Project contains one or more forms
Each form contains mul ple components
JFrame Form- JFrame is a window that contains all GUI components.
Common Proper es
Methods- Used to perform ac ons on components. Examples: getText(), setText()
Events- Ac ons performed by user. Examples: mouseClick, keyPressed
Important Points
Ge er methods retrieve data
Se er methods modify data
Events connect GUI with code
Page-2 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
JBu on- JBu on is a component used to perform an ac on when the user clicks on it. It is commonly used for
submi ng data, performing calcula ons, or triggering events in a program. When a bu on is clicked, an
Ac onEvent is generated and the associated code is executed.
Common Proper es of JBu on-
text – sets the text displayed on the bu on
background – sets background color
foreground – sets text color
font – sets font style and size
enabled – enables or disables the bu on
Common Methods
getText() – retrieves the text of the bu on
setText(String t) – changes bu on text at run me
setEnabled(boolean b) – enables or disables the bu on
setVisible(boolean b) – shows or hides the bu on
Important Points
Used to trigger ac ons
Generates Ac on Event on click
One of the most commonly used components
JTextField- JTextField is a component used to accept or display single-line text. It is mainly used to take input
such as name, roll number, marks etc. The user can type text into a text field at run me.
Common Proper es
text – sets the text displayed
editable – allows or restricts edi ng
enabled – enables or disables the text field
background – sets background color
font – sets font style
Common Methods
getText() – retrieves text entered by user
setText(String t) – sets text programma cally
setEditable(boolean b) – allows or restricts edi ng
setVisible(boolean b) – shows or hides component
JBu on
Important Points JTextField
Accepts single-line input
Editable by default
Used for user data entry
JLabel- JLabel is a component used to display read-only text or images on a form. It is commonly used to
display instruc ons, headings, or output messages. The user cannot edit the text displayed in a label.
Common Proper es
text – sets the label text
foreground – sets text color
font – sets font style
enabled – enables or disables label
Common Methods
getText() – retrieves label text
setText(String t) – changes label text at run me
setVisible(boolean b) – shows or hides label
Important Points
Used for displaying informa on
Cannot accept user input
Improves clarity of GUI
Page-3 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
JTextArea- JTextArea is a component used to accept or display mul -line text. It is useful when large amounts
of text are required, such as address, descrip on, or output logs. Scroll bars appear automa cally when required.
Common Proper es
text – sets displayed text
rows – sets number of rows
columns – sets number of columns
lineWrap – wraps text automa cally
editable – allows or restricts edi ng
Common Methods
append(String s) – adds text at the end
getText() – retrieves text
setText(String t) – replaces exis ng text
JPasswordField- Used to enter confiden al data like passwords.
Important Points
Characters are hidden
Uses echo character (*)
JRadioBu on- Radio bu ons are groups of bu ons in which, by
conven on, only one bu on at a me can be selected.
Important Points
Grouped using Bu onGroup
Single selec on allowed
JCheckBox- Used to select mul ple op ons.
Important Points
Can be checked or unchecked
Mul ple selec ons allowed
JComboBox- Used to select one op on from drop-
down list.
Important Points
Saves space
Less clu ered than radio bu ons
JList
Displays list of items for selec on.
Important Points
Allows single or mul ple selec on
Useful for large lists
JOp onPane- Used to display dialog boxes.
Common Methods
showMessageDialog()
showConfirmDialog()
showInputDialog()
Page-4 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
List of GUI Components
Name of Method Use / Purpose Components List Example
setText(String t) Sets or changes the text JBu on, JLabel, [Link]("Result");
displayed on a component at JTextField, JTextArea,
run me. JCheckBox, JRadioBu on
getText() Retrieves the text from a JBu on, JLabel, String s = [Link]();
component. JTextField, JTextArea
setEnabled(boolean b) Enables or disables a JBu on, JTextField, jBu [Link](false);
component. JLabel, JRadioBu on
isEnabled() Checks whether a component JBu on, JTextField, boolean b =
is enabled or not. JLabel jBu [Link]();
setVisible(boolean b) Makes a component visible or JFrame, JBu on, JLabel, [Link](true);
invisible. JTextField, JTextArea
setEditable(boolean b) Allows or restricts user edi ng JTextField, JTextArea [Link](false);
in text components.
isEditable() Checks whether a text JTextField, JTextArea boolean e =
component is editable. [Link]();
setSelected(boolean b) Selects or deselects a JCheckBox, JRadioBu on [Link](true);
component.
isSelected() Checks whether a component JCheckBox, JRadioBu on boolean s =
is selected or not. jRadioBu [Link]();
getSelectedItem() Retrieves the selected item JComboBox Object o =
from a combo box. [Link]();
getSelectedIndex() Returns index of selectedItem. JComboBox, JList int i = [Link]();
List of Some Common Methods Used in Many GUI Components: -
Component Descrip on / Use Important Proper es List of Common Methods
JFrame Acts as main window of a GUI tle, defaultCloseOpera on, setTitle(), setVisible(), setSize(),
applica on & holds all other background, font setDefaultCloseOpera on()
components.
JBu on Used to perform an ac on when text, background, foreground, getText(), setText(),
clicked by the user. font, enabled setEnabled(), setVisible()
JLabel Displays read-only text or images text, font, foreground, enabled getText(), setText(), setVisible()
for informa on or instruc ons.
JTextField Accepts or displays single-line text, editable, enabled, font, getText(), setText(),
text input from the user. background setEditable(), setVisible()
JTextArea Used to accept or display text, rows, columns, lineWrap, getText(), setText(), append()
mul -line text with scroll bars. editable
JPasswordField Used to enter confiden al text, echoChar, font, getText(), setEchoChar()
informa on like passwords. foreground
JRadioBu on Allows selec on of only one text, selected, bu onGroup, isSelected(), setSelected(),
op on from a group. enabled getText()
JCheckBox Allows selec on of mul ple text, selected, enabled, font isSelected(), setSelected(),
op ons. getText()
JComboBox Displays a drop-down list to model, selectedItem, getSelectedItem(),
select one item. selectedIndex, editable getSelectedIndex(), setModel()
JList Displays a scrollable list for single model, selectedIndex, getSelectedValue(),
or mul ple selec on. selec onMode isSelectedIndex()
JOp onPane Used to show dialog boxes for — showMessageDialog(),
message, input or confirma on. showInputDialog()
Page-5 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
List of Common Events Used in Java GUI Components: -
Event Name Meaning / When it Occurs Components List Example (Method Name)
ac onPerformed Occurs when the user clicks a JBu on, JRadioBu on, public void
bu on or selects an op on. JCheckBox, JComboBox, ac onPerformed(Ac onEvent e)
JTextField
mouseClicked Occurs when the mouse bu on JBu on, JLabel, JPanel, public void
is clicked on a component. JTextField mouseClicked(MouseEvent e)
mouseEntered Occurs when the mouse JBu on, JLabel, JPanel public void
pointer enters a component mouseEntered(MouseEvent e)
area.
mouseExited Occurs when the mouse JBu on, JLabel, JPanel public void
pointer leaves a component mouseExited(MouseEvent e)
area.
keyPressed Occurs when a key is pressed JTextField, JTextArea, public void keyPressed(KeyEvent e)
on the keyboard. JPasswordField
keyReleased Occurs when a pressed key is JTextField, JTextArea public void keyReleased(KeyEvent
released. e)
keyTyped Occurs when a character key is JTextField, JTextArea public void keyTyped(KeyEvent e)
typed.
itemStateChanged Occurs when the state of an JCheckBox, public void
item changes JRadioBu on, itemStateChanged(ItemEvent e)
(selected/deselected). JComboBox
valueChanged Occurs when the selec on JList public void
value changes. valueChanged(ListSelec onEvent e)
windowClosing Occurs when the user clicks the JFrame public void
close bu on of a window. windowClosing(WindowEvent e)
focusGained Occurs when a component JTextField, JTextArea public void focusGained(FocusEvent
gains focus. e)
focusLost Occurs when a component JTextField, JTextArea public void focusLost(FocusEvent e)
loses focus.
Chapter-2 (JAVA Programming)
Object Oriented Programming: - Java is an Object-Oriented Programming (OOP) language.
In OOP, a program is developed as a collec on of objects. These objects interact with each other to solve a problem.
Object Oriented Programming follows a bo om-up approach, where small parts (objects) are created first and then
combined to form a complete program.
The main focus of OOP is on data safety and security, rather than only on performing opera ons. Java supports important
OOP concepts such as Encapsula on, Inheritance, and Polymorphism.
Encapsula on- It keeps data and the methods that use it together in one class. This prevents data from being
changed directly and improves security.
Inheritance- It allows a new class to use features of an exis ng class. This saves me and avoids wri ng the same
code again.
Polymorphism- It allows one method name to perform different tasks. This makes programs flexible and easy to
understand.
Components of Object-Oriented Programming
Object Oriented Programming is based on the following main components:
Class
Object
Data Members and Methods
Access Specifiers
These components together help in organising programs and protec ng data.
Page-6 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Class in Java- A class is a logical en ty that represents a blueprint or template.
It defines:
A ributes (data) – what informa on will be stored
Behaviour (methods) – what opera ons can be performed
A class combines data and methods into a single unit, which is called encapsula on.
To use the features of a class, an object of the class must be created.
Object in Java- An object is an instance of a class.
It is capable of storing actual data in memory loca ons. Class and object are related in the same way as data type and
variable. From one class, mul ple objects can be created, and each object can store different values of the same data
members.
Data Members and Methods- A class contains:
Data Members – variables that store data
Methods – a group of statements wri en together to perform a specific task
Methods work on data members to process, modify, or display data.
Example-
In Java, a class is designed by deciding what data it will store and what work it will do.
To understand this clearly, let us take a real-life example.
Crea ng a Class: Shape Class
Suppose we want to work with different shapes like Rectangle, Square, Circle.
All these shapes have:
Some data (length, breadth, radius)
Some work (calculate area)
So, we create a class named Shape.
Data Members (What the object knows)
length
breadth
These store the dimensions of the shape.
Methods (What the object does)
calculateArea()
This method calculates the area using the given dimensions.
Design of Shape Class (Syntax)
class Shape
{
int length;
int breadth;
int area()
{
return length * breadth;
}
}
Crea ng Object from Shape Class
Now, we create an object of Shape to represent a Rectangle.
Shape rectangle = new Shape();
[Link] = 10;
[Link] = 5;
int result = [Link]();
Page-7 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Access Modifiers (Access Specifiers)
Access modifiers define the accessibility scope of data members and methods in a class.
Java provides the following access modifiers:
private
public
protected
Private Access Modifier- Members declared as private cannot be accessed outside the class.
Data members are usually kept private to protect data from unauthorized access.
Public Access Modifier- Members declared as public can be accessed from anywhere.
Methods are generally kept public so that they can be used by objects.
Protected Access Modifier- Members declared as protected can be accessed:
within the same class
in derived (child) classes
Data members can be kept protected when limited access is required.
Variables- Variables are containers used to store the values for some input, intermediate result or the
final result of an opera on. The characteris cs of a variable are:
• It has a name.
• It is capable of storing values.
• It provides temporary storage.
• It is capable of changing its value during program execu on.
Data Types- A data type specifies the type of data that a variable can store.
It also tells the compiler:
how much memory to allocate
what type of opera ons can be performed
Numeric Data Types: These data types are used to store integer values only i.e. whole numbers only. The
storage size and range is listed below:
Floa ng Data Types: - These data types are used to store numbers having decimal points i.e. they can store
numbers having frac onal values.
Page-8 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Character Data Types: - Character data types are used to store le ers, symbols, and text in a program.
In Java, character data is mainly stored using char and String data types. When we want to store only one
character, we use the char data type. When we want to store more than one character (a word or name), we
use the String data type.
char Data Type
Used to store a single character
Value is wri en inside single quotes (' ')
Example:
char grade = 'A';
This is useful for storing:
Grades (A, B, C, D)
Gender (M, F)
Single symbols
String Data Type
Used to store a group of characters (text)
Value is wri en inside double quotes (" ")
Example:
String name = "Rahul";
This is useful for storing:
Names
Messages
Sentences
Difference Between char and String (Easy Understanding)
Data Type Stores Example
char One single character 'A'
String Mul ple characters (text) "Amit"
Declaring Variables
data_type variable_name;
Example:
int a;
float degree;
char grade;
Rules for Naming Variables
Must begin with a le er, underscore (_) or dollar sign ($)
No spaces allowed
Reserved words cannot be used
Java is case-sensi ve
Operators- When we use variables and constants in a program, we need to perform ac ons such as
calcula on, comparison, or decision making.
These ac ons are performed using operators.
Operators are symbols that are used to:
assign values
perform calcula ons
compare values
make decisions
Java provides different types of operators for different purposes.
Page-9 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
1. Assignment Operator- The assignment operator (=) is used to assign a value to a variable.
The value wri en on the right side is stored in the variable on the le side.
Example
int sum = 0;
int prime = 4 * 5;
2. Arithme c Operators- Arithme c operators are used to perform mathema cal calcula ons, similar to
mathema cs.
Arithme c Operators List
Operator Meaning Example
+ Addi on a+b
- Subtrac on a-b
* Mul plica on a * b
/ Division a/b
% Remainder a%b
Example
int a = 10;
int b = 3;
int r = a % b; // r = 1
3. Rela onal Operators- Rela onal operators are used to compare two values.
The result of a rela onal opera on is always true or false.
These operators are mostly used in condi ons (if, while, switch).
Rela onal Operators Table
Operator Meaning Example
== Equal to a == b
!= Not equal to a != b
> Greater than a>b
< Less than a<b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
Example
if (marks >= 40)
{ [Link]("Pass"); }
Important Point
== is used for comparison
= is used for assignment
4. Logical Operators- Logical operators are used to combine two or more condi ons.
They are usually used along with rela onal operators.
Logical Operators Table
Operator Meaning Example
&& AND a > 10 && b < 8
|| OR a > 10 || b < 8
! NOT !a
Example
if (age >= 18 && age <= 60)
{ [Link]("Eligible"); }
How to Understand
&& → all condi ons must be true
|| → any one condi on can be true
! → reverses the result
5. Bitwise Operators- Bitwise operators work on binary form (0 and 1) of numbers.
They are used to manipulate individual bits of integer data.
Page-10 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Crea ng GUI Project
Open NetBeans -> click on File -> New Project -> Select ‘Java with Ant’ from Category and ‘Java Applica on’
from project -> next -> Enter Project Name -> Finish
NetBeans will create a folder of project name on designated loca on in system. Now the next step is to create
a form.
Crea ng Form- Right click the project name in Projects window -> New -> JFrame -> Enter Class Name ->
Finish
Page-11 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Adding and modifying components to Form
Adding a Bu on to Form
A er crea ng form (JFrame) select Bu on from ‘Swing Control’ pale e
Place the cursor in the form where you want to display bu on and click once. The bu on will be
added.
A aching code to Form Component
Double click on the component (for exp bu on we created) and it opens the Code editor window
where you can write code.
A specific sec on of code for the component is automa cally generated in code editor window which is
indicated in ray/blue areas called Guarded blocks. Code in Guarded block cannot be edited.
We can only add/edit code appearing in white areas of the Code editor window.
Execu ng a File
Click Run -> Run File
OR
Right on file in Project window -> click Run File
The window in which we have designed our form is called the Design window and the window in which we
have wri en the code is called the Source window.
Page-12 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Changing Proper es of Component
Right click on the component -> select Proper es -> proper es window appears from where we can edit
values of different proper es.
How to display a message in a Dialog box?
We use JOp onPane to display message in a dialog box. We need to write following code:
JOp [Link](null, “Your Message”);
Above wri en code can be associated with the event of any component for which you want to display
message.
A er execu ng above wri en code, if you get error, than you need to import JOp onPane at the top of
the program as given below:
[Link] onPane;
OR
import [Link].*;
Example: Display a message on the click of a bu on.
Page-13 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Page-14 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
How to display a message using Text Field?
Create a form using a jTextField and jBu on as given below
Double click on jBu on to switch to Source code window and Write code as given below
How to accept input using Text Field component?
We can use getText() method of Text Field to accept input from the user.
Example: Addi on of two numbers
Create a GUI form using jLabel, jTextField and jBu on as given below and set text property of all components
as given below:
Page-15 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Write following code for add bu on and exit bu on as given below:
Execute the applica on and we will see the output screen as given below:
Note:
In the given code
x = [Link]([Link]());
to read numeric data input, we have used sta c method parseInt() of Integer class that takes String as
parameter and returns its equivalent as given.
In the given code
[Link]([Link](z));
We have used sta c method toString() of Integer class to convert numeric data in string to display in
Text Field.
Page-16 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Control Structure- A program is considered as finite set of instruc ons that are executed in a sequence.
But some mes the program may require execu ng some instruc ons condi onally or repeatedly. In such
situa ons java provides:
Selec on structures
Repe on structures
Selec on structure with if else- Selec on in program
refers to Condi onals which means performing opera ons
(sequence of instruc ons) depending on True or False value
of given condi ons. Structure of Condi onal in a program
can be as follow:
Java provides two statements for execu ng block of code
condi onally:
If else statement
Switch statement
if else statement
The if statement in Java lets us execute a block of code depending upon
whether an expression evaluates to true or false. The structure of the Java if
statement is as below:
Example: Java program to check no is even or odd
Form Design
Source Code
Output
Page-17 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
if else with logical operators- Some mes, you may want to execute a block of code based on the
evalua on of two or more condi onal expressions. For this, you can combine expressions using the logical &&
or || operators.
Example: Program to check a character is vowel or consonant
Design:
Source Code
Output
Page-18 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Mul ple if statements
Mul ple if refers to using more than one if statement together in a program.
It is basically used to execute more than two block of statements on the evalua on of different
condi ons.
Example: Program to display largest among three numbers
GUI Form Design
Source Code
OUTPUT
Page-19 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Switch case statement
The switch statement is used to execute a block
of code matching one value out of many possible
values.
It is basically used to implement choices from
which user can select anyone.
Example: Develop a discount calculator using switch
case statement. Discount should be calculated as given
below:
GUI Design Form
Source Code
Page-20 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
OUTPUT
EXERCISES
MULTIPLE CHOICE QUESTIONS
Q1. What will be the final value of sum1 a er the execu on of the program given below?
int sum1 = 3;
sum1 = sum1 + 1; // sum1 = 4
[Link](""+sum1);
sum1 = sum1 + 1; // sum1 = 5
[Link](""+sum1);
sum1 = sum1 + 1; // sum1 = 6
[Link](""+(sum1));
sum1 = sum1 + 1; // sum1 = 7
[Link](""+sum1);
[Link](""+sum1);
a. 4 b. 5 c. 6 d. 7 ✔
Q2. Consider the following code:
int anumber = 14;
if (anumber >= 10)
[Link]("first string");
else
[Link]("second string");
[Link]("third string");
What will be the output when anumber=14
a. first string b. second string c. first string, third string ✔ d. third string
Q3. What's wrong with the following statement?
if( (ctr < 5) && (ctr > 30) )
a. the logical operator && cannot be used in a test condi on
b. the test condi on is always false ✔
c. the test condi on is always true
d. none of these
Q4. Given the following informa on:
int a = 11; int b = 22; int c = 33; int d = 11;
Which of the following statements are true :
i) a = = b
ii) b != d
iii) c <= b
Page-21 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
iv) a < c
v) a = = d
vi) c > a
vii) a >= c
a. i),iv) & vii) b. ii),iv), v) & vi) ✔ c. ii),iv), vi) & vii) d. iii),v),vi) & vii)
Q5. In GUI applica ons, what are events?
(a) Predefined data types used in programming
(b) Ac ons performed on controls, such as mouseClick or keyPressed (Right)
(c) Reserved keywords in SQL
(d) Func ons used to create databases
Q6. The ____________ component allows us to accept mul line input from the user or display mul ple lines
of informa on.
(a) JTextField
(b) JTextArea (Right)
(c) JLabel
(d) JBu on
Q7. Which one of the following statements is false about Object Oriented Programming (OOP)?
(a) OOP follows a top-down approach and ignores data security. (Right)
(b) OOP wraps data and methods into a single unit called encapsula on.
(c) OOP supports polymorphism and inheritance.
(d) OOP is based on objects that combine data and func ons.
Q8. Given the following informa on:
int a = 11;
int b = 22;
int c = 33;
int d = 11;
Which of the following statements is true?
(a) a == b
(b) b <= d
(c) c >= b (Right)
(d) c <= a
Q9. Which of the following is NOT an arithme c operator in Java?
(a) +
(b) -
(c) *
(d) && (Right)
ANSWER THE FOLLOWING QUESTIONS
Q1. Explain the following terms
Answer: - a) IDE (Integrated Development Environment)- An IDE is a so ware tool that provides all facili es
needed to create a program at one place.
It allows us to write code, design GUI forms, compile, run, and debug programs easily.
Example: NetBeans is an IDE used for Java GUI programming.
b) Inspector Window- The Inspector Window in NetBeans is used to view and manage all components added
to a form in a tree structure.
It helps the programmer to select, rename, or delete components easily.
c) Form- A form is a GUI window used to design the user interface of an applica on.
In Java, a form is usually a JFrame that contains components like bu ons, labels, and text fields.
Page-22 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Q2. Differen ate between
a) TextField and TextArea
TextField TextArea
Accepts single-line input Accepts mul -line input
Used for small input like name Used for large text like address
Does not support line break Supports line break
b) ComboBox and ListBox
ComboBox ListBox
Displays items in drop-down form Displays items as a list
Saves screen space Takes more screen space
Allows single selec on Allows single or mul ple selec on
c) getText() and setText()
getText() setText()
Used to read text from component Used to display/change text
Retrieves user input Sets output on component
Returns a String Takes a String as argument
Q3. What is the significance of the following proper es in TextArea?
Answer: LineWrap- LineWrap automa cally moves text to the next line when the line becomes too long.
WrapStyleWord- It ensures that words are not broken in between while moving to the next line.
Q4. What are list type controls used for?
Answer: List type controls are used to display a list of op ons from which the user can select one or more
items. Examples include JList and JComboBox.
Q5. How would you determine whether a combo box is editable or not?
Answer: By checking the editable property of the combo box.
If editable is true, the user can type values.
If editable is false, the user can only select from the list.
Q6. List different selec on modes of a list.
Answer: The selec on modes of a list are:
Single selec on
Single interval selec on
Mul ple interval selec on
Q7. What is a bu on group? Which control is generally used with a bu on group?
Answer: A bu on group is used to group radio bu ons so that only one op on can be selected at a me.
The control generally used with a bu on group is JRadioBu on.
Q8. Write and explain two methods each of check box and radio bu on
Anwer: Methods of CheckBox
1. setSelected(boolean b- Used to select or deselect a checkbox.
2. isSelected()- Used to check whether a checkbox is selected or not.
Methods of RadioBu on
1. setSelected(boolean b)- Used to select a radio bu on.
2. isSelected()- Used to check whether a radio bu on is selected.
Q9. What is difference between = and == in Java Programming?
Answer: In Java programming, = and == are used for different purposes.
The = (assignment operator) is used to assign a value to a variable. It stores the value wri en on the
right side into the variable on the le side.
Example: int a = 10;
The == (equality operator) is used to compare two values. It checks whether both values are equal and
returns true or false.
Example: if (a == 10)
Page-23 Notes by Abhishek Tyagi (1002028)
IT (802) Class-XI Unit-5 (Fundamentals of Java Programming)
Q10. Suppose you are designing a GUI-based calculator in Java. You are using components like JBu on,
JTextField, and JLabel.
(a) Give one property and one method of JTextField.
(b) Which event will be generated when a JBu on is clicked?
(c) Why are events important in GUI applica ons?
Answer: (a) One property and one method of JTextField
Property: text – used to set or display the text inside the text field.
Method: getText() – used to read the text entered by the user.
(b) Event generated when a JBu on is clicked
When a JBu on is clicked, an Ac onEvent is generated.
(c) Why events are important in GUI applica ons
Events are important because they allow the program to respond to user ac ons such as bu on clicks,
key presses, or mouse movements.
Without events, a GUI applica on would not be interac ve and could not perform ac ons based on
user input.
Q11. Neha wrote the following Java code to check if a number is even or odd, but it contains errors. Iden fy
the errors and rewrite the correct code:
If(num % 2 = 0);
{
[Link](“Even Number”)
}
else
{
[Link](“Odd Number”)
}
Answer: The given code contains mul ple errors:
Errors in the code
1. If should be wri en as if
Java is case-sensi ve, so If is incorrect.
2. Wrong operator used (= instead of ==)
= is an assignment operator.
== is required for comparison.
3. Extra semicolon (;) a er the if condi on
This ends the if statement incorrectly.
4. Missing semicolons a er setText statements
Every Java statement must end with a semicolon.
5. Incorrect quota on marks
Java uses double quotes (" "), not curly quotes.
Page-24 Notes by Abhishek Tyagi (1002028)