Vous êtes sur la page 1sur 8

August 2011 Bachelor of Science in Information Technology (BScIT) Semester 3 BT0074 OOPS with Java 4 Credits (Book ID:

ID: B1002) Assignment Set 1 (60 Marks) 1. Give the features of Java. Answer: - The concept of Write-once-run-anywhere (known as the Platform independent) is one of the important key features of java language that makes java as the most powerful language. Platform IndependentThe concept of Write-once-run-anywhere (known as the Platform independent) is one of the important key feature of java language that makes java as the most powerful language. Not even a single language is idle to this feature but java is closer to this feature. The programs written on one platform can run on any platform provided the platform must have the JVM. SimpleThere are various features that makes the java as a simple language. Programs are easy to write and debug because java does not use the pointers explicitly. It is much harder to write the java programs that can crash the system but we can not say about the other programming languages. Java provides the bug free system due to the strong memory management. It also has the automatic memory allocation and deallocation system. Object OrientedTo be an Object Oriented language, any language must follow at least the four characteristics. InheritanceIt is the process of creating the new classes and using the behavior of the existing classes by extending them just to reuse the existing code and adding the additional features as needed. EncapsulationIt is the mechanism of combining the information and providing the abstraction. PolymorphismAs the name suggest one name multiple form, Polymorphism is the way of providing the different functionality by the functions having the same name based on the signatures of the methods. Dynamic binding-

Sometimes we don't have the knowledge of objects about their specific types while writing our code. It is the way of providing the maximum functionality to a program about the specific type at runtime. RobustJava has the strong memory allocation and automatic garbage collection mechanism. It provides the powerful exception handling and type checking mechanism as compare to other programming languages. Compiler checks the program whether there any error and interpreter checks any run time error and makes the system secure from crash. All of the above features make the java language robust. DistributedThe widely used protocols like HTTP and FTP are developed in java. Internet programmers can call functions on these protocols and can get access the files from any remote machine on the internet rather than writing codes on their local system. Portable- The feature Write-once-run-anywhere makes the java language portable provided that the system must have interpreter for the JVM. Java also has the standard data size irrespective of operating system or the processor. These features make the java as a portable language. DynamicWhile executing the java program the user can get the required files dynamically from a local drive or from a computer thousands of miles away from the user just by connecting with the Internet. SecureJava does not use memory pointers explicitly. All the programs in java are run under an area known as the sand box. Security manager determines the accessibility options of a class like reading and writing a file to the local disk. Java uses the public key encryption system to allow the java applications to transmit over the internet in the secure encrypted form. The bytecode Verifier checks the classes after loading. Performance- Java uses native code usage, and lightweight process called threads. In the beginning interpretation of bytecode resulted the performance slow but the advance version of JVM uses the adaptive and just in time compilation technique that improves the performance. MultithreadedAs we all know several features of Java like Secure, Robust, Portable, dynamic etc; you will be more delighted to know another feature of Java which is Multithreaded- Java is also a multithreaded programming language. Multithreading means a single program having different threads executing independently at the same time. Multiple threads execute instructions according to the program code in a process or a program. Multithreading works the similar way as multiple processes run on one computer. Multithreading programming is a very interesting concept in Java. In multithreaded programs not even a single thread disturbs the execution of other thread. Threads are obtained from the pool of available ready to run threads and they run on the system CPUs. This is how Multithreading works in Java which you will soon come to know in details in later chapters.

InterpretedWe all know that Java is an interpreted language as well. With an interpreted language such as Java, programs run directly from the source code. The interpreter program reads the source code and translates it on the fly into computations. Thus, Java as an interpreted language depends on an interpreter program. The versatility of being platform independent makes Java to outshine from other languages. The source code to be written and distributed is platform independent. Another advantage of Java as an interpreted language is its error debugging quality. Due to this any error occurring in the program gets traced. This is how it is different to work with Java. Architecture NeutralThe term architectural neutral seems to be weird, but yes Java is an architectural neutral language as well. The growing popularity of networks makes developers think distributed. In the world of network it is essential that the applications must be able to migrate easily to different computer systems. Not only to computer systems but to a wide variety of hardware architecture and Operating system architectures as well. The Java compiler does this by generating byte code instructions, to be easily interpreted on any machine and to be easily translated into native machine code on the fly. The compiler generates an architecture-neutral object file format to enable a Java application to execute anywhere on the network and then the compiled code is executed on many processors, given the presence of the Java runtime system. Hence Java was designed to support applications on network. This feature of Java has thrived the programming language.

2. How do you execute a Java program? Answer: - Steps for compiling and running java program using the Java Development kit from Sun Microsystems Step1: Open the Windows Command prompt and type this command notepad Class.java Step2: Notepad would ask if you want to create the file. Click on Yes. This would create the file and you can type the code in it Step3: Now Save the file and get back to the command prompt and type the following command javac Class.java (where Class.java is a file name).This command would compile Class.java and produce Class.class which contains Java byte code which is machine independent. Step4: If you have any errors, you would see them on the screen. Please go back to notepad and correct the errors and resave the file. If you see a blank output after the javac command, it means there were no errors and you are ready to run the program. You could run the program using the command java Class.

3. What are the different types of operators used in Java? Answer: - Operators play an important role in Java. There are three kinds of operators in Java. They are:Arithmetic Operators :Addition, Subtraction, Multiplication, Division and Modulus are the various arithmetic operations that can be performed in Java. Comparison / Relational Operators:The increment operator is ++ and decrement operator is . This is used to add 1 to the value of a variable or subtract 1 from the value of a variable. These operators are placed either before the variable or after the variable name. Comparison operators are used to compare two values and give the results. The comparison operators are Equal(= =), Not Equal(!=),Less than(<),Greater than(>),Less than or equal(<=) and Greater than or equal(>=). Logical Operators:Logical operators are used to perform Boolean operations on the operands. Logical operators are &&(Short-circuit AND), ||(Short-circuit OR), !( Logical unary NOT), &(Logical AND), |(Logical OR) 4. What are the various character extraction functions available in Java? Answer: - The String class provides a number of ways in which characters can be extracted from a String object. Each is examined here. Although the characters that comprise a string within a String object cannot be indexed as if they were a character array, many of the String methods employ an index (or offset) into the string for their operation. Like arrays, the string indexes begin at zero. The character extraction functions are:charAt( ):To extract a single character from a String, you can refer directly to an individual character via the charAt( ) method. It has this general form:char charAt(int where). getChars( ):If you need to extract more than one character at a time, you can use the getChars( ) method. It has this general form: void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart). getBytes( ):There is an alternative to getChars( ) that stores the characters in an array of bytes. This method is called getBytes( ), and it uses the default character-to-byte conversions provided by the platform. Here is its simplest form: byte[ ] getBytes( ).

toCharArray( ):If you want to convert all the characters in a String object into a character array, the easiest way is to call toCharArray( ). It returns an array of characters for the entire string. It has this general form: char[ ] toCharArray( ); This function is provided as a convenience, since it is possible to use getChars( ) to achieve the same result. 5. What are the various types of relationships? Answer: - Relationships are classified as follows: 1. A Kind-Of relationship. 2. A Is-A relationship. 3. A Part-Of-relationship. 4. A Has-A relationship. A-Kind-Of Relationship Taking the example of a human being and an elephant, both are kind-of mammals. As human beings and elephants are kind-of mammals, they share the attributes and behaviors of mammals. Human being and elephants are subset of the mammals class. The following figure depicts the relationship between the Mammals and Human Being classes:

Figure 5.1: Example for A-Kind-Of relationship Is-A Relationship Lets take an instance of the human being class peter, who is a human being and, therefore, a mammal. The following figure depicts the is a relationship.

Figure 5.2: Example for Is-A relationship Has-A Relationship/Part-Of Relationship A human being has a heart. This represents has-a relationship. Heart is a part of the human being. This represents part-of relationship. The following figure depicts the relationship between a human being and a heart.

Figure 5.3: Example for Has-A/Part-Of Relationship

6. Differentiate between errors and exceptions. Answer: - An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions. Exceptions are things you can create/throw yourself or that might be thrown because of an obvious run-time error such as trying to access a null object or reading an array out of bounds. They can also be caught and repaired by a developer. Errors should never be created, thrown, caught, or repaired. They are usually beyond the scope of your application code such as 'out of memory'. Even if you caught an out of memory error, you wouldn't likely be in a better place than the system garbage collector to fix this (and you'd have to fix it while allocating no new memory) so its better to let the system crash and repair the root cause. When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

7. Give the syntax for FileInputStream and FileOutputStream classes. Answer: The syntax for FileinputStream String theFileName="aFile.txt"; // this was read in from the user. File theFille = new File(theFileName); if (theFile.exists() ) { // the user has entered a name that is a valid file. if (theFile.isDirectory() ) { // the file is a directory. } else { // the file is not a directory, possibly a file. } }

else { // the user has entered a name that is not a file or directory that exists. } The syntax for FileoutputStream import java.io.*; public class CreateFileOutputStreamObjectFromFile { public static void main(String[] args) { File file = new File("C://FileIO//demo.txt"); /* * To create FileOutputStream object from File object use, * FileOutputStream(File file) constructor. */ Try { FileOutputStream fos = new FileOutputStream(file); }catch(FileNotFoundException ex) {System.out.println("Exception : " + ex);}}}

8. What is an applet? Explain with an example. Answer: - Applet is java program that can be embedded into HTML pages. Java applets run on the java enables web browsers such as mozila and internet explorer. Applet is designed to run remotely on the client browser, so there are some restrictions on it. Applet can't access system resources on the local computer. Applets are used to make the web site more dynamic and entertaining. Here is the example of applet: import java.applet.*; import java.awt.*; public class FirstApplet extends Applet{ public void paint(Graphics g){ g.drawString("Welcome in Java Applet.",40,20); } } 9. Give the uses of adapter class. Answer: - Java Language rule are such that we must implement all the methods of an interface even if we put them into empty braces.i.e. we must override all the methods declared in the interface. But we can create our classes as subclasses of one of the adapter classes, then we need to override only some of the methods we need. i.e., An adapter classes provide empty implementation of all methods declared in an EventListener interface. 10. What is JDBC? Explain.

Answer: - The JDBC API (Java Data Base Connectivity Application Program Interface) can access any kind of tabular data, especially data stored in a Relational Database. It works on top of ODBC (Open Data Base Connectivity) which was the driver for database connectivity since age old days but since ODBC was implemented in C so people from the VB background had some problems in understanding the implementation intricacies. Since JDBC works on top of ODBC we have something called as a JDBC-ODBC bridge to access the database. JDBC helps you to write Java applications that manage mainly three programming activities listed below namely: a) Connecting to a data source, like a database, b) Sending queries and updating statements to the database and c) Retrieving and processing the results received from the database in answer to your query

Vous aimerez peut-être aussi