Modern Programming Languages (The Java Language) Assignment No. 3 BSCS/BSIT/BSSE Instructions for Assignment Submission Hand Written Assignment Submission is required Copy Paste Assignment will award you 0 Marks You are required to answer all the Questions. Submission Deadline is: Before or On Final MPL Paper Question# 1 Write note on Composition vs. inheritance Question# 2 A. Create a class with a static final field and a final field and demonstrate the difference between the two. B. Create a class with a blank final reference to an object. Perform the initialization of the blank final inside all constructors. Demonstrate the guarantee that the final must be initialized before use, and that it cannot be changed once initialized. Question# 3 A. Create a class as abstract without including any abstract methods and verify that you cannot create any instances of that class. B. Create a base class with an abstract print( ) method that is overridden in a derived class. The overridden version of the method prints the value of an int variable defined in the derived class. At the point of definition of this variable, give it a nonzero value. In the base-class constructor, call this method. In main( ), create an object of the derived type, and then call its print( ) method. Explain the results. C. Create an abstract class with no methods. Derive a class and add a method. Create a static method that takes a reference to the base class, downcasts it to the derived class, and calls the method. In main( ), demonstrate that it works. Now put the abstract declaration for the method in the base class, thus eliminating the need for the downcast. Question# 4 Write Complete Java Code for each of the followings A. Create an interface containing three methods, in its own package. Implement the interface in a different package. B. Prove that all the methods in an interface are automatically public. Question# 5 A. Define Inner Classes B. Differentiate between downcasting and upcasting C. Write a class named Outer that contains an inner class named Inner. Add a method to Outer that returns an object of type Inner. In main( ), create and initialize a reference to an Inner. D. Create an interface with at least one method, in its own package. Create a class in a separate package. Add a protected inner class that implements the interface. In a third package, inherit from your class and, inside a method, return an object of the protected inner class, upcasting to the interface during the return. E. Create a class with a private field and a private method. Create an inner class with a method that modifies the outer-class field and calls the outer-class method. In a second outer-class method, create an object of the inner class and call its method, then show the effect on the outer-class object. F. Determine whether an outer class has access to the private elements of its inner class. G. Create a private inner class that implements a public interface. Write a method that returns a reference to an instance of the private inner class, upcast to the interface. Show that the inner class is completely hidden by trying to downcast to it. H. Write short note on anonymous classes and nested classes in java. Give Java Code example of each of these two types Question# 6 A. Create a generator class that produces character names (as String objects) from your favorite movie (you can use Snow White or Star Wars as a fallback) each time you call next( ), and loops around to the beginning of the character list when it runs out of names. Use this generator to fill an array, an ArrayList, a LinkedList, a HashSet, a LinkedHashSet, and a TreeSet, then print each container. B. Create a class, then make an initialized array of objects of your class. Fill a List from your array. Create a subset of your List by using subList( ), then remove this subset from your List. C. Create a Set of the vowels. Working from UniqueWords.Java, count and display the number of vowels in each input word, and also display the total number of vowels in the input file. Question# 7 A. Create a class with a main( ) that throws an object of class Exception inside a try block. Give the constructor for Exception a String argument. Catch the exception inside a catch clause and print the String argument. Add a finally clause and print a message to prove you were there. B. Define an object reference and initialize it to null. Try to call a method through this reference. Now wrap the code in a try-catch clause to catch the exception. C. Write code to generate and catch an ArraylndexOutOfBoundsException. D. Create your own exception class using the extends keyword. Write a constructor for this class that takes a String argument and stores it inside the object with a String reference. E. Write a method that displays the stored String. Create a try-catch clause to exercise your new exception. E. Create three new types of exceptions. Write a class with a method that throws all three. In main( ), call the method but only use a single catch clause that will catch all three types of exceptions. F. Create a class with two methods, f( ) and g( ). In g( ), throw an exception of a new type that you define. In f( ), call g( ), catch its exception and, in the catch clause, throw a different exception (of a second type that you define). Test your code in main( ) G. Create a three-level hierarchy of exceptions. Now create a base-class A with a method that throws an exception at the base of your hierarchy. Inherit B from A and override the method so it throws an exception at level two of your hierarchy. Repeat by inheriting class C from B. In main( ), create a C and upcast it to A, then call the method. Question# 8 A. Create a class called SortedDirList with a constructor that takes a File object and builds a sorted directory list from the files at that File. Add to this class two overloaded list( ) methods: the first produces the whole list, and the second produces the subset of the list that matches its argument (which is a regular expression). Question# 9 What is Java FX? Why Java FX is used? what are the advantages of using Java FX? Create a sample calculator using Java FX Question# 10 Answer the following Qs a. What is Android? b. When Android was first launched? c. Who first launched Android? d. What are the basic requirements of Android? e. What is API level in Android application development? f. What is Activity? g. What is activity manager in Android application development? h. What are the basic folders (Project Structure) for Android application development? i. What is manifesto file in Android application development? j. Create a sample calculator in Android application development? k. Create a sample media player application in Android application development? l. Create a guest book in Android application development? Question# 11:How to create a stored procedure in SQL Server? Consider the following table tbl_Projects (ProjectSID, ProjectTitle, ProjectSupervisor,CellNo,Email) create a stored procedure for this table in SQL Server to insert a new Record to Database from Java. Question# 32: Why jTable control is used in Java? Consider the following table tbl_Projects (ProjectSID, ProjectTitle, ProjectSupervisor,CellNo,Email) Suppose there are 100 Records in this table, you are required to show all the 100 Record to jTable control using Java code. Write complete code Self-Study : How Java Differs from C and C++ Here is a brief description of most of the major differences between C, C++ , and the Java language. If you are a programmer familiar with either C or C++, you may will find to catch some of the common mistakes and assumptions programmers make when using Java. Pointers Java does not have an explicit pointer type. Instead of pointers, all references to objects— including variable assignments, arguments passed into methods, and array elements—are accomplished by using implicit references. References and pointers are essentially the same thing except that you can’t do pointer arithmetic on references (nor do you need to). Reference semantics also enable structures such as linked lists to be created easily in Java without explicit pointers; merely create a linked list node with variables that point to the next and the previous node. Then, to insert items in the list, assign those variables to other node objects. Arrays Arrays in Java are first class objects, and references to arrays and their contents are accomplished through explicit references rather than via point arithmetic. Array boundaries are strictly enforced; attempting to read past the ends of an array is a compile or run-time error. As with other objects, passing an array to a method passes a reference to the original array, so changing the contents of that array reference changes the original array object. Arrays of objects are arrays of references that are not automatically initialized to contain actual objects. Using the following Java code produces an array of type MyObject with ten elements, but that array initially contains only nulls: MyObject arrayofobjs[] = new MyObject[10]; You must now add actual MyObject objects to that array: for (int i; i< arrayofobjs.length. i++) { arrayofobjs[i] = new MyObject(); Java does not support multidimensional arrays as in C and C++. In Java, you must create arrays that contain other arrays. Strings Strings in C and C++ are arrays of characters, terminated by a null character (\0). To operate on and manage strings, you treat them as you would any other array, with all the inherent difficulties of keeping track of pointer arithmetic and being careful not to stray off the end of the array. Strings in Java are objects, and all methods that operate on strings can treat the string as a complete entity. Strings are not terminated by a null, nor can you accidentally overstep the end of a string (like arrays, string boundaries are strictly enforced). Memory Management All memory management in Java is automatic; memory is allocated automatically when an object is created, and a run-time garbage collector (the “GC”) frees that memory when the object is no longer in use. C’s malloc and free functions do not exist in Java. To “force” an object to be freed, remove all references to that object (assign variables holding it to null, remove it from arrays, and so on). The next time the Java GC runs, that object is reclaimed. Data Types As mentioned in the early part of this book, all Java primitive data types (char, int, long, and so on) have consistent sizes and behavior across platforms and operating systems. There are no unsigned data types as in C and C++ (except for char, which is a 16-bit unsigned integer). The boolean primitive data type can have two values: true or false. Boolean is not an integer, nor can it be treated as one, although you cannot cast 0 or 1 (integers) to boolean types in Java. Composite data types are accomplished in Java exclusively through the use of class definitions. The struct, union, and typedef keywords have all been removed in favor of classes. Casting between data types is much more controlled in Java; automatic casting occurs only when there will be no loss of information. All other casts must be explicit. The primitive data types (int, float, long, char, boolean, and so on) cannot be cast to objects or vice versa; there are methods and special “wrapper” classes to convert values between objects and primitive types. Operators Operator precedence and association behaves as it does in C. Note, however, that the new keyword (for creating a new object) binds tighter than dot notation (.), which is different behavior from C++. In particular, note the following expression: new foo().bar; This expression operates as if it were written like this: (new foo()).bar; Operator overloading, as in C++, cannot be accomplished in Java. The , operator of C has been deleted. The >>> operator produces an unsigned logical right shift (remember, there are no unsigned data types). The + operator can be used to concatenate strings. Control Flow Although the if, while, for, and do statements in Java are syntactically the same as they are in C and C++, there is one significant difference. The test expression for each control flow construct must return an actual boolean value (true or false). In C and C++, the expression can return an integer. Arguments Java does not support mechanisms for optional arguments or for variable-length argument lists to functions as in C and C++. All method definitions must have a specific number of arguments. Command-line arguments in Java behave differently from those in C and C++. The first element in the argument vector (argv[0]) in C and C++ is the name of the program itself; in Java, that first argument is the first of the additional arguments. In other words, in Java, argv[0] is argv[1] in C and C++; there is no way to get hold of the actual name of the Java program. Other Differences The following other minor differences from C and C++ exist in Java: n Java does not have a preprocessor, and as such, does not have #defines or macros. Constants can be created by using the final modifier when declaring class and instance variables. n Java does not have template classes as in C++. n Java does not include C’s const keyword or the ability to pass by const reference explicitly. n Java classes are singly inherited, with some multiple-inheritance features provided through interfaces. n All functions are implemented as methods. There are no functions that are not tied to classes. n The goto keyword does not exist in Java (it’s a reserved word, but currently unimplemented). You can, however, use labeled breaks and continues to break out of and continue executing complex switch or loop constructs. Prepared By: Saif ur Rehman, Assistant Professor UIIT, PMAS Arid Agriculture University, Rawalpindi You can download in PDF as well as Video Lectures ON Java, PHP, ASP.Net with C-Sharp, Working With SQL Server, Oracle 10g, Fusion Charts in ASP.Net, Using Crystal Reports in ASP.Net, FYP Project Ideas, Software Development Latest Jobs @ http://www.ASPUiiTBlogSpot.com Send your Valuable Suggestion & Comments @ Saif@uaar.edu.pk Our YouTube Channel https://www.youtube.com/channel/UC972jiOhmfhHiA1_ylAjEAg