Java Programming - A+b
Java Programming - A+b
Semester 5
Applications qs
B
c. Create the following form in Java:-
Subject should display name of subjects :
C++
VB
Java
ActionListener
AdjustmentListener
ComponentListener
ContainerListener
Focus Listener
ItemListener
KeyListener
MouseListener
MouseMotionListener
MouseWheelListener
TextListener
WindowFocusListener
WindowListener
Each source must provide code for registering and unregistering (if needed) the
listener, which receives event notifications. .
e.g. KeyEvent is generated when keybaord input occurs. In this class, integer
constants are defined for
ASCII equivalents of numbers and letters: VK_0 through VK_9 and VK_A
through V_Z
KeyEvent and MouseEvent are subclasses of the abstract class InputEvent, a subclass
of ComponentEvent
Methods for keyboard and mouse event listeners are found in the Component class.
modifiers: modifiers that were pressed when this key event occurred
code: VK coke
Some common methods are defined for all events, in for example KeyEvent,
char getKeyChar( )
int getKeyCode( )
Each listener must have been registered with one or more sources, and must provide
implementation methods to receive and process the notifications.
5 What is a static method?
Why cant a static method use this and super? Explain with
example
The purpose of a static method in Java is to have a unit of program is
independent of any of its instances. No updates on a static variable or method. It
can be declared in a class, so it can be inherited and passed information in
subclasses. Main() is declared like this, only once and before any object exists. A
static member is accessible before any objects of its class are created, and
outside its class. Two ways to access static methods are with import static
statement or a dot operator reference ,
Example
import static java.lang.Math.pow; //can use this method by name i.e. pow
import static java.lang.*; //can use all and any method of this class
Math.pow() //have to reference the class to use this method, useful in avoiding
namespace collisions when the method is not used much
A static method is independent of the instance of an object, class, interface. It
does not have a current instance. Therefore they cannot use 'this'. Static
methods can only call other static methods and can access static data. Therefore
they cannot use 'super'.
A static block can be used to initialise variables of a class, before they are used.,
when the class is first loaded.
Static methods can be declared in an interface, and can be called without an
instance of the interface.
Methods of local inner classes can't be marked as static.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
add(displayShape);
displayShape.addActionListener(this);
super.paint(g);
if (displayShape) {
g.drawOval(45, 75, 75);
g.drawRect(10, 10, 60, 50);
g.drawOval(10, 10, 50, 50);
}
}
8 What are the most important features of Java? How does Java achieve
platform independence?
From the whitepapers, Java is described as