Advertisement
udaykumar1997

kkk

Mar 4th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.61 KB | None | 0 0
  1. --------------------------
  2. Post Title 'OOC Assignment-4 Answers'
  3. Post by user 'Adusumilli-Uday-Kumar'
  4. Post encryped with secure 256-bit MD5 Encryption
  5. Post accesable only VIA link
  6. Post will Expire on '3-OCT-2017'
  7. --------------------------
  8.  
  9.  
  10.  
  11. Q1.
  12.  
  13. "Write once, run anywhere" (WORA), is a slogan created by Sun Microsystems to illustrate the cross-platform benefits of the Java language. Ideally, this means Java can be developed on any device, compiled into a standard bytecode and be expected to run on any device equipped with a Java virtual machine (JVM).
  14.  
  15. This means a programmer can develop code on a PC and can expect it to run on Java enabled cell phones, as well as on routers and mainframes equipped with Java, without any adjustments. This is intended to save software developers the effort of writing a different version of their software for each platform or operating system they intend to deploy on.
  16.  
  17.  
  18.  
  19. Q2.
  20.  
  21. *) JVM
  22.  
  23. JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. JVMs are available for many hardware and software platforms. JVM, JRE and JDK are platform dependent because configuration of each OS differs. But, Java is platform independent.
  24. The JVM performs following main tasks:
  25. Loads code
  26. Verifies code
  27. Executes code
  28. Provides runtime environment
  29.  
  30. *) JDK
  31.  
  32. It is a bundle of software that you can use to develop Java based applications. It's the full featured Software Development Kit for Java, including JRE, and the compilers and tools (like JavaDoc, and Java Debugger) to create and compile programs. The JDK is a superset of the JRE, and contains everything that is in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications. It includes the JRE, set of API classes, Java compiler, Webstart and additional files needed to write Java applets and applications.
  33.  
  34.  
  35.  
  36. Q3.
  37.  
  38. The key considerations were summed up by the Java team in the following list of buzzwords:
  39. • Simple
  40. • Secure
  41. • Portable
  42. • Object-oriented
  43. • Robust
  44. • Multithreaded
  45. • Architecture-neutral
  46. • Interpreted
  47. • High performance
  48. • Distributed
  49. • Dynamic
  50.  
  51. - Security
  52. Java was able to acheive a high level of security by confining an applet to the Java execution environment and not allowing it access to other parts of the computer. The ability to download applets with confidence that no harm will be done and that no security will be breached is considered by many to be the single most innovative aspect of Java.
  53.  
  54. - Portability
  55. Output of a Java compiler is Non Executable Code i.e Bytecode. Bytecode is executed by Java run-time system, which is called the Java Virtual Machine (JVM). Once JVM is installed for particular system then any java program can run on it. Internal details of JVM will differ from platform to platform but still all understand the Same Java Bytecode.
  56.  
  57. - Simple
  58. Java was designed to be easy for the professional programmer to learn and use effectively. If you already understand the basic concepts of object-oriented programming, learning Java will be even easier. Best of all, if you are an experienced C programmer, moving to Java will require very little effort. Because Java inherits the C/C syntax and many of the object-oriented features of C , most programmers have little trouble learning Java.
  59.  
  60. - Object-Oriented
  61. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance. The object model in Java is simple and easy to extend, while primitive types, such as integers, are kept as high-performance nonobjects.
  62.  
  63. - Robust
  64. In today's world, programs will need to execute reliably in a variety of systems. Thus, the ability to create robust programs was given a high priority in the design of Java. To gain reliability, Java restricts you in a few key areas to force you to find your mistakes early in program development. At the same time, Java frees you from having to worry about many of the most common causes of programming errors. Because Java is a strictly typed language, it checks your code at compile time. However, it also checks your code at run time. Many hard-to-track-down bugs that often turn up in hard-to-reproduce run-time situations are simply impossible to create in Java. Knowing that what you have written will behave in a predictable way under diverse conditions is a key feature of Java.
  65.  
  66. - Multithreaded
  67. Java supports multithreaded programming, which allows you to write programs that do many things simultaneously. The Java run-time system
  68. comes with an elegant yet sophisticated solution for multiprocess synchronization. Java’s easy-to-use approach to multithreading allows you to think about the specific behavior of your program, not the multitasking subsystem.
  69.  
  70. - Architecture-Neutral
  71. A central issue for the Java designers was that of code longevity and portability. Operating system upgrades, processor upgrades, and changes in core system resources can all combine to make a program malfunction. The Java designers made several hard decisions in the Java language and the Java Virtual Machine in an attempt to alter this situation. Their goal was “write once; run anywhere, any time, forever.” To a great extent, this goal was accomplished.
  72.  
  73. - Interpreted and High Performance
  74. Most previous attempts at cross-platform solutions have done so at the expense of performance. The Java bytecode was carefully designed so that it would be easy to translate directly into native machine code for very high performance by using a just-in-time compiler. Java run-time systems that provide this feature lose none of the benefits of the platform-independent code.
  75.  
  76. - Distributed
  77. Java is designed for the distributed environment of the Internet because it handles TCP/IP protocols. In fact, accessing a resource using a URL is not much different from accessing a file. Java also supports Remote Method Invocation (RMI). This feature enables a program to invoke methods across a network.
  78.  
  79. - Dynamic
  80. Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe and expedient manner. This is crucial to the robustness of the Java environment, in which small fragments of bytecode may be dynamically updated on a running system.
  81.  
  82.  
  83.  
  84. Q4.
  85.  
  86. All object-oriented programming languages provide mechanisms that help you implement the object-oriented model.
  87.  
  88. *) Encapsulation
  89. Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. The power of encapsulated code is that everyone knows how to access it and thus can use it regardless of the implementation details—and without fear of unexpected side effects.
  90. In Java, the basis of encapsulation is the class. The data defined by the class are referred to as member variables or instance variables. The code that operates on that data is referred to as member methods or just methods. The behavior and interface of a class are defined by the methods that operate on its instance data.
  91. Since the purpose of a class is to encapsulate complexity, there are mechanisms for hiding the complexity of the implementation inside the class. Each method or variable in a class may be marked private or public. The public interface of a class represents everything that external users of the class need to know, or may know. The private methods and data can only be accessed by code that is a member of the class. Therefore, any other code that
  92. is not a member of the class cannot access a private method or variable. Since the private members of a class may only be accessed by other parts of your program through the class public methods, you can ensure that no improper actions take place.
  93.  
  94. *) Inheritance
  95. Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification. Most knowledge is made manageable by hierarchical (that is, top-down) classifications.
  96. Without the use of hierarchies, each object would need to define all of its characteristics explicitly. However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. Thus, it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case.
  97. Inheritance interacts with encapsulation as well. If a given class encapsulates some attributes, then any subclass will have the same attributes plus any that it adds as part of its specialization. A new subclass inherits all of the attributes of all of its ancestors. It does not have unpredictable interactions with the majority of the rest of the code in the system.
  98.  
  99. *) Polymorphism
  100. Polymorphism is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature
  101. of the situation. This means that it is possible to design a generic interface to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is the compiler’s job to select the specific action (that is, method) as it applies to each situation. You, the programmer, do not need to make this selection manually. You need only remember and utilize the general interface.
  102.  
  103. Polymorphism, Encapsulation, and Inheritance Work Together....
  104. When properly applied, polymorphism, encapsulation, and inheritance combine to produce a programming environment that supports the development of far more robust and scalables programs than does the process-oriented model.
  105. - A well-designed hierarchy of classes is the basis for reusing the code in which you have invested time and effort developing and testing.
  106. - Encapsulation allows you to migrate your implementations over time without breaking the code that depends on the public interface of your classes.
  107. - Polymorphism allows you to create clean, sensible, readable, and resilient code.
  108.  
  109.  
  110.  
  111. Q5.
  112.  
  113. Condiser compiling and running a short sample program....
  114. ----
  115. // Call this file "Example.java".
  116. class Example {
  117. public static void main(String args[]) {
  118. System.out.println("Hello World");
  119. }
  120. }
  121. ----
  122.  
  123. *) Basics of the Program
  124. In Java, a source file is officially called a compilation unit. It is a text file that contains one or more class definitions. The Java compiler requires that a source file use the .java filename extension.
  125. As you can see by looking at the program, the name of the class defined by the program is also Example. This is not a coincidence. In Java, all code must reside inside a class. By convention, the name of that class should match the name of the file that holds the program. You should also make sure that the capitalization of the filename matches the class name.
  126. When Java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension. This is why it is a good idea to give your Java source files the same name as the class they contain—the name of the source file will match the name of the .class file. When you execute java as just shown, you are actually specifying the name of the class that you want to execute. It will automatically search for a file by that name that has the .class extension. If it finds the file, it will execute the code contained in the specified class.
  127.  
  128. *) Compiling the Program
  129. To compile the Example program, execute the compiler, javac, specifying the name of the
  130. source file on the command line, as shown....
  131. ----
  132. C:\>javac Example.java
  133. ----
  134. The javac compiler creates a file called Example.class that contains the bytecode version
  135. of the program. As discussed earlier, the Java bytecode is the intermediate representation of
  136. your program that contains instructions the Java Virtual Machine will execute. Thus, the
  137. output of javac is not code that can be directly executed.
  138.  
  139. *) Running the Program
  140. To actually run the program, you must use the Java application launcher, called java.
  141. To do so, pass the class name Example as a command-line argument, as shown....
  142. ----
  143. C:\>java Example
  144. ----
  145.  
  146. *) Output of the Program
  147. When the program is run, the following output is displayed....
  148. ----
  149. Hello World
  150. ----
  151.  
  152.  
  153.  
  154. Q6.
  155.  
  156. Here's a program that demostrates initialization and assignment of different integer and floating point variables....
  157. ----
  158. public class HelloWorld{
  159. public static void main(String []args){
  160. // initializations
  161. int a = 2147483647 ; // variable of type integer
  162. byte b = 127 ; // variable of type byte
  163. short c = 32767 ; // variable of type short
  164. long d = 2147483647 ; // variable of type long
  165. float e = 12.233; // variable of type float
  166. double f = 21474833.648; // variable of type double
  167.  
  168.  
  169. System.out.println("\n The max value that an integer type variable can hold is = " a "\n The max value that an byte type variable can hold is = " b "\n The max value that an short type variable can hold is = " c "\n The max value that an long type variable can hold is = " d "\n The max value that an float type variable can hold is = " e "\n The max value that an double type variable can hold is = " f);
  170. }
  171. }
  172. ----
  173.  
  174.  
  175.  
  176. Q7.
  177.  
  178. output of the given program is....
  179. ----
  180. 1
  181. 67
  182. ----
  183.  
  184.  
  185.  
  186. Q8.
  187.  
  188. We get this error....
  189. ----
  190. incompatible types: possible lossy conversion from int to byte
  191. ----
  192. it can be avoided by making b an integer data type as such...
  193. int b =50 ; b = b*2 ;
  194.  
  195.  
  196.  
  197. Q9.
  198.  
  199. The following is the outpput of the code....
  200. ----
  201. -1
  202. 50
  203. -1
  204. 50
  205. -1
  206. 50
  207. ----
  208. If we add another 'int b' outside the for loop, then we'll get an error for defining the variable 'b' mltiple times.
  209.  
  210.  
  211.  
  212. Q10.
  213.  
  214. Assigning a value of one type to a variable of another type is known as Type Casting.
  215. In Java, type casting is classified into two types,
  216.  
  217. 1) Widening Casting(Implicit)
  218. Automatic Type casting take place when, the two types are compatible or when the target type is larger than the source type.
  219.  
  220. 2) Narrowing Casting(Explicitly done)
  221. When you are assigning a larger type value to a variable of smaller type, then you need to perform explicit type casting.
  222.  
  223. Example....
  224. ----
  225. public class Test
  226. {
  227. public static void main(String[] args)
  228. {
  229. int i = 100;
  230. long l = i; //no explicit type casting required, as long is a bigger data type than int
  231. float f = l; //no explicit type casting required, as float is a bigger data type than int
  232. double da = 100.04;
  233. long la = (long)da; //explicit type casting required, as long is a smaller data type than double
  234. int ia = (int)da; //explicit type casting required, as int is a smaller data type than double
  235. }
  236.  
  237. }
  238. ----
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement