Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 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.         this.printAll();
  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 void printAll() {
  31.         System.out.print("The number " + originalNumber + " [base 10] = ");
  32.         System.out.print(convertNumber() + " [base " + newBase + "]");
  33.     }
  34.  
  35.     private String convertNumber() {
  36.         double value = 0;
  37.         double decDigit = 0;
  38.         char chDigit = 0;
  39.         int length = originalNumber.length();
  40.  
  41.         // loop through each digit of the original number
  42.         for (int power = 0; power < length; power++) {
  43.             // get the digit character (0 - 9, A - Z)
  44.             chDigit = Character.toUpperCase(originalNumber.charAt(length - 1 - power));
  45.  
  46.             // get the decimal value of the character
  47.             if (Character.isLetter(chDigit)) {
  48.                 decDigit = chDigit - 'A' + 10;
  49.             }
  50.  
  51.             else if (Character.isDigit(chDigit)) {
  52.                 decDigit = chDigit - '0';
  53.             }
  54.  
  55.             else {
  56.                 System.out.println("Not a recognized digit.");
  57.                 System.exit(1);
  58.             }
  59.  
  60.             // add this digit's value to the total
  61.             value += decDigit * Math.pow(originalBase, power);
  62.         }
  63.  
  64.         int D = 1;
  65.         for (; Math.pow(newBase, D) <= value; D++) {
  66.         }
  67.         // use char array to hold the new digits
  68.         char[] newNum = new char[D];
  69.  
  70.         double pwr;
  71.         for (int i = D - 1; i >= 0; i--) {
  72.             // calculate the digit for this power of newBase
  73.             pwr = Math.pow(newBase, i);
  74.             decDigit = Math.floor(value / pwr);
  75.             value -= decDigit * pwr;
  76.  
  77.             // store the digit character
  78.             if (decDigit <= 9) {
  79.                 newNum[D - 1 - i] = (char) ('0' + (int) decDigit);
  80.             }
  81.  
  82.             else {
  83.                 newNum[D - 1 - i] = (char) ('A' + (int) (decDigit - 10));
  84.             }
  85.         }
  86.  
  87.         return new String(newNum);
  88.     }
  89.  
  90.     public static void main(String[] args) {
  91.         new CoveringBases();
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement