Java Study Material
Java Study Material
UNIT-I
Data Types and Variables: The Simple Types - Literals - Variables - Type
Conversion and Casting - Automatic Type Promotion in Expressions -Arrays
Strings - Classes and Methods: Class Fundamentals - Declaring Class Objects
Constructors - Garbage Collection - The finalize() Method - Overloading Methods -
Argument Passing - Recursion - Understanding Static - Access Control--: The main
( ) method.
UNIT- II
Operators: Arithmetic Operators - Bit wise Operators - Relational
OperatorsBoolean Logical Operators - The Assignment Operator - The? Operator-
The Dot Operator - Operator Precedence - Inheritance, Packages, and Interfaces:
Inheritance - Using Super - When Constructors are called - Method Overriding -
Abstract Classes - The final Keyword -Packages - Importing Packages - Access
ControlInterfaces - Keyword Summary.
UNIT- III
The Language Classes and Interfaces - The Utility Classes and Interfaces - The
Input/Output Classes and Interfaces.
UNIT-IV
The Networking Classes and Interfaces - The Java Applet Class and Interfaces.
IT- V
The Abstract Window Toolkit Classes and Interfaces - The Event Classes and
Interfaces. .
Text Book :
1."Java - Programmer's Reference", Herbert Schildt
with Joe O'Neil, Tata McGraw Hill, 1998.
Reference Books:
1. "Internet Programming", Kris James Ph.D., and Ke
n Cope, Galgotia Publication, Reprint 2000
2. "Complete Reference", 'Patrick Naughton and Herb
ert Schildt, 3rd Edition,Tata McGraw Hill
Publishing Company Ltd., 199
JAVA PROGRAMMING
UNIT-I
TWO MARK
1.What is java?
local variable
instance variable
static variable
6.What is an Object?
Object is an instance of a class. It has state,behaviour and identity. It is also called as
an
instance of a class.
1. class A{
2. int data=50;//instance variable
3. static int m=100;//static variable
4. void method(){
5. int n=90;//local variable
6. }
7. }//end of class
If the two types are compatible, then Java will perform the conversion automatically.
An automatic type conversion will be used if the following two conditions are met:
An array is a container object that holds a fixed number of values of a single type.
The length of an array is established when the array is created. After creation, its length is
fixed.
An array of 10 elements.
15.Define class.
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}
Constructor in java is a special type of method that is used to initialize the object. Java
constructor is invoked at the time of object creation. It constructs the values i.e. provides
data for the object that is why it is known as constructor.
17. What are the Types of java constructors?
It frees memory allocated to objects that are not being used by the program any more -
hence the name "garbage". For example:
If a class have multiple methods by same name but different 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.
20.What is recursion?
5 MARK
Distributed
Interpreted
The Java compiler generates byte-codes, rather than native machine code. To actually run
a Java program, you use the Java interpreter to execute the compiled byte-codes. Java
byte-codes provide an architecture-neutral object file format. The code is designed to
transport programs efficiently to multiple platforms.
-around development
source code
Robust
Java has been designed for writing highly reliable or robust software:
uage restrictions (e.g. no pointer arithmetic and real arrays) to make it impossible
for applications to smash memory (e.g overwriting memory and corrupting data)
automatic garbage collection, which prevents memory leaks
-time checking so bugs can be found early; this is repeated at
runtime for flexibilty and to check consistency
Secure
ostile compiler
Architecture-Neutral
architecture
Portable
compliant .
High-Performance
Dynamic
Local variables:
Example:
Here, age is a local variable. This is defined inside pupAge() method and its scope is
limited to this method only.
void barking(){
}
void hungry(){
}
void sleeping(){
}
}
Arrays
An array is a container object that holds a fixed number of values of a single type. The
length of an array is established when the array is created. After creation, its length is
fixed. You have seen an example of arrays already, in the main method of the "Hello
World!" application. This section discusses arrays in greater detail.
An array of 10 elements.
Each item in an array is called an element, and each element is accessed by its numerical
index..
You can place strings of text into arrays. This is done in the same way as for integers:
aryString[0]="This";
aryString[1]="is";
aryString[2]="a";
aryString[3]="string";
aryString[4] = "array";
The code above sets up a string array with 5 positions. Text is then assigned to each
position in the array.
example
class ArrayDemo {
public static void main(String[] args) {
// declares an array of integers
int[] anArray;
10 MARK
Arrays
An array is a container object that holds a fixed number of values of a single type. The
length of an array is established when the array is created. After creation, its length is
fixed. You have seen an example of arrays already, in the main method of the "Hello
World!" application. This section discusses arrays in greater detail.
An array of 10 elements.
Each item in an array is called an element, and each element is accessed by its numerical
index..You can place strings of text into arrays. This is done in the same way as for
integers:
aryString[0]="This";
aryString[1]="is";
aryString[2]="a";
aryString[3]="string";
aryString[4] = "array";
The code above sets up a string array with 5 positions. Text is then assigned to each
position in the array.
example
class ArrayDemo {
public static void main(String[] args) {
// declares an array of integers
int[] anArray;
Copying Arrays
The System class has an arraycopy method that you can use to efficiently copy data from
one array into another:
class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
Caffeine
A constructor is a bit of code that allows you to create objects from a class. You call the
constructor by using the keyword new, followed by the name of the class, followed by
any necessary parameters. For example, if you have a Dog class, you can create new
objects of this type by saying new Dog().
access NameOfClass(parameters) {
initialization code
}
where
The term "constructor" is misleading since, as soon as you enter the constructor, the new
object has actually been created for you. The job of the constructor is to ensure that the
new object is in a valid state, usually by giving initial values to the instance variables of
the object. So a "constructor" should really be called an "initializer."
Every class has at least one constructor. There are two cases:
1. If you do not write a constructor for a class, Java generates one for you. This
generated constructor is called a default constructor. It's not visible in your code,
but it's there just the same. If you could see it, it would look like this (for the class
Dog):
public Dog() { }
Notice that this default constructor takes no arguments and has a body that does
nothing.
2. If you do write a constructor for your class, Java does not generate a default
constructor. This could be a problem if you have pre-existing code that uses the
default constructor.
Example constructors:
// second constructor
public Dog(String name, String breed) {
this.name = name;
this.breed = breed;
}
}
To avoid having to use different names for the same thing, the second constructor uses a
simple trick. The parameter names are the same as the names of some instance variables;
to distinguish the two, this.variable refers to the instance variable, while variable refers
to the parameter. This naming convention is very commonly used.
OOPs Concept
Method Overloading is a feature that allows a class to have two or more methods having
same name, if their argument lists are different. In the last tutorial we discussed
constructor overloading that allows a class to have more than one constructors having
different argument lists.
As discussed above, method overloading can be done by having different argument list.
Lets see examples of each and every case.
When methods name are same but number of arguments are different.
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
Output:
a
a 10
In the above example – method disp() has been overloaded based on the number of
arguments – We have two definition of method disp(), one with one argument and
another with two arguments.
In this example, method disp() is overloaded based on the data type of arguments – Like
example 1 here also, we have two definition of method disp(), one with char argument
and another with int argument.
class DisplayOverloading2
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(int c)
{
System.out.println(c );
}
}
class Sample2
{
public static void main(String args[])
{
DisplayOverloading2 obj = new DisplayOverloading2();
obj.disp('a');
obj.disp(5);
}}
Output:
a
5
Here method disp() is overloaded based on sequence of data type of arguments – Both the
methods have different sequence of data type in argument list. First method is having
argument list as (char, int) and second is having (int, char). Since the sequence is
different, the method can be overloaded without any issues.
class DisplayOverloading3
{
public void disp(char c, int num)
{
System.out.println("I’m the first definition of method disp");
}
public void disp(int num, char c)
{
System.out.println("I’m the second definition of method disp" );
}
}
class Sample3
{
public static void main(String args[])
{
DisplayOverloading3 obj = new DisplayOverloading3();
obj.disp('x', 51 );
obj.disp(52, 'y');
}
}
Output:
UNIT-II
TWO MARKS
3.What gives java it’s “write once and run anywhere” nature?
All Java programs are compiled into class files that contain bytecodes. These byte
codes can be run in any platform and hence java is said to be platform independent.
Arithmetic operators perform the same basic operations you would expect if you used
them in mathematics (with the exception of the percentage sign). They take two operands
and return the result of the mathematical calculation.
Bitwise operators perform operations on the bits of their operands. The operands can
only be byte, short, int, long or char data types. For example, if an operand is the number
48, the bitwise operator will perform its operation on the binary representation of 48 (i.e.,
110000).
6.Precedence order.
When two operators share an operand the operator with the higher precedence goes first.
For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3
since multiplication has a higher precedence than addition.
Finalize () method is used just before an object is destroyed and can be called just prior
to garbage collection.
11.What is an Interface?
Interface is an outside view of a class or object which emphaizes its
abstraction while hiding its structure and secrets of its behaviour.
5 MARK
Arithmetic operators are used in mathematical expressions in the same way that they
are used in algebra. The following table lists the arithmetic operators:Assume integer
variable A holds 10 and variable B holds 20, then:
Show Examples
Show Examples
13.Operator precedence
Java has well-defined rules for specifying the order in which the operators in an
expression are evaluated when the expression has several operators. For example,
multiplication and division have a higher precedence than addition and subtraction.
Precedence rules can be overridden by explicit parentheses.
Precedence order.
When two operators share an operand the operator with the higher precedence goes first.
For example, 1 + 2 * 3 is treated as 1 + (2 * 3), whereas 1 * 2 + 3 is treated as (1 * 2) + 3
since multiplication has a higher precedence than addition.
Associativity.
When an expression has two operators with the same precedence, the expression is
evaluated according to its associativity. For example x = y = z = 17 is treated as x = (y =
(z = 17)), leaving all three variables with the value 17, since the = operator has right-to-
left associativity (and an assignment statement evaluates to the value on the right hand
side). On the other hand, 72 / 2 / 3 is treated as (72 / 2) / 3 since the / operator has left-to-
right associativity.
++ pre-increment
-- pre-decrement
+ unary plus
2 right to left
- unary minus
! logical NOT
~ bitwise NOT
() cast
3 right to left
new object creation
*
/ multiplicative 4 left to right
%
+ - additive
5 left to right
+ string concatenation
<< >>
shift 6 left to right
>>>
< <=
relational
> >= 7 left to right
type comparison
instanceof
==
equality 8 left to right
!=
There is no explicit operator precedence table in the Java Language Specification and
different tables on the web and in textbooks disagree in some minor ways.
If a class inherits a method from its super class, then there is a chance to override the
method provided that it is not marked final.
The benefit of overriding is: ability to define a behavior that's specific to the subclass
type which means a subclass can implement a parent class method based on its
requirement.
Example:
class Animal{
In the above example, you can see that the even though b is a type of Animal it runs the
move method in the Dog class. The reason for this is: In compile time, the check is made
on the reference type. However, in the runtime, JVM figures out the object type and
would run the method that belongs to that particular object.
Therefore, in the above example, the program will compile properly since Animal class
has the method move. Then, at the runtime, it runs the method specific for that object.
This program will throw a compile time error since b's reference type Animal doesn't
have a method by the name of bark.
The argument list should be exactly the same as that of the overridden method.
The return type should be the same or a subtype of the return type declared in the
original overridden method in the superclass.
The access level cannot be more restrictive than the overridden method's access
level. For example: if the superclass method is declared public then the
overridding method in the sub class cannot be either private or protected.
Instance methods can be overridden only if they are inherited by the subclass.
A method declared final cannot be overridden.
A method declared static cannot be overridden but can be re-declared.
If a method cannot be inherited, then it cannot be overridden.
A subclass within the same package as the instance's superclass can override any
superclass method that is not declared private or final.
A subclass in a different package can only override the non-final methods
declared public or protected.
An overriding method can throw any uncheck exceptions, regardless of whether
the overridden method throws exceptions or not. However the overriding method
should not throw checked exceptions that are new or broader than the ones
declared by the overridden method. The overriding method can throw narrower or
fewer exceptions than the overridden method.
Constructors cannot be overridden.
10 MARK
java provides a rich set of operators to manipulate variables. We can divide all the Java
operators into the following groups:
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
Arithmetic operators are used in mathematical expressions in the same way that they are
used in algebra. The following table lists the arithmetic operators:
Show Examples
Show Examples
Java defines several bitwise operators, which can be applied to the integer types, long,
int, short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b
= 13; now in binary format they will be as follows:
a = 0011 1100
b = 0000 1101
-----------------
~a = 1100 0011
Show Examples
Assume Boolean variables A holds true and variable B holds false, then:
Show Examples
Show Examples
Misc Operators
Conditional Operator ( ? : ):
Conditional operator is also known as the ternary operator. This operator consists of three
operands and is used to evaluate Boolean expressions. The goal of the operator is to
decide which value should be assigned to the variable. The operator is written as:
Value of b is : 30
Value of b is : 20
instanceof Operator:
This operator is used only for object reference variables. The operator checks whether the
object is of a particular type(class type or interface type). instanceof operator is wriiten
as:
If the object referred by the variable on the left side of the operator passes the IS-A check
for the class/interface type on the right side, then the result will be true. Following is the
example:
true
This operator will still return true if the object being compared is the assignment
compatible with the type on the right. Following is one more example:
class Vehicle {}
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.
When we talk about inheritance, the most commonly used keyword would be extends
and implements. These words would determine whether one object IS-A type of another.
By using these keywords we can make one object acquire the properties of another
object.
IS-A Relationship:
IS-A is a way of saying : This object is a type of that object. Let us see how the extends
keyword is used to achieve inheritance.
Now, based on the above example, In Object Oriented terms, the following are true:
With use of the extends keyword the subclasses will be able to inherit all the properties of
the superclass except for the private properties of the superclass.
We can assure that Mammal is actually an Animal with the use of the instance operator.
Example:
public class Dog extends Mammal{
true
true
true
Since we have a good understanding of the extends keyword let us look into how the
implements keyword is used to get the IS-A relationship.
The implements keyword is used by classes by inherit from interfaces. Interfaces can
never be extended by the classes.
Example:
public interface Animal {}
If the methods of the inner class can only be accessed via the instance of the inner class,
then it is called inner class.
It loads the class into the ClassLoader. It returns the Class. Using that you can get the
instance ( ―class-instance‖.newInstance() ).
6. What is an Interface?
Interface is an outside view of a class or object which emphaizes its
abstraction while
hiding its structure and secrets of its behaviour.
1. the Standard I/O (in package java.io), introduced since JDK 1.0 for stream-based
I/O, and
2. the New I/O (in packages java.nio), introduced in JDK 1.4, for more efficient
buffer-based I/O.
A stream is a sequential and contiguous one-way flow of data.Java does not differentiate
between the various types of data sources or sinks (e.g., file or network) in stream I/O.
1. Open an input/output stream associated with a physical device (e.g., file, network,
console/keyboard), by constructing an appropriate I/O stream instance.
2. Read from the opened input stream until "end-of-stream" encountered, or write to
the opened output stream (and optionally flush the buffered output).
3. Close the input/output stream.
5 MARK
1.Write notes on Languages in Java programming.
Boolean class --- Encapsulates a Boolean value and the constants are Flase and
True.Boolean,Boolean Value,getboolean
Byte class ------ Encapsulates a Byte value and two byte constants are Min-value and
Max-value.Byte,bytevalue,decode,doublevalue.
Character class ----- Encapsulates a Character value and two int constants are Max-radix
and Min-radix.Character,charvalue,digit,hashcode.
The Class Class ---------Encapsulates the rub-time state of an
object.forName,getinterface,getname,getsuperclass.isinterface.
2.Write notes on Thread.
Thread class encapsulates a thread of execution and provides several methods help
manages threads.
Public thread(),publicThread(runnablethreadOb),publicThread(runnable threadOb,string
threadName)
3.Explain the utility classes in java.
Classes found in java.util as bitset,calendar,date,dictionary,eventobject,locale,hash table.
Defines Enumeration,Evenlistener,observer.
4.Briefly explain the stringtokenizer class.
Stringtokenizer class used to break a string into its indidual tokens.
Nexttoken method(),hasmoretokens()method,hasmoreelements() nextelement()
methods.counttokens- public int counttokens()
5.Explain the bufferedinputstream class in brief.
BufferedInputStream class allows to wrap any InputStream into a buttfered stream and
achieve a performance improvement. Classes are FilterInpputStream,InputStream
10 MARK
1. Explain in detail about the String Class in java programming to
manipulate the string.
String - Provide a set of methods to manipulate a string.Classes are StringBuffer. Public
string(),public string(byte asciichars[]),public string(byte asciichars[],int
highorderbyte)public string(byte asciichars[],int start,int size)
3.Explain the file class to manipulate the information with disk file or
directory.
File does not operate on streams.Most common methods are
FileDescriptor,File,canRead,canWrite,delete,equals,exists,getAbsolutepath,getname,getp
arent,getpath,hashcode
UNIT IV
2 MARK
1.What is the networking classes for web-based programming?
Java.net contains classes as
contenthandler,inetaddress,URL,datagrampacket,datagramsocket
serversocket,URLconnection,URLencoder, URLstreamhandler.
2.What URLEncoder class?
URLEncoder class contains static method that takes a String object and converts to
corresponding URL-encoded.
3.Mention about InetAddress class.
InetAddress class provides methods for working with internet addresses.
4.Define AppletClass.
Applet class contains several methods to control over the execution of applet.All applets
are subclasses of Applet.
5.How the destroy the execution environment before an applet is
terminated.
Using public void destroy() method.The execution environment is terminated before an
applet.
6.What is the use of Audioclip interface?
AudioClip is an interface to get and control audio files.
7.Mention the different classes of Applet methods.
Component,Container,panel are the classes of Applet methods.
8.What is SocketClass?
Socket Class is designed to connect to server sockets and initiate protocol exchanges.
9.What is the setSoTimeout?
Sets the timeout period for invoking serversocket.Determines how long accept() waits for
a connection request.
To create a URL object is from a String that represents the human-readable form of the
URL address. Another person can use a URL.Java program, using a String containing
the text to create a URL object:
13.What is Bind?
public void bind(SocketAddress addr)
throws SocketException
5 MARK
1.Briefly write note on TCP and UDP.
TCP
TCP provides a point-to-point channel for applications that require reliable
communications. The Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP),
and Telnet are all examples of applications that require a reliable communication
channel. TCP (Transmission Control Protocol) is a connection-based protocol that
provides a reliable flow of data between two computers.
UDP
The UDP protocol provides for communication that is not guaranteed between two
applications on the network. UDP is not connection-based like TCP. Rather, it sends
independent packets of data, called datagrams, from one application to another. UDP
(User Datagram Protocol) is a protocol that sends independent packets of data, called
datagrams, from one computer to another with no guarantees about arrival. UDP is not
connection-based like TCP.
Three classes which are modifications of the three classes from the previous example:
MulticastServer, MulticastServerThread, and MulticastClient. This discussion
highlights the interesting parts of these classes.
Four methods in the Applet class give you the framework on which you build any serious
applet:
init: This method is intended for whatever initialization is needed for your applet.
It is called after the param tags inside the applet tag have been processed.
start: This method is automatically called after the browser calls the init method.
It is also called whenever the user returns to the page containing the applet after
having gone off to other pages.
stop: This method is automatically called when the user moves off the page on
which the applet sits. It can, therefore, be called repeatedly in the same applet.
destroy: This method is only called when the browser shuts down normally.
Because applets are meant to live on an HTML page, you should not normally
leave resources behind after a user leaves the page that contains the applet.
paint: Invoked immediately after the start() method, and also any time the applet
needs to repaint itself in the browser. The paint() method is actually inherited
from the java.awt.
The applet displays "initializing the applet. Starting the applet” click inside the rectangle
"mouse clicked" be displayed as well.
10 MARK
java.applet
Class Applet
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
getProtocol
Returns the protocol identifier component of the URL.
getAuthority
Returns the authority component of the URL.
getHost
Returns the host name component of the URL.
getPort
Returns the port number component of the URL. The getPort method returns an
integer that is the port number. If the port is not set, getPort returns -1.
getPath
Returns the path component of this URL.
getQuery
Returns the query component of this URL.
getFile
Returns the filename component of the URL. The getFile method returns the same
as getPath, plus the concatenation of the value of getQuery, if any.
getRef
Returns the reference component of the URL.
UNIT V
2 MARK
1.What is Event classes?
Event classes represent the event. Java provides us various Event classes but we will
discuss those which are more frequently used.
2.How to declare the class for Eventobject class in java?
Following is the declaration for java.util.EventObject class:
Constructs a new menu with the specified label, indicating whether the menu can be torn
off.
Frame(String title) : Constructs a new, initially invisible Frame object with the specified
title.
2.static int SAVE -- This constant value indicates that the purpose of the file
dialog window is to locate a file to which to write.
It implements all optional list operations and it also permits all elements, includes
null.
It provides methods to manipulate the size of the array that is used internally to
store the list.
The constant factor is low compared to that for the LinkedList implementation.
The TextArea control in AWT provide us multiline editor area. The user can type here as
much as he wants. When the text in the text area become larger than the viewable area the
scroll bar is automatically appears which help us to scroll the text up & down and right &
left.
5 MARK
1.Brifely write about AWTEvent Classes utilized in AWT Event class.
AWTEvent:
It is the root event class for all AWT events. This class and its subclasses supercede the
original java.awt.Event class.
ActionEvent:
The ActionEvent is generated when button is clicked or the item of a list is double
clicked.
InputEvent:
The InputEvent class is root event class for all component-level input events.
KeyEvent:
On entering the character the Key event is generated.
MouseEvent:
This event indicates a mouse action occurred in a component.
TextEvent:
The object of this class represents the text events.
WindowEvent:
The object of this class represents the change in state of a window.
AdjustmentEvent:
The object of this class represents the adjustment event emitted by Adjustable objects.
ComponentEvent:
The object of this class represents the change in state of a window.
ContainerEvent:
The object of this class represents the change in state of a window.
MouseMotionEvent:
The object of this class represents the change in state of a window.
PaintEvent:
The object of this class represents the change in state of a window.
Class declaration
Field
static int COMPONENT_FIRST -- The first number in the range of ids used for
component events.
static int COMPONENT_HIDDEN --This event indicates that the component was
rendered invisible.
static int COMPONENT_LAST -- The last number in the range of ids used for
component events.
static int COMPONENT_MOVED -- This event indicates that the component's
position changed.
static int COMPONENT_RESIZED -- This event indicates that the component's
size changed.
static int COMPONENT_SHOWN -- This event indicates that the component was
made visible.
Class constructors
Constructor & Description
ComponentEvent(Component source, int id)
Constructs a ComponentEvent object.
Class methods
Method & Description
Component getComponent()
Returns the originator of the event.
String paramString()
Returns a parameter string identifying this event.
Methods inherited
java.awt.AWTEvent
java.util.EventObject
java.lang.Object
Dialog control represents a top-level window with a title and a border used to take
some form of input from the user.
Class declaration
java.awt.Dialog class:
public class Dialog
extends Window
Class constructors
Class methods
Methods inherited
java.awt.Window
java.awt.Component
java.lang.Object
A Graphics object encapsulates all state information required for the basic rendering
operations that Java supports. State information includes the following properties.
KEY_PRESSED
KEY_RELASED
KEY_TYPED
10 MARK
1.Discuss the AWT checkbox class constructors and methods in java.
Checkbox control is used to turn an option on(true) or off(false). There is label for each
checkbox representing what the checkbox does.The state of a checkbox can be changed
by clicking on it.
Class declaration:
Class constructors:
Checkbox() :
Creates a check box with an empty string for its label.
Checkbox(String label) :
Creates a check box with the specified label.
Checkbox(String label, boolean state) :
Creates a check box with the specified label and sets the specified state.
Checkbox(String label, boolean state, CheckboxGroup group) :
Constructs a Checkbox with the specified label, set to the specified state, and in the
specified check box group.
Checkbox(String label, CheckboxGroup group, boolean state) :
Creates a check box with the specified label, in the specified check box group, and
set to the specified state.
Methods inherited:
java.awt.Component
java.lang.Object
The layout manager automatically positions all the components within the container. If
we do not use layout manager then also the components are positioned by the default
layout manager. It is possible to layout the controls by hand but it becomes very difficult
because of the following two reasons.
Java provide us with various layout manager to position the controls. The properties like
size,shape and arrangement varies from one layout manager to other layout manager.
When the size of the applet or the application window changes the size, shape and
arrangement of the components also changes in response i.e. the layout managers adapt to
the dimensions of appletviewer or the application window.
The layout manager is associated with every Container object. Each layout manager is an
object of the class that implements the LayoutManager interface.
LayoutManager:
The LayoutManager interface declares those methods which need to be implemented by
the class whose object will act as a layout manager.
LayoutManager2:
The LayoutManager2 is the sub-interface of the LayoutManager.This interface is for
those classes that know how to layout containers based on layout constraint object.
Following is the list of commonly used controls while designed GUI using AWT.
BorderLayout:
The borderlayout arranges the components to fit in the five regions: east, west, north,
south and center.
CardLayout:
The CardLayout object treats each component in the container as a card. Only one card
is visible at a time.
FlowLayout:
The FlowLayout is the default layout.It layouts the components in a directional flow.
GridLayout:
The GridLayout manages the components in form of a rectangular grid.
GridBagLayout:
This is the most flexible layout manager class.The object of GridBagLayout aligns the
component vertically,horizontally or along their baseline without requiring the
components of same size.
3.Discuss the WindowEvent to implement AWT WindowListener
Interface in detail.
The class which processes the WindowEvent should implement this interface.The object
of that class must be registered with a component. The object can be registered using the
addWindowListener() method.
Interface declaration:
java.awt.event.WindowListener interface:
Methods inherited
java.awt.EventListener