Scala calculator that accepts three string parameters
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
add a comment |
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
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
add a comment |
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
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
scala functional-programming
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 19 '18 at 12:47
Markus AppelMarkus Appel
745220
745220
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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