Calculate with numbers from different number systems












0














This program calculates with numbers from different number systems and outputs the result in the desired number system.



You call it like that:



java Calculator <operator> <number> <base> <otherNumber> <base> (solutionBase)


Unfortunately it does not work with floating point numbers.



Example:



$java Calculator + 5234 7 FABCD43 16 3
200022201200110011


My gut feeling says me that my code is quite ugly but it doesnt tell me how to improve it. Do you have some hints for me how to make this more beautiful?



public class Calculator {
public static void main(String args) {
// display usage if user wants so
if (args[0].contains("help")) {
displayHelp();
return;
}

// parse arguments
String operator = args[0];
String number1 = args[1];
int baseOfNumber1 = Integer.parseInt(args[2]);
String number2 = args[3];
int baseOfNumber2 = Integer.parseInt(args[4]);
int baseOfSolution = 0;
if (args.length == 6) {
baseOfSolution = Integer.parseInt(args[5]);
}

int number1Dec = randomBaseToDecimal(number1, baseOfNumber1);
int number2Dec = randomBaseToDecimal(number2, baseOfNumber2);

// calculate and print out
int solutionDec = 0;
if (operator.equals("+")) {
solutionDec = number1Dec + number2Dec;
} else if (operator.equals("-")) {
solutionDec = number1Dec - number2Dec;
} else if (operator.equals("x")) {
solutionDec = number1Dec * number2Dec;
} else if (operator.equals("/")) {
solutionDec = number1Dec / number2Dec;
}

if (args.length == 6) {
System.out.println(decimalToRandomBase(solutionDec,
baseOfSolution));
} else {
System.out.println(solutionDec);
}
}

private static int randomBaseToDecimal(String number, int base) {
int result = 0;
for (int i = 0; i < number.length(); i++) {
int digit = Character.getNumericValue(number.charAt(i));
result = base * result + digit;
}
return result;
}

// works only till base 16
private static String decimalToRandomBase(int number, int base) {
StringBuilder finalNumber = new StringBuilder();
while (number != 0) {
if ((number % base) > 9) {
switch ((number % base)) {
case 10: finalNumber.append("A"); break;
case 11: finalNumber.append("B"); break;
case 12: finalNumber.append("C"); break;
case 13: finalNumber.append("D"); break;
case 14: finalNumber.append("E"); break;
case 15: finalNumber.append("F"); break;
}
} else {
finalNumber.append(number % base);
}
number = number / base;
}
return new StringBuilder(finalNumber).reverse().toString();
}

private static void displayHelp() {
System.out.println("This program calculates with numbers of different bases");
System.out.println("Example: ");
System.out.println("java Calculator + 34 5 554 6");
System.out.println("You can also specify the base of the output number as the last argument:");
System.out.println("java Calculator + 34 5 554 6 2");
}
}









share|improve this question



























    0














    This program calculates with numbers from different number systems and outputs the result in the desired number system.



    You call it like that:



    java Calculator <operator> <number> <base> <otherNumber> <base> (solutionBase)


    Unfortunately it does not work with floating point numbers.



    Example:



    $java Calculator + 5234 7 FABCD43 16 3
    200022201200110011


    My gut feeling says me that my code is quite ugly but it doesnt tell me how to improve it. Do you have some hints for me how to make this more beautiful?



    public class Calculator {
    public static void main(String args) {
    // display usage if user wants so
    if (args[0].contains("help")) {
    displayHelp();
    return;
    }

    // parse arguments
    String operator = args[0];
    String number1 = args[1];
    int baseOfNumber1 = Integer.parseInt(args[2]);
    String number2 = args[3];
    int baseOfNumber2 = Integer.parseInt(args[4]);
    int baseOfSolution = 0;
    if (args.length == 6) {
    baseOfSolution = Integer.parseInt(args[5]);
    }

    int number1Dec = randomBaseToDecimal(number1, baseOfNumber1);
    int number2Dec = randomBaseToDecimal(number2, baseOfNumber2);

    // calculate and print out
    int solutionDec = 0;
    if (operator.equals("+")) {
    solutionDec = number1Dec + number2Dec;
    } else if (operator.equals("-")) {
    solutionDec = number1Dec - number2Dec;
    } else if (operator.equals("x")) {
    solutionDec = number1Dec * number2Dec;
    } else if (operator.equals("/")) {
    solutionDec = number1Dec / number2Dec;
    }

    if (args.length == 6) {
    System.out.println(decimalToRandomBase(solutionDec,
    baseOfSolution));
    } else {
    System.out.println(solutionDec);
    }
    }

    private static int randomBaseToDecimal(String number, int base) {
    int result = 0;
    for (int i = 0; i < number.length(); i++) {
    int digit = Character.getNumericValue(number.charAt(i));
    result = base * result + digit;
    }
    return result;
    }

    // works only till base 16
    private static String decimalToRandomBase(int number, int base) {
    StringBuilder finalNumber = new StringBuilder();
    while (number != 0) {
    if ((number % base) > 9) {
    switch ((number % base)) {
    case 10: finalNumber.append("A"); break;
    case 11: finalNumber.append("B"); break;
    case 12: finalNumber.append("C"); break;
    case 13: finalNumber.append("D"); break;
    case 14: finalNumber.append("E"); break;
    case 15: finalNumber.append("F"); break;
    }
    } else {
    finalNumber.append(number % base);
    }
    number = number / base;
    }
    return new StringBuilder(finalNumber).reverse().toString();
    }

    private static void displayHelp() {
    System.out.println("This program calculates with numbers of different bases");
    System.out.println("Example: ");
    System.out.println("java Calculator + 34 5 554 6");
    System.out.println("You can also specify the base of the output number as the last argument:");
    System.out.println("java Calculator + 34 5 554 6 2");
    }
    }









    share|improve this question

























      0












      0








      0







      This program calculates with numbers from different number systems and outputs the result in the desired number system.



      You call it like that:



      java Calculator <operator> <number> <base> <otherNumber> <base> (solutionBase)


      Unfortunately it does not work with floating point numbers.



      Example:



      $java Calculator + 5234 7 FABCD43 16 3
      200022201200110011


      My gut feeling says me that my code is quite ugly but it doesnt tell me how to improve it. Do you have some hints for me how to make this more beautiful?



      public class Calculator {
      public static void main(String args) {
      // display usage if user wants so
      if (args[0].contains("help")) {
      displayHelp();
      return;
      }

      // parse arguments
      String operator = args[0];
      String number1 = args[1];
      int baseOfNumber1 = Integer.parseInt(args[2]);
      String number2 = args[3];
      int baseOfNumber2 = Integer.parseInt(args[4]);
      int baseOfSolution = 0;
      if (args.length == 6) {
      baseOfSolution = Integer.parseInt(args[5]);
      }

      int number1Dec = randomBaseToDecimal(number1, baseOfNumber1);
      int number2Dec = randomBaseToDecimal(number2, baseOfNumber2);

      // calculate and print out
      int solutionDec = 0;
      if (operator.equals("+")) {
      solutionDec = number1Dec + number2Dec;
      } else if (operator.equals("-")) {
      solutionDec = number1Dec - number2Dec;
      } else if (operator.equals("x")) {
      solutionDec = number1Dec * number2Dec;
      } else if (operator.equals("/")) {
      solutionDec = number1Dec / number2Dec;
      }

      if (args.length == 6) {
      System.out.println(decimalToRandomBase(solutionDec,
      baseOfSolution));
      } else {
      System.out.println(solutionDec);
      }
      }

      private static int randomBaseToDecimal(String number, int base) {
      int result = 0;
      for (int i = 0; i < number.length(); i++) {
      int digit = Character.getNumericValue(number.charAt(i));
      result = base * result + digit;
      }
      return result;
      }

      // works only till base 16
      private static String decimalToRandomBase(int number, int base) {
      StringBuilder finalNumber = new StringBuilder();
      while (number != 0) {
      if ((number % base) > 9) {
      switch ((number % base)) {
      case 10: finalNumber.append("A"); break;
      case 11: finalNumber.append("B"); break;
      case 12: finalNumber.append("C"); break;
      case 13: finalNumber.append("D"); break;
      case 14: finalNumber.append("E"); break;
      case 15: finalNumber.append("F"); break;
      }
      } else {
      finalNumber.append(number % base);
      }
      number = number / base;
      }
      return new StringBuilder(finalNumber).reverse().toString();
      }

      private static void displayHelp() {
      System.out.println("This program calculates with numbers of different bases");
      System.out.println("Example: ");
      System.out.println("java Calculator + 34 5 554 6");
      System.out.println("You can also specify the base of the output number as the last argument:");
      System.out.println("java Calculator + 34 5 554 6 2");
      }
      }









      share|improve this question













      This program calculates with numbers from different number systems and outputs the result in the desired number system.



      You call it like that:



      java Calculator <operator> <number> <base> <otherNumber> <base> (solutionBase)


      Unfortunately it does not work with floating point numbers.



      Example:



      $java Calculator + 5234 7 FABCD43 16 3
      200022201200110011


      My gut feeling says me that my code is quite ugly but it doesnt tell me how to improve it. Do you have some hints for me how to make this more beautiful?



      public class Calculator {
      public static void main(String args) {
      // display usage if user wants so
      if (args[0].contains("help")) {
      displayHelp();
      return;
      }

      // parse arguments
      String operator = args[0];
      String number1 = args[1];
      int baseOfNumber1 = Integer.parseInt(args[2]);
      String number2 = args[3];
      int baseOfNumber2 = Integer.parseInt(args[4]);
      int baseOfSolution = 0;
      if (args.length == 6) {
      baseOfSolution = Integer.parseInt(args[5]);
      }

      int number1Dec = randomBaseToDecimal(number1, baseOfNumber1);
      int number2Dec = randomBaseToDecimal(number2, baseOfNumber2);

      // calculate and print out
      int solutionDec = 0;
      if (operator.equals("+")) {
      solutionDec = number1Dec + number2Dec;
      } else if (operator.equals("-")) {
      solutionDec = number1Dec - number2Dec;
      } else if (operator.equals("x")) {
      solutionDec = number1Dec * number2Dec;
      } else if (operator.equals("/")) {
      solutionDec = number1Dec / number2Dec;
      }

      if (args.length == 6) {
      System.out.println(decimalToRandomBase(solutionDec,
      baseOfSolution));
      } else {
      System.out.println(solutionDec);
      }
      }

      private static int randomBaseToDecimal(String number, int base) {
      int result = 0;
      for (int i = 0; i < number.length(); i++) {
      int digit = Character.getNumericValue(number.charAt(i));
      result = base * result + digit;
      }
      return result;
      }

      // works only till base 16
      private static String decimalToRandomBase(int number, int base) {
      StringBuilder finalNumber = new StringBuilder();
      while (number != 0) {
      if ((number % base) > 9) {
      switch ((number % base)) {
      case 10: finalNumber.append("A"); break;
      case 11: finalNumber.append("B"); break;
      case 12: finalNumber.append("C"); break;
      case 13: finalNumber.append("D"); break;
      case 14: finalNumber.append("E"); break;
      case 15: finalNumber.append("F"); break;
      }
      } else {
      finalNumber.append(number % base);
      }
      number = number / base;
      }
      return new StringBuilder(finalNumber).reverse().toString();
      }

      private static void displayHelp() {
      System.out.println("This program calculates with numbers of different bases");
      System.out.println("Example: ");
      System.out.println("java Calculator + 34 5 554 6");
      System.out.println("You can also specify the base of the output number as the last argument:");
      System.out.println("java Calculator + 34 5 554 6 2");
      }
      }






      java calculator






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 17 mins ago









      Dexter Thorn

      622825




      622825



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210316%2fcalculate-with-numbers-from-different-number-systems%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210316%2fcalculate-with-numbers-from-different-number-systems%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python