Hi friends, welcome back to Java Code Online. This tutorial discusses the Java Code to sort the numbers in an Array in ascending order. This Java Code is helpful in getting a better idea of the Java fundamentals, and also provide the advantage of one of the most commonly used Java Collection named TreeSet.
The Java Code is given below:-
import java.util.TreeSet;
public class SortingAscending {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
int[] num = {3,5,9,2,1,0,45,23,67,93};
TreeSet ts = new TreeSet();
for(int i = 0; i < num.length; i++)
{
ts.add(num[i]);
}
System.out.println("The numbers of the array in ascending order are: "+ts);
}
}
For more information on Java, keep buzzing Java Code Online.
Tuesday, July 28, 2009
Reverse a String - Java Program
Hi friends, welcome back to Java Code Online. The Java Code discussed today, helps in reversing any String that is entered to the system. It displays the reverse String of the String that is entered in the system.
The Java Code is given below:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReverseString {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("Enter a string to be reversed: ");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
StringBuffer sb = new StringBuffer().append(str);
System.out.println("The reversed string is:\n"+sb.reverse().toString());
}
}
Keep buzzing Java Code Online for more info on Java.
The Java Code is given below:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReverseString {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("Enter a string to be reversed: ");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
StringBuffer sb = new StringBuffer().append(str);
System.out.println("The reversed string is:\n"+sb.reverse().toString());
}
}
Keep buzzing Java Code Online for more info on Java.
Length of a String - Java Program
Hi friends, welcome back to Java Code Online. Today's tutorial consists of a Java Program to find the number of characters in a String or number. This Java Program is helpful in finding the length of any String that is entered.
The Java Code begins by asking the user to enter any String or number, and then it displays the length of that entered String.
The Java Code is given below:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NoOfWordsInString {
public static void main(String[] args) throws IOException {
System.out.println("Enter a string or number for which the number of words is to be counted: ");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
System.out.println("The no of words in the string are: "+str.length());
}
}
For more information on Java, and to get more of Java Code and Examples, keep buzzing Java Code Online.
The Java Code begins by asking the user to enter any String or number, and then it displays the length of that entered String.
The Java Code is given below:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NoOfWordsInString {
public static void main(String[] args) throws IOException {
System.out.println("Enter a string or number for which the number of words is to be counted: ");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
System.out.println("The no of words in the string are: "+str.length());
}
}
For more information on Java, and to get more of Java Code and Examples, keep buzzing Java Code Online.
Monday, July 27, 2009
Java Program for Area and Perimeter of Rectangle
Hi friends, welcome back to Java Code Online, the Java code that is discussed today, finds the area and Perimeter of a rectangle depending on the length and width of the rectangle. When you run this Java Code, then provide the length and width of the rectangle as the input arguments yo the file.
The Java code for finding the Area and Permiter of Rectangle is given below:-
import java.io.IOException;
public class AreaPerimeterRectangle {
public static void main(String[] args) throws NumberFormatException, IOException {
System.out.println("Enter the length and width of the rectangle: ");
float length = Float.parseFloat(args[0]);
float width = Float.parseFloat(args[1]);
System.out.println("Area of the Rectangle is: "+length*width);
System.out.println("Perimeter of the Rectangle is: "+2*(length+width));
}
}
For more information on Java, kepp buzzing Java Code Online.
The Java code for finding the Area and Permiter of Rectangle is given below:-
import java.io.IOException;
public class AreaPerimeterRectangle {
public static void main(String[] args) throws NumberFormatException, IOException {
System.out.println("Enter the length and width of the rectangle: ");
float length = Float.parseFloat(args[0]);
float width = Float.parseFloat(args[1]);
System.out.println("Area of the Rectangle is: "+length*width);
System.out.println("Perimeter of the Rectangle is: "+2*(length+width));
}
}
For more information on Java, kepp buzzing Java Code Online.
Labels:
Java Code,
Java Example,
Java Program,
Rectangle
Java Program to find the Area and Circumference of Circle
Hi friends, welcome back to Java Code Online. Today I will give you a simple Java Program to find the Area and Circumference of a Circle.
The Area and Circumference depends on the radius of the circle, so the code first asks the user for the radius of the circle, and then computes the area and circumference of the circle.
The code is given below:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AreaCircumferenceCircle {
final static float pi = 22/7f;
public static void main(String[] args) throws NumberFormatException, IOException {
System.out.println("Enter the radius of the circle: ");
InputStreamReader io= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(io);
float radius = Float.parseFloat(br.readLine());
System.out.println("Area of the Circle is: "+radius*radius*pi);
System.out.println("Circumference of the Circle is: "+2*radius*pi);
}
}
I hope the above code was helpful to you all. For more info on Java, keep checking Java Code Online.
The Area and Circumference depends on the radius of the circle, so the code first asks the user for the radius of the circle, and then computes the area and circumference of the circle.
The code is given below:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AreaCircumferenceCircle {
final static float pi = 22/7f;
public static void main(String[] args) throws NumberFormatException, IOException {
System.out.println("Enter the radius of the circle: ");
InputStreamReader io= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(io);
float radius = Float.parseFloat(br.readLine());
System.out.println("Area of the Circle is: "+radius*radius*pi);
System.out.println("Circumference of the Circle is: "+2*radius*pi);
}
}
I hope the above code was helpful to you all. For more info on Java, keep checking Java Code Online.
Labels:
Circle,
Java Code,
Java Example,
Java Program
Wednesday, July 22, 2009
Factorial Program in Java, Java program for finding the Factorial of a number
Hi friends, welcome back to Java Code Online. Today I will give you the Java code for finding the factorial of an entered number. The factorial of a number is the product of all the numbers starting from 1 till that particular number.
The Java code provided asks the user to enter a number for which the factorial is desired. It then uses recursion to find the factorial of that number. Though recursion is not always the best approach for solving a Java problem, since it can lead to core dump or Dr. Watson error in Windows. It implies that if recursion is used improperly, then it can loop again and again into itself, causing stackOverflowError in Java, leading to crashing of the JVM.
The Java Code is provided below:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Factorial {
public static void main(String[] args) throws NumberFormatException, IOException {
long num;
System.out.println("Enter a number greater then or equal to 0 and less then 40 for which the factorial is desired:-");
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
num = Integer.parseInt(br.readLine());
if(num>=0 && num < 40)
{
if(num == 0)
{
num =1;
}
System.out.println(factorial(num));
}
else
{
System.out.println("Number out of range");
}
}
private static long factorial(long num) {
if(num > 1)
{
num = num * factorial(num-1);
}
else
{
return num;
}
return num;
}
}
I hope that the Java code provided for finding the factorial of a number is useful to you all. If you like the post, then do leave a comment. For more info on Java, keep buzzing Java Code Online.
The Java code provided asks the user to enter a number for which the factorial is desired. It then uses recursion to find the factorial of that number. Though recursion is not always the best approach for solving a Java problem, since it can lead to core dump or Dr. Watson error in Windows. It implies that if recursion is used improperly, then it can loop again and again into itself, causing stackOverflowError in Java, leading to crashing of the JVM.
The Java Code is provided below:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Factorial {
public static void main(String[] args) throws NumberFormatException, IOException {
long num;
System.out.println("Enter a number greater then or equal to 0 and less then 40 for which the factorial is desired:-");
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
num = Integer.parseInt(br.readLine());
if(num>=0 && num < 40)
{
if(num == 0)
{
num =1;
}
System.out.println(factorial(num));
}
else
{
System.out.println("Number out of range");
}
}
private static long factorial(long num) {
if(num > 1)
{
num = num * factorial(num-1);
}
else
{
return num;
}
return num;
}
}
I hope that the Java code provided for finding the factorial of a number is useful to you all. If you like the post, then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Labels:
Factorial,
Java Code,
Java Example,
Java Program,
Recursion
Tuesday, July 21, 2009
Java Program for Fibonacci Series, Fibonacci Series Example in Java
Welcome back to Java Code Online. Today I am going to give you the Java code for generating Fibonacci Series up to a particular number. A Fibonacci Number is one which is the sum of previous two numbers in that series.
A Fibonacci series always starts with 0, 1 and then the sum of last two digits in the series i.e 0 1 1 2 3 5 8 13 21.... This is an example of a Fibonacci series.
This Java Code asks for a number till which the Fibonacci series is to be generated, and then generates the Fibonacci series up till that number.
The Java code is shown below:-
import java.io.*;
public class Fibonacci {
public static void main(String[] args)
{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println("Enter the number till which Fibonnaci Series is desired:");
int num;
try {
num = Integer.parseInt(br.readLine());
getFibonnaci(num);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static void getFibonnaci(int fibonnaci)
{
System.out.println("The Fibonnaci Series till "+fibonnaci+" is:-");
System.out.print("0 ");
int f1 =0, f2 = 1,f3;
while(f2 <=fibonnaci){
System.out.print(f2+" ");
f1 = f1 + f2;
f3 = f2;
f2 = f1;
f1 = f3;
}
}
}
I hope the code for generating the Fibonacci series is helpful to you all. If you like the post useful, then do leave a comment. For more info on Java, keep buzzing Java Code Online.
A Fibonacci series always starts with 0, 1 and then the sum of last two digits in the series i.e 0 1 1 2 3 5 8 13 21.... This is an example of a Fibonacci series.
This Java Code asks for a number till which the Fibonacci series is to be generated, and then generates the Fibonacci series up till that number.
The Java code is shown below:-
import java.io.*;
public class Fibonacci {
public static void main(String[] args)
{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println("Enter the number till which Fibonnaci Series is desired:");
int num;
try {
num = Integer.parseInt(br.readLine());
getFibonnaci(num);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static void getFibonnaci(int fibonnaci)
{
System.out.println("The Fibonnaci Series till "+fibonnaci+" is:-");
System.out.print("0 ");
int f1 =0, f2 = 1,f3;
while(f2 <=fibonnaci){
System.out.print(f2+" ");
f1 = f1 + f2;
f3 = f2;
f2 = f1;
f1 = f3;
}
}
}
I hope the code for generating the Fibonacci series is helpful to you all. If you like the post useful, then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Labels:
Fibonacci Series,
Java Code,
Java Example,
Java Program
Monday, July 20, 2009
Java code for Prime Number, Prime Number Example in Java
Hi friends, welcome back to Java Code Online. Today I am going to give you the Java Code for finding out whether a number is prime or not. A prime number is a number which is divisible by 1 or by itself only. The prime number is divisible by no other number except one and itself. Prime number is of utmost importance in Mathematics, and is used extensively in this field.
The Java code for finding a number is prime or not, is displayed below:-
import java.io.*;
public class primeNumber {
public static void main(String[] args)
{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println("Enter the number you want to test for prime number:");
int num =0;
int check = 0;
try {
num = Integer.parseInt(br.readLine());
for (int i=2; i < num/2; i++ ){
if(num%i == 0)
{
check = 0;
break;
}
else
{
check = 1;
}
}
if(check == 1 || num == 2)
{
System.out.println("The number is prime");
}
else
{
System.out.println("The number is not prime");
}
} catch (NumberFormatException e) {
System.out.println("Kindly enter a proper integer number");
} catch (IOException e) {
System.out.println("There is some error in the input output handling");
}
}
}
I hope that this Java Code for finding a number is prime or not is helpful to you all. For more info on Java,keep buzzing Java Code Online.
The Java code for finding a number is prime or not, is displayed below:-
import java.io.*;
public class primeNumber {
public static void main(String[] args)
{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println("Enter the number you want to test for prime number:");
int num =0;
int check = 0;
try {
num = Integer.parseInt(br.readLine());
for (int i=2; i < num/2; i++ ){
if(num%i == 0)
{
check = 0;
break;
}
else
{
check = 1;
}
}
if(check == 1 || num == 2)
{
System.out.println("The number is prime");
}
else
{
System.out.println("The number is not prime");
}
} catch (NumberFormatException e) {
System.out.println("Kindly enter a proper integer number");
} catch (IOException e) {
System.out.println("There is some error in the input output handling");
}
}
}
I hope that this Java Code for finding a number is prime or not is helpful to you all. For more info on Java,keep buzzing Java Code Online.
Labels:
Java Code,
Java Example,
Java Program,
Prime Number
Tuesday, July 7, 2009
Sun Certified Java Programmer (SCJP)
Hello friends, welcome back to Java Code Online. I am sorry for being absent for such a long time. Actually I had a lot of job load, and was very busy in that. As a Java programmer I have to meet many deadlines one after another.
Meeting deadlines was not the only thing that kept me apart from blogging. The main thing was SCJP 5.0 exam. Actually recentely I gave the SCJP exam, and cleared it easily. I found that preparing for SCJP 5.0 was one of the best way for strenthening your Java roots.
I found the exam to be truly overwhelming. As an overview SCJP stands for Sun Certified Java Programmer. It is a 3 and 1/2 hour exam, though the questions are pretty simple and could be finished much before the given time. Once again I will like to tell you that the level and comfort of exam depends upon your preparation.
It took me around 1 month, to cover the entire syllabus (I had mostly weekends). Regarding the book and course material to study, I strongly recommend Kathy Sierra for SCJP 5.0. Go ahead, read this and you will love Java.
The question are objective in nature and there are also drag and drop type, where you are suppose to drag the right answer to its right place. These questions actually test your Java Coding knowledge.
SCJP 5.0 includes core Java. They just want to test you on the core Java concepts. This includes basic programming, threads, generics, collections, etc. I will advise a strong hold on collections and threads, since they seem to have many questions.
The sections were cool and comfortable, though I have to fight a bit in the sections of Threads and Garbage collection concepts. These topic seem to be simple, but beleive me, garbage collection is linked with JVM, and very difficult questions can be made on it.
I will suggest, that all of you preparing for Java, and wish to have a carrier in Java, should go ahead and give the SCJP exam. My best wishes to all. For more info on Java keep buzzing Java Code Online.
Meeting deadlines was not the only thing that kept me apart from blogging. The main thing was SCJP 5.0 exam. Actually recentely I gave the SCJP exam, and cleared it easily. I found that preparing for SCJP 5.0 was one of the best way for strenthening your Java roots.
I found the exam to be truly overwhelming. As an overview SCJP stands for Sun Certified Java Programmer. It is a 3 and 1/2 hour exam, though the questions are pretty simple and could be finished much before the given time. Once again I will like to tell you that the level and comfort of exam depends upon your preparation.
It took me around 1 month, to cover the entire syllabus (I had mostly weekends). Regarding the book and course material to study, I strongly recommend Kathy Sierra for SCJP 5.0. Go ahead, read this and you will love Java.
The question are objective in nature and there are also drag and drop type, where you are suppose to drag the right answer to its right place. These questions actually test your Java Coding knowledge.
SCJP 5.0 includes core Java. They just want to test you on the core Java concepts. This includes basic programming, threads, generics, collections, etc. I will advise a strong hold on collections and threads, since they seem to have many questions.
The sections were cool and comfortable, though I have to fight a bit in the sections of Threads and Garbage collection concepts. These topic seem to be simple, but beleive me, garbage collection is linked with JVM, and very difficult questions can be made on it.
I will suggest, that all of you preparing for Java, and wish to have a carrier in Java, should go ahead and give the SCJP exam. My best wishes to all. For more info on Java keep buzzing Java Code Online.
Subscribe to:
Posts (Atom)