0% found this document useful (0 votes)
4 views37 pages

Introduction to Java

Java is a widely used high-level object-oriented programming language suitable for various applications, including database, desktop, mobile, and web-based applications. The document explains key concepts such as Java IDE, GUI components, object-oriented programming principles, and the process of creating and managing Java projects using NetBeans. It also covers the creation of classes and objects, access modifiers, variable declaration, and basic GUI project setup and execution.

Uploaded by

techytanzo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views37 pages

Introduction to Java

Java is a widely used high-level object-oriented programming language suitable for various applications, including database, desktop, mobile, and web-based applications. The document explains key concepts such as Java IDE, GUI components, object-oriented programming principles, and the process of creating and managing Java projects using NetBeans. It also covers the creation of classes and objects, access modifiers, variable declaration, and basic GUI project setup and execution.

Uploaded by

techytanzo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Introduction to Java

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
• Compiler
• Debugger
• Form designer etc.
• It helps in writing code, compiling and executing java
programs easily.
What is NetBeans?
• open source IDE used for writing java code, compiling and
executing java programs conveniently.
Java GUI Components (Properties &
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
• JFrame is a container control in java where other
controls are placed. It is a window with title, border,
menu bar (optional).
jButton
• a button is control that is used to trigger specific action
when pressed.
jTextField
• jTextField is a control which allows
entering/editing/displaying information in single line.
• It is also considered as input box.
jLabel
• jLabel is used to display information which can not be
edited.
• It can display image, text or both text and image.
TextArea
• It allows accept/display multiple line text.
jPassword
• 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,
*.
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.
jCheckBox
• 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.
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
• Class
• Object
• 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.
Syntax:
Class <class_name>
{
Data_type variable_name;
Data_type variable_name;
Return_type method_name (list of parameters separated by comma)
{
Statements;
….
Return statements; //optional
}
}
Creating Class Object
• <class name> <object name> = new <class name()>;
Access Modifier
• Access Modifiers in java are keywords that specify the
accessibility scope of data members of class.
• Java offers following Access Modifiers:
• private
• public
• protected
Variables & Data Types

• Variables
• 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 types
• 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.
• Declaring variable
• <Data type> <variable name>;
• int x;
float degree;
int a,b,c;
char ac_no;
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.
Creating 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
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 written the code is called the Source window.
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:
import.javax.swing.JOptionPane;
OR
import javax.swing.*;
display a message using 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

You might also like