Anda di halaman 1dari 7

Bachelor of Computer Application (BCA) Semester 4 BC0047 Java Programming Assignment Set 1

1. How is Java different than any other OOP language like C++? Explain. Ans-Java programs are platform-independent. In Java, the program is compiled into byte code (.class file) that runs on the Java Virtual Machine, which can interpret and run the program on any operating system. This makes Java programs platform-independent.

2. Write a program in Java to find the highest of any five numbers. How do you compile and execute this Java program? Ans- Create file test.java with below code.

Class test {
int[] nums; // assume this is an array of length 5 and you want to find the highest // finding it with a loop int max = nums[0]; for(int i = 1; i < nums.length; ++i) { if(nums[i] > max) { max = nums[i]; } } // max is now the Highest compile this program in a command window with the "javac" command: C:\>\progra~1\java\jdk1.6.0_02\bin\javac test.java To execute the program, use the java command: C:\>\progra~1\java\jdk1.6.0_02\bin\java test

3. Write a simple Java program to illustrate the use of for loop statement.

Ans- Class Hello {


int[] nums; int max = nums[0]; for(int i = 1; i < nums.length; ++i) { if(nums[i] > max) { max = nums[i]; } }

4. What do you mean by an array? Explain with an example. Ans- Arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitives or object references but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements. In other words, there is no such thing as a primitive array, but you can make an array of primitives. The following is an example of constructing an array of type int: int[] myArray; // Declares the array of ints myArray = new int[4]; // constructs an array and assigns it to the myArray variable

5. Write a program to explain the Exception Handling mechanisms in Java using the keywords: try, catch and finally. Ans- A Java exception is an object that describes an exceptional condition which has occurred in a piece of code. Many things can lead to exceptions , including hardware failures, resource exhaustion and bugs in the program. When an exceptional event occurs in the Java program , an exception is thrown. The code that is responsible for doing something about the exception is called exception handler. The Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. We have tell the JVM what code to execute when certain exceptional condition occurs in the program. To do this we use try and catch keywords. Within a try block we will write a code in which exception may occur. This block of code is called guarded region. One or more catch clauses match a specific exception to a block of code that handles it.

Example: public class ExcepTest{ public static void main(String args[]){ int a[] = new int[2]; try{ System.out.println("Access element three :" + a[3]); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Exception thrown :" + e); } finally{ a[0] = 6; System.out.println("First element value: " +a[0]); System.out.println("The finally statement is executed"); } } } This would produce following result: Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3 First element value: 6 The finally statement is executed.

5. Define (i) Inheritance (ii) Package and (iii) Interface. Inheritance: Inheritance is a compile-time mechanism in Java that allows you to extend a class (called the base class or superclass) with another class (called the derived class or subclass). In Java,inheritance is used for two purposes: 1. class inheritance - create a new class as an extension of another class, primarily for the purpose

of code reuse. That is, the derived class inherits the public methods and public data of the base class. Java only allows a class to have one immediate base class, i.e., single class inheritance. 2. interface inheritance - create a new class to implement the methods defined as part of an interface for the purpose of subtyping. That is a class that implements an interface conforms to (or is constrained by the type of) the interface. Java supports multiple interface inheritance. Package: Java provides a mechanism for partitioning the class name space into more manageable chunks. This mechanism is called package. The package is both a naming and a visibility control mechanism. Classes can be defined inside packages that are not accessible by code outside that package. Class members that are defined exposed to other members of the same package. This allows your classes to have intimate knowledge of each other, but not expose that knowledge to the rest of the world. Interface: Interfaces are designed to support dynamic method resolution at run time. Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible. Interfaces are designed to avoid this problem. They disconnect the definition of a method or set of methods from the inheritance hierarchy.

Bachelor of Computer Application (BCA) Semester 4 BC0047 Java Programming Assignment Set 2

1. What are the uses of FileInputStream and FileOutputStream? Write short notes on each. Ans- FileInputStream: The FileInputStream is the subclass of the InputStream abstract class. The FileInputStream is used to read data from a file. The read() method of an InputStream returns an int which contains the byte value of the byte read. If there is no more data left in stream to read, the read() method returns -1 and it can be closed after this. One thing to note here, the value -1 is an int not a byte value. FileOutputStream: A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open. FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

2. Write an applet program to change the background colour of an applet window as soon as you click on a button? Ans- import java.awt.*; import java.applet.*; public class HelloWorldApplet2 extends Applet { public void init() { // Initialize the applet by setting it to use blue // and yellow as background and foreground colors. setBackground(Color.blue); setForeground(Color.yellow); }

public void paint(Graphics g) { g.drawString("Hello World!", 10, 30); } } // end of class HelloWorldApplet2

3. What are the uses of ODBC, JDBC and Driver Manager?.

Ans- ODBC: ODBC is an abbreviation of Open Database Connectivity, a standard database access method developed by Microsoft Corporation. The goal of ODBC is to make it possible to access any data from any application, regardless of which database management system (DBMS) is handling the data. ODBC manages this by inserting a middle layer, called a driver, between an application and the DBMS. The purpose of this layer is to translate the queries of the application into commands that the DBMS understands JDBC: JDBC provides a database-programming interface for Java programs. Since the ODBC is written in C language, a Java program cannot directly communicate with an ODBC driver. JavaSoft created the JDBC-ODBC Bridge driver that translates the JDBC API to the ODBC API. It is used with ODBC drivers. Driver Manager: The JDBC driver manager is the backbone of the JDBC architecture. The function of the JDBC driver manager is to connect a Java application to the appropriate driver.

4. Write short notes on (i) RMI and (ii) CORBA

Ans- RMI : Remote Method Invocation (RMI) is the mechanism which allows objects in different hosts to send and receive messages. Both the methods achieve the same goal. RMI allows objects in different JVMs belonging to different hosts to send and receive message. CORBA: CORBA stands for Common Object Request Broker Architecture. CORBA is a distributed computing technology where the participating objects need not only be written in Java.

5. Write an essay on history of web application. Ans- In earlier computing models, e.g. in client-server, the load for the application was shared between code on the server and code installed on each client locally. In other words, an application had its own client program which served as its user interface and had to be separately installed on each user's personal computer. An upgrade to the server-side code of the application would typically also require an upgrade to the client-side code installed on each user workstation, adding to the support cost and decreasing productivity. In contrast, web applications use web documents written in a standard format such as HTML and JavaScript, which are supported by a variety of web browsers. Web applications can be considered as a specific variant of client-server software where the client software is downloaded to the client machine when visiting the relevant web page, using standard procedures such as Http. Client web software update may happen each time the web page is visited. During the session, the web browser interprets and displays the pages, and acts as the universal client [2] for any web application. In the early days of the Web each individual web page was delivered to the client as a static document, but the sequence of pages could provide an interactive experience, as user input is returned through web form elements embedded in the page markup. In 1995 Netscape introduced a client-side scripting language called JavaScript allowing programmers to add some dynamic elements to the user interface that ran on the client side. So instead of sending data to the server in order to generate an entire web page, the embedded scripts of the downloaded page can perform various tasks such as input validation or showing/hiding parts of the page. In 1996, Macromedia introduced Flash, a vector animation player that could be added to browsers as a plug-in to embed animations on the web pages. It allowed the use of a scripting language to program interactions on the client side with no need to communicate with the server. In 1999, the "web application" concept was introduced in the Java language in the Servlet Specification version 2.2. [2.1?].[3][4] At that time both JavaScript and XML had already been developed, but Ajax had still not yet been coined and the XMLHttpRequest object had only been recently introduced on Internet Explorer 5 as an ActiveX object.[5] In 2005, the term Ajax was coined, and applications like Gmail started to make their client sides more and more interactive. A web page script is able to contact the server for storing/retrieving data without downloading an entire web page. In 2011 HTML5 was created, which provides graphic and multimedia capabilities without the need of client side plugins. HTML5 also enriched the semantic content of documents. The APIs and document object model (DOM) are no longer afterthoughts, but are fundamental parts of the HTML5 specification. WebGL API paved the way for advanced 3D graphics based on HTML5 canvas and JavaScript language. These have significant importance in creating truly platform and browser independent rich web applications.

Anda mungkin juga menyukai