0% found this document useful (0 votes)
15 views10 pages

AJP Practical 10 12

Uploaded by

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

AJP Practical 10 12

Uploaded by

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

Practical 10

Aim:

Write a Program to demonstrate the use of JTextField and JPassword Using


Listener Interface.
Theory:

Overview of Components :

1. JTextField:
o A JTextField is a component that allows users to enter and edit a single
line of text.
o It is commonly used for inputs like usernames, email addresses, or any
other single-line data.
o You can retrieve the inputted text using the getText() method.
o
2. JPasswordField:
o A JPasswordField is similar to JTextField, but it masks the input
characters (typically displaying asterisks or dots).
o This component is designed for sensitive data, like passwords, to
prevent onlookers from seeing what is being typed.
o You can retrieve the entered password using the getPassword()
method, which returns a character array for security reasons.
o
Listener Interface
 ActionListener: This interface is part of the Java AWT event package and is
used to handle action events (like button clicks).

When the user triggers an action (for example, clicking a button), the
actionPerformed method is invoked, allowing you to define the behavior in response
to that action.

Event Handling Process

1. Registering Listeners: You need to register a listener with a component (like a


button) to handle user actions. This is typically done by calling the
addActionListener method on the component.
2. Handling Events: When the registered action occurs (e.g., the button is
clicked), the actionPerformed method is called. You can then implement logic
to handle the input from JTextField and JPasswordField.
3. Input Validation: Inside the actionPerformed method, you can validate the
user inputs, process them, or trigger other actions, such as showing
messages, navigating to different screens, etc.

Program For Demonstration of JTextField :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JTextAdd implements ActionListener


{
JTextField tf , tf1 ;
JLabel res;

JTextAdd()
{
JFrame f = new JFrame();

f.setVisible(true);
f.setSize(200,200);
f.setLayout(new FlowLayout());

JLabel jl = new JLabel("Enter 1st Number:");


tf = new JTextField(5);
jl.setBounds(0,0,100,70);

JLabel jl1 = new JLabel("Enter 2nd Number:");


tf1 = new JTextField(5);
res = new JLabel("Addition");

tf1.addActionListener(this);

f.add(jl);
f.add(tf);
f.add(jl1);
f.add(tf1);
f.add(res);
tf.setBounds(0,0,100,70);
tf1.setBounds(0,0,100,70);
}
public static void main(String[] args) {
JTextAdd jt = new JTextAdd();
}

public void actionPerformed(ActionEvent ae)


{
String str1 = tf.getText();
double fn = Double.parseDouble(str1);
double sn = Double.parseDouble(tf1.getText());

res.setText("Multiplication is " + (fn*sn));


}
}

Output :

Program For Demonstration Of JPasswordField :

import javax.swing.*;
import java.awt.*;

public class JPasswordChange


{
public static void main(String[] args) {
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(400,400);
f.setLayout(new FlowLayout());

JPasswordField pf = new JPasswordField(20);

pf.setEchoChar('#');

f.add(pf);
}

Output :

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 11

Aim:

Write a Program to demonstrate the use of InetAddress class and its factory
methods.
Theory:

Overview of InetAddress :

The InetAddress class in Java is part of the java.net package and is used to
represent an Internet Protocol (IP) address. It provides methods to handle both IPv4
and IPv6 addresses, allowing for operations such as hostname resolution and IP
address manipulation.

Purpose of InetAddress

1. Hostname Resolution: Convert hostnames (e.g., "www.example.com") into IP


addresses.
2. IP Address Representation: Provide a way to represent IP addresses in a
Java application.
3. Network Communication: Facilitate network communication by allowing
applications to work with IP addresses and hostnames.

Key Features

 Static Factory Methods: The InetAddress class provides several static factory
methods for creating instances of InetAddress.
 Retrieving Local Host Information: You can get the IP address and hostname
of the local machine.
 Network Utilities: Useful for network programming, such as server-client
communication.

Here’s a list of the factory methods provided by the InetAddress class in


Java:

1. static InetAddress getByName(String host)


o Resolves the specified hostname (or IP address) into an
InetAddress object.
2. static InetAddress getByAddress(byte[] addr)
o Creates an InetAddress object from a raw IP address in byte array
form.
3. static InetAddress getByAddress(String host, byte[] addr)
o Creates an InetAddress object for the specified hostname with a
given IP address in byte array form.
4. static InetAddress[] getAllByName(String host)
o Resolves a hostname into an array of InetAddress objects. This is
useful for hosts with multiple IP addresses.
5. static InetAddress getLocalHost()
o Returns an InetAddress object that represents the local host (the
machine on which the program is running).

Program :

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

public class RetriveIP


{
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter Host Name: ");


String host = sc.nextLine();

try
{
InetAddress ip = InetAddress.getByName(host);

System.out.println("IP Adress of Computer is:"+ip.getHostAddress());


}
catch(UnknownHostException e)
{
System.out.print(e);
}

}
}

Output :

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 12

Aim:

Write a Program to demonstrate the use of URL and URLConnection class and its
methods.
Theory:

Overview of URL and URLConnection

URL (Uniform Resource Locator)

 The URL class in Java represents a URL, which is a pointer to a resource on


the World Wide Web.
 It encapsulates various components of a URL, such as the protocol (HTTP,
HTTPS, FTP, etc.), host, port, path, and query parameters.
 The URL class provides methods for creating, parsing, and manipulating

.
URLConnection

 The URLConnection class represents a connection to a URL. It provides


methods to read from and write to a resource identified by a URL.
 This class is a superclass for classes that represent different types of
connections, such as HttpURLConnection for HTTP connections.

URL Class
 Creating a URL: You can create a URL object from a string
representation of a URL.
 Parsing Components: The URL class provides methods to retrieve
different parts of the URL, such as:
o getProtocol(): Returns the protocol (e.g., "http").
o getHost(): Returns the host name or IP address.
o getPort(): Returns the port number.
o getPath(): Returns the file path.
o getQuery(): Returns the query string.
o
URLConnection Class

 Opening a Connection: You can obtain a URLConnection object by


calling the openConnection() method on a URL object.
 Setting Request Properties: The URLConnection class allows you to set
request headers and properties (like user-agent, content-type).
 Reading Data: You can read data from the resource using input streams
provided by the URLConnection class.
 Handling Input and Output Streams:
o getInputStream(): Returns an input stream for reading data from
the resource.
o getOutputStream(): Returns an output stream for writing data to the
resource (typically used in HttpURLConnection).

Program :

import java.net.URL;
import java.net.MalformedURLException;
public class URLRetrive
{
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("https://www.spotify.com/");
System.out.println("Authority: "+ url.getAuthority());
System.out.println("Default Port: "+ url.getDefaultPort());
System.out.println("File: "+ url.getFile());
System.out.println("Path: "+ url.getPath());
System.out.println("Protocol: "+ url.getProtocol());
System.out.println("Reference: "+ url.getRef());
}
}
Output :

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)

You might also like