Java
Java
Number of Credits: 5
Internet: Internet, Connecting to Internet: Telephone, Cable, Satellite Connection, Choosing an ISP,
Introduction to Internet Services, E-Mail Concepts, Sending and Receiving Secure E-Mail, Voice and
Video Conferencing.
INTERNET
• The Internet is a network of networks that connects computers all over the world.
• The Internet has its roots in the U.S. military, which funded a network in 1969, called the
ARPANET, to connect the computers at some of the colleges and universities where military
research took place. As more and more computers connected, the ARPANET was replaced by the
NSFNET, which was run by the National Science Foundation.
• By the late 1980s, the Internet had shed its military and research heritage and was available for
use by the general public..
CONNECTING to INTERNET: TELEPHONE,
CABLE, SATELLITE CONNECTION
Your computer is connected to the Internet if it is connected to another computer or network that
is connected to the Internet.
• ISP Features
To choose an ISP, consider the following factors:
➢ Local phone number
➢ Price
➢ Software
➢ Support
➢ Speed
➢ Accessibility
INTRODUCTION to INTERNET SERVICES
Many services are available over the Internet, and the following are the most popular ones:
• E-mail
• Usenet newsgroups
• Online chat
• Voice and video conferencing
• The World Wide Web
• File transfer
E-MAIL CONCEPTS
E-mail is a means of communication. E-mail has its drawbacks too because e-mail lacks the nuances
of face-to-face or phone conversation, an e-mail message can be more easily misunderstood than
verbal communication.
➢ Digital certificates
➢ PGP
VOICE AND VIDEO CONFERENCING
A step beyond written messages is by adding voice and videoconferencing to our kit of
communication tools. Three programs for this are Microsoft NetMeeting, Netscape Conference,
and CU-SeeMe.
• Voice conferencing is talking to another person via the microphone and speakers connected to
your computer.
• Videoconferencing is sending our image and voice to one or more other people, through the
camera and microphone attached to computer, and receiving pictures and voices back.
➢ Does the person you want to talk to have a computer and the hardware and software required
for conferencing?
➢ A computer isn’t as portable as cellular phone; will this lack of portability affect our
conferencing?
➢ Since both parties have to be using their computers at the same time, how will we schedule our
conversation?
VOICE AND VIDEO CONFERENCING contd…
• Equipments required for Conferencing
➢ Conferencing Hardware
Speakers, microphone, and a camera.
➢ Conferencing Software
Java applications
According to Sun, 3 billion devices run Java. There are many devices where Java is currently used.
Some of them are
• Desktop Applications such as acrobat reader, media player, antivirus, etc.
• Web Applications such as irctc.co.in, javatpoint.com, etc.
• Enterprise Applications such as banking applications.
• Mobile
• Embedded System
• Smart Card
• Robotics
• Games, etc.
INTRODUCTION contd…
• JavaFX
It is used to develop rich internet applications. It uses a light-weight user interface API.
INTRODUCTION contd…
Features of Java
The features of Java are also known as java buzzwords. A list of most important features of Java
language are
• Simple
• Object-Oriented
• Portable
• Platform independent
• Secured
• Robust
• Architecture neutral
• Interpreted
• High Performance
• Multithreaded
• Distributed
• Dynamic
INTRODUCTION contd…
public class a
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
To compile and run the above program, go to the current directory first c:\ Write here:
To Compile: javac a.java
To execute : java a
OPERATOR
Operator in Java is a symbol which is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
• Unary Operator,
• Arithmetic Operator
• Shift Operator
• Relational Operator
• Bitwise Operator
• Logical Operator
• Ternary Operator
• Assignment Operator.
Data Types in Java
Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:
• Primitive data types: These are the most basic data types available in Java language. There are 8
types of primitive data types.The primitive data types include boolean, char, byte, short, int,
long, float and double.
• Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
VAR
• Java Variables
A variable is a container which holds the value while the Java program is executed. A variable is
assigned with a data type. Variable is a name of memory location.
class a {
int i=5 ; // instance variable
static int j=6 ; // static variable
void n ()
{
int k= 7 ; // local variable
}
}
CONTROL STATEMENTS (If-Else)
The Java if statement is used to test the condition. It checks boolean condition: true or false.
➢ if statement
➢ if-else statement
➢ if-else-if ladder
➢ nested if statement
➢ Java if Statement
CONTROL STATEMENTS(If-Else) contd…
➢ The Java if statement tests the condition. It executes the if block if condition is true.
Syntax:
if(condition) {
//code to be executed
}
➢ The Java if-else statement also tests the condition. It executes the ‘if block’ if condition is true
otherwise else block is executed.
if(condition) {
//code if condition is true
}
else {
//code if condition is false
}
CONTROL STATEMENTS (If-Else) contd…
if-else statement
public class a
{
public static void main(String[] args)
{
int n=10; //defining a variable
if(n%2==0) //Check if the number is divisible by 2 or not
{
System.out.println(“Even number");
}
else
{
System.out.println(“Odd number");
}
}
}
CONTROL STATEMENTS (Switch)
The Java switch statement executes one statement from multiple conditions. It is like if-else-
if ladder statement.
Syntax:
switch(expression) {
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
CONTROL STATEMENTS (Switch) contd…
public class S
{
public static void main(String[] args)
{
int n=1; //Declaring a variable for switch
switch(n) //Switch expression
{
case 1: System.out.println("10"); //Case statements
break;
case 2: System.out.println("20");
break;
• for loop
• while loop
• do-while loop
class arr
{
public static void main(String args[])
{
int a[]=new int[5]; //declaration and instantiation
a[0]=2; //initialization
a[1]=4;
a[2]=6;
a[3]=7;
a[4]=3;
}
}
EXCEPTION HANDLING
The Exception Handling in Java is the mechanism to handle the runtime errors so that normal
flow of the application can be maintained e.g. ClassNotFoundException, IOException,
SQLException, RemoteException, etc.
• Starting a thread:
start() method of Thread class is used to start a newly created thread.
MULTITHREAD PROGRAMMING contd…
class ct
{
public static void main(String args[])
{
d objd=new d();
e obje=new e();
objd.start();
obje.start();
}
}
MULTITHREAD PROGRAMMING contd…
class cu
{
public static void main(String args[])
{
d objd=new d();
e obje=new e();
objd.setPriority(Thread.MIN_PRIORITY);
obje.setPriority(Thread.MAX_PRIORITY);
objd.start();
obje.start();
}
}
I/O
Java I/O (Input and Output) is used to process the input and produce the output . Java uses
the concept of a stream to make I/O operation fast. The java.io package contains all the
classes required for input and output operations . File handling in Java is done by Java
I/O API.
• Java FileInputStream Class
Java FileInputStream class obtains input bytes from a file. It is used for reading byte-oriented
data (streams of raw bytes) such as image data, audio, video etc
• Java Scanner
Scanner class in Java is found in the java.util package. Java provides various ways to read
input from the keyboard.
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
JAVA APPLET
Applet is a special type of program that is embedded in the webpage to generate the dynamic
content. It runs inside the browser and works at client side.
public abstract void drawString(String str, int x, int y): is used to draw the specified string.
public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified
width and height.
public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the
default color and specified width and height.
public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the
specified width and height.
public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the default
color and specified width and height.
JAVA APPLET contd…
public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the
points(x1, y1) and (x2, y2).
public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used
draw the specified image.
public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is
used draw a circular or elliptical arc.
public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is
used to fill a circular or elliptical arc.
public abstract void setColor(Color c): is used to set the graphics current color to the specified
color.
public abstract void setFont(Font font): is used to set the graphics current font to the specified
font.
JAVA APPLET contd…
import java.applet.Applet;
import java.awt.Graphics;
public class ap extends Applet
{
System.out.println(s1.equals(s2));
System.out.println(s1==s2);
System.out.println(s1+" "+s2);
System.out.println(s1.concat(s2));
}
}
NETWORKING
Java Networking is a concept of connecting two or more computing devices together so that we
can share resources. Java socket programming provides facility to share data between different
computing devices.
• IP Address
IP address is a unique number assigned to a node of a network e.g. 192.168.0.1 . It is composed of
octets that range from 0 to 255.It is a logical address that can be changed.
• java.net package
The java.net package provides classes to deal with networking applications in Java.
e.g. InetAddress class
NETWORKING contd…
• Java InetAddress class
Java InetAddress class represents an IP address. An IP address is represented by 32-bit (IPV4)or
128-bit (IPV6) unsigned number.
Method Description
public static InetAddress getByName(String host) It returns the instance of InetAddress containing
throws UnknownHostException LocalHost IP and name.
public static InetAddress getLocalHost() throws It returns the instance of InetAdddress containing local
UnknownHostException host name and address.
import java.net.*;
class nw
{ public static void main(String args[]) throws UnknownHostException
{ InetAddress ad = InetAddress.getLocalHost();
System.out.println(ad);
ad = InetAddress.getByName("www.yahoo.com");
System.out.println(ad);
InetAddress a[] = InetAddress.getAllByName("www.yahoo.com");
for (int i=0; i<a.length; i++)
System.out.println(a[i]);
}
}
EVENT HANDLING
Changing the state of an object is known as an event. For example, click on button, dragging
mouse etc. The java.awt.event package provides many event classes and Listener interfaces for
event handling.
ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
EVENT HANDLING contd…
import java.awt.*;
import java.awt.event.*;
public class evh
{ public static void main(String args[])
{ Frame f=new Frame();
TextField t=new TextField();
t.setBounds(50,50, 150,20);
Button b=new Button("click me");
b.setBounds(50,100,60,30);
b.addActionListener ( new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ t.setText("Welcome to Java");
}
}
);
f.add(b); f.add(t); f.setSize(300,300); f.setLayout(null); f.setVisible(true);
}
}
INTRODUCTION TO AWT
Java AWT(Abstract Window Toolkit)
It is an API to develop GUI or window-based applications in Java. The
java.awt package provides classes for AWT API such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
INTRODUCTION
TO AWT contd…
➢ Window
The window is the container that have no borders and menu bars. You must use frame, dialog or another
window for creating a window.
➢ Panel
The Panel is the container that doesn't contain title bar and menu bars. It can have other components
like button, textfield etc
➢ Frame
The Frame is the container that contain title bar and can have menu bars. It can have other components
like button, textfield etc.
AWT CONTROLS
• Frame
• Label
• Button
• TextField
• TextArea
• Checkbox
• CheckboxGroup
➢ Frame
The Frame is the container that contain title bar and can have menu bars. It can have other
components like button, textfield etc.
Frame f=new Frame ( ) ;
AWT CONTROLS contd…
➢ Label
The object of Label class is a component for placing text in a container. It is used to display
a single line of read only text.
Label l=new Label("First Label.");
➢ Button
The button class is used to create a labeled button that h.as platform independent implementation.
Button b=new Button("click me");
➢ TextField
The object of a TextField class is a text component that allows the editing of a single line text.
TextField t =new TextField("Enter your name:");
AWT CONTROLS contd…
➢ TextArea
The object of a TextArea class is a multi line region that displays text. It allows the editing of
multiple line text.
TextArea a =new TextArea("Enter your name:");
➢ Checkbox
The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (false)
Checkbox c =new Checkbox("Java");
➢ CheckboxGroup
The object of CheckboxGroup class is used to group together a set of Checkbox. At a time only
one check box button is allowed to be in "on" state and remaining check box button in "off" state.
CheckboxGroup c1= new CheckboxGroup();
AWT CONTROLS contd…
import java.awt.*;
class fr
{
fr()
{ Frame f=new Frame();
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
fr f=new fr();
}
}
AWT CONTROLS contd…
import java.awt.*;
class la
{ la()
{ Frame f=new Frame();
Label l=new Label("First label");
l.setBounds(30,100,80,30);
f.add(l);
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String args[])
{ la f=new la();
}
}
AWT CONTROLS contd…
import java.awt.*;
class bu
{
bu()
{ Frame f=new Frame();
Button b=new Button("click me");
b.setBounds(30,100,80,30); // setting button position
f.add(b); //adding button into frame
f.setSize(300,300); //frame size 300 width and 300 height
f.setLayout(null); //no layout manager
f.setVisible(true); //now frame will be visible, by default not visible
}
public static void main(String args[])
{
bu f =new bu();
}
}
AWT CONTROLS contd…
import java.awt.*;
class tf
{ tf()
{ Frame f=new Frame();
TextField t =new TextField("Enter your name:");
t.setBounds(50,100,200,30);
f.add(t);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{ tf f=new tf();
}}
AWT CONTROLS contd…
import java.awt.*;
class ta
{ ta()
{ Frame f=new Frame();
TextArea a =new TextArea("Enter your name:");
a.setBounds(10,30, 300,300);
f.add(a);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{ ta f =new ta();
}}
AWT CONTROLS contd…
import java.awt.*;
class cb
{ cb()
{ Frame f=new Frame();
Checkbox c =new Checkbox("Java");
c.setBounds(10,30, 300,300);
f.add(c);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{ cb f =new cb();
}}
AWT CONTROLS contd…
import java.awt.*;
class cbgp
{ cbgp()
{ Frame f=new Frame();
CheckboxGroup c1= new CheckboxGroup();
Checkbox c2 =new Checkbox("Java",c1,true);
c2.setBounds(10,30, 300,300);
f.add(c2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{ cbgp f =new cbgp();
}}
LAYOUT MANAGERS
The LayoutManagers are used to arrange components in a particular manner.
➢ GridLayout
The GridLayout is used to arrange the components in rectangular grid. One component is
displayed in each rectangle.
f.setLayout(new GridLayout(2,2));
➢ BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south, east, west and
center. Each region (area) may contain one component only. It is the default layout of frame or
window.
f.add(b1,BorderLayout.NORTH);
LAYOUT MANAGERS contd…
➢ CardLayout
The CardLayout class manages the components in such a manner that only one component is visible
at a time.
card=new CardLayout(40,30);
➢ GridBagLayout
The Java GridBagLayout class is used to align components vertically, horizontally or along their
baseline.
GridBagLayout layout = new GridBagLayout();
➢ BoxLayout
The BoxLayout is used to arrange the components either vertically or horizontally.
setLayout (new BoxLayout(this, BoxLayout.X_AXIS));
LAYOUT MANAGERS contd…
import java.awt.*;
class fl
{ fl()
{ Frame f=new Frame();
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
f.add(b1);
f.add(b2);
f.add(b3);
f.setLayout(new FlowLayout(FlowLayout.RIGHT)); //setting flow layout of right alignment
f.setSize(300,300);
f.setVisible(true); }
public static void main(String[] args)
{ fl f= new fl(); } }
Thank You