You are on page 1of 7

February 2013 (Spring drive) Bachelor of Computer Application (BCA) Semester 4 BC0047 Java Programming Q.NO.

O.1: Write a program in Java to find the highest of any five numbers. How do you compile and execute this Java program? SOLUTION:Program to find Highest of any Five numbers:
class maximum { public static void main(String args[]) { int[]n= new int[5]; int r; int max; for(r=0;r<5;r++) { n[r]=Integer.parseInt(args[r]); } max=n[0]; for(r=1;r<5;r++) { if(n[r]>max) max=n[r]; } System.out.println("maximum="+max); } } This program can be compiled using Command Prompt and Executed in Interpreter such as Java Virtual Machine. Steps to compile and execute this program. 1. Install java development kit software (jdk). 2. Type above java program code in notepad and give file name as maximum.java and save as type All files in c:\program files\java\jdk1.7.0_07\bin.{jdk1.7.0_07 is software version for java virtual machine} 3. Open command prompt and change directory to c:\program files\java\jdk1.7.0_07\bin. command:* c:\ cd\ * c:\>cd program files\java\jdk1.7.0_07\bin compile this program in a command window with the "javac <filename> " command: c:\program files\java\jdk1.7.0_07\bin >javac maximum.java To execute the program, use the java command: c:\program files\java\jdk1.7.0_07\bin >java maximum " enter 5 numbers to compare with each having space between" then press enter. The result will be: maximum=result OUTPUT SCREEN

Q.NO.2: Write a program to explain the Exception Handling mechanisms in Java using the keywords: try, catch and finally. ANSWER:- 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. program to explain the Exception Handling mechanisms in Java 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. OUTPUT SCREEN

Q.NO.3: What are the uses of FileInputStream and FileOutputStream? Write short notes on each.
ANSWER:- All programs accept input from a user process it and produce an output. Therefore, all programming language input and output operation. Java provides support for input and output through the classes of the java.io package. FileInputStream: The FileInputStream is the subclass of the InputStream abstract class. TheFileInputStream 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. A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its check Read method is called with the path represented by the file argument as its argument. If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown.

FileOutputStream: A file output stream is an output stream for writing data to a File or to aFileDescriptor. 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. Creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with name as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown. .

Q.NO.4:- What are the uses of ODBC, JDBC and Driver Manager? ANSWER: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 inserted 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. For this to work, both the application and the DBMS must be ODBC-compliant-that is, the application must be capable of issuing ODBC commands and the DBMS must be capable of responding to them. 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. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the JVM host environment. JDBC allows multiple implementations to exist and be used by the same application. The API provides a mechanism for dynamically loading the correct Java packages and registering them with the JDBC Driver Manager. The Driver Manager is used as a connection factory for creating JDBC connections. DRIVER MANAGER: The Driver Manager (DM) is the software that loads a particular driver based on the connection information. An application is actually linked to the DM. When the application calls the ODBC function to connect to the DBMS, the DM parses the connection string and loads the appropriate driver. The JDBC DriverManager class defines objects which can connect Java applications to a JDBC driver. DriverManager has traditionally been the backbone of the JDBC architecture. It is quite small and simple. This is a very important class. Its main purpose is to provide a means of managing the different types of JDBC database driver. On running an application, it is the DriverManager's responsibility to load all the drivers found in the system property jdbc. drivers. For example, this is where the driver for the Oracle database may be defined. This is not to say that a new driver cannot be explicitly stated in a program at runtime which is not included in jdbc. drivers. When opening a connection to a database it is the DriverManager' s role to choose the most appropriate driver from the previously loaded drivers.

Q.NO.5: Write short notes on (1.) RMI and (2.) CORBA. ANSWER:RMI: RMI(Remote Method Invocation) is a distributed object system that enables you to easily develop distributed Java applications. Developing distributed applications in RMI is simpler than developing with sockets since there is no need to design a protocol, which is an error-prone task. In RMI, the developer has the illusion of calling a local method from a local class file, when in fact the arguments are shipped to the remote target and interpreted, and the results are sent back to the callers. The Genesis of an RMI Application Developing a distributed application using RMI involves the following steps: 1. Define a remote interface 2. Implement the remote interface 3. Develop the server 4. Develop a client 5. Generate Stubs and Skeletons, start the RMI registry, server, and client RMI is built upon the specification of how local and remote objects interoperate. Local objects are objects that execute on the local machine. Remote objects are objects that execute on all the other machines. Objects that are exported for remote access must implement the interface Remote Interface. This interface identifies the object to be accessed remotely. All the methods that are to be invoked remotely must throw a RemoteExeption. The exception is used to handle the errors that may occur during the invocation of a remote method. CORBA: The Common Object Request Broker Architecture (or CORBA) is an industry standard developed by the Object Management Group (OMG) to aid in distributed objects programming. It is important to note that CORBA is simply a specification. A CORBA implementation is known as an ORB (or Object Request Broker). There are several CORBA implementations available on the market such as VisiBroker, ORBIX, and others. JavaIDL is another implementation that comes as a core package with the JDK1.3 or above. CORBA was designed to be platform and language independent. Therefore, CORBA objects can run on any platform, located anywhere on the network, and can be written in any language that has Interface Definition Language (IDL) mappings. Similar to RMI, CORBA objects are specified with interfaces. Interfaces in CORBA, however, are specified in IDL. While IDL is similar to C++, it is important to note that IDL is not a programming language. CORBA is a distributed computing technology where the participating objects need not only be written in java. Developing distributed object-based applications can be done in Java using RMI or JavaIDL (an implementation of CORBA). The use of both technologies is similar since the first step is to define an interface for the object. Unlike RMI, however, where interfaces are defined in Java, CORBA interfaces are defined in the Interface Definition Language (IDL).

Practical Questions

Q.NO.6: Write a Java program to write the first 10 terms of Fibonacci sequence.
SOLUTION:Java program to write the first 10 terms of Fibonacci sequence. class fibonacci { public static void main(String args[]) { int n=10,first=0,second=1,next,i; System.out.println("printing first "+n+" numbers in Finonacci Series\n"); for(i=0;i<n;i++) { if(i<=1) next=i; else { next=first+second; first=second; second=next; } System.out.print(next +"\t"); } } }

OUTPUT SCREEN

Q.NO.7: Write a Java program that creates a string object, initialize it with your name, and performs the following operations. (a) To find the length of the string object using appropriate String method. (b) To find whether the character a is present in the string. If yes find the number of times a appear in the name and the location where it appears. SOLUTION:public class name { public static void main(String args[]) { String name = new String (); name = "manish"; System.out.println("My Name is:"+name); String str = name; int length = str.length(); int count = 0; int index = 0; while (index < length) { if (str.charAt(index) == 'a') { count = count + 1; } index = index + 1; } System.out.println("(a) Length of String is :" +length); System.out.println("(b) Number of times 'a' appear in the name is :" +count); System.out.println("And the location of character 'a' starting from 0 is : " +str.indexOf('a')); } } OUTPUT SCREEN

You might also like