Answer
Java is a simple language that can be learned easily, even if you have just started programming. A Java programmer need not know the internal of Java. The syntax of Java is similar to C++. Unlike C++, in which the programmer handles memory manipulation, Java handles the required memory manipulations, and thus prevents errors that arise due to improper memory usage. Java defines data as objects with methods that support the objects. Java is purely object-oriented and provides abstraction, encapsulation, inheritance and polymorphism. Even the most basic program has a class. Any code that you write in Java is inside a class. Java is tuned of Web. Java programs can access data across the Web as easily as they access data from a local system. You can build distributed applications in Java that use resources from any other networked computer. Java is both interpreted and compiled. The code is compiled to a bytecode that is binary and platform independent. When the program has to be executed, the code is fetched into the memory and interpreted on the users machine. As an interpreted language, Java has simple syntax. When you compile a piece of code, all the errors are listed together. You can execute only when all the errors are rectified. An interpreter, on the other hand, verifies the code and executes it line by line. Only when the execution reaches the statement with error is the error reported. This makes it easy for a programmer to debug the code. The drawback is that this takes more time than compilation. Compilation is the process of converting the code that you type, into a language that the computer understands- machine language. When you compile a program using a compiler, the compiler checks for syntactic errors in code and list all the errors on the screen. You have to rectify the errors and recompile the program to get the machine language code. The Java compiler compiles the code to a bytecode that is understood by the Java environment. Bytecode is the result of compiling a Java program. You can execute this code on any platform. In other words, due to the bytecode compilation process and interpretation by a browser, Java programs can be executed on a variety of hardware and operating systems. The only requirement is that the system should have a Java-enabled Internet browser. The Java interpreter can execute Java code directly on any machine on which a Java interpreter has been installed. Thanks to bytecode, a Java program can run on any machine that has a Java interpreter. The bytecode supports connection to multiple databases. Java code is portable. Therefore, other people can use the programs that you write Java event if they have different machines with different operating systems. Java forces you to handle unexpected errors. This ensures that Java programs are robust (reliable), and bug free and do not crash. Due to strong type-checking done by Java on the users machine, any changes to the program are tagged as error and the program will not execute. Java is, therefore, secure. Java is faster than other interpreter-based language like BASIC since it is compiled and interpreted. Multithreading is the ability of an application to perform multiple tasks at the same time. You can create multithreading programs using Java. The core of Java is also multithreaded. The following definition of Java by Sun Microsystems lists all the features of Java. Java is a simple, Object-oriented, distributed, interpreted, robust, secure, architecture neural, portable, highperformance, multithreaded, and dynamic language.
2. Write a program in Java to find the highest of any five numbers. How do you compile and execute this Java program? Answer
public class Assignment1 { public static void main (String[] args) { //Declare 5 integers int number1; int number2; int number3; int number4; int number5; //Declare input as a Scanner to allow for user input Scanner input = new Scanner(System.in); //Accept input for the 5 numbers System.out.print("Enter the first number: "); number1 = input.nextInt(); System.out.print("Enter the second number: "); number2 = input.nextInt(); System.out.print("Enter the third number: "); number3 = input.nextInt(); System.out.print("Enter the fourth number: "); number4 = input.nextInt(); System.out.print("Enter the fith number: "); number5 = input.nextInt(); //Declare 2 integers, for highest and lowest number intnumHigh; intnumLow; //Assign numHigh and numLow as the first number so we can compare numHigh = number1; numLow = number1; //Find the highest number if (numHigh< number2) {numHigh = number2;} if (numHigh< number3) {numHigh = number3;} if (numHigh< number4) {numHigh = number4;} if (numHigh< number5) {numHigh = number5;} //Find the lowest number if (numLow> number2) {numLow = number2;} if (numLow> number3) {numLow = number3;} if (numLow> number4) {numLow = number4;} if (numLow> number5) {numLow = number5;} //Print out the results System.out.printf("\nThe Highest number is %d\n", numHigh);
} }
This program can compile using Command Prompt and Execute in Interpreter such as Java Virtual Machine
3. Write a simple Java program to illustrate the use of for loop statement. Answer
Code of the program : public class ForLoop{ public static void main(String[] args){ for(int i = 1;i <= 5;i++){ for(int j = 1;j <= i;j++){ System.out.print(i); } System.out.println(); } } } Output of the program : 1 22 333 4444 55555
5. Write a program to explain the Exception Handling mechanisms in Java using the keywords: try, catch and finally. Answer
Exception, that means exceptional errors. Actually exceptions are used for handling errors in programs that occurs during the program execution. During the program execution if any error occurs and you want to print your own message or the system message about the error then you write the part of the program which generate the error in the try{} block and catch the errors using catch() block. Exception turns the direction of normal flow of the program control and send to the related catch() block. Error that occurs during the program execution generate a specific object which has the information about the errors occurred in the program. In the following example code you will see that how the exception handling can be done in java program. This example reads two integer numbers for the variables a and b. If you enter any other character except number ( 0 - 9 ) then the error is caught by NumberFormatException object. After that ex.getMessage() prints the information about the error occurring causes. Code of the program : import java.io.*; public class exceptionHandle{ public static void main(String[] args) throws Exception{ try{ int a,b; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); a = Integer.parseInt(in.readLine()); b = Integer.parseInt(in.readLine()); } catch(NumberFormatException ex){ System.out.println(ex.getMessage() + " is not a numeric value."); System.exit(0); } } }