Java BC0047 - Software-Engineering-Spring-2013-Assignment
Java BC0047 - Software-Engineering-Spring-2013-Assignment
How do you compile and execute this Java program? import java.io.*; class great { public static void main(String args[])throws IOException { InputStreamReader read=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(read); int a,b,c,d,e,max=0; System.out.println("enter the numbers"); a=Integer.parseInt(in.readLine()); b=Integer.parseInt(in.readLine()); c=Integer.parseInt(in.readLine()); d=Integer.parseInt(in.readLine()); e=Integer.parseInt(in.readLine()); if(a>max) { max=a; } if(b>max) { max=b; } if(c>max) { max=c; } if(d>max) { max=d; } if(e>max) { max=e; } System.out.println("greater number="+max); } } Compiling Java Source Code Before the Java virtual machine (VM) can run a Java program, the program's Java source code must be compiled into byte-code using the javac compiler. Java byte-code is a platform independent version of machine code; the target machine is the Java VM rather than the underlying architecture If there are no errors in your source file, the Java compiler will produce one or more .class files .
Syntax: javac <filename>.java Running a Java application Once we have successfully compiled your Java source code, we can invoke the Java VM to run our application's byte-code: Syntax Java <fileb\name>
Q.2. Write a program to explain the Exception Handling mechanisms in Java using the keywords: try, catch and finally. A Java exception is an object that describes an exceptional condition which has occurred in a piece of code. 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. Here is the basic form of exception handling block. class finally { public static void test()throws Arithmetic Exception, Exception { throw new Arithmetic Exception("demo throw"); } public static void main(String rags[]) { try { test(); }catch(Exception e) { System.out.println("throw&throws are not working"); } finally { System.out.println("any how finally blocks executed"); }
Q. 3. What are the uses of FileInputStream and FileOutputStream? Write short notes on each. FileInputStream The FileInputStream class creates an InputStream that we can use to read bytes froma file.
Its two most common constructors are shown here: (1) FileInputStream(String filepath) (2) FileInputStream(File fileObj) Either can throw a FileNotFoundException. Here, filepath is the full path name of a file, and fileObj is a File object that describes the file. When a FileInputStream is created, it is also opened for reading.FileInputStream overrides six of the methods in the abstract class InputStream. Themark( ) and reset( ) methods are not overridden, and any attempt to use reset( ) on aFileInputStream will generate an IOException. The next example shows how to read a single byte, an array of bytes, and a subrange array of bytes. It also illustrates how to use available( ) to determine the number of bytes remaining, and how to use the skip( ) method to skip over unwanted bytes. The program reads its own source file, which must be in the current directory.
FileOutputStream A file output stream is an output stream for writing data to a File or to a File Descriptor. 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. 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 File Descriptor object is created to represent this file connection. First, if there is a security manager, its check Write 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. Parameters: Name - the system-dependent file name Append - if true, then bytes will be written to the end of the file rather than the beginning Throws: FileNotFoundException - 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. Security Exception - if a security manager exists and its check write method denies write access to the file.
Q.4. what are the uses of ODBC, JDBC and Driver Manager? 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. JDBC 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.
Q.5. Write short notes on (i) RMI and (ii) CORBA RMI RMI 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. 2. 3. 4. 5. Define a remote interface Implement the remote interface Develop the server Develop a client Generate Stubs and Skeletons, start the RMI registry, server, and client
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. Number of steps involved in developing CORBA applications. These are: 1. 2. 3. 4. 5. 6. Define an interface in IDL Map the IDL interface to Java (done automatically) Implement the interface Develop the server Develop a client Run the naming service, the server, and the client.
Section-B(practical section)
6. Write a Java program to write the first 10 terms of Fibonacci sequence. class fibonacci { public static void main(String rags[]) { int i,f1,f2=0,f3=1; System.out.println("fibonacci series is:"); for(i=1;i<=10;i++) { System.out.print(" "+f3+" "); f1 = f2; f2 = f3; f3 = f1 + f2; }
} }
Q.7. Write a Java program that creates a string object, initialize it with your name, and performs the following operations. [5+5 marks] (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. IMport java.io.*; class StringLength{ public static void main(String[] rags){ try{ BufferedReader object= new BufferedReader (new InputStreamReader(System.in)); System.out.println("Eneter string value:"); String s=object.readLine(); int len=s.length(); System.out.println(len); } catch(Exception e){} } } Output this program: public boolean containsChar(String s, char search) { if (s.length() == 0) return false; else return s.charAt(0) == search || containsChar(s.substring(1), search); } The other is far less elegant, but completeness...: /** * Works for strings of up to 5 characters */ public boolean containsChar(String s, char search) { if (s.length() > 5) throw IllegalArgumentException(); try { if (s.charAt(0) == search) return true; if (s.charAt(1) == search) return true;
if (s.charAt(2) == search) return if (s.charAt(3) == search) return if (s.charAt(4) == search) return } catch (IndexOutOfBoundsException e) // this should never happen... return false; } return false; }