S.1 Object Oriented Programming (April/May-2012, Set-1) JNTU-Anantapur Code No: 9A05402/R09 II B.Tech II Semester Regular & Supplementary Examinations April/May - 2012 OBJECT ORIENTED PROGRAMMING Set-1 Solutions ( Common to CSS, IT and CSE ) Time: 3 Hours Max. Marks: 70 Answer any FIVE Questions All Questions carry equal marks --- 1. 2. 3. (a) Briefly write the OOP principles. (Unit-I, Topic No. 1.2.8) (b) What is an object oriented programming? How is it different from procedure oriented programming? (Unit-I, Topic No. 1.1) (a) Explain java buzz words. (Unit-II, Topic No. 2.2) (b) How java is suitable for internet? (Unit-II, Topic No. 2.1) (a) Write a java program to demonstrate static polymorphism. (b) What is inheritance? Write its advantages. (Unit-III, Topic No. 3.1) 4. Write a program to create a package Pkg 1 which includes an interface ABC with two methods Read( ) and Area( ) and a constant PI. Create another package Pkg 2, which include two class circle and rectangle implements ABC interface to compute area of circle and area of rectangle. And also explain the compilation and execution of above program. (Unit-IV, Topic No. 4.10) 5. (a) How are synchronized methods implemented? (Unit-V, Topic No. 5.15) (b) What is inter thread communication? What methods are employed? (Unit-V, Topic No. 5.15) 6. What is the task performed by layout manager? Explain different layout managers. (Unit-VI, Topic No. 6.14.1) 7. (a) What is an applet? Explain applet life cycle. (Unit-VII, Topic No. 7.3) (b) Write the differences between applet and standalone applications. (Unit-VII, Topic No. 7.2) 8. Define the following, (a) Socket (b) Proxy server (c) Internet address (d) Domain name service. (Unit-VIII, Topic No. 8.1) B.Tech. II-Year II-Sem. ( JNTU-Anantapur ) Spectrum ALL-IN-ONE Journal for Engineering Students, 2013 S.2 SOLUTIONS TO APRIL/MAY-2012, SET-1, QP Q1. (a) Briefly write the OOP principles. April/May-12, Set-1, Q1(a) Answer : For answer refer April/May-11, Set-4, Q1(b). (b) What is an object oriented programming? How is it different from procedure oriented programming? April/May-12, Set-1, Q1(b) Answer : Object Oriented Programming For answer refer Unit-I, Q1. Differences between Object Oriented and Procedure Oriented Programming Procedure-Oriented Programming 1. Procedure-Oriented Programming (POP) is an Object-Oriented Programming 1. Object-Oriented Programming (OOP) is an approach that approach in which a sequence of tasks are to modularizes the programs by creating partitioned be done. memory area for data and methods. 2. POP emphasizes on a procedure which involves 2. OOP emphasizes on data rather than procedures. doing things. 3. In POP, large programs are divided into functions. 3. In OOP, large programs are divided into objects. 4. Data moves openly from one function to another 4. Data is hidden and is difficult for external function function in POP. 5. In POP functions share global data as there are no access specifiers. to access in OOP. 5. In OOP functions are tied together in the data structure because of access specifiers like private or public. 6. It is not possible to add data and functions whenever necessary. 7. It follows top-down approach for designing programs. 8. Examples of POP languages are C, COBOL, 6. New data and functions can be easily added, when required. 7. It follows bottom-up approach in designing programs. 8. Examples of OOP languages are C++, Java. FORTRAN. Q2. (a) Explain java buzz words. April/May-12, Set-1, Q2(a) Answer : For answer refer Unit-II, Q6. (b) How java is suitable for internet? Answer : April/May-12, Set-1, Q2(b) For answer refer Unit-II, Q2. B.Tech. II-Year II-Sem. ( JNTU-Anantapur ) S.3 Object Oriented Programming (April/May-2012, Set-1) JNTU-Anantapur Q3. (a) Write a java program to demonstrate static polymorphism. April/May-12, Set-1, Q3(a) Answer : import <java.io.*; import <java.util.*; class static { public void display(int n) Q4. Write a program to create a package Pkg1 which includes an interface ABC with two methods Read( ) and Area( ) and a constant PI. Create another package Pkg2, which include two class circle and rectangle implements ABC interface to compute area of circle and area of rectangle. And also explain the compilation and execution of above program. April/May-12, Set-1, Q4 Answer : This is ABC.java { system.out.println(“\n\t integer value is :” + n); package Pk1; public interface ABC { } public void display(float val) public void read( ); public void area( ); { system.out.println(“\n\t float value is :” + val); public double PI = 3.14; } } public void display(char c) This is Circle.java package Pk2; { import Pk1.ABC; system.out.println(“\n\t character value:”+c); public class circle implements ABC { } double r; } public void read ( ) { public class staticpolymorphism system.out.println(“Enter the value of { radius:”); public static void main(string args[ ]) r = system.in.read( ); { } staticpolymorphism obj = new staticpolymorphism( ); public void area( ) { obj.display(35); system.out.println(“ The area of circle is=:” + (PI*r*r)); obj.display(2.36f); } obj.display(‘x’); } } This is Rectangle.java } (b) package Pk2; What is inheritance? Write its advantages. Answer : April/May-12, Set-1, Q3(b) import Pk1.ABC; public class Rectangle implements ABC { double l; Inheritance double b; For answer refer Unit-III, Q1, Topic: Inheritance. Advantages For answer refer Unit-III, Q11. B.Tech. II-Year II-Sem. public void read( ) { ( JNTU-Anantapur ) Spectrum ALL-IN-ONE Journal for Engineering Students, 2013 S.4 ⇒ system.out.println(“Enter the values of length and breadth”); l = system.in.read( ); c: \pk1\ javac ABC.java b = system.in.read( ); c: \pk2\ javac Circle.java c: \pk2\ javac Rectangle.java } public void area( ) c: \pk2\ javac AreaDemo.java { ⇒ ⇒ } The output is obtained as follows: Circle and Rectangle Areas: } Enter the value of radius : 2 This is AreaDemo.java The area of circle is = : 12.56 package pk2; Enter the values of length and breadth : 2 import pk1.ABC; The area of rectangle is = : 6 public class AreaDemo { Q5. (a) public static void main(String args[ ]) { system.out.println(“Circle and Rectangle Areas:”); ABC in = new ABC( ); Circle c = new circle( ) ; Rectangle r = new Rectangle( ); in = c; c.read( ); c.area( ); in = r; c.read( ); c.area( ); } } Compilation and Execution ⇒ First create two directors in the C drive. name the directories as pk1 and pk2. Save ABC.java file into the directory pk1. ⇒ Save Circle.java, Rectangle.java and AreaDemo.java file into the directory pk2. ⇒ Finally run the main file as follows: c: \pk2\ java pk2.AreaDemo system.out.println(“Thte area of rectangular is=:”+(l*b)); ⇒ Open the command prompt and compile each and every file to find out the errors as follows: Set the class path as: Set CLASSPATH = .; c : \; B.Tech. II-Year II-Sem. Answer : 3. How are synchronized methods implemented? April/May-12, Set-1, Q5(a) Synchronization is a process in which an access to a shared resource is allowed to only one thread at a time. Synchronization is required in multithreaded programming when two or more threads tries to access the shared resources at the same time. If two threads are allowed to access the shared resource at the same time then the data will be changed, hence producing the wrong output. Thread synchronization is important in multithreaded programming in order to maintain the data consistency. In Java, synchronization is achieved using “synchronized” keyword. A block or a method that should be shared among different threads is declared as synchronized. A thread that calls the synchronized method gets the access to the shared resource. Another thread tries to call the same method will block until the first thread relinquish or leave that method. Once the thread leaves method then other threads waiting for that synchronized method can make a call. And hence in this way the data consistency is maintained. The following program shows the importance of synchronization in multithreaded programming. Example class First { synchronized void call(String str) { ( JNTU-Anantapur ) S.5 Object Oriented Programming (April/May-2012, Set-1) JNTU-Anantapur System.out.print("[ "+str); Second s2 = new Second(f, "World"); try Second s3 = new Second(f, "Synchronized"); { try { Thread.sleep(1000); s1.t.join( ); } s2.t.join( ); catch(InterruptedException e) s3.t.join( ); { } System.out.println("Interrupted"); catch(InterruptedException e) } { System.out.println(" ] "); System.out.println("Interrupted"); } } } } class Second implements Runnable { } Output String str; [ Hello ] First f; [ Synchronized ] Thread t; [ World ] public Second(First fir, String S) As shown in the program, by calling sleep( ), the call( ) method allows another thread to get executed. This will result in the mixed-up output of three message strings. To prevent this, method call( ) is declared as synchronized. When the sleep( ) is called by call( ), it makes other threads to wait until the call( ) resumes and returns to the calling method. This prevents other threads to access the shared resource at the same time. { f = fir; str = S; t = new Thread(this); t.start( ); (b) } public void run( ) { f.call(str); } } class SynchDemo Answer : What is inter thread communication? What methods are employed? April/May-12, Set-1, Q5(b) In Java, interthread communication is possible using three methods wait( ), notify( ) and notifyAll( ). These methods are defined in Object class as final. Thus they are available to all the classes. These methods are used with synchronization. That is, they can be invoked only from within a synchronization method or within a synchronized block. wait( ) { public static void main(String args[ ]) { First f = new First( ); Second s1 = new Second(f,"Hello"); B.Tech. II-Year II-Sem. This method causes the calling thread to wait until some other thread wakes it up using notify( ) or notifyAll( ) method. notify( ) This method wakes up a single thread that is waiting on this object’s monitor. ( JNTU-Anantapur ) Spectrum ALL-IN-ONE Journal for Engineering Students, 2013 S.6 notifyAll( ) System.out.println(“Caught interrupted exception”); This method wakes up all thread that are waiting on this object monitor. } Example The following program illustrates interthread communication producer/consumer problem. this.m = m; value = true; System.out.println(“Produced:”+m); //Creation of class “Queue” notify( ); class Queue } { } int m; boolean value = false; synchronized int get( ) { while(!value) try //Creation of a threaded object, the “Producer” generate // Entries of a “Queue” class Producer implements Runnable { Queue q; Producer(Queue q) { { wait( ); this.q = q; } new Thread(this, “The Producer”).start( ); catch(InterruptedException ie) } { public void run( ) System.out.println(“Caught interrupted exception”); { } int j = 0; System.out.println(“Received:”+m); while(true) value = false; { notify( ); q.put(j++); return m; } } } synchronized void put(int m) } { while (value) // Creation of a threaded object, The “Consumer” to // Consume entries of a “Queue” try class Consumer implements Runnable { { wait( ); Queue q; } Consumer(Queue q) catch(InterruptedException ie) { { this.q = q; B.Tech. II-Year II-Sem. ( JNTU-Anantapur ) S.7 Object Oriented Programming (April/May-2012, Set-1) JNTU-Anantapur new Thread(this, “The Consumer”).start( ); Q8. Define the following, } (a) Socket public void run( ) (b) Proxy server { (c) Internet address (d) Domain name service. while(true) { Answer : q.get( ); (a) } April/May-12, Set-1, Q8 Socket For answer refer Unit-VIII, Q1, Topic: Overview of Socket. } } // Creation of a class IThreadComm to create objects // of class Queue, Producer and Consumer class IThreadComm (b) Proxy Server For answer refer Unit-VIII, Q1, Topic: Proxy Servers. (c) { Internet Address For answer refer Unit-VIII, Q1, Internet Addressing. public static void main(String args[ ]) { (d) Domain Name Service For answer refer Unit-VIII, Q1, Topic: DNS. Queue q = new Queue( ); new Producer(q); new Consumer(q); System.out.println(“To stop, press Ctrl” + C); } } Q6. What is the task performed by layout manager? Explain different layout managers. April/May-12, Set-1, Q6 Answer : Task Performed by Layout Manager For answer refer Unit-VI, Q43, Topic: Layout Manager. Different Types of Layout Managers For answer refer Unit-VI, Q45. Q7. (a) What is an applet? Explain applet life cycle. April/May-12, Set-1, Q7(a) Answer : For answer refer Unit-VII, Q4. (b) Write the differences between applet and standalone applications. Answer : April/May-12, Set-1, Q7(b) For answer refer Unit-VII, Q3. B.Tech. II-Year II-Sem. ( JNTU-Anantapur )
© Copyright 2024 ExpyDoc