Write a Java Program to swap digits from a given integer number. Use Constructor with parameter. Use necessary method and instance, if required.
Reference:
1.
Course Book(The Complete Reference Java j2se) ; Page 119
2.
http://c-programming-sourcecode.blogspot.com/2014/12/c-program-to-swap-numbers.html?updated-min=2014-12-01T00:00:00-08:00&updated-max=2015-01-01T00:00:00-08:00&max-results=41
Source Code:
package Swap;
import java.util.Scanner;
class Swapnum
{
int number;
Swapnum(int digit)
{
number=digit;
}
int get_Swap(){
int swa = 0;
int remainder = 0;
do{
remainder = number%10;
swa = swa*10 + remainder;
number = number/10;
}while(number > 0);
return swa;
}
}
public class Swap {
public static void main(String args[]) {
Scanner input =new Scanner(System.in);
int number;
System.out.println("Please enter number to be Swap using Parameterized Constructor: ");
number = input.nextInt();
Swapnum r =new Swapnum(number);
System.out.println("After Swap : "+r.get_Swap());
}
}
Reference:
1.
Course Book(The Complete Reference Java j2se) ; Page 119
2.
http://c-programming-sourcecode.blogspot.com/2014/12/c-program-to-swap-numbers.html?updated-min=2014-12-01T00:00:00-08:00&updated-max=2015-01-01T00:00:00-08:00&max-results=41
Source Code:
package Swap;
import java.util.Scanner;
class Swapnum
{
int number;
Swapnum(int digit)
{
number=digit;
}
int get_Swap(){
int swa = 0;
int remainder = 0;
do{
remainder = number%10;
swa = swa*10 + remainder;
number = number/10;
}while(number > 0);
return swa;
}
}
public class Swap {
public static void main(String args[]) {
Scanner input =new Scanner(System.in);
int number;
System.out.println("Please enter number to be Swap using Parameterized Constructor: ");
number = input.nextInt();
Swapnum r =new Swapnum(number);
System.out.println("After Swap : "+r.get_Swap());
}
}