Fundamentals of Java Programming Notes Class 11 is prepared as per CBSE Syllabus. This Java Class 11 Notes is best for students of IT 802 XI. This Fundamentals of Java Class 11 Notes covers all Java Programming Fundamentals, Java GUI Components, Object Oriented Programming Concepts, Java GUI Programming etc.
- 1 Chapter 1 understanding Integrated Development Environment (NETBEANS)
- 2 Chapter 2 Object Oriented Java Programming
Chapter 1 understanding Integrated Development Environment (NETBEANS)
Introduction to Java
Java is one of most popular high level ‘Object Oriented’ programming language and has been used widely to create various computer applications.
Applications of java
- Database applications
- Desktop applications
- Mobile applications
- Web based applications
- Games etc.
What is Java IDE?
- Java Integrated Development Environment is software development kit equipped with
- Text (code) editor
- Form designer etc.
- It helps in writing code, compiling and executing java programs easily.
What is NetBeans?
Java NetBeans IDE is open source IDE used for writing java code, compiling and executing java programs conveniently.
Elements of Netbeans IDE Window
- Title Bar: display title of currently opened project.
- Menu Bar: with pull down menus: Provides various options in form of menu for project development,
- Toolbars: options present in menu bar are displayed as button.
- GUI Builder: it provides a form where different GUI components are placed to create a program interface.
- Inspector Window: This window is used to display a hierarchy of all the components or controls placed on the current form.
- Properties Window: This window displays various properties associated with java component that we can set as per requirement.
- Code Editor Window: it is the area where we can write code for our java application.
Java GUI Components with its Properties and Methods
- Components are the basic interface elements the user interacts with.
- Components are placed on a container like jFrame.
- There are two types of components
- Parent or container control: they act as a background for other controls. All the child controls are placed in it. For exp- Frame
- Child controls: controls placed inside a container control are called child control. For exp- Text Field, Label, Button etc.
- Any GUI component of an application is an object which belongs to its predefined class in java.
- Each object (GUI component) has some properties, methods and events which can be set to control object appearance and behavior.
- Properties of an object are used to set the appearance on the form such as background color, font etc.
- Methods are used to perform some action on the object such as setText(), getText() etc.
- Methods can be divided into two categories:
- Getters: these are methods which extracts some information from the object and return it to program. It starts with the word get. For exp- getText(), getModel(), isEditable etc.
- Setters: These are the methods which set some properties of the object so that the object’s appearance changes. Setters start with the word set. For exp- setText(), setForground(), setModel() etc.
- Events are the actions which are performed on controls. For exp- mouseClick, mouseMoved, keyPressed etc.
JFrame is a container control in java where other controls are placed. It is a window with title, border, menu bar (optional).
a button is control that is used to trigger specific action when pressed.
- jTextField is a control which allows entering/editing/displaying information in single line.
- It is also considered as input box.
- jLabel is used to display information which can not be edited.
- It can display image, text or both text and image.
- It allows accept/display multiple line text.
The jPassword component is used to enter confidential input like passwords which are single line. We can suppress the display of input as this component allows us to input confidential information like passwords. Each character entered can be replaced by an echo character. By default, the echo character is the asterisk, *.
Radio button (jRadioButton)
- Radio buttons are used to provide multiple options from which user can select any one.
- In case of radio button user can select only one option at a time.
- Checkboxes are used to provide multiple options from which user can select any one.
- In case of checkbox user can select more than one option at a time.
Combo Box (jComboBox)
- jComboBox is also known as drop down list which provides list of option from which user can select any one.
- Only one option can be selected at a time from jComboBox.
List Box (jList)
- jList is also known as drop down list which provides list of option from which user can select any one.
- Only one option can be selected at a time from jList.
We use JOptionPane when we want to request information from the user, display information to the user or a combination of both. It requires an import statement at the top of the program.
Chapter 2 Object Oriented Java Programming
Object oriented programming
- Java is an Object Oriented Programming language.
- In OOP language program is collection of objects that interact with other object to solve a problem.
- It follows bottom up approach in designing a program.
- Its primary focus is on safety and security of data in program rather operation of program.
- It supports concept of Inheritance and Polymorphism.
Components of OOPs
- Data member and methods
- Access Specifier
What is class in java?
- Class is physical or logical entity that has certain attributes (data), behavior (method).
- It is like a template which defines how information will be managed.
- It encapsulates data and methods together in a single unit.
- To use attributes of a class its object must be declared.
What is object in java?
- Object is instance of a class in java that is capable of holding actual data in memory locations.
- Both Class and Objects are related to each other as data type and variables.
- To use attributes and invoke method of class must be associated with its corresponding object.
- From a class multiple objects can be declared and all objects can have the same type of data members.
Designing a class
- A class contains data and methods.
- Methods are set of sequential steps written together to perform a specific task on the data.
Return_type method_name (list of parameters separated by comma)
Return statements; //optional
Return_type method_name (list of parameters separated by comma)
Return statements; //optional
Creating Class Object
<class name> <object name> = new <class name()>;
- Access Modifiers in java are keywords that specify the accessibility scope of data members of class.
- Java offers following Access Modifiers:
private access modifier
- Members defied as private of a class cannot be accessed outside of the class.
- Data member should be kept as private to avoid vulnerable to security issues.
public access modifier
- Members defined as public of a class cab be accessed outside of the class.
- Member methods are generally kept as public to make them available to access.
- By default all the members of a class are public.
protected access specifier
- members defined as protected can be accessed within class and its derived class.
- Data members can be kept as protected.
Variables & Data Types
- Variable is a placeholder for data that can change its value during program execution.
- Technically, a variable is the name for a storage location in the computer’s internal memory and the value of the variable is the contents at that location.
- Data type determines the type of data to be stored in a variable.
- Data type tells the compiler, how much memory to reserve when storing a variable of that type.
<Data type> <variable name>;
Rules for naming variable
- Variable names can begin with either an alphabetic character, an underscore (_), or a dollar sign ($).
- Variable names must be one word. Spaces are not allowed in variable names. Underscores are allowed.
- There are some reserved words in Java that cannot be used as variable names, for example – int
- Java is a case-sensitive language. Variable names written in capital letters differ from variable names with the same spelling but written in small letters.
Creating GUI Project
Open NetBeans -> click on File -> New Project -> Select ‘Java with Ant’ from Category and ‘Java Application’ from project -> next -> Enter Project Name -> Finish
NetBeans will create a folder of project name on designated location in system. Now the next step is to create a form.
Right click the project name in Projects window -> New -> JFrame -> Enter Class Name -> Finish
Adding and modifying components to Form
Adding a Button to Form
- After creating form (JFrame) select Button from ‘Swing Control’ palette
- Place the cursor in the form where you want to display button and click once. The button will be added.
Attaching code to Form Component
- Double click on the component (for exp button we created) and it opens the Code editor window where you can write code.
- A specific section of code for the component is automatically 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.
Executing a File
Click Run -> Run File
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 written the code is called the Source window.
Changing Properties of Component
Right click on the component -> select Properties -> properties window appears from where we can edit values of different properties.
How to display a message in a Dialog box?
- We use JOptionPane to display message in a dialog box. We need to write following code:
JOptionPane.showMessageDialog(null, “Your Message”);
- Above written code can be associated with the event of any component for which you want to display message.
- After executing above written code, if you get error, than you need to import JOptionPane at the top of the program as given below:
Example: Display a message on the click of a button.
How to display a message using Text Field?
- Create a form using a jTextField and jButton as given below
- Double click on jButton 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: Addition of two numbers
Create a GUI form using jLabel, jTextField and jButton as given below and set text property of all components as given below:
Write following code for add button and exit button as given below:
Execute the application and you will see the output screen as given below:
- In the given code
x = Integer.parseInt(jTextField1.getText());
to read numeric data input, we have used static method parseInt() of Integer class that takes String as parameter and returns its equivalent as given.
- In the given code
We have used static method toString() of Integer class to convert numeric data in string to display in Text Field.
A program is considered as finite set of instructions that are executed in a sequence. But sometimes the program may require executing some instructions conditionally or repeatedly. In such situations java provides:
- Selection structures
- Repetition structures
Repetition structure is excluded in your syllabus and hence not explained in this notes. to know more about repeatition structure click here:
Repetition Structure IT 802 Class 12 Notes
Selection structure with if else
Selection in program refers to Conditionals which means performing operations (sequence of instructions) depending on True or False value of given conditions. Structure of Conditional in a program can be as follow:
Java provides two statements for executing block of code conditionally:
- 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
if else with logical operators
Sometimes, you may want to execute a block of code based on the evaluation of two or more conditional expressions. For this, you can combine expressions using the logical && or || operators.
Example: Program to check a character is vowel or consonant
Multiple if statements
- Multiple 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 evaluation of different conditions.
Example: Program to display largest among three numbers
GUI Form Design
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