0% found this document useful (0 votes)
22 views5 pages

Unit 4 - QB

Uploaded by

aruna.aids114
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)
22 views5 pages

Unit 4 - QB

Uploaded by

aruna.aids114
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/ 5

UNIT IV GENERIC PROGRAMMING AND EVENT DRIVEN PROGRAMMING

Introduction to Generic Programming – Generic classes – Generic Methods – BoundedTypes –


Restrictions and Limitations. Graphics Programming using AWT: Frame –Components -
Working with Color, Font, and Image – Layout Management - Basics of event handling – Java
Event classes and Listener interfaces - Adaptor classes – MouseEvent, KeyEvent, WindowEvent,
ActionEvent, ItemEvent, Dialog Boxes
PART -A
1 Define Graphics Programming.
Graphics programming in Java is the process of using the Graphics class to draw on
components and off-screen images in Java applications. The Graphics class is the base class
for all graphics contexts and includes methods for drawing strings, lines, shapes, and
images. It also manages a graphics context, which allows drawing on the screen by drawing
pixels that represent graphical objects and text
2 Define Event Listener.
The Event listener represent the interfaces responsible to handle events. Java provides us
various Event listener classes. Every method of an event listener method has a single
argument as an object which is subclass of EventObject class. For example, mouse event
listener methods will accept instance of MouseEvent, where MouseEvent derives from
EventObject.
3 What is the purpose of the KeyEvent() method?
In Java, the KeyEvent method is a low-level event that indicates a keystroke has occurred in a
component. The method is generated by a component object, such as a text field, when a key
is pressed, released, or typed.
There are three types of KeyEvent that correspond to these three actions:
✓ keyPressed: Called when the user presses a key
✓ keyReleased: Called when the user releases a key
✓ keyTyped: Called when the user types a character
4 How to declare a java generic bounded type parameter?
1. List the type parameter’s name,
2. Along with the extends keyword
3. And by its upper bound. (which in the below example c is A.)
Syntax
<T extends superClassName>
Note that, in this context, extends is used in a general sense to mean either “extends” (as in
classes). Also, This specifies that T can only be replaced by superClassName or subclasses
of superClassName. Thus, a superclass defines an inclusive, upper limit.
5 How Events are handled?
Event Handling is the mechanism that controls the event and decides what should happen if an
event occurs. This mechanism have the code which is known as event handler that is executed
when an event occurs. Java Uses the Delegation Event Model to handle the events.
6 Why do we need generics in Java?
Code that uses generics has many benefits over non-generic code: Stronger type checks

1
at compile time. A Java compiler applies strong type checking to generic code and
issues errors if the code violates type safety. Fixing compile-time errors is easier than
fixing runtime errors,which can be difficult to find.
7 How to create generic class?
A class that can refer to any type is known as generic class. Here, we are using T type
parameter to create the generic class of specific type.
Example to create and use the generic class.
class MyGen<T>{
T obj;
void add(T obj)
{
this.obj=obj;
}
T get()
{
return obj;
}}

Creating generic class:


The T type indicates that it can refer to any type (like String, Integer, Employee etc.).
The type you specify for the class, will be used to store and retrieve the data.
8 How to declare a java generic bounded type parameter?
To declare a bounded type parameter, list the type parameter’s name, followed by the
extends keyword, followed by its upper bound, similar like below method.
Public static<T extends Comparable<T>>int compare(Tt1, Tt2)
{ return t1.compareTo(t2);}
The invocation of these methods is similar to unbounded method except that if we will
try to use any class that is not Comparable, it will throw compile time error.Bounded type
parameters can be used with methods as well as classes and interfaces
9 State the two challenges of generic programming in virtual machines.
✓ Generics are checked at compile-time for type-correctness. The generic type
information is then removed in a process called type erasure.
✓ Type parameters cannot be determined at run-time
10 What are wildcards in generics?
In generic code, the question mark (?), called the wildcard, represents an unknown
type. The wildcard can be used in a variety of situations: as the type of a parameter, field,
or local variable; sometimes as a return type (though it is better programming practice to
be more specific).
11 When to use bounded type parameter?
There may be times when you want to restrict the types that can be used as type arguments
in a parameterized type. For example, a method that operates on numbers might only want
to accept instances of Number or its subclasses. This is what bounded type parameters are

2
for.
12 What are the restrictions on generics?
To use Java generics effectively, it must consider the following restrictions:
✓ Cannot Instantiate Generic Types with Primitive Types
✓ Cannot Create Instances of Type Parameters
✓ Cannot Declare Static Fields Whose Types are Type Parameters
✓ Cannot Use Casts or instance of With Parameterized Types
✓ Cannot Create Arrays of Parameterized Types
✓ Cannot Create, Catch, or Throw Objects of Parameterized Types
✓ Cannot Overload a Method Where the Formal Parameter Types of Each Overload
Erase to the Same Raw Type
13 Mention some Event Classes and Interface
Event Classes Description Listener Interface
generated when button is pressed, menu-
ActionEvent ActionListener
item is selected, list-item is double clicked
generated when mouse is dragged,
MouseEvent moved,clicked,pressed or released and MouseListener
also when it enters or exit a component
generated when input is received from
KeyEvent KeyListener
keyboard
generated when check-box or list item is
ItemEvent ItemListener
clicked
generated when value of textarea or
TextEvent TextListener
textfield
is changed
generated when component is hidden,
ComponentEvent ComponentEventListener
moved, resized or set visible
generated when component is added or
ContainerEvent ContainerListener
removed from container
14 How Does a radio button in java differ from a check box?
✓ Radio buttons are used when there is a list of two or more options that are mutually
exclusive and the user must select exactly one choice. In other words, clicking a non-
selected radio button will deselect whatever other button was previously selected in
the list.
✓ Checkboxes are used when there are lists of options and the user may select any
number of choices, including zero, one, or several. In other words, each checkbox is
independent of all other checkboxes in the list, so checking one box doesn't uncheck
the others.
15 Components of Event Handling
Event handling has three main components,

3
✓ Events : An event is a change in state of an object.
✓ Events Source : Event source is an object that generates an event.
✓ Listeners : A listener is an object that listens to the event. A listener gets notified
when an event occurs.
16 How Events are handled in java ?
A source generates an Event and send it to one or more listeners registered with the
source.Once event is received by the listener, they process the event and then return.
Events are supported by a number of Java packages, like java.util, java.awt and
java.awt.event.
17 What is a layout manager and what are different types of layout managers available in
java AWT?
A layout manager is an object that is used to organize components in a container. The
different layouts are available are FlowLayout, BorderLayout, CardLayout, GridLayout
and GridBagLayout.
18 Mention the Differences between AWT and swing
Java AWT Java Swing
AWT components are platform- Java swing components are
dependent. platform-
independent.
AWT components are heavyweight. Swing components are lightweight.
AWT provides less components than Swing provides more powerful
Swing. components such as tables, lists,
scrollpanes, colorchooser,
tabbedpane etc.
AWT doesn't follows MVC Swing follows MVC.
19 What is the difference between scrollbar and scrollpane?
A Scrollbar is a Component, but not a Container whereas Scrollpane is a Conatiner and
handles its own events and perform its own scrolling.
20 What is the use of JButton and mention the constructor of JButton class.
JButton class provides functionality of a button. JButton class has three
constuctors,Button(Iconic),JButton(String str),JButton(String str, Icon ic)
PART –B & C
1 Describe the key aspects of working with color, font, and image in AWT. Provide examples
demonstrating their usage in a graphical application.
2 Illustrate briefly about the working of frames and setting the properties to it.
3 Explain in detail about event classes provided by java.
4 Discuss the benefits of using generic classes in software development. How generic classes
differ from generic method. Give its Restrictions and Limitations.
5 Which steps are must for event handling and what are the models available for event
handling?
6 Explain in detail about generic classes and methods in java with suitable example.
7 Develop a java program that have 11 text fields one submit button. When you press the

4
button first 10 text field’s average has to be displayed in the 11th text field.
8 What is an adapter class? Describe about various adapter classes in detail?
9 Explain the layout managers in Java also describe the concept of menu creation.
10 What is event handling in java? List out the available event classes and listener
interfaces with suitable example

You might also like