Tuesday, April 01, 2014

WBUT 2012 -2013 JAVA SOLUTIONS PART6


1.         Write an applet program to change the Text color and font.

import java.awt.*;
import java.applet.*;
public class font1 extends Applet
{
            public void paint(Graphics g)
            {
                        String s1=new String("Font & Color Changes");
                        g.setColor(Color.red);
                        Font f1=new Font("Arial",Font.PLAIN,16);
                        g.setFont(f1);
                        g.drawString(s1,50,50);

                       
                        g.setColor(Color.blue);          
                        Font f2=new Font("Arial",Font.BOLD,20);
                        g.setFont(f2);
                        g.drawString(s1,50,150);
                       
                        g.setColor(Color.green);       
                        Font f3=new Font("Arial",Font.ITALIC,24);
                        g.setFont(f3);
                        g.drawString(s1,50,250);

                       
            }
}
2.          Write the difference between C, java and C++.

There is a lot of difference between C++ and Java but there has been a misconception among several people that both Java and C++ are object oriented language and there is hardly any difference between the two. Let us look at the major differences between these 2 languages. Let us look at the differences between the two languages:
C++ is object based language while Java is a pure object oriented language.
C++ makes use of pointers to address memory locations while Java doesn't allows pointers, rather it manages all the memory handling tasks by itself, thus freeing the user from memory handling complexities and provides security.
In C++, if value of one variable is changed then the change in value of that variable is reflected in other areas also where that variable is used, whereas, Java ensures that if value of one variable has been changed in one area then in other area the change is not reflected.
C++ is confined to a particular system and thus the object code generated by the compiler of C++ can't be used on other machines, whereas, Java is a portable language that allows execution of the code on any machine where JVM (Java Virtual Machine) is installed.
C++ is not a secure language and may contain malicious code, whereas, Java is a secure language that uses a Bytecode verifier to make sure that code doesn't contain malicious code within it.
C++ can't be used on distributed systems and also, it can't be used over the web, whereas, Java can be run on distributed systems and over the web.
In C++ we first include a preprocessor and then proceed to the main function, whereas, in Java no such thing is done, everything is done within the class.
C++ supports multiple inheritance, whereas, java doesn't support multiple inheritance as it leads to ambiguity.
C++ defines GOTO keyword and also supports its usage, whereas, Java has a GOTO keyword but doesn't allow its usage.
Operating overloading is a prominent feature of C++, whereas, Java doesn't support operator overloading.
In C++ garbage collection is done manually, whereas, in Java garbage collection is done automatically.
Java has built-in multithreading process, whereas, C++ has no such process.
In Java you can't have "inline" methods but as an alternative option we have "final" keyword.
In Java you can't specify public, private and protected inheritance as in C++ and the overridden methods in a derived class cannot change the access of the method in the base class.

3.         What is an object referencing? Give a demo program.
class test
{
            int a,b;
            test(int i,int j)
            {
                        int a=i;b=j;
            }
            void meth(test o)
            {
                        o.a*=2;
                        o.b/=2;
            }
}
class callbyref
{
            public static void main(String args[])
            {
                        test ob=new test(15,20);
                        System.out.println("Before call, a ="+ob.a+" b= "+ob.b);
                        ob.meth(ob);
                        System.out.println("After call, a ="+ob.a+" b= "+ob.b);
            }
}
When an object reference is passed to a method, the reference itself is passed by use of call – by – value. However, since the value being passed refers to an object, the copy of that value will still refer to the same object that it’s corresponding arguments does.

NB:-When a simple type is passed to a method, it is done by use of call – by – reference.

No comments:

Post a Comment