Method Overloading: Operating on basic data types.
Description:
Explanation
Method overloading is useful when accepting multiple different data types. Many objects have multiple different methods with the same name: the input that the method receives determines which method is used.
Method overloading allows the same named method to output different results depending on the parameters. Alternatively, returning generic Objects works fine too, so long as the Objects returned are the correct types. In strongly-typed languages, like Java and C#, overloading is more important than it is in languages like Python and Ruby. Overloading is similar to overriding, except that it occurs inside the class itself, as opposed to taking a method from a parent class and redefining it.
Problem
In this problem, you are given a helper class by the name of Operate. You need to define four methods, Add, Sub, Mult, and Div. Each method will have a different output based on the input given. There also will be a base method that will be called if the inputs do not fall into the allowed categories that should output "Invalid Types."
Addition
// This is what addition should look like
Ops.Operate.Add(10, 10) == 20; // Integers
Ops.Operate.Add(2.5, 3.3) == 5.8; // Doubles
Ops.Operate.Add("C", "#") == "C#"; // Strings
Ops.Operate.Add('a', 3) == 'd'; // Char
Ops.Operate.Add(true, false) == true; // Bool is an OR statement for add
Any other parameter combinations should output "Invalid Types"
Subtraction
// This is what subtraction should look like
Ops.Operate.Sub(5, 3) == 2; // Integers
Ops.Operate.Sub(6.4, 2.3) == 4.1; // Doubles
Ops.Operate.Sub('d', 2) == 'b'; // Char
Ops.Operate.Sub("Hello World!", "orl") == "He Wd!"; // String
Ops.Operate.Sub(true, false) == false; // Bool is a NOR statement for subtract
Multiplication
// This is what multiplication should look like
Ops.Operate.Mult(5, 3) == 15; // Integers
Ops.Operate.Mult(1.2, 1.2) == 1.44; // Doubles
Ops.Operate.Mult("Hello", 3) == "HelloHelloHello"; // Strings
Ops.Operate.Mult(true, false) == false // Bool is an AND statement for multiplication
Division
// This is what division should look like
Ops.Operate.Div(7, 5) == 1; // Integers
Ops.Operate.Div(9.0, 5.0) == 1.8; // Doubles
Ops.Operate.Div(true, false) == true; // Bool is an XOR statement for division
The only data types that you need to worry about are ints, chars, strings, bools, and doubles (in C#). All other data types do not matter in this problem, and should return "Invalid Types."
Similar Kata:
Stats:
Created | Feb 23, 2021 |
Published | Mar 10, 2021 |
Warriors Trained | 72 |
Total Skips | 5 |
Total Code Submissions | 163 |
Total Times Completed | 15 |
C# Completions | 15 |
Total Stars | 4 |
% of votes with a positive feedback rating | 30% of 5 |
Total "Very Satisfied" Votes | 0 |
Total "Somewhat Satisfied" Votes | 3 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 5 |
Average Assessed Rank | 7 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |