Question1: answer the following in object operating programming :- A.Tell whethe
ID: 3820270 • Letter: Q
Question
Question1: answer the following in object operating programming :-
A.Tell whether the following piece of code is valid or not? if not, tell the reason also.
class Super {
...
public void superToSuper(Super anotherSuper){
int i = anotherSuper.public_Super_Field;
int j = anotherSuper.protected_Super_Field;
int l = anotherSuper.private_Super_Field;
}
...
}
class Sub extends Super {
...
public void subToSub(Sub anotherSub){
int i = anotherSub.public_Sub_Field;
int j = anotherSub.protected_Sub_Field;
int k = anotherSub.private_Sub_Field;
int l = anotherSub.public_Super_Field; //inherited
int m = anotherSub.protected_Super_Field; //members
int n = anotherSub.private_Super_Field;
}
...
}
B. Consider that following is the definition of an interface:
interface Callback {
void callback(int param);
}
Depict how the class Client implement this interface
C. How a thread can be suspended, resumed and stopped by using a flag variable
D.How monitor help in synchronization?
E.There are three threads T1, T2, and T3? How do you ensure sequence T1, T2, T3 in Java?
F.What is multicasting or multicast transmission? Which Protocol is generally used for multicast? TCP or UDP?
G.Write the missing Code?
import java.______;
import java.______;
class Whois {
public static void main(String args[]) throws Exception {
int c;
// Create a socket connected to internic.net, port 43.
Socket s = new Socket("internic.net", 43);
InputStream in = _____ getInputStream();
OutputStream out = s.getOutputStream();
// Construct a request string.
String str = (args.length == 0 ? "osborne.com" : args[0]) + " ";
byte buf[] = str._________(); // Convert to bytes.
out.write(buf); // Send request
// Read and display response.
while ((c = in.read()) != -1) {
System.out.print((char) c); }
s.close(); } }
H.How to implement an applet into a web page using applet tag?
I.Explain how to implement an applet into a web page using applet tag?
J.How to implement an applet into a Java program?
K.Explain how to set the background color within the applet area?
L.What are the methods that control an applet’s on-screen appearance?
M.Explain how to play sound in an applet?
N.Write the missing code in the following applet.
import java.awt.*;
import java.applet.*;
public class SimpleApplet ________________ {
public void paint(Graphics g) {
_______drawString("A Simple Applet", 20, 20); } }
Explanation / Answer
A the given code is not valid
class Super {
...
public void superToSuper(Super anotherSuper){
int i = anotherSuper.public_Super_Field;
int j = anotherSuper.protected_Super_Field;
int l = anotherSuper.private_Super_Field;
}
...
}
class Sub extends Super {
...
public void subToSub(Sub anotherSub){
int i = anotherSub.public_Sub_Field;
int j = anotherSub.protected_Sub_Field;
int k = anotherSub.private_Sub_Field;
int l = anotherSub.public_Super_Field; //inherited
int m = anotherSub.protected_Super_Field; //members
int n = anotherSub.private_Super_Field;
}
...
since in Sub class we are accessing the private member of the super class which will give error.
since we can access the private members of the class inside the member function of the same class
B)
interface Callback {
void callback(int param);
}
Depict how the class Client implement this interface
==> class Client implements interface{
}
C. How a thread can be suspended, resumed and stopped by using a flag variable
please find the code
class ClassThread implements Runnable
{
String threadName;
Thread thread;
boolean suspendFlag;
ClassThread(String threadname)
{
threadName = threadname;
thread = new Thread(this, threadName);
System.out.println("New thread : " + threadName);
suspendFlag = false;
thread.start();
}
public void run()
{
try
{
for(int i=0; i<12; i++)
{
System.out.println(threadName + " : " + i);
Thread.sleep(200);
synchronized(this)
{
while(suspendFlag)
{
wait();
}
}
}
}
catch(InterruptedException e)
{
System.out.println(threadName + " interrupted");
}
System.out.println(threadName + " exiting...");
}
synchronized void mysuspend()
{
suspendFlag = true;
}
synchronized void myresume()
{
suspendFlag = false;
notify();
}
}
class ThreadClass
{
public static void main(String args[])
{
ClassThread obj1 = new ClassThread("First");
ClassThread obj2 = new ClassThread("Second");
try
{
Thread.sleep(1000);
obj1.mysuspend();
System.out.println("Suspending first thread ");
Thread.sleep(1000);
obj1.myresume();
System.out.println("Resuming first thread");
obj2.mysuspend();
System.out.println("Suspending second thread");
Thread.sleep(1000);
obj2.myresume();
System.out.println("Resuming second thread");
}
catch(InterruptedException e)
{
System.out.println("interupted main thread ");
}
System.out.println("exiting...");
}
}
D.How monitor help in synchronization?
A monitor is like a building that contains one special room that can be occupied by only one thread at a time. The room usually contains some data. From the time a thread enters this room to the time it leaves, it has exclusive access to any data in the room. Entering the monitor building is called entering the monitor. Entering the special room inside the building is called acquiring the monitor. Occupying the room is called owning the monitor and leaving the room is called releasing the monitor. Leaving the entire building is called exiting the monitor.
E .There are three threads T1, T2, and T3? How do you ensure sequence T1, T2, T3 in Java?
to get the order of the sequence of thread T1, T2 T3 join () method is used
fiirst the main thread will call the join method on t1 e.g. T1.join()
and in the run() method of T1, T1 will call join() on T2 and then run() of T2 will call join() method on T3.
in java monitor implementaion is called by following methods
1) wait() then this method is invoked the current thread will enter the waiting state and wait untill there will be notification from other thread to resume it's processing. when a thread call the wait() method any lock acquire by that thread will be released.
2) notify() whent the notify() is called any thread from the thread scheduler waiting for the CPU will be given the chance to process and resume its execution.
3) notifyall() will makes all the thread waiting for the CPU to starts its operation
due to time constraint i am not able to answer all the ques
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.