Sunteți pe pagina 1din 12

PROGRAM BSc IT

SEMESTER THIRD
SUBJECT CODE & NAME: BT0074, OOPS with JAVA

Qus:1What are the keywords that java supports? Describe the data types available
in java programming language.
Answer:
The keywords that java supports:
Keywords are special words that are of significance to the Java compiler. There are 48 reserved
keywords currently defined in the Java language. These keywords combined with the syntax of
the operators andseparators, form the definition of the Java language. These keywordscannot be
used as names for a variable, class, or method

The keywords const and goto are reserved but not used. In addition to thekeywords, Java
reserves the following: true, false, and null. These arevalues defined by Java.
The data types available in java programming language:
Data Types in Java
There are two kinds of data types in Java:

Primitive / Standard data types.


Abstract / Derived data types.
Primitives Data Types
Primitive data types (also known as standard data types) are the data typesthat are built into the
Java language. The Java compiler holds detailedinstructions on each legal operation the data
type supports. There are eightprimitive data types in Java:

The data types byte, short, int, long, float and double are numeric data types. The first four of
these can hold only whole numbers whereas the last two (float and double) can hold decimal
values like 5.05. All these data types can hold negative values. However, the keyword unsigned
can be used to restrict the range of values to positive numbers. Amongst others, boolean can
hold only the value true or false and char can hold only a single character.
Abstract / Derived Data Types:
Abstract data types are based on primitive data types and have more functionality than the
primitive data types. For example, String is an abstract data type that can store alphabets,
digits and other special characters like /, (); :$#. You cannot perform calculations on a variable
of the String data type even if the data stored in it has digits.

Qus:2 Describe StringBuffer class in java. List all the functions relevant to it and
explain any five of them.
Answer:
StringBuffer class in java:
StringBuffer is a peer class of String that provides much of the functionality of strings. As you
know, String represents fixed-length, immutable character sequences. In contrast, StringBuffer
represents grow able and writeable character sequences. StringBuffer may have characters and
substrings inserted in the middle or appended to the end.
The functions relevant to it are:.
1.insert( ):
The insert( ) method inserts one string into another. It is overloaded toaccept values of all the
simple types, plus Strings and Objects. This string is then inserted into the invoking
StringBuffer object. These are a few of its forms:
StringBufferinsert(intindex, String str);
StringBufferinsert(intindex, char ch);
StringBufferinsert(intindex, Object obj);
Here, index specifies the index at which point the string will be inserted into the
invokingStringBufferobject.
The following sample program inserts "like" between "I" and "Java":
// Demonstrate insert().
classinsertDemo {
public static void main(String args[ ]) {
StringBuffersb = new StringBuffer("I Java!");
sb.insert(2, "like ");
System.out.println(sb);
}
}
The output of this example is shown here:
I like Java!
2. reverse( ):
You can reverse the characters within a StringBufferobject using reverse(),as shown here:
StringBufferreverse( );
This method returns the reversed object on which it was called. The following program
demonstrates reverse( ):

// Using reverse() to reverse a StringBuffer.


classReverseDemo {
public static void main(String args[ ]) {
StringBuffer s = new StringBuffer("abcdef");
System.out.println(s);
s.reverse();
System.out.println(s);
}
}
Here is the output produced by the program:
abcdef
fedcba
3. delete( ) and deleteCharAt( ):
Java 2 adds to StringBuffer the ability to delete characters using the methods delete( )
anddeleteCharAt( ). These methods are shown here:
StringBufferdelete(intstartIndex, intendIndex);
StringBufferdeleteCharAt(intloc);
The delete( ) method deletes a sequence of characters from the invokingobject. Here,
startIndexspecifies the index of the first character to remove,and endIndexspecifies an index
one past the last character to remove.
Here is a program that demonstrates the delete( ) and deleteCharAt( )methods:
// Demonstrate delete() and deleteCharAt()
classdeleteDemo {
public static void main(String args[ ]) {
StringBuffersb = new StringBuffer("This is a test.");
sb.delete(4, 7);
System.out.println("After delete: " + sb);
sb.deleteCharAt(0);
System.out.println("After deleteCharAt: " + sb);
}
}
The following output is produced:
After delete: This a test.
After deleteCharAt: his a test.
4. replace( ):

Another new method added to StringBuffer by Java 2 is replace( ). It replaces one set of
characters with another set inside a StringBufferobject. Its signature is shown here:
StringBufferreplace(intstartIndex, intendIndex, String str);
The following program demonstrates replace( ):
// Demonstrate replace()
classreplaceDemo {
public static void main(String args[ ]) {
StringBuffersb = new StringBuffer("This is a test.");
sb.replace(5, 7, "was");
System.out.println("After replace: " + sb);
}
}
Here is the output:
After replace: This was a test.
5. substring( ):
Java 2 also adds the substring( ) method, which returns a portion of aStringBuffer. It has
the following two forms:
String substring(intstartIndex);
String substring(intstartIndex, intendIndex);
The first form returns the substring that starts at start Index and runs to the end of the invoking
StringBufferobject. The second form returns the substring that starts at start Index and runs
through endIndex1. These methods work just like those defined for String that were described
earlier.

Qus:3 Describe with the help of an example the implementation of inheritance in


java.
Answer:
The implementation of inheritance in java:
The extendskeyword is used to derive a class from a superclass, or in other words, extend the
functionality of a superclass.
Syntax
public class <subclass_name> extends <superclass_name>
Example
public class Confirmed extends Ticket

{
}
Rules for Overriding Methods
The method name and the order of arguments should be identical to that of the
superclass method.
The return type of both the methods must be the same.
The overriding method cannot be less accessible than the method it overrides. For
example, if the method to override is declared as public in the superclass, you cannot
override it with the private keyword in the subclass.
An overriding method cannot raise more exceptions than those raised by the superclass.
Example
// Create a superclass.
class A {
inti, j;
voidshowij() {
System.out.println("i and j: " + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
voidshowk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
}
classSimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();
/* The subclass has access to all public members of
its superclass. */
subOb.i = 7;
subOb.j = 8;

subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}
}
The output from this program is shown here:
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
Sum of i, j and k in subOb:
i+j+k: 24
As you can see, the subclass B includes all of the members of its superclass, A. This is why
subOb can access I and j and call show ij ( ). Also, inside sum ( ), I and j can be referred to
directly, as if they were part of B.
Even though A is a superclass for B, it is also a completely independent, stand-alone class. Being
a superclass for a subclass does not mean that the superclass cannot be used by itself. Further, a
subclass can be a superclass for another subclass.
The general form of a class declaration that inherits a superclass is shown here:
classsubclass-name extends superclass-name {
// body of class
}
It can only specify one superclass for any subclass that you create. Java does not support the
inheritance of multiple superclasses into a single subclass. (This differs from C++, in which you
can inherit multiple base classes.) You can, as stated, create a hierarchy of inheritance in which a
subclass becomes a superclass of another subclass.

Qus:4Describe the catch and finally block statement in java with examples.
Answer:
The catch and finally block statement in java:
Catch block:

It can associate an exception-handler with the try block by providing one or more catch handlers
immediately after try block. The following skeletal code illustrates the use of the catch block.
try
{
//statements that may cause an exception
}
catch ()
{
// error handling code
}
The catch statement takes an object of an exception class as a parameter. If an exception is
thrown, the statements in the catch block are executed. The scope of the catch block is restricted
to the statements in the preceding try block only.
The finally Block:
When an exception is raised, the rest of the statements in the try block are ignored. Sometimes,
it is necessary to process certain statements irrespective of whether an exception is raised or not.
The finally block is used for this purpose.
try
{
openFile();
writeFile(); //may cause an exception
}
catch ()
{
//process the exception
}
In the above example, the file has to be closed irrespective of whether an exception is raised or
not. You can place the code to close the file in both the try and catch blocks. To avoid duplication
of code, you can place the code in the finally block. The code in the finally block is executed
regardless of whether an exception is thrown or not. The finally block follows the catch blocks.
You have only one finally block for an exception-handler. However, it is not mandatory to have a
finally block.
finally
{
closeFile ();
}

Qus:5 Draw and Explain the Life cycle of a java applet with an example.
Answer:
Diagram of the Life cycle of a java applet:

Diagram depicting the life cycle of an applet

Explaination of life cycle of java applet:


The life cycle of an applet can be describe by four methods. These methods are:
The init() method.
The start() method.
The stop() method.
The destroy() method.
The init() method:
The init() method is called the first time an applet is loaded into the memory of a computer. You
can initialize variables, and add components like buttons and check boxes to the applet in the
init() method.
The start() method:
The start() method is called immediately after the init() method and every time the applet
receives focus as a result of scrolling in the active window. You can use this method when you
want to restart a process, such as thread animation.
The stop() method:
The stop() method is called every time the applet loses the focus. You can use this method to
reset variables and stop the threads that are running.

The destroy() method:


The destroy() method is called by the browser when the user moves to another page. You can use
this method to perform clean-up operations like closing a file.
Example:
public void init()
{
}
All but the most trivial applets override a set of methods that provides the basic mechanism by
which the browser or appletviewer interfaces to the applet and controls its execution. Four of
these methodsinit(), start(), stop(), and destroy() are defined by Applet. Another, paint()
method, is defined by the AWT Component class. Default implementations for all of these
methods are provided. Applets do not need to override those methods that they do not use.
However, only very simple applets will not need to define all of them. These five methods can be
assembled into the skeleton shown here:
// An Applet skeleton.
import java.awt.*;
importjava.applet.*;
/*
<applet code="AppletSkel" width=300 height=100>
</applet>
*/
public class AppletSkel extends Applet {
// Called first.
public void init() {
// initialization
}
/* Called second, after init(). Also called whenever
the applet is restarted. */
public void start() {
// start or resume execution
}
// Called when the applet is stopped.
public void stop() {
// suspends execution
}
/* Called when applet is terminated. This is the last
method executed. */
public void destroy() {
// perform shutdown activities

Qus:6Explain all the steps of running the beanbox.


Answer:
The steps of running the beanbox:
The steps are as follows:

Dropping beans onto the composition window : To add a bean to the composition
window first click on the beans name or icon in the left-hand ToolBox window. Then
click on the location in the center composition window where you want the new bean to
appear. The chosen bean will appear centered at the new location and will become the
current selected bean for editing.

Selecting a bean in the composition window: The currently selected bean is


marked with a black-and-white hashed boundary. The right-hand PropertySheet window
shows its properties and the edit menu provides access to its events, bound properties,
etc. To select a bean you must click the mouse just outside of the bean in the boundary
area where the black-and-white hashed boundary appears.

Moving or resizing a bean: You can move the currently selected bean by clicking the
mouse on one of the black-and-white hashed borders and the holding the mouse down
and dragging the bean. Some beans, such as the BeanBox bean or the ExplicitButton,
also allow themselves to be resized.

Editing a beans properties : You can edit the public properties of the currently
selected bean using the right-hand PropertySheet window. For each editable property
the PropertySheet window shows the name of the property and its current value. The
current value may be shown as An editable text field.

Using a bean Customizer : For those beans that have customizers, the BeanBox
allows you to run the customizer to configure your beans. If the currently selected bean
has a customizer, then the edit menu on the central window will include a
Customize. entry.

Connecting an event handler : The BeanBox allows you to connect an event from the
currently selected bean to a target event handling method on any other bean. The edit
menu on the central composition window has an events menu that has a sub-menu for
all the different kinds of events that the currently selected bean fires

Connecting a bound property : The BeanBox allows you to connect a bound


property from a source bean to a target property on some other bean. Then when the
bound property on the source bean is changed, the associated target property is also
automatically updated.

Saving and restoring beans: Once you have set up some beans in the BeanBox, you
can use the File menus Save sub-menu to save away the current state of the
BeanBox. This uses Java Object Serialization to automatically store away all the state of
the beans into a named file.

Getting an introspection report on a beans : If you want to see all the properties,
methods, and events that the Beans introspector has found on a selected bean, you can
use the BeanBoxs report menu item under edit menu. This generates a summary
report to the standard output of the introspection information for the selected bean.

Adding your bean to the BeanBox :When the BeanBox starts it loads all the JAR
files that it finds in the jars directory. The BeanBox uses the manifest file in each JAR
file to identify any bean classes in the JAR file, and adds those beans to the ToolBox
palatte in the BeanBox.

S-ar putea să vă placă și