Web Technology
Web Technology
The applet life cycle can be defined as the process of how the
object is created, started, stopped, and destroyed during the
entire execution of its application. It basically has five core
methods namely init(), start(), stop(), paint() and
destroy().These methods are invoked by the browser to execute.
There are five methods of an applet life cycle, and they are:
o init(): The init() method is the first method to run that initializes the applet.
It can be invoked only once at the time of initialization.
o start(): The start() method contains the actual code of the applet and starts
the applet. It is invoked immediately after the init() method is invoked. It
is in an inactive state until the init() method is invoked.
o stop(): The stop() method stops the execution of the applet. The stop ()
method is invoked whenever the applet is stopped.
o destroy(): The destroy() method destroys the applet after its work is done.
o paint(): The paint() method belongs to the Graphics class in Java. It is used
to draw shapes like circle, square, trapezium, etc., in the applet. It is
executed after the start() method and when the browser or applet
windows are resized.
3. Give example of Constructor of Applet Class.
{
System.out.println("Stopping an applet");
}
public void destroy()
{
System.out.println("Destroying an applet");
}
}
Advantage of Applet
It works at client side so less response time.
Secured
It can be executed by browsers running under many
plateforms, including Linux, Windows, Mac Os etc.
Drawback of Applet
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
c:\>javac First.java
c:\>appletviewer First.java
1. Explain applet architecture?
We know that java provides console-based programming language
environment and window-based programming environment. An
applet is a window-based programming environment. So applet
architecture is different than console base program.
Java applets are essentially java window programs that can run
within a web page. Applete programs are java classes that extend
that java.applet. Applet class and are enabled by reference with
HTML page. You can observed that when applet are combined with
HTML, thet can make an interface more dynamic and powerful than
with HTML alone
While some Applet do not more than scroll text or play movements,
but by incorporating these basic features in a webpage you can make
them dynamic. These dynamic web page can be used in an enterprise
application to view or manipulate data comming from some source
on the server.
The Applet and there class files are distribute through standard
HTTP request and therefore can be sent across firewall with the web
page data. Applete code is referenced automatically each time the
user revisit the hosting website. Therefore keeps full application up
to date on each client desktop on which it is running.
2. What are five methods can be assembled into the skeleton?
3.How to Change Background And Foreground Color of Applet in Java?
The Color class provides various methods to use any color you want in display. It defines
various color constants which can be directly used only by specifying the color of your
choice. In addition, the Color class allows creation of millions of colors. The Color class
contains three primitive colors namely, red, blue and green and all other colors are a
combination of these three colors. One of the constructors that is used to create color of
your choice is
Color (int red, int green, int blue)
where,
red, green, blue can take any value between 0 and 255.
Setting Background and Foreground Color
To set the color of the background of an applet window, setBackground () method is used.
The general form of the setBackground () method is
void setBackground(mycolor)
Similarly, to set the foreground color to a specific color, that is, the color of text,
setForeground () method is used.
The general form of the setForeground () method is
void setForeground(mycolor)
where,
mycolor is one of the color constants or the new color created by the user
The list of color constants is given below:
• Color.red
• Color.orange
• Color.gray
• Color.darkGray
• Color.lightGray
• Color.cyan
• Color.pink
• Color.white
• Color.blue
• Color.green
• Color.black
• Color.yellow
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class ColorApplet extends Applet
{
public static void main(String[] args)
{
Frame ForegroundBackgroundColor = new Frame("Change
Background and Foreground Color of Applet");
ForegroundBackgroundColor.setSize(420, 180);
Applet ColorApplet = new ColorApplet();
ForegroundBackgroundColor.add(ColorApplet);
ForegroundBackgroundColor.setVisible(true);
ForegroundBackgroundColor.addWindowListener(new
WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0); }
});
}
public void paint(Graphics g)
{
Color c = getForeground();
setBackground(Color.yellow);
setForeground(Color.red);
g.drawString("Foreground color set to red", 100, 50);
g.drawString(c.toString(), 100, 80);
g.drawString("Change Background and Foreground Color of
Applet", 50, 100);
}
}
4. How do you extract the directory holding the HTML file that
started the applet and the directory from which the applet's
class file was loaded?
1.What are classes in Java with example?
Create a Class
Main.java
int x = 5;
Create an Object
Example
Create an object called "myObj" and print the value of x:
int x = 5;
System.out.println(myObj.x);
}
2 . Define constructors with example?
1) No-argument constructor
2) Parameterized Constructor
Example:
public Puppy() {
}
}
The rules for writing constructors are as follows:
1. The constructor (s) of a class must have the same name as the class name in
which it resides.
Declaring Classes
class MyClass {
// field, constructor, and
// method declarations
}
This is a class declaration. The class body (the area between the
braces) contains all the code that provides for the life cycle of
the objects created from the class: constructors for initializing
new objects, declarations for the fields that provide the state of
the class and its objects, and methods to implement the behavior
of the class and its objects.
You can also add modifiers like public or private at the very
beginning—so you can see that the opening line of a class
declaration can become quite complicated. The
modifiers public and private, which determine what other classes
can access MyClass, are discussed later in this lesson. The lesson
on interfaces and inheritance will explain how and why you would
use the extends and implements keywords in a class declaration.
For the moment you do not need to worry about these extra
complications.
In general, class declarations can include these components, in
order:
int total=0;
int marks[] = new int[3]:
try {
BufferedReader br= newBufferedReader( new InputStreamReader(System.in));
for(int i=0; i<n 1; i++) {
marks[i]= Integer.parselnt(br.readLine());
total+=marks[i];
}
}
catch(Exception e) {
System.out.println("Array out of range");
}
return total;
}
public static void main(String args[]) throws IOException {
int n,Max;
System.out.println("Enter the numbers :");
Max = CalculateTotal(3);
System.out.println("Sum of the numbers is:" +Max);
}
}
The output of Program is: 2 , 3 , 4
Sum of the numbers is: 9
In Program the value 3 has been passed to the method Calculate Total. The
argument n1 of Calculate Total method takes its value as 3 and performs the
rest of the execution of program.
Pass by reference : In the pass by reference mechanism, when parameters are passed to the
methods, the calling method returns the changed value of the variables to the called
method. The call by reference mechanism is not used by Java for passing parameters to the
methods. Rather it manipulates objects by reference and hence all object variables are
referenced. Program illustrates the call by reference mechanism in Java.
If a class has multiple methods having same name but different in parameters,
it is known as Method Overloading. If we have to perform only one operation,
having same name of the methods increases the readability of the program.
There are two ways to overload the method in java
In this example, we are creating static methods so that we don't need to create
instance for calling methods.
Output: 22 ,33 .
If subclass (child class) has the same method as declared in the parent class, it
is known as method overriding in Java. In other words, If a subclass provides
the specific implementation of the method that has been declared by one of
its parent class, it is known as method overriding.
The JVM firstly checks whether the exception is handled or not. If exception is
not handled, JVM provides a default exception handler that performs the
following tasks: 1.Prints out exception description.
2.Prints the stack trace (Hierarchy of methods where the exception occurred).
3.Causes the program to terminate.
2. Explain throw and throws in Java?
Throw: The throw keyword in Java is used to explicitly throw an exception from
a method or any block of code. We can throw either checked or unchecked
exception. The throw keyword is mainly used to throw custom exceptions.
Syntax:
throw Instance
Example :
throw new ArithmeticException( "/ by zero" ) ;
The flow of execution of the program stops immediately after the throw
statement is executed and the nearest enclosing try block is checked to see if it
has a catch statement that matches the type of exception. If it finds a match,
controlled is transferred to that statement otherwise next enclosing try block is
checked and so on. If no matching catch is found then the default exception
handler will halt the program.
Syntax:
type method _ name (parameters) throws exception _ list
exception _ list is a comma separated list of all the
exceptions which a method might throw.
An exception is an issue (run time error) that occurred during the execution of
a program. When an exception occurred the program gets terminated abruptly
and, the code past the line that generated the exception never gets executed.
Java provides us the facility to create our own exceptions which are basically
derived classes of Exception. Creating our own Exception is known as a
custom exception or user-defined exception. Basically, Java custom
exceptions are used to customize the exception according to user needs. In
simple words, we can say that a User-Defined Exception or custom exception
is creating your own exception class and throwing that exception using the
‘throw’ keyword.
For example, MyException in the below code extends the Exception class
The java.awt package provides classes for AWT API such as TextField, Label,
TextArea, RadioButton, CheckBox, Choice, List etc.
The AWT tutorial will help the user to understand Java GUI programming in simple
and easy steps.
AWT is platform-independent:
Java AWT calls the native platform calls the native platform (operating systems)
subroutine for creating API components like TextField, ChechBox, button, etc.
For example, an AWT GUI with components like TextField, label and button will
have different look and feel for the different platforms like Windows, MAC OS, and
Unix. The reason for this is the platforms have different view for their native
components and AWT directly calls the native subroutine that creates those
components.
In simple words, an AWT application will look like a windows application in
Windows OS whereas it will look like a Mac application in the MAC OS.
3. Explain the Hierarchy of AWT?
Components: All the elements like the button, text fields, scroll bars, etc. are
called components. In Java AWT, there are classes for each component as shown
in above diagram.
Container: The Container is a component in AWT that can contain another
components like buttons, textfields, labels etc. The classes that extends
Container class are known as container such as Frame, Dialog and Panel.
Types of containers:
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. We need to create an
instance of Window class to create this container.
Panel: The Panel is the container that doesn't contain title bar, border or menu
bar. It is generic container for holding the components. It can have other
components like button, text field etc. An instance of Panel class creates a
container, in which we can add components.
Frame:
The Frame is the container that contain title bar and border and can have menu
bars. It can have other components like button, text field, scrollbar etc. Frame is
most widely used container while developing an AWT application.
4. Give the example of AWT by Inheritance?
Let's see a simple example of AWT where we are inheriting Frame
class. Here, we are showing Button component on the Frame.
AWTExample1.java
import java.awt.*;
AWTExample1() {
b.setBounds(30,100,80,30);
add(b);
setSize(300,300);
setVisible(true);
}
}
1. Explain about Inner Class in Java?
An inner class in Java is defined as a class that is declared inside
another class. Inner classes are often used to create helper
classes, such as views or adapters that are used by the outer class.
Inner classes can also be used to create nested data structures,
such as a linked list.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
Arithmetic multiplicative * / %
additive + -
equality == !=
bitwise exclusive ^
OR
bitwise inclusive |
OR
logical OR ||
Ternary ternary ? :
Class and objects: A class in Java is a user defined data type i.e. it is
created by the user. It acts a template to the data which consists of
member variables and methods. An object is the variable of the class,
which can access the elements of class i.e. methods and variables.
Character streams have two main abstract classes, Reader and Writer,
which control the flow of Unicode characters. The Reader class is an
abstract class that defines character streaming input. The Writer class
is an abstract class that defines character stream output. In case of
errors, all class methods throw an IOException .
2. Which super structure class allows reading data from an input byte
stream in the format of primitive data types?
To read byte data (not strings), the DatalnputStream class is used . In
this case, you must use the classes from the InputStream group .
To convert a string to a byte array suitable for placement into a
ByteArraylnputStream stream, the getBytes () method is provided in
the Stringclass. The resulting ByteArraylnputStream is an
InputStream stream suitable for passing a DatalnputStream .
import java.io.FileWriter;
import java.io.IOException;
}
4. Explain byte stream and character stream with
examples?
Byte Streams:
Java byte streams are used to perform input and output of
8-bit bytes. Though there are many classes related to byte
streams but the most frequently used classes are,
FilelnputStream and FileOutputStream. Following is an
example which makes use of these two classes to copy an
input file into an output file –
Character Streams:
Java Byte streams are used to perform input and output of
8-bit bytes, whereas Java Character streams are used to
perform input and output for 16-bit Unicode. Though there
are many classes related to character streams but the
most frequently used classes are, FileReader and
FileWriter. Though internally FileReader uses Filel
nputStream and FileWriter uses FileOutputStream but here
the major difference is that FileReader reads two bytes at
a time and FileWriter writes two bytes at a time.
1.State difference between HTML and XHTML?
HTML XHTML
2) Ordered List:
3) Description List:
3. Explain HTML Tables with examples?
HTML Table is an arrangement of data in rows and columns, or possibly in a
more complex structure.Tables are widely used in communication, research,
and data analysis. Tables are useful for various tasks such as presenting text
information and numerical data. It can be used to compare two or more
items in the tabular form layout. Tables are used to create databases.
Defining Tables in HTML: An HTML table is defined with the "table" tag. Each
table row is defined with the "tri' tag. A table header is defined with the "th"
tag. By default, table headings are bold and centered. A table data/cell is
defined with the "td" tag.
<!DOCTYPE html>
<html>
<head>
<title>HTML Tables</title>
</head>
<body>
<table border = "1">
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>
</body>
</html>
4. Explain HTML style sheet with example?
Cascading Style Sheets (CSS) provide easy and effective
alternatives to specify various attributes for the HTML tags. Using
CSS, you can specify a number of style properties for a given HTML
element. Each property has a name and a value, separated by a colon
(:). Each property declaration is separated by a semi-colon (;).
Example
First let's consider an example of HTML document which makes use
of <font> tag and associated attributes to specify text color and font
size –
<!DOCTYPE html>
<html>
<head>
<title>HTML CSS</title>
</head>
<body>
<p><font color = "green" size = "5">Hello, World!</font></p>
</body>
</html>
We can re-write above example with the help of Style Sheet as
follows −
<!DOCTYPE html>
<html>
<head>
<title>HTML CSS</title>
</head>
<body>
<p style = "color:green; font-size:24px;" >Hello, World!</p>
</body>
</html>
This will produce the following result -
Hello, World!
4. What are the String Comparison Methods?
The String class includes a number of methods that compare strings or
substrings within strings.
i. equals(Object anObject)
ii. equalslgnoreCase(String str)
iii. regionMatches( )
iv. startsWith( ) methods
v. endsWith( ) methods
vi. equals( ) Versus ==
vii. compareTo( )
viii. compareTolgnoreCase(String str)
3. What is the difference between Array List and Linked List?
ArrayList LinkedList