Write a program to accept three command line arguments from the user and to display it in sorted order.import java.util.Arrays;class sorting{ public static void main(String args[]) { if (args.length>0) { Arrays.sort(args); System.out.println(""); for(int i=0;iargs.length;i++) { System.out.println(args[i]); } } }}/args.length;i...
Tuesday, July 3, 2012
find the Factorial of a number using Recursion
Write a program to find the Factorial of a number using Recursion. Factorial can be defined as Factorial(n) = 1 * 2 * 3 ….* (n-1) * n.class rfactorial{ public static void main(String args[]) { int num=Integer.parseInt(args[0]); int fact; recursion r1=new recursion(); fact=r1.rec(num); System.out.println("The factorial for "+num+" is = "+fact); }}class recursion{ public int rec(int a) { int f; if(a==1 || a==0) { return(1); } else { f=a*rec(a-1);...
a program which accept amount in dollars and convert it to the rupees.
Write a program, which accept amount in dollars, and convert it to the rupees.class rupeeDollar{ public static void main(String args[]) { double rup,dol; dol=Double.parseDouble(args[0]); if(dol>0) { rup=dol*45.38; System.out.println("$"+dol+" is equal to "+rup+" INR"); } ...
find Prime numbers program in java
Write a program that prints prime numbers between 1 to n. Number n should be acepted as command line input.class PrimeNumber { public static void main (String args[]) { int num=Integer.parseInt(args[0]); int i,j; for(i=1;inum;i++) { for(j=2;ji;j++) { int n=i%j; if(n==0) {break;} } if(i==j) { System.out.print(" " + i); } } }}/i;j++)/num;i...
simple interest program in java
Write a Java program that calculates and prints the simple interest using the formula Simple Interest = PNR / 100Input values P,N,R should be accepted as command line input as below.e.g. java SimpleInterest 5 10 15class one{ public static void main(String args[]) { double p,n,r,SI; p=Double.parseDouble(args[0]); n=Double.parseDouble(args[1]); r=Double.parseDouble(args[2]); SI=(p*n*r)/100; System.out.println("The simple interese is: "+SI);...
program that detects successive repeated occurrence of a letter in a word in java
Write a program that detects successive repeated occurrence of a letter in a word. For example, in the word “explanation” letter ‘a’ and ‘n’ occurs twice.class occurance{ public static void main(String args[]) { String a; String[] Arr; Arr = new String[20]; int f=0,len; len=args[0].length(); String str=args[0]; System.out.println(str); for(int i=0;ilen;i++) { Arr[i]=str.substring(i,i+1); System.out.println(Arr[i]); } }}/len;i...
Find Odd and Even Numbers in java
Write a program to accept a number from the user. Use condition checking statement and display whether the number is odd or even.class oddEven{ public static void main(String args[]) { int num=Integer.parseInt(args[0]); if(num%2==0) { System.out.println("The number "+num+" is EVEN"); } else { System.out.println("The number "+num+" is ODD"); }...
Example on inheritance in java
Write a class vehicle .Define suitable attributes and methods. Write subclasses of Vehicle like Car, Bicycle, Scooter. Assume suitable required attributes. Write constructor for each and define a method maxSpeed() in each class which prints the maximum speed of the vehicle. (use of super keyword is expected in the constructor of inherited classes)class vehicle{ vehicle(String c) { System.out.println(" Color is " +c+" "); }}class bicycle extends vehicle{...
How to find length of a number
Write a program that calculates the length(i.e. number of characters) in the input string.class length{ public static void main(String args[]) { String val=args[0]; System.out.println("The length of "+val+" is : "+val.length());...
interChange program in java
Write a program to accept a String and interchange the first character with the last character.class interChange{ public static void main(String args[]) { String val=args[0]; String f,l,newrep; int len; len=val.length(); f=val.substring(0,1); l=val.substring(len-1,len); System.out.println(val); newrep=l+val.substring(1,len-1)+f; System.out.println(newrep);...
Sum of digit, Length of number, average and reverse of digit program in java
To find Sum of digit, Length of number, average of digit and reverse of digit program in javaclass digits{ public static void main(String args[]) { int dig=Integer.parseInt(args[0]); int sum=0,rev=0; float avg; int len=args[0].length(); while(dig>0) { int r=dig%10; sum=sum+r; dig=dig/10; rev=rev*10; rev=rev+r; } avg=(float)sum/len; System.out.println("The sum of digits is: "+sum); System.out.println("The length of number is: "+len);...
fibonacci program in java
The numbers in the following sequence are called the fibonacci numbers .0 , 1 , 1 , 2, 3 , 5 , 8 , 13 , …………..class fibonacci{ public static void main(String args[]) { int n=Integer.parseInt(args[0]); int f=0,s=1; int cur; System.out.print(f+" "+s+" "); do{ cur=f+s; f=s; s=cur; System.out.print(cur+" "); }while(curn); }}/...
Monday, July 2, 2012
find if the given number is palindrome number or not.
/* Java Palindrome Number Example This Java Palindrome Number Example shows how to find if the given number is palindrome number or not.*/ public class JavaPalindromeNumberExample { public static void main(String[] args) { //array of numbers to be checked int numbers[] = new int[]{121,13,34,11,22,54}; //iterate through the numbers for(int i=0; i < numbers.length; i++){ int number = numbers[i]; int reversedNumber = 0; int temp=0; /* ...
Friday, June 29, 2012
JDBC Connection code to retrieve data from database
Normal 0 false false false EN-US X-NONE X-NONE ...
Sorting Algorithm in java
This program will sort the given number in array.import java.util.*;import java.lang.*;public class sort{public static void main(String args[]){int a[]={2,3,1,5,4};for(int i=0;i<5;i++){ for(int j=i;j<5;j++) { if(a[i]>a[j]) { int temp; temp=a[i]; a[i]=a[j]; a[j]=temp; } }System.out.print(" " + a[i]);...
Wednesday, June 20, 2012
Core Java interview questions top 100 - part 5
----------------------------------------------------CORE JAVA----------------------------------------------------81. What is the range of the char type?The range of the char type is 0 to 216 - 1 (i.e. 0 to 65535.)82. What is the range of the short type?The range of the short type is -(215) to 215 - 1. (i.e. -32,768 to 32,767)83. Why isn't there operator overloading?Because C++ has proven by example that operator overloading makes code almost impossible to...
Core Java interview questions top 100 - part 4
----------------------------------------------------CORE JAVA----------------------------------------------------61. What value does read() return when it has reached the end of a file?The read() method returns -1 when it has reached the end of a file.62. Can a Byte object be cast to a double value?No, an object cannot be cast to a primitive value.63. What is the difference between a static and a non-static inner class?A non-static inner class may have object...
Core Java interview questions top 100 - part 3
----------------------------------------------------CORE JAVA----------------------------------------------------41. What is use of a abstract variable?Variables can't be declared as abstract. only classes and methods can be declared as abstract.42. Can you create an object of an abstract class?Not possible. Abstract classes can't be instantiated.43. Can a abstract class be defined without any abstract methods?Yes it's possible. This is basically to avoid...
Core Java interview questions top 100 - part 2
----------------------------------------------------CORE JAVA----------------------------------------------------21. Does the order of public and static declaration matter in main() method?No. It doesn't matter but void should always come before main().22. Can a source file contain more than one class declaration?Yes a single source file can contain any number of Class declarations but only one of the class can be declared as public.23. What is a package?Package...
Java interview questions top 100 - part 1
----------------------------------------------------CORE JAVA----------------------------------------------------1. What is the most important feature of Java?Java is a platform independent language.2. What do you mean by platform independence?Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).3. What is a JVM?JVM is Java Virtual...
Subscribe to:
Posts (Atom)