Tomislav Bijelic
Tomislav Bijelic

Reputation: 13

How to add to a class a string with two integers and show the sum

Greeting fellow programmers!

I am currently studying software development (I started not a month ago) and I have a problem that needs a solution. Underneath you can find the code and as you can see, the method WriteNumber needs help. I need to write the code in a way that when I run the program, on the console screen the following two lines will be shown: Hello World! 81 The Main method code cannot be changed and also I cannot add more methods to the class Calculator so the code needs to be done only within the WriteNumbers method. I have tried a lot of things but I am still grasping how everything works so any help is welcome! Thank you in advance for your time.

Namespace CalculatorTest
{

class Calculator
{
    public static string WriteText (string input)
    {
        return "" + input;
    }
    public static string WriteNumber()
    {


    }
}

class Program
{
    static void Main(string[] args)
    {
        string s = Calculator.WriteText("Hello World!");
        Console.WriteLine(s);

        string n = Calculator.WriteNumber(53 + 28);
        Console.WriteLine(n);

        Console.Read();
    }
}

Upvotes: 0

Views: 592

Answers (3)

Tomislav Bijelic
Tomislav Bijelic

Reputation: 13

Thank you all for your valuable input but special thanks to David because he showed where I made my error. I forgot that the two numbers in the main function will be summed up FIRST and THEN forwarded to the method in the class Calculator. After that got cleared up, it was easy to understand what to do (basically adjust the type of the input parameter to int).

namespace CalculatorTest
{

class Calculator
{
    public static string WriteText (string input)
    {
        return "" + input;
    }
    public static string WriteNumber(int sumOfNumbers)
    {
        return "" + sumOfNumbers;                 
    }
}

class Program
{
    static void Main(string[] args)
    {
        string s = Calculator.WriteText("Hello World!");
        Console.WriteLine(s);

        string n = Calculator.WriteNumber(53 + 28);
        Console.WriteLine(n);

        Console.Read();
    }
}

}

Upvotes: 0

TrueEddie
TrueEddie

Reputation: 2233

Please read David's answer, it's important that you make the effort to understand why this works the way it does. That being said:

public static string WriteNumber(int number)
{
   return number.ToString();
}

Upvotes: 0

David
David

Reputation: 218847

Not to do your homework for you to just be copied/pasted, hopefully I can give you some hints...

Notice how the method is being invoked:

Calculator.WriteNumber(53 + 28)

The 53 + 28 part happens first, then the result of that operation is passed to the method. That result, naturally, is 81. What's important about that is its type, which is an integer.

So, reasonably, the method signature needs to accept an int as a parameter. This would be done very similarly to how the other method accepts a string as a parameter:

public static string WriteText(string input)

What, then, does that method need to do with that input? Well, it's only a single value, so there aren't any calculations to be performed on it. It would appear that the method simply needs to return the value as a string. (It's your homework so you tell me, is that correct?)

This can be done with exactly two things:

  • Calling .ToString() on the value
  • Using the return keyword to return the result of that operation

(Note: The .ToString() operation does something very intuitive on value types, such as int or double or bool. As you progress into using reference types, you're going to find that it does something very different. Any time you have a custom class on which you want to call .ToString(), you'll need to override the .ToString() method on that class first.)

Upvotes: 2

Related Questions