Scala calculator that accepts three string parameters












-7















How to write a method called calculator that accepts three string parameters:



def calculator(operand1: String, operator: String, operand2: String): Unit


Converts the operands to Int;
Performs the desired mathematical operator (+, -, *, or /) on the two operands
Prints the result, or a generic error messages










share|improve this question




















  • 2





    Welcome to StackOverflow. This site rewards effort. What have you tried so far? Show us code that didn't work so we can better address your understanding of the Scala language.

    – jwvh
    Nov 19 '18 at 7:37
















-7















How to write a method called calculator that accepts three string parameters:



def calculator(operand1: String, operator: String, operand2: String): Unit


Converts the operands to Int;
Performs the desired mathematical operator (+, -, *, or /) on the two operands
Prints the result, or a generic error messages










share|improve this question




















  • 2





    Welcome to StackOverflow. This site rewards effort. What have you tried so far? Show us code that didn't work so we can better address your understanding of the Scala language.

    – jwvh
    Nov 19 '18 at 7:37














-7












-7








-7








How to write a method called calculator that accepts three string parameters:



def calculator(operand1: String, operator: String, operand2: String): Unit


Converts the operands to Int;
Performs the desired mathematical operator (+, -, *, or /) on the two operands
Prints the result, or a generic error messages










share|improve this question
















How to write a method called calculator that accepts three string parameters:



def calculator(operand1: String, operator: String, operand2: String): Unit


Converts the operands to Int;
Performs the desired mathematical operator (+, -, *, or /) on the two operands
Prints the result, or a generic error messages







scala functional-programming






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 17:20









Amit Prasad

575315




575315










asked Nov 19 '18 at 7:20







user10673283















  • 2





    Welcome to StackOverflow. This site rewards effort. What have you tried so far? Show us code that didn't work so we can better address your understanding of the Scala language.

    – jwvh
    Nov 19 '18 at 7:37














  • 2





    Welcome to StackOverflow. This site rewards effort. What have you tried so far? Show us code that didn't work so we can better address your understanding of the Scala language.

    – jwvh
    Nov 19 '18 at 7:37








2




2





Welcome to StackOverflow. This site rewards effort. What have you tried so far? Show us code that didn't work so we can better address your understanding of the Scala language.

– jwvh
Nov 19 '18 at 7:37





Welcome to StackOverflow. This site rewards effort. What have you tried so far? Show us code that didn't work so we can better address your understanding of the Scala language.

– jwvh
Nov 19 '18 at 7:37












1 Answer
1






active

oldest

votes


















1














Your question shows that you put little to no effort into finding the solution yourself.



When asking a question on StackOverflow next time, ask a question about existing code (e.g. "Why am I getting this exception?" or "Why doesn't my code compile?") and don't assume some internet code monkey will magically write your code.





Anyways, as you seem to be a new member of SO, def calculator would look something like this:



import scala.collection.immutable.StringOps._
import scala.util.{Try, Success, Failure}

def calculator(left: String, op: String, right: String): Unit = {

def parse(value: String) = Try(value.toDouble)

(parse(left), parse(right)) match {
case (Success(leftDouble), Success(rightDouble)) => {
op match {
case "/" => println(leftDouble / rightDouble)
case "*" => println(leftDouble * rightDouble)
case "+" => println(leftDouble + rightDouble)
case "-" => println(leftDouble - rightDouble)
case invalid: String => println(s"Invalid operator $invalid.")
}
}
case (Failure(e), _) => println(s"Could not parse $left.")
case(_, Failure(e)) => println(s"Could not parse $right.")
case(Failure(e1), Failure(e2)) => println(s"Could not parse $left and $right.")
}

}


Try it out!



If you need any explanation don't hesitate to drop a comment.



I hope this helps.






share|improve this answer























    Your Answer






    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: "1"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53369976%2fscala-calculator-that-accepts-three-string-parameters%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Your question shows that you put little to no effort into finding the solution yourself.



    When asking a question on StackOverflow next time, ask a question about existing code (e.g. "Why am I getting this exception?" or "Why doesn't my code compile?") and don't assume some internet code monkey will magically write your code.





    Anyways, as you seem to be a new member of SO, def calculator would look something like this:



    import scala.collection.immutable.StringOps._
    import scala.util.{Try, Success, Failure}

    def calculator(left: String, op: String, right: String): Unit = {

    def parse(value: String) = Try(value.toDouble)

    (parse(left), parse(right)) match {
    case (Success(leftDouble), Success(rightDouble)) => {
    op match {
    case "/" => println(leftDouble / rightDouble)
    case "*" => println(leftDouble * rightDouble)
    case "+" => println(leftDouble + rightDouble)
    case "-" => println(leftDouble - rightDouble)
    case invalid: String => println(s"Invalid operator $invalid.")
    }
    }
    case (Failure(e), _) => println(s"Could not parse $left.")
    case(_, Failure(e)) => println(s"Could not parse $right.")
    case(Failure(e1), Failure(e2)) => println(s"Could not parse $left and $right.")
    }

    }


    Try it out!



    If you need any explanation don't hesitate to drop a comment.



    I hope this helps.






    share|improve this answer




























      1














      Your question shows that you put little to no effort into finding the solution yourself.



      When asking a question on StackOverflow next time, ask a question about existing code (e.g. "Why am I getting this exception?" or "Why doesn't my code compile?") and don't assume some internet code monkey will magically write your code.





      Anyways, as you seem to be a new member of SO, def calculator would look something like this:



      import scala.collection.immutable.StringOps._
      import scala.util.{Try, Success, Failure}

      def calculator(left: String, op: String, right: String): Unit = {

      def parse(value: String) = Try(value.toDouble)

      (parse(left), parse(right)) match {
      case (Success(leftDouble), Success(rightDouble)) => {
      op match {
      case "/" => println(leftDouble / rightDouble)
      case "*" => println(leftDouble * rightDouble)
      case "+" => println(leftDouble + rightDouble)
      case "-" => println(leftDouble - rightDouble)
      case invalid: String => println(s"Invalid operator $invalid.")
      }
      }
      case (Failure(e), _) => println(s"Could not parse $left.")
      case(_, Failure(e)) => println(s"Could not parse $right.")
      case(Failure(e1), Failure(e2)) => println(s"Could not parse $left and $right.")
      }

      }


      Try it out!



      If you need any explanation don't hesitate to drop a comment.



      I hope this helps.






      share|improve this answer


























        1












        1








        1







        Your question shows that you put little to no effort into finding the solution yourself.



        When asking a question on StackOverflow next time, ask a question about existing code (e.g. "Why am I getting this exception?" or "Why doesn't my code compile?") and don't assume some internet code monkey will magically write your code.





        Anyways, as you seem to be a new member of SO, def calculator would look something like this:



        import scala.collection.immutable.StringOps._
        import scala.util.{Try, Success, Failure}

        def calculator(left: String, op: String, right: String): Unit = {

        def parse(value: String) = Try(value.toDouble)

        (parse(left), parse(right)) match {
        case (Success(leftDouble), Success(rightDouble)) => {
        op match {
        case "/" => println(leftDouble / rightDouble)
        case "*" => println(leftDouble * rightDouble)
        case "+" => println(leftDouble + rightDouble)
        case "-" => println(leftDouble - rightDouble)
        case invalid: String => println(s"Invalid operator $invalid.")
        }
        }
        case (Failure(e), _) => println(s"Could not parse $left.")
        case(_, Failure(e)) => println(s"Could not parse $right.")
        case(Failure(e1), Failure(e2)) => println(s"Could not parse $left and $right.")
        }

        }


        Try it out!



        If you need any explanation don't hesitate to drop a comment.



        I hope this helps.






        share|improve this answer













        Your question shows that you put little to no effort into finding the solution yourself.



        When asking a question on StackOverflow next time, ask a question about existing code (e.g. "Why am I getting this exception?" or "Why doesn't my code compile?") and don't assume some internet code monkey will magically write your code.





        Anyways, as you seem to be a new member of SO, def calculator would look something like this:



        import scala.collection.immutable.StringOps._
        import scala.util.{Try, Success, Failure}

        def calculator(left: String, op: String, right: String): Unit = {

        def parse(value: String) = Try(value.toDouble)

        (parse(left), parse(right)) match {
        case (Success(leftDouble), Success(rightDouble)) => {
        op match {
        case "/" => println(leftDouble / rightDouble)
        case "*" => println(leftDouble * rightDouble)
        case "+" => println(leftDouble + rightDouble)
        case "-" => println(leftDouble - rightDouble)
        case invalid: String => println(s"Invalid operator $invalid.")
        }
        }
        case (Failure(e), _) => println(s"Could not parse $left.")
        case(_, Failure(e)) => println(s"Could not parse $right.")
        case(Failure(e1), Failure(e2)) => println(s"Could not parse $left and $right.")
        }

        }


        Try it out!



        If you need any explanation don't hesitate to drop a comment.



        I hope this helps.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 12:47









        Markus AppelMarkus Appel

        745220




        745220






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53369976%2fscala-calculator-that-accepts-three-string-parameters%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

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?