Advanced Java Programming 2077

Tribhuwan University
Institute of Science and Technology
2077
Bachelor Level / Seventh Semester / Science
Computer Science and Information Technology ( CSC409 )
( Advanced Java Programming )
Full Marks: 60
Pass Marks: 24
Time: 3 hours
Candidates are required to give their answers in their own words as far as practicable.
The figures in the margin indicate full marks.

Group A

Attempt any two questions. (20 x 10 =20)

1, What is the significance of stub and skeleton In RMI? Create a RMI application such that a client sends an Integer number to the server and the server return the factorial value of that integer. Give a clear specification for every step. (10)

10 marks view

2. You are hired by a reputed software company which is going to design an application for "Movie Rental System". Your responsibility is to design a schema named MRS and create a table named Movie(id, Tille, Genre, Language, Length). Write a program to design a GUI form to take input for this table and insert the data into table after clicking the OK button (10)

10 marks view

3. Describe the responsibility of Serializable interface. Write a program to read an input string from the user and write the vowels of that string in VOWEL.TXT and consonants in CONSOLNANT.TXT (2+8)

10 marks view

Group B

Attempt any eight questions. (8x5=40)

4. A non-empty array A of length n is called on array of all possibilities if it contains all numbers between 0 and A.length-1 inclusive. Write a method named isAllPossibilities that accepts an integer array and returns 1 if the array is an array of all possiblities, otherwise it returns 0. (15)

5 marks view

public static int isAllPossibilities(int[] a)

        {

            int isAllPosibilities = 1;

            if (a.Length == 0) isAllPosibilities = 0;

            for (int i = 0; i < a.Length && isAllPosibilities==1; i++)

            {

                int index = -1;

                for (int j = 0; j < a.Length && index==-1; j++)

                {

                    if (i == a[j]) index = j;

                }

                if (index == -1)

                    isAllPosibilities = 0;

            }

            return isAllPosibilities;

        }

5. Define event delegation model. Why do we need adapter class in event handling? (2+3)

5 marks view

6. What is the task of Layout manager? Describe about default layout manager. (1 +4)

5 marks view

The layout managers are used to arrange components in a particular manner. The Java LayoutManagers facilitates us to control the positioning and size of the components in GUI forms.

Border layout is the default layout manager for windows and dialog boxes.

Border Layout

The BorderLayout is used to arrange the components in five regions: north, south, east, west and center. Each region (area) may contain one component only.

Components of the BorderLayout Manager

·         BorderLayout.NORTH

·         BorderLayout.SOUTH

·         BorderLayout..EAST

·         BorderLayout.WEST

·         BorderLayout.CENTER

BorderLayout Constructors:

BorderLayout()

creates a border layout but with no gaps between the components.

BorderLayout(int hgap, int vgap)

creates a border layout with the given horizontal and vertical gaps between the components.


Example:

import java.awt.BorderLayout;  

import javax.swing.JButton;

import javax.swing.JFrame;

  

public class BorderLayoutExample {  

BorderLayoutExample(){  

    JFrame f=new JFrame();  

      

    JButton b1=new JButton("NORTH");;  

    JButton b2=new JButton("SOUTH");;  

    JButton b3=new JButton("EAST");;  

    JButton b4=new JButton("WEST");;  

    JButton b5=new JButton("CENTER");;  

      

    f.add(b1,BorderLayout.NORTH);  

    f.add(b2,BorderLayout.SOUTH);  

    f.add(b3,BorderLayout.EAST);  

    f.add(b4,BorderLayout.WEST);  

    f.add(b5,BorderLayout.CENTER);  

      

    f.setSize(300,300);  

    f.setVisible(true);  

}  

public static void main(String[] args) {  

    new BorderLayoutExample();  

}  

}  

7. When does the finally block is mandatory in while handling exception? Describe with a suitable scenario. (5)

5 marks view

Java finally block is a block used to execute important code such as closing the connection, etc. It is always executed whether an exception is handled or not. Therefore, it contains all the necessary statements that need to be printed regardless of the exception occurs or not. The finally block follows the try-catch block.

  • finally block in Java can be used to put "cleanup" code such as closing a file, closing connection, etc.
  • The important statements to be printed can be placed in the finally block.

Example:

class TestFinallyBlock {    

  public static void main(String args[]){    

  try{    

//below code do not throw any exception  

   int data=25/5;    

   System.out.println(data);    

  }    

//catch won't be executed  

  catch(NullPointerException e){  

System.out.println(e);  

}    

//executed regardless of exception occurred or not  

 finally {  

System.out.println("finally block is always executed ");  

}      

System.out.println("rest of the code...");    

  }    

}    

8. Explain the life cycle of a servlet. (5)

5 marks view

A servlet is a small java program that executes on the server-side of web connection and dynamically extends the functionality of a web server. Servlet technology is used to create Dynamic web application.


In the life cycle of servlet there are three important methods. These methods are: init(), service() and destroy().



The client enters the URL in the web browser and makes a request. The browser then generates the HTTP request for this URL and sends it to the web server. This HTTP request is received by the web server. The web server maps this request to the corresponding servlet.

init():

-          The server invokes the init( ) method of the servletThis method is invoked only when the servlet is loaded in the memory for the first time. It is possible to pass initialization parameters to the servlet so it may configure itself.

service():

-          The service() method is the main method to perform the actual task.

-          The web server calls the service() method to handle requests coming from the client/browsers and to write the response back to the client (to process the HTTP rerquest). The service() method is called for each HTTP request.

destroy():

-          Finally server unloads the servlet from the memory using the destroy() method to clean any resources.

-          The destroy() method is called only once at the end of the life cycle of a servlet.

9. What is the task of manifest file? Write the procedure to create it. [2 + 3]

5 marks view

10. Why multiple inheritance is not allowed in Java using classes? Give an example.(5)

5 marks view

Multiple inheritance is where we inherit the properties and behavior of multiple classes to a single class. In java, multiple inheritance is not supported because of ambiguity problem. We can take the below example where we have two classes Class1 and Class2 which have same method display(). If multiple inheritance is possible than Test class can inherit data members and methods of both Class1 and Class2 classes. Now, Test class have two display() methods inherited from Class1 and Class2. Problem occurs in method call, when display() method will be called with Test class object which method will be called, will it be of Class1 or Class2. This is ambiguity problem because of which multiple inheritance is not allowed in java.

Example:

class Class1{

      public void display(){

            System.out.println("Class 1 Display Method");

      }

}

class Class2{

      public void display(){

            System.out.println("Class 2 Display");

      }

}

public class Test extends Class1, Class2{

      public static void main(String args[]){

            Test obj = new Test();

            //Ambiguity problem in method call which class display() method will be called.

            obj.display();

      }

}

11. How forms can be created and processed using JSP? Make it clear with your own assumptions. [5]

5 marks view

12. Why synchronization in essential in multithreading? Describe. (5) 

5 marks view

Synchronization is a process of handling resource accessibility by multiple thread requests. The main purpose of synchronization is to avoid thread interference. At times when more than one thread try to access a shared resource, we need to ensure that resource will be used by only one thread at a time. The process by which this is achieved is called synchronization. 

If we do not use synchronization, and let two or more threads access a shared resource at the same time, it will lead to distorted results.

Consider an example, Suppose we have two different threads T1 and T2, T1 starts execution and save certain values in a file temporary.txt which will be used to calculate some result when T1 returns. Meanwhile, T2 starts and before T1 returns, T2 change the values saved by T1 in the file temporary.txt (temporary.txt is the shared resource). Now obviously T1 will return wrong result.

To prevent such problems, synchronization was introduced. With synchronization in above case, once T1 starts using temporary.txt file, this file will be locked(LOCK mode), and no other thread will be able to access or modify it until T1 returns.

13. Write short notes on.[2.5+2.5]

a. JAVA beans and JAR file

b. MVC design pattern

5 marks view

a. JAVA beans and JAR file



b. MVC design pattern

MVC design pattern is also known as Model-View-Controller. It is a common architectural pattern which is used to design and create interfaces and the structure of an application. This pattern divides the application into three parts that are dependent and connected to each other. 



1. Model: The Model contains only the pure application data. It doesn’t contain any information on how to show the data to the user. It is independent of the user interface. It controls the logic and rules of application.


2. View: The View presents the model’s data to the user. The view knows how to access the model’s data, but it does not know what this data means or what the user can do to manipulate it.


3. Controller: The Controller acts as an interface between View and Model. It receives the user requests and processes them, including the necessary validations. The requests are then sent to model for data processing. Once they are processed, the data is again sent back to the controller and then displayed on the view.