|

Fundamentals of Java Programming Notes – Class 11 IT (802)

Fundamentals of Java Programming Notes based on CBSE syllabus. It covers Java IDE components, Java fundamentals, control structures, with practical programs.

Java Programming is the most important and core unit of the Class 11 Information Technology (802) syllabus. From the examination perspective, this unit carries 15 marks in the theory paper and 10 marks in the practical examination, making it one of the highest-scoring sections of the course.

Beyond exams, Java introduces you to the fundamentals of programming, logical thinking, object-oriented programming (OOP), and practical coding skills, helping you understand how real-world software and applications are developed.

Whether you’re preparing for board exams or strengthening your programming skills, these java programming notes will help you learn Java with confidence.

Contents hide

Chapter 1: Understand Integrated Development Environment (NetBeans)

NetBeans IDE

  • NetBeans IDE is a software tool used to develop Java applications easily and efficiently. One of its key features is the GUI Builder, which allows developers to create user interfaces without writing extensive code manually.
  • Instead of coding every component, you can simply drag components from a palette and place them onto a form. This makes the development process faster and more intuitive.

Components of NetBeans IDE

  • Title Bar
    The title bar is located at the top of the IDE window and displays the name of the current project or file. It helps the user identify what is currently open.
  • Menu Bar with Pull-down Menus
    The menu bar contains different menus such as File, Edit, View, etc. Each menu provides a list of commands that allow the user to perform various operations like creating a project, saving files, or editing content.
  • Toolbars
    Toolbars provide shortcut buttons for frequently used tasks such as creating a new project, saving files, or running the program. They help improve efficiency by reducing the need to navigate through menus.
  • GUI Builder
    This is the main area where you design the form visually. It allows you to place components on the form. It has two views:
    • Design View, where you visually arrange components.
    • Source View, where you can see and edit the Java code.
      You can switch between these views using tabs above the design area.
  • Palette
    The palette contains various GUI components such as buttons, labels, text fields, etc. These components can be dragged and dropped onto the form to build the interface.
  • Inspector Window
    This window displays the hierarchy of all components placed on the form. It shows how components are organized within containers.
  • Properties Window
    This window allows you to view and modify the properties of the currently selected component. For example, you can change its color, font, or text.
  • Code Editor Window
    This is where you write the Java code for your application. It is used to implement the logic behind the interface.

Components (Widgets) of IDE

  • Components, also known as widgets, are the basic elements of a user interface that users interact with. Examples include labels, buttons, and text fields.
  • These components are always placed inside a container such as a JFrame. They do not function independently without being part of a container.

Types of Controls

  • Parent or Container Controls
    These controls act as a base or background for other components. A common example is a Frame. When a parent control is deleted, all the child components inside it are also deleted automatically. Similarly, when the parent is moved, all child components move along with it.
  • Child Controls
    These are the components placed inside a container control. Examples include text fields, labels, and buttons. They rely on the parent container for their placement and display.

Objects, Properties, Methods, and Events

  • Every GUI component is considered an object that belongs to a predefined class in Java. For example, a text field is an object of the JTextField class.
  • Properties define the appearance of an object. For example, changing the background property changes the background color of a component.
  • Methods are used to perform actions on an object. For example, setText() is used to set text, and getText() is used to retrieve text.
  • Methods are divided into:
    • Getters, which retrieve information from an object (e.g., getText(), isEditable()).
    • Setters, which modify properties of an object (e.g., setText(), setEnabled()).
  • Events are actions performed by the user, such as clicking a button or pressing a key. When an event occurs, it triggers a corresponding piece of code, causing the application to respond.

Creating a New Project

  • To create a new project, select the New Project option from the File menu or click the New Project button on the toolbar.
  • In the Categories section, choose General, and in the Projects section, select Java Application. Then click Next.
  • Enter the project name and specify the location where the project will be stored. It is important not to create a main class at this stage.
  • Finally, click the Finish button, and the project will be created.

JFrame Form

  • A JFrame is a window that serves as the main container for all other components. It can include a title, borders, and optionally a menu bar.
  • It is the first component that is added when designing a GUI application.

Properties of JFrame

  • defaultCloseOperation
    This property defines what action should be taken when the user attempts to close the window, such as exiting the application.
  • Title
    This property sets the text that appears in the title bar of the window.

JButton

  • A JButton is a component that the user clicks to perform an action. When the button is pressed, the associated code is executed.

Properties of JButton

  • Background sets the background color of the button.
  • Enabled determines whether the button is active or not.
  • Font defines the style of the text.
  • Foreground sets the color of the text.
  • Horizontal Alignment controls how the text is aligned.
  • Label/Text defines the text displayed on the button.

Methods of JButton

  • getText() retrieves the text displayed on the button.
    Example: String result = <button-name>.getText();
  • setEnabled(boolean b) enables or disables the button.
  • setText(String text) changes the text displayed on the button at runtime.
  • setVisible(boolean flag) makes the button visible or invisible.

JTextField

  • A JTextField is used to input or display a single line of text. It is commonly used to accept user input.

Properties of JTextField

  • Background sets the background color.
  • Border defines the border style around the text field.
  • Editable determines whether the user can edit the text.
  • Enabled indicates whether the component is active.
  • Font sets the text style.
  • Foreground sets the text color.
  • HorizontalAlignment controls text alignment.
  • Text sets the displayed content.
  • ToolTipText displays a message when the cursor hovers over the component.

Methods of JTextField

  • getText() retrieves the text entered by the user.
    Example: String result = <textfield-name>.getText();
  • isEditable() checks if the text field is editable.
  • isEnabled() checks if the component is enabled.
  • setEditable(boolean b) allows or prevents editing.
  • setText(String t) changes the displayed text.
  • setVisible(boolean b) controls visibility.

JLabel

  • A JLabel is used to display text or images. It is not editable and is mainly used to provide instructions or information.

Properties of JLabel

  • Background, Font, and Foreground control the appearance.
  • HorizontalAlignment controls alignment of the text.
  • Text defines the content displayed.

Methods of JLabel

  • getText() retrieves the displayed text.
  • isEnabled() checks if it is active.
  • setText(String t) updates the text.
  • setVisible(boolean b) controls visibility.

JTextArea

  • A JTextArea allows multiple lines of text input or display. It is useful when large amounts of text need to be handled.
  • It automatically provides scroll bars when the content exceeds the visible area.

Properties of JTextArea

  • Background, Font, Foreground control appearance.
  • Columns and Rows define the size.
  • Editable determines if text can be modified.
  • Enabled defines active state.
  • LineWrap determines if text moves to the next line.
  • WrapStyleWord ensures words are not broken while wrapping.
  • Text defines displayed content.

Methods of JTextArea

  • append(String str) adds text at the end.
  • getText() retrieves text.
  • isEditable() and isEnabled() check status.
  • setText(String t) changes content.

JPasswordField

  • This component is used to input confidential data like passwords. The characters entered are not displayed directly; instead, they are replaced by a symbol (usually *).

Properties of JPasswordField

  • Background, Font, Foreground control appearance.
  • Text sets the content.
  • EchoChar defines the character used to mask the input.

JRadioButton

  • Radio buttons allow the user to select only one option from a group of options.

Properties

  • ButtonGroup defines grouping.
  • Selected determines whether it is selected by default.
  • Background, Font, Foreground control appearance.
  • Text/Label defines display text.

Methods

  • getText() retrieves text.
  • isSelected() checks if selected.
  • setText(String t) changes text.
  • setSelected(boolean b) selects or deselects.

JCheckBox

  • A JCheckBox allows multiple selections. It toggles between checked and unchecked states when clicked.

Properties

  • Background, Font, Foreground control appearance.
  • Text/Label defines display text.
  • Selected indicates checked state.

Methods

  • getText() retrieves text.
  • isSelected() checks state.
  • setText(String t) changes text.
  • setSelected(boolean b) updates selection.

JComboBox

  • A JComboBox is a dropdown list that allows the user to select one option from multiple choices.

Properties

  • Model stores the list of items.
  • SelectedIndex sets default selection by index.
  • SelectedItem sets default selected value.
  • Editable allows user input if enabled.
  • Background, Font, Foreground control appearance.

Methods

  • getSelectedItem() retrieves selected item.
  • getSelectedIndex() retrieves index.
  • setModel() sets data.

JList

  • A JList displays a list of items and allows single or multiple selections.

Properties

  • Model stores items.
  • SelectedIndex defines selected item.
  • SelectionMode defines how selection works:
    • Single
    • Single Interval
    • Multiple Interval

Methods

  • getSelectedValue() returns selected value.
  • isSelectedIndex(int index) checks if a specific index is selected.

JOptionPane

  • JOptionPane is used to display dialog boxes or take input from the user.

Import Statements

  • import javax.swing.JOptionPane;
    OR
  • import javax.swing.*;

Methods and Examples

  • showMessageDialog()
    Displays a message box.
    Example: JOptionPane.showMessageDialog(this,”Java and NetBeans”);
  • showConfirmDialog()
    Displays a confirmation dialog.
    Example: Confirm = JOptionPane.showConfirmDialog(null,”quit?”);
  • showInputDialog()
    Takes input from the user.
    Example: name = JOptionPane.showInputDialog(this,”Name:”);

Chapter 2: JAVA Programming

Object Oriented Programming (OOP)

  • Object Oriented Programming follows a bottom-up approach and focuses on data safety and security.
  • It combines data and methods into a single unit, which is known as encapsulation.

Key Features of OOP

  • Polymorphism allows the use of a single name for multiple methods or operations, reducing the need to remember many names.
  • Inheritance allows one class to use the properties and methods of another class, promoting code reuse.

Main Components of OOP

  • Class
  • Object
  • Data Members and Methods
  • Access Specifiers (private, public, protected)

Class

  • A class is a blueprint that combines data and methods into one unit.
  • It controls access using visibility modes such as private, public, and protected.
  • Typically, data members are kept private or protected, while methods are public.

Object

  • An object is an instance of a class that stores actual data in memory.
  • Example:
    If Human is a class, then individuals like Mr. Arun Shah and Mr. Aneek Ram are objects.
  • The relationship between class and object is similar to data type and variable.

Data Members and Methods

  • Data members store data, while methods perform operations on that data.
  • Example:
    In a human analogy, stored phone numbers represent data, while body parts like eyes and ears act as methods that help process or interact with that data.
  • In Java:
    • Data must follow specific types such as int, float, char.
    • Methods are sequences of instructions that perform tasks.

OOP’s Components Mapping with GUI Components

  • Classes: JTextField, JLabel, JButton, etc.
  • Objects: jTextField1, jLabel1, jButton1, etc.
  • Methods: setText(), setEnabled(), pow(), substring()

Variables

  • Variables are containers used to store data such as input values, intermediate results, or final outputs of a program.
  • Every variable has a name, which is used to identify it in the program.
  • It provides temporary storage in memory, which is used while the program is running.
  • In Java, a variable must be declared before it is used, otherwise the program will give an error.
  • Since different types of data require different storage, Java uses data types along with variables.

Data Types

  • A data type defines how a value is stored, what operations can be performed on it, and what range of values it can hold.

Numeric Data Types (Integer Types)

  • These are used to store whole numbers (without decimals).
  • Different types are used based on the size and range of values required.
TypeSizeDescription
byte1 byteStores very small integers
short2 bytesStores small integers
int4 bytesMost commonly used integer type
long8 bytesUsed for very large integers
  • The choice of type depends on the range of values the variable will store.

Floating Data Types

  • These are used to store decimal (fractional) values.
TypeDescription
floatSingle precision decimal number
doubleDouble precision (more accurate) decimal number
  • double is more precise than float, so it is generally preferred.

Character Data Types

  • These are used to store characters and text values.
  • char is used to store a single character (e.g., ‘A’, ‘5’, ‘$’) and is written in single quotes (‘ ‘).
  • String is used to store multiple characters (words or sentences) and is written in double quotes (” “).
  • Example:
    • char grade = ‘A’;
    • String name = “Rahul”;

Operators in Java

  • Operators are symbols used to perform operations on variables and values.
  • They help in calculations, comparisons, and logical decisions.

Assignment Operator (=)

  • Used to assign a value to a variable.
  • The value on the right side is stored in the variable on the left side.

Example:

  • int sum = 0;
  • int result = 4 * 5;

Arithmetic Operators

  • Used to perform mathematical operations.
OperatorFunction
+Addition
Subtraction
*Multiplication
/Division
%Remainder
  • % returns the remainder after division.

Relational Operators

  • Used to compare two values and return a true or false result.
OperatorMeaning
==Equal to
!=Not equal to
Greater than
Less than
>=Greater than or equal to
<=Less than or equal to
  • These are mainly used in conditions (if statements).

Logical Operators

  • Used to combine multiple conditions.
OperatorMeaning
&&Both conditions must be true
!Reverses the condition
  • Example:
    • a > 10 && b < 5 → true only if both are true

Bitwise Operators

  • Used to perform operations on binary values (bits) of numbers.
  • Mostly used in low-level programming or advanced data structures.

Variable Declaration

  • Before using a variable, we must declare it with a data type and name.
  • Declaration tells the compiler:
    • the name of the variable
    • the type of data it will store
    • optionally, an initial value

Examples:

  • int apples; → declaration only
  • float sum = 4; → declaration with initialization

Variable Naming Rules

  • Variable names are case-sensitive (sum and SUM are different).
  • Keywords (like int, class) cannot be used as variable names.
  • Names should be short, meaningful, and readable.
  • Must start with a letter, underscore (_), or dollar sign ($) (prefer letters).
  • Cannot contain spaces or special characters.

Examples:
✔ Valid: Grade, testScore, total_marks
❌ Invalid: 2value, test grade, marks#1

Control Structures

  • Control structures are used to control the flow of a program.
  • They help in decision-making and repetition.

Selection Statements (if-else)

  • Used to execute code based on a condition.
  • If the condition is true, one block runs; otherwise, another block runs.

Syntax:

if (condition) {
   // code if true
} else {
   // code if false
}

  • The condition must be inside parentheses ( ).
  • else part is optional.

Nested if-else

  • Used when there are multiple conditions to check.
  • Conditions are checked one by one from top to bottom.

Switch Statement

  • Used when we need to compare a variable with multiple fixed values.
  • It is easier to read when there are many conditions.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *