| 1. |
Answer the following:1. Name two types of Java Programs.2. Define Instance Variable. Give an example of the same.3. Differentiate between Binary Search and. Linear Search. |
|
Answer» 1. There are two types of Java programs — Java Stand-Alone Applications and Java Applets. (i) Java Stand-Alone Applications A stand-alone Java application refers to a Java program that can run independently on a computer. Acrobat Reader is an excellent example of this type of application. In Java, every stand-alone application begins its execution with the main() method. Java stand-alone applications can be classified into two types: a. Console based applications b. Graphical User Interface based applications (ii) Java Applets Java applets are Java applications that run within a web browser. They are mainly used for internet programming. The applet is capable of performing many tasks on a web page, such as displaying graphics, playing sounds, and accepting user input. 2. Variables that are declared inside a class without using the keyword 'static' and outside any member methods are termed instance variables. Each object of the class gets its own copy of instance variables. For example, in the below class: class Cuboid { public void input(int h, int w, int d) { public void computeVolume() { height, width, depth and volume are instance variables. 3. Linear Search: (i) Linear search performs on unsorted list of elements as well as sorted list. (ii) Compare the desired element with all elements in an array until the match is found. (iii) Insertion of an element in an array can be performed very efficiently when array is not ordered. (iv) For large size of array, time required for this search is very larger. (v) Time complexity is as follows : Worst case : N comparison Best case : 1 comparison Binary Search: (i) For binary search, the elements in array are stored in alphabetically or numerically (ii) Compare the value of midpoint with desired value. If the value is greater than midpoint value, the first half is checked, otherwise second half checked until search is successful or interval empty. (iii) An insertion of a new element requires that many elements be physically moved to preserved order. (iv) For large size of array, comparatively time required is less. (v) Time complexity as follows: Worst case : log2 N comparison Best case : 1 comparison |
|