Tuesday, October 21, 2014

WBUT 5 years java solutions for BCA, BTech, MCA Course



1.Write a program to create a window and set its title with "IETE" using AWT.(WBUT 2013)
Sol:

import java.awt.*;
import java.awt.event.*;
class example1 extends Frame
{
            public example1(String title)
            {
                        super(title);
                        MyWindowAdapter adapter=new MyWindowAdapter(this);
                        addWindowListener(adapter);
            }
            class MyWindowAdapter extends WindowAdapter
            {
                        example1 e1;
                        public MyWindowAdapter(example1 e1)
                        {
                                    this.e1=e1;
                        }
                        public void windowClosing(WindowEvent ae)
                        {
                                    e1.dispose();
                        }
            }
            public static void main(String args[])
            {
                        Frame f1=new Frame("IETE" );
                        f1.setVisible(true);
                        f1.setSize(200,200);
            }
}

Output:

 
2. How will you call parameterized constructor and override method from parent class in sub-class ?
(WBUT 2013)
or

In Java, explain how to call a constructor from another. (WBUT 2013)

Sol:
Definition of Constructor:
A constructor is a special member function whose have the same name as the class name, but the function (constructor) never returns any value. A constructor initializes an objects immediately upon creation. Once defined, the constructor is automatically called immediately after the object is created.

Parameterized Constructor:
            Passing arguments to the function is called parameterized function. Similarly passing arguments to the constructor is called parameterized constructor.

Method overriding:
            When a method in a subclass has the same name and type signature as a method in its super class , then the method in the subclass is said to be override the method in the super class.

 
Describe all above feature with the following example:

class A
{

            int x,y;
            A()
            {          // zero argument constructor
            }
            A(int i,int j) // parameterized constructor
            {
                        x=i;y=j;
            }
            void show() // override methods by subclass version
            {
                        System.out.println("x="+x+" y="+y);
            }
}
class B extends A
{
            int z;
            B(int i, int j, int k) // parameterized constructor
            {
                        super(i,j);
                        z=k;
            }
            void show()
            {
                        System.out.println("z="+z);
            }
}

class override
{
            public static void main(String args[])
            {
                        B subob=new B(1,2,3); // call constructor by creating objects
                        subob.show(); // this calls subclass version
            }
}

3. Indicate the difference between PATH and CLASSPATH. (WBUT 2013)

4.Illustrate the uses of 'this' and 'super' keywords. (WBUT 2013)
“this” Keyword:
     Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. We can use this anywhere a reference to an object of the current class’s type is permitted.

From above example:
     B(int i, int j, int k) // parameterized constructor
            {
                        this.x=i;
                        this.y=j;
                        this.z=k;
            }

“super” keyword:
            super has two general forms: i) The first calls the superclass’s constructor. ii) super used to access a member of the superclass that has been hidden by a member of a subclass.
From above example:
B(int i, int j, int k) // parameterized constructor
            {
                        super(i,j);
                        z=k;
            }

Class A{
int i;
}
class B extends A{
int i;
B(int i1,int j1)
{
   super.i=i1;
   i=j1;
}
}


5. What are Adapter classes? (WBUT 2013)
     Java provides a special feature, called an adapter class that can simplify the creation of event handles in certain situations. An adapter class provides an empty implementation of all methods in an event listener interface. Adapter classes are useful when we want to receive and process only some the events that are handled by a particular event listener interface. We can define a new class to act as an event listener by extending one of the adapter classes and implementing only those events in which we are interested.
Ex of adapter class:
i)                    Component Adapter
ii)                   Container Adapter
iii)                 Focus Adapter
iv)                 Key Adapter
v)                  Mouse Adapter
vi)                 Window Adapter

NB: Example is given in first program.

6. What is multithreading? What are the two different ways to create multithreaded program?
Sol:
     Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, & each thread defines a separate form of execution. Thus, multithreading is a specialized form of multitasking.

     Multithreading enables us to write very efficient programs that maximize the CPU utilization. 

7. Display Traingle as follow: (WBUT 2012)
1
2 3
4 5 6
7 8 9 10 ... N 

Sol:
class triangle
{
            public static void main(String args[])
            {
                        int i,j,n=4,k=1;
                        for(i=0;i<n;i++)
                        {
                                    for(j=0;j<=i;j++)
                                    {
                                                System.out.print(" "+k);
                                                k++;
                                    }
                                    System.out.println();
                        }
            }
}
8.                  What is the package? What is the difference between throw and throws keywords? (WBUT 2012)
Sol:
1st Part:
Packages are the containers for the classes that are used to keep the class name space compartmentalized. Packages are stored in a hierarchical manner and are explicitly imported into new class definitions.
     To create the packages use the “package” command as follows:
     package p1;
Here p1 is name of the package and we insert the package p1 in the program with the import statements as follows:
     import p1;
Some packages are
i)                    import java.awt.*;
ii)                   import java.awt.event.*;
iii)                 import java.applet.*;

2nd Part:
            throw:

            User can catching exceptions that are thrown by the Java run – time system. However, it is possible for program to throw an exception explicitly, using the throw statement. The general form of throw is shown here:
                        throw ThrowableInstance;
Here, ThrowableInstance must be an object of type Throwable or a subclass of Throwable. There is two ways obtain a Throwable object: using a parameter into a catch clause, or creating one with the new operator.
            The flow of execution stops immediately after the throw statement; any subsequent statements are not executed.

            Class bug extends Exception
            {
            ………//statement
}         
            ……
…….
                                    int i=10;
                                    try
{
            if(i<12) throw new OutOfRange();
}catch(OutOfRange e)
{
            …….//statement
}

3rd Part:
            throws:

If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception. We do this by including a throws clause in the method declaration. A throw clause lists the types of exception that a method might throw. This is necessary for all exception, except those of type Error or RuntimeException or any of their subclasses. All other exceptions that a method can throw must be declared in the throws clause. If they are not, a compile – time error will result.
            type method – name (parameter list) throws exception list
            {
            // body of the mewthod
}

6 comments:

  1. This is really useful piece of information shared by you. I am sure it might help many java earners. Thank you for sharing this information here
    Home Tutors in Delhi | Home Tutors Delhi

    ReplyDelete
  2. Nice blog, it's so knowledgeable, informative, and good looking site. I appreciate your hard work. Good job. Thank you for this wonderful sharing with us. Keep Sharing.
    home tutor in Allahabad | Home Tutor in Allahabad

    ReplyDelete
  3. However, some of these dreams get modified as time goes by. New ideas crop up and get integrated into the dreams. Those who started with the Toklaapp.com purpose of sharing their thoughts, updating their albums, providing tutorials or solving problems begin to see why they should add a little way of making money so as to keep Isshtech.com maintaining their blogs or take care of other personal needs.

    Blogging is a business and needs to be treated as such. Climge.com Stop treating it with levity because your future could largely depend on it. If you want your blogging business to prosper, you must be willing to invest money, time and intellectuality into it. Freebies alone can never give you the best in your business. Remember that you don't pay the bills, the customers do. So never treat your customers Isshpath.com shabbily because they are the reason you are still in business.

    You have shared very good article so Bookmarked this website . keep it up and again thanks.
    Thetodaytalk.com .

    ReplyDelete