Categories

# Class 11 ArrangeNum Program ISC 2020

A class ArrangeNum enables the user to sort any integer number according to the value of 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.
s[]: array to store the digits of the number.
Member functions/methods:
ArrangeNum(int nn): constructor to initialize the data member n = nn.
void fillArray(): 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 fillArray(), void arrange() and void show(). Define the main() function to create an object and call the functions accordingly to enable the task.

``````import java.io.*;
class ArrangeNum{
int n;
int s[];
public ArrangeNum(int nn){
n = nn;
int len = 0;
if(n == 0)
len = 1;
for(int i = n; i > 0; i /= 10)
len++;
s = new int[len];
}
public void fill_array(){
int num = n;
for(int i = 0; i < s.length; i++){
s[i] = num % 10;
num /= 10;
}
}
public void arrange(){
for(int i = 0; i < s.length; i++){
for(int j = 0; j < s.length - 1 - i; j++){
if(s[j] > s[j + 1]){
int temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
public void show(){
System.out.println("Original number: " + n);
System.out.println("Elements of the sorted array:");
for(int i = 0; i < s.length; i++)
System.out.print(s[i]);
System.out.println();
}
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number: ");
int num = Integer.parseInt(br.readLine());
ArrangeNum obj = new ArrangeNum(num);
obj.fill_array();
obj.arrange();
obj.show();
}
}``````

This site uses Akismet to reduce spam. Learn how your comment data is processed.