Tuesday, March 18, 2008

type promotion

/* Program to demonstrate type promotion */

class TypePromotion
{
public static void main(String args[])
{
byte b = 52;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 7.67f;
double d = .1234;
double result = (f*b) + (i/c) - (d*s);

System.out.println((f*b)+" + "+(i/c)+" - "+(d*s)+" = "+result);

}
}

ternary Operator

/* Program to demonstrate the ternary Operator */



class Ternary
{

public static void main(String args[])
{
int value1 = 1;
int value2 = 2;
int result;
boolean Condition = true;

result = Condition ? value1 : value2;

System.out.println("Result is : "+result);

}
}

Remainder

class Remainder
{

public static void main (String args[])
{

int i = 10;
int j = 3;

System.out.println("i is : " + i);
System.out.println("j is : " + j);

int k = i % j;
System.out.println("i % j is : " + k);
}

}

Relational Operators

/* Program to demonstrate Relational Operators */

class RelationalOp
{
public static void main(String args[])
{
float x = 15.0F, y = 10.65F, z = 25.0F;
System.out.println("x = "+x);
System.out.println("y = "+y);
System.out.println("z = "+z);
System.out.println("x < y is : "+(xless thany));
System.out.println("x > y is : "+(xgreater than y));
System.out.println("x == z is : "+(x==z));
System.out.println("x <= z is : "+(x<=z));
System.out.println("x >= y is : "+(x>=y));
System.out.println("y != z is : "+(y!=z));
System.out.println("y == x+z is : "+(y==x+z));
}
}

using DataInputStream

import java.io.DataInputStream;

class Read
{
public static void main(String args[])
{
int i;
float f;
DataInputStream in= new DataInputStream(System.in);
try
{

System.out.println("Enter Integer number:");
i=Integer.parseInt(in.readLine());

System.out.println("Enter Float number:");
f = Float.valueof(in.readLine()).floatvalue();
}
catch(Exception e)
{

}

System.out.println("Integer Number is : "+i);
System.out.println("Float Number is : "+f);
}
}

implements the quadratic formula

/* Program to implements the quadratic formula */

class P9
{

public static void main(String args[])
{

//implements the quadratic formula

double a=2,b=8.001,c=8.002 ;

System.out.println("a :"+a);
System.out.println("b :"+b);
System.out.println("c :"+c);


System.out.println("\n The equation is : "+a+" * x * x + "+b+" * x + "+c+" = 0 ");

double d = b * b - 4 * a * c ;
double sqrtd = Math.sqrt(d);
double x1 = ( - b + sqrtd) / (2 * a ) ;
double x2 = ( - b - sqrtd) / (2 * a ) ;

System.out.println("The Solution are : ");
System.out.println("\t x1 ="+x1);
System.out.println("\t x2 ="+x2);

}
}

exchange the digits of 2-digit number

/* Program to exchange the digits of 2-digit number */

class P8
{
public static void main(String args[])
{
int number= 71, temp, tens;

System.out.println("\n The Original Number : "+number);

temp = number % 10;
tens = 10 * temp;
temp = number /10;
number = tens + temp ;

System.out.println("\n The Exchanged Number : "+number);

}
}