Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class CoveringBases {
  4.     private String originalNumber;
  5.     private String newNumber;
  6.     private int originalBase = 10;
  7.     private int newBase;
  8.  
  9.     public CoveringBases() {
  10.         this.askUser();
  11.         this.checkValidity();
  12.         System.out.println(convertNumber());
  13.     }
  14.  
  15.     private void askUser() {
  16.         Scanner input = new Scanner(System.in);
  17.         System.out.print("Enter the number base 10 to convert: ");
  18.         originalNumber = input.nextLine();
  19.         System.out.print("Enter the new base: ");
  20.         newBase = input.nextInt();
  21.     }
  22.  
  23.     private void checkValidity() {
  24.         if (newBase < 2 || 16 < newBase) {
  25.             System.out.println("Not an acceptable base.");
  26.             System.exit(0);
  27.         }
  28.     }
  29.  
  30.     private String convertNumber() {
  31.         double value = 0;
  32.         double decDigit = 0;
  33.         char chDigit = 0;
  34.         int length = originalNumber.length();
  35.  
  36.         // loop through each digit of the original number
  37.         for (int i = 0; i < length; i++) {
  38.             // get the digit character (0 - 9, A - Z)
  39.             chDigit = Character.toUpperCase(originalNumber.charAt(length - 1 - i));
  40.  
  41.             // get the decimal value of the character
  42.             if (Character.isLetter(chDigit)) {
  43.                 decDigit = chDigit - 'A' + 10;
  44.             }
  45.  
  46.             else if (Character.isDigit(chDigit)) {
  47.                 decDigit = chDigit - '0';
  48.             }
  49.  
  50.             else {
  51.                 System.out.println("Not a recognized digit.");
  52.                 System.exit(1);
  53.             }
  54.  
  55.             // add this digit's value to the total
  56.             value += decDigit * Math.pow(originalBase, i);
  57.         }
  58.  
  59.         int D = 1;
  60.         for (; Math.pow(newBase, D) <= value; D++) {
  61.         }
  62.         // use char array to hold the new digits
  63.         char[] newNum = new char[D];
  64.  
  65.         double pwr;
  66.         for (int i = D - 1; i >= 0; i--) {
  67.             // calculate the digit for this power of newBase
  68.             pwr = Math.pow(newBase, i);
  69.             decDigit = Math.floor(value / pwr);
  70.             value -= decDigit * pwr;
  71.  
  72.             // store the digit character
  73.             if (decDigit <= 9) {
  74.                 newNum[D - 1 - i] = (char) ('0' + (int) decDigit);
  75.             }
  76.  
  77.             else {
  78.                 newNum[D - 1 - i] = (char) ('A' + (int) (decDigit - 10));
  79.             }
  80.         }
  81.  
  82.         return new String(newNum);
  83.     }
  84.  
  85.     public static void main(String[] args) {
  86.         new CoveringBases();
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement