Translate

Monday, 9 July 2012

Write java code to print following pattern

..........
.........
........
.......
......
.....
....
...
..
.
Solution:

class Pattern4
{
public static void main(String args[])
{
for(int i=0;i<10;i++)
{
for(int j=i;j<10;j++)
{
System.out.print(".");
}
System.out.println();
}
}
}



Write java code to reverse a given string using "StringBuffer" class


import java.io.*;

class ReverseString
{
public static void main(String args[]) throws IOException
{
String str;
System.out.println("Enter a string");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
StringBuffer sb=new StringBuffer(str);
String revstr=new String(sb.reverse());
System.out.println("Reverse of "+str+" is "+revstr);
}

}

Write java code to reverse a given number


import java.io.*;

class ReverseNum
{
public static void main(String args[]) throws IOException
{
int num,rem,revnum=0,originalnum;
System.out.println("Enter a number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num=Integer.parseInt(br.readLine());
originalnum=num;
while(num>0)
{
rem=num%10;
num=(num-rem)/10;
revnum=revnum*10+rem;
}
System.out.println("Reverse of "+originalnum+" is "+revnum);
}
}

Saturday, 7 July 2012

Write java code to find the Smallest of 3 numbers using ternary operator


import java.io.*;

class Small
{
public static void main(String args[]) throws IOException
{
int num1,num2,num3,small_num;
System.out.println("Enter first number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num1=Integer.parseInt(br.readLine());
System.out.println("Enter second number");
num2=Integer.parseInt(br.readLine());
System.out.println("Enter third number");
num3=Integer.parseInt(br.readLine());
small_num = (num1 < num2) ?( (num1<num3) ? num1:num3):(num2<num3?num2:num3);
System.out.println("The Smallest Number is: "+small_num);
}

}

Write java code to find the Biggest of 3 numbers using ternary operator


import java.io.*;

class Big
{
public static void main(String args[]) throws IOException
{
int num1,num2,num3,big_num;
System.out.println("Enter first number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num1=Integer.parseInt(br.readLine());
System.out.println("Enter second number");
num2=Integer.parseInt(br.readLine());
System.out.println("Enter third number");
num3=Integer.parseInt(br.readLine());
big_num = (num1 > num2) ?( (num1>num3) ? num1:num3):(num2>num3?num2:num3);
System.out.println("The Biggest Number is: "+big_num);
}

}

Write java code to obtain absolute value of given number using "ternary(?)" operator


import java.io.*;

class Ternary
{
public static void main(String args[]) throws IOException
{
int num,k;
System.out.println("Enter a number");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
num=Integer.parseInt(br.readLine());
k = num < 0 ? -num : num;   //get absolute value of num
System.out.println("Absolute value of "+num+" is "+k);
}

}

The ? operator

Java includes a special ternary (three-way) operator that can replace certain types of if-then-else statements. This operator is the ?, and it works in Java much like it does in C,C++, and C#. It can seem somewhat confusing at first, but the ? can be used very effectively once mastered. The ? has this general form:

expression1 ? expression2 : expression3

Here, expression1 can be any expression that evaluates to a boolean value. If expression1 is true, then expression2 is evaluated; otherwise expression3 is evaluated. The result of the ? operation is that of the expression evaluated. Both expression2 and expression3 are required to return the same type, which can't be void.
Here is an example of the way that the ? is employed:

ratio = denom == 0 ? 0 : num/denom;

When Java evaluates this assignment expression, it first looks at the expression to the left of the question mark. If denom equals zero, then the expression between the question mark and the colon is evaluated and used as the value of the entire ? expression. If denom does not equal zero, then the expression after the colon is evaluated and used for the value of the entire ? expression. The result produced by the ? operator is then assigned to ratio.