A class ArrangeNum enables the user to sort any integer number according to the value of [10]
its digits.
Example: Input: 59672
Output: 25679 after sorting the digits in ascending order of their values.
Some of the members of the class are given below.
Class name
: ArrangeNum
Data members/instance variables:
n
: to store the integer number
sa
: array to store the digits of the number
Member functions/methods:
ArrangeNum( int nn) : constructor to initialize the data member n=nn
void fill_array()
: extracts the digits of the number and stores it in
the array
void arrange()
· sorts the array elements in ascending order using
any standard sorting technique
void show
• displays the original number along with the
elements of the sorted array with an appropriate
message
Define the class ArrangeNum giving details of the constructor(int), void fill_array(),
void arrange() and void show(). Define the main() function to create an object and call
the functions accordingly to enable the task.
Answers
Answer:
import java.util.*;
public class ArrangeNum
{
int arr[] = new int[10];
int num, i, j, t, p = 0;
void ArrangeNum(int n)
{
num = n;
}
void extract_digit()
{
while(num > 0)
{
arr[p] = num % 10;
num /= 10;
p++;
}
}
void sort()
{
for(i = 0; i < p; i++)
{
for(j = 0; j < (p-i); j++)
{
if(arr[j] < arr[j+1])
{
t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
}
void display()
{
System.out.println("Digits sorted in ascending order : ");
for(i = 0; i < p; i++)
{
System.out.print(arr[i]+" ");
}
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a number : ");
int n1 = in.nextInt();
ArrangeNum obj = new ArrangeNum();
obj.NumSort(n1);
obj.extract_digit();
obj.sort();
obj.display();
}
}
Explanation:
There you go!