Program to demonstrate DataInputStream class Using Conditional Operator

/* Program to demonstrate DataInputStream class Using Conditional Operator */

import java.io.DataInputStream;   // to load DataInputStream class
class P22B
{   
    public static void main(String args[])
    {
        int x,y;
        // creating object of class DataInputStream.
                   DataInputStream in = new DataInputStream(System.in);

        try
           {

        System.out.print(" Enter a number  : ");
        x = Integer.parseInt(in.readLine());
   
        if(x>0)
             y=1;
        else if(x<0)
            y=-1;
             else
            y=0;

        System.out.println(" x = "+x+"   y = "+y);
        }

        catch(Exception e) {  System.out.println("I/O Error");   }
    }
}

/* Note : This program might give some Note at compile time, ignore it and go ahead. */





/* Using Conditional Operator */

import java.io.DataInputStream;   // to load DataInputStream class
class P22C
{   
    public static void main(String args[])
    {
        int x,y;
        // creating object of class DataInputStream.
                   DataInputStream in = new DataInputStream(System.in);

        try
           {

        System.out.print(" Enter a number  : ");
        x = Integer.parseInt(in.readLine());
   
        y = (x>0)? 1: ( (x<0) ? -1 : 0 );   // using conditional operator

        System.out.println(" x = "+x+"   y = "+y);
        }

        catch(Exception e) {  System.out.println("I/O Error");   }
    }
}

    /* Note : This program might give some Note at compile time, ignore it and go ahead. */    

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|