TY_BCS_JAVA_SLIP 18_1



Slip 18_1 : Define a class MyNumber having one private int no 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 and perform the above tests.

class MyNumber
{
                int no;
                                MyNumber()
                                {no=0;}
                MyNumber(int no)
                {
                                this.no=no;
                }
               
                                void isNegative()
                                {
                                                if(no<0)
                                                                System.out.println("Given number is negative.\n ");
                                }
               
                                void isPositive()
                                {
                                                if(no>0)
                                                                System.out.println("Given number is positive..\n");
                                                                               
                                }
               
                                void isZero()
                                {
                                                if(no==0)
                                                                System.out.println("Given number is negative..\n ");
                                                                               
                                }
               
                                void isOdd()
                                {
                                                if((no%2)==1)
                                                                System.out.println("Given number is Odd..\n ");
                                                                               
                                }
               
                                void isEven()
                                {
                                                if((no%2)==0)
                                                                System.out.println("Given number is Even.\n ");
                                }
}

class Slip18_1
{
                public static void main(String args[])
                {
                               
                                                int num=Integer.parseInt(args[0]);
                                               
                                                MyNumber ob=new MyNumber(num);
                                                ob.isNegative();
                                                ob.isPositive();
                                                ob.isZero();
                                                ob.isOdd();
                                                ob.isEven();
                }
}

No comments:

Post a Comment