Internal Examination 1 Answer Key DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Branch & Sec : CSE Date : 08.08.2014 Semester : V Sem Max Marks : 50 Marks Sub Code& Title : CS2305 Programming Paradigms Staff I/c : R.Venkadeshan PART A Answer all the Questions (5*2=10 Marks) 1. Define Objects and Classes in Java. Objects are the basic runtime entities in an object oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program may handle. They may also represent user defined data types such as vectors and lists. The entire set of data and code of an object can be made a user defined data type using the concept of a class. A class may be thought of as a data type and an object as a variable of that data type. A class is thus a collection of objects of similar type. 2. What is the use of super keyword in Java? 1. Using super to call Superclass Constructors 2. Using super to call Superclass Methods 3. List out the different JavaDoc Comments. Types of Comments Class Comments - /*.*/ and /* * */ Method Comments - @param, @return, @throws Field Comments Data Members - /* * */ General Comments - @author, @version, @since. 4. Define the different forms of Inheritance with its syntax. 1. Classical form of Inheritance is-a relationship 2. Containment form of Inheritance has-a relationship 5. Write the syntax of foreach in Java? Syntax: for (variable : collection) Statement; PART B (40 Marks) Answer All the Questions 6. a) i. Create a class Student, declare the non- static data members such as studid, studname, and static data members such as m1, m2, m3, tot, avg. Define the Constructor to initialize the non-static members, use the static member function to get the marks details from the user and perform total and average calculations. Finally use the member function display () to print all the details to the user. (12)
class Student int studid; String studname; static double m1,m2,m3,tot,avg; Student() Scanner in = new Scanner(System.in); System.out.println( Enter Student ID: ); studid = in.nextint(); System.out.println( Enter Student Name: ); studname = in.next(); static void process() System.out.println( Enter Student s Marks-1: ); Scanner mk = new Scanner(System.in); m1 = mk.nextint(); System.out.println( Enter Student s Marks-2: ); m2 = mk.nextint(); System.out.println( Enter Student s Marks-3: ); m3 = mk.nextint(); total = m1+m2+m3; avg = total/3.0; void display() if(m1>=50 && m2>==50 && m3>=50) System.out.println( Student ID is: +studid); System.out.println( Student Name is: +studname); System.out.println( Mark-1 : +m1); System.out.println( Mark-2 : +m2); System.out.println( Mark-3 : +m3); System.out.println( Total : +total); System.out.println( Average : +avg); System.out.println( Result: Pass ); else System.out.println( Result: Fail );
class Demo public static void main(string args[]) Student s = new Student(); Student.process(); s.display(); ii. Define the use of finalize() method in Java. (4) Sometimes an object will need to perform some action when it is destroyed. By using finalization, object can reclaimed by the garbage collection. To add a finalizer to a class, simply define the finalize() method. Java runtime calls that method whenever it is about to recycle an object of that class. Inside the finalize() method you will specify those actions that must be performed before an object is destroyed. The garbage collector runs periodically, checking for objects that are no longer referenced by any running state or indirectly through other referenced objects. User doesn t know when this method will invoke, it depends JVM. Syntax: protected void finalize() //Body of the Method Protected is a specifier that prevents access to finalize() by code define outside its class. finalize() method is just prior to garbage collection, and not called when an object goes out-of-scope means you cannot know when or even if finalize() will be executed. Java does not support destructors, finalize() method only approximates this function. finalize() method provides other means of releasing System resources. (or) b) i. Define Package in Java. Explain in details about the creation of user-defined package with the support of Access Specifiers. (16) Packages are java s way of grouping a variety of classes and / or interfaces together. The grouping is usually done according to functionality. In fact, packages act as containers for classes. Creating our own package involves the following steps: 1. Declare the package at the beginning of a file using the form package package_name; 2. Define the class that is to be put in the package and declare it public. 3. Create a subdirectory under the directory where the main source files are stored. 4. Save the Package file in.java compile the file creates.class file in the subdirectory. 5. The subdirectory name must match the package name exactly. Note: Java also supports the concept of package hierarchy. This done by specifying multiple names in a package statement, separated by dots. Example: package firstpackage.secondpackage;
package package1; public class ClassA public void displaya() System.out.println("Class A"); Access Modifier Access Location Public Protected friendly (default) private Same Class Yes Yes Yes Yes Subclass in same package Yes Yes Yes No Other classes in same package Yes Yes Yes No Subclass in other package Yes Yes No No Non subclasses in other packages Yes No No No import package1.classa; class PackageTest1 public static void main(string args[]) ClassA objecta = new ClassA() objecta.displaya(); 7. a) i. Explain the following String Handling functions in Java. (16) String Constructor String str = new String(); String(char ch[], int start,int numchar); String(String strobj); String (byte asciichars[]); String(byte asciichars[], int start, int numchars); Character Extraction functions char charat(int where) void getchars(int sourcestart,int sourceend,char target [ ],int targetstart) byte [] getbytes(); char [ ] tochararray() String Modification functions String substring(int startindex) String substring(int startindex,int endindex) String concat(string str); String replace(char original, char replacement)
String trim() String touppercase(); String tolowercase(); String Comparison Functions. boolean equals(string str) boolean equalsignorecase(string str) boolean regionmatches(int str1startindex, String str2, int str2startindex, int numchars) boolean regionmatches(boolean ignorcase, int str1startindex, String str2, int str2startindex, int numchars) boolean startswith(string str) boolean endswith(string str) obj1.equals(obj2); (obj1 == obj2); int compareto(string str); (or) b) i. Give an elaborate discussion on Inheritance and its types with the suitable support of program. (16) Inheritance is the mechanism which allows a class B to inherit properties/characteristics-attributes and methods of a class A. We say B inherits from A". What are the Advantages of Inheritance 1. Reusability of the code. 2. To Increase the reliability of the code. 3. To add some enhancements to the base class. Inheritance achieved in two different forms 1. Classical form of Inheritance 2. Containment form of Inheritance Classical form of Inheritance We can now create objects of classes A and B independently. Example: A a; //a is object of A B b; //b is object of B Containment Inheritance We can also define another form of inheritance relationship known as containership between class A and B. Example: class A -------- class B --------- A a; // a is contained in b B b;
Types of Inheritance 1. Single Inheritance (Only one Super Class and One Only Sub Class) 2. Multilevel Inheritance (Derived from a Derived Class) 3. Hierarchical Inheritance (One Super Class, Many Subclasses) 8. a) Write a program to implement the Method Overriding concept in Java.(8) class Room void Room_Super() System.out.println("The Room Base is Displayed"); class HallRoom extends Room void Room_Super() System.out.println("The Sub Class Room Base is Displayed"); super.room_super(); class MainRoom public static void main(string [] args) HallRoom br = new HallRoom(); br.room_super(); ***** All the Best *****