Created
August 17, 2015 02:21
-
-
Save maderaka/c8b617755d061659266a to your computer and use it in GitHub Desktop.
Sort an Integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Company { | |
public static void main(String args[]) { | |
try{ | |
System.out.println(Company.sort(145263)); | |
} catch(NumberFormatException e) { | |
System.out.println("NumberFormatException : " + e.getMessage()); | |
} catch(AsPositiveException e) { | |
System.out.println("AsPositiveException : " + e.getMessage()); | |
} | |
} | |
public static int sort(int random) throws AsPositiveException{ | |
if(int < random) { | |
throw new AsPositiveException(random); | |
} | |
int[] numbers = Company.toArray(random); | |
int length = numbers.length; | |
for(int i = 0; i < length; i++) { | |
for(int j = 0; j < length; j ++) { | |
int a = numbers[i]; | |
int b = numbers[j]; | |
if(a > b){ | |
numbers[i] = b; | |
numbers[j] = a; | |
} | |
} | |
} | |
return Company.toInteger(numbers); | |
} | |
public static int[] toArray(int random) { | |
String argument = Integer.toString(random); | |
int length = argument.length(); | |
int[] numbers = new int[length]; | |
for(int i = 0; i < length; i++){ | |
numbers[i] = Integer.parseInt(argument.substring(i,i+1)); | |
} | |
return numbers; | |
} | |
public static int toInteger(int[] numbers) { | |
String result = ""; | |
for (int i = 0; i < numbers.length; i++) { | |
result += numbers[i]; | |
} | |
return Integer.parseInt(result); | |
} | |
} | |
class AsPositiveException extends Exception{ | |
public AsPositiveException(int random){ | |
super("The number must be in integer positive: " + random); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment