Pages

Monday, 7 March 2011

SEM1 java ass1


****************************Ass:1.Set:A.Que:1*********************************
Que)Using javap, view the methods of the following classes from the lang package:
Object, String and Math.
*********************************Java Code************************************
C:\>javap java.lang.String

    public static java.lang.String format(java.lang.String, java.lang.Object[]);

    public static java.lang.String format(java.util.Locale, java.lang.String, java.lang.Object[]);
    public static java.lang.String valueOf(java.lang.Object);
    public static java.lang.String valueOf(char[]);
    public static java.lang.String valueOf(char[], int, int);
    public static java.lang.String copyValueOf(char[], int, int);
    public static java.lang.String copyValueOf(char[]);
    public static java.lang.String valueOf(boolean);
    public static java.lang.String valueOf(char);
    public static java.lang.String valueOf(int);
    public static java.lang.String valueOf(long);
    public static java.lang.String valueOf(float);
    public static java.lang.String valueOf(double);
    public native java.lang.String intern();
    public int compareTo(java.lang.Object);
    static {};

C:\>javap java.lang.Object
    Compiled from "Object.java"
    public class java.lang.Object{
    public java.lang.Object();
    public final native java.lang.Class getClass();
    public native int hashCode();
    public boolean equals(java.lang.Object);
    protected native java.lang.Object clone()throws java.lang.CloneNotSupportedException;
    public java.lang.String toString();
    public final native void notify();
    public final native void notifyAll();
    public final native void wait(long)throws java.lang.InterruptedException;
    public final void wait(long, int)throws java.lang.InterruptedException;
    public final void wait()throws java.lang.InterruptedException;
    protected void finalize()throws java.lang.Throwable;
    static {};
}

C:\>javap java.lang.Math

    public static double ulp(double);
    public static float ulp(float);
    public static double signum(double);
    public static float signum(float);
    public static double sinh(double);
    public static double cosh(double);
    public static double tanh(double);
    public static double hypot(double, double);
    public static double expm1(double);
    public static double log1p(double);
    public static double copySign(double, double);
    public static float copySign(float, float);
    public static int getExponent(float);
    public static int getExponent(double);
    public static double nextAfter(double, double);
    public static float nextAfter(float, double);
    public static double nextUp(double);
    public static float nextUp(float);
    public static double scalb(double, int);
    public static float scalb(float, int);
    static {};

******************************************************************************



****************************Ass:1.Set:A.Que:2*********************************
Que)    Compile sample program 2. Type the following command and view the bytecodes.
javap –c MyClass
*********************************Java Code************************************
  public class Myclass
{
        int num;
        public Myclass()
        {
                num=0;
        }
       public Myclass(int num)
       {
       this.num = num;
       }
public static void main(String[] args)
{
        Myclass m1 = new Myclass();
        int n = Integer.parseInt(args[0]);
        Myclass m2 = new Myclass(n);
        System.out.println(m1);
        System.out.println(m2);
}
}
/*
Output:
 11:  invokestatic    #5; //Method java/lang/Integer.parseInt:(Ljava/lang/Stri
ng;)I
   14:  istore_2
   15:  new     #3; //class Myclass
   18:  dup
   19:  iload_2
   20:  invokespecial   #6; //Method "<init>":(I)V
   23:  astore_3
   24:  getstatic       #7; //Field java/lang/System.out:Ljava/io/PrintStream;
   27:  aload_1
   28:  invokevirtual   #8; //Method java/io/PrintStream.println:(Ljava/lang/Obj
ect;)V
   31:  getstatic       #7; //Field java/lang/System.out:Ljava/io/PrintStream;
   34:  aload_3
   35:  invokevirtual   #8; //Method java/io/PrintStream.println:(Ljava/lang/Obj
ect;)V
   38:  return
*/

************************************************************************


****************************Ass:1.Set:A.Que:3*********************************
Que)Compile sample program 2. Execute it using the following command. This gives a
list of the classes loaded by the JVM.
java –verbose MyClass
*********************************Java Code************************************
public class Myclass
{
        int num;
        public Myclass()
        {
                num=0;
        }
       public Myclass(int num)
       {
       this.num = num;
       }
public static void main(String[] args)
{
        Myclass m1 = new Myclass();
        int n = Integer.parseInt(args[0]);
        Myclass m2 = new Myclass(n);
        System.out.println(m1);
        System.out.println(m2);
}
}
/*
Output:
[Loaded java.security.AllPermission from shared objects file]
[Loaded java.security.UnresolvedPermission from shared objects file]
[Loaded java.security.BasicPermissionCollection from shared objects file]
[Loaded java.security.Principal from shared objects file]
[Loaded java.security.cert.Certificate from shared objects file]
[Loaded Myclass from file:/C:/]
[Loaded java.lang.IndexOutOfBoundsException from shared objects file]
[Loaded java.lang.ArrayIndexOutOfBoundsException from shared objects file]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at Myclass.main(Myclass.java:15)
[Loaded java.util.AbstractList$Itr from shared objects file]
[Loaded java.util.IdentityHashMap$KeySet from shared objects file]
[Loaded java.util.IdentityHashMap$IdentityHashMapIterator from shared objects fi
le]
[Loaded java.util.IdentityHashMap$KeyIterator from shared objects file]
[Loaded java.io.DeleteOnExitHook from shared objects file]
[Loaded java.util.LinkedHashSet from shared objects file]
[Loaded java.util.HashMap$KeySet from shared objects file]
[Loaded java.util.LinkedHashMap$LinkedHashIterator from shared objects file]
[Loaded java.util.LinkedHashMap$KeyIterator from shared objects file]
*/
******************************************************************************


****************************Ass:1.Set:B.Que:1*********************************
Que)      Define a class MyNumber having one private int data member. Write a default
constructor to initialize it to 0 and another constructor to initialize it to a value (Use this).
Write methods isNegative, isPositive, isZero, isOdd, isEven. Create an object in main.
Use command line arguments to pass a value to the object (Hint : convert string argument to integer)
and perform the above tests. Provide javadoc comments for all constructors
and methods and generate the html help file.

*********************************Java Code***********************************

class MyNumber
{
    private int num;
    public  MyNumber()
    {
        num=0;
    }
     public  MyNumber(int num)
    {
        this.num=num;
    }
    public void isnegative()
    {
        if(num<0)
        System.out.println("Number is negative:");
    }
    public void ispositive()
    {
        if(num>0)
         System.out.println("Number is positive:");
        else
         System.out.println("Number is not positive");
    }
    public void iszero()
    {
        if(num==0)
         System.out.println("Number is zero");
        else
         System.out.println("Number is not zero");
    }
    public void isodd()
    {
        if((num%2)!=0)
         System.out.println("Number is odd:");
        else
         System.out.println("Number is not odd");
    }
    public void iseven()
    {
        if((num%2)==0)
         System.out.println("Number is even");
        else
          System.out.println("Number is not even");
    }

}
class MyNumberDemo
{
    public static void main(String  []args)
    {   
       
//        int n = Integer.parseInt(args[0]);
        if(args.length==1)
        {
                 int n = Integer.parseInt(args[0]);
        MyNumber n1 = new MyNumber(n);
              
        n1.isnegative();
        n1.ispositive();
        n1.iszero();
        n1.isodd();
        n1.iseven();   
        }
        else
        {
        System.out.println("Wrong number of arguments");
        }
    }
}
/*
Output:
C:\>java MyNumberDemo
Wrong number of arguments

C:\>java MyNumberDemo 4
Number is positive:
Number is not zero
Number is not odd
Number is even

C:\>java MyNumberDemo -4
Number is negative:
Number is not positive
Number is not zero
Number is not odd
Number is even
*/
******************************************************************************

****************************Ass:1.Set:B.Que:2*********************************
Que) Save the sample program 2 in a folder named javaprgs. Set the CLASSPATH to this
folder. Compile the program and use jdb to trace the program execution. Type the
following commands and see the execution.
jdb MyClass 10
help
stop in MyClass.main
run
next
next
“
Continue typing next till the program ends.
Repeat the above process and type command step instead of next. Observe the output.

*********************************Java Code***********************************
public class Myclass
{
        int num;
        public Myclass()
        {
                num=0;
        }
       public Myclass(int num)
       {
       this.num = num;
       }
public static void main(String[] args)
{
        Myclass m1 = new Myclass();
        int n = Integer.parseInt(args[0]);
        Myclass m2 = new Myclass(n);
        System.out.println(m1);
        System.out.println(m2);
}
}
************************************************************************************
/*Output:
C:\>jdb Myclass 10
Initializing jdb ...
>help
redefine <class id> <class file name>
                          -- redefine the code for a class

disablegc <expr>          -- prevent garbage collection of an object
enablegc <expr>           -- permit garbage collection of an object

!!                        -- repeat last command
<n> <command>             -- repeat command n times
# <command>               -- discard (no-op)
help (or ?)               -- list commands
version                   -- print version information
exit (or quit)            -- exit debugger

<class id>: a full class name with package qualifiers
<class pattern>: a class name with a leading or trailing wildcard ('*')
<thread id>: thread number as reported in the 'threads' command
<expr>: a Java(tm) Programming Language expression.
Most common syntax is supported.

Startup commands can be placed in either "jdb.ini" or ".jdbrc"
in user.home or user.dir

> stop in Myclass.main
Deferring breakpoint Myclass.main.
It will be set after the class is loaded.

>run
run Myclass 10
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint Myclass.main

Breakpoint hit: "thread=main", Myclass.main(), line=14 bci=0
14            Myclass m1 = new Myclass();



main[1] next
>
Step completed: "thread=main", Myclass.main(), line=15 bci=8
15            int n = Integer.parseInt(args[0]);

main[1] next
>
Step completed: "thread=main", Myclass.main(), line=16 bci=15
16            Myclass m2 = new Myclass(n);

main[1] next

Step completed: main[1] "thread=main", Myclass.main(), line=17 bci=24
17            System.out.println(m1);


main[1] next
> Myclass@116ab4e

Step completed: "thread=main", Myclass.main(), line=18 bci=31
18            System.out.println(m2);

main[1] next
Myclass@1f4689e
>
Step completed: "thread=main", Myclass.main(), line=21 bci=38
21    }

main[1] next
>
The application exited

C:\>
*/
******************************************************************************
















2 comments: