Search code examples
c#methodsvoid

How to create a void method that outputs an integer and have the method divide the data passed to it by 2?


I'm really brand new at this and still learning C#. I've had a hard time trying to look for a good example to the assignment I'm working on.

So you can see exactly what I've been asked, here is the verbatim text of my assignment:

Perform these actions and create a console app that includes the following:

  1. Create a class. In that class, create a void method that outputs an integer. Have the method divide the data passed to it by 2.
  2. In the Main() method, instantiate that class.
  3. Have the user enter a number. Call the method on that number. Display the output to the screen. It should be the entered number, divided by two.
  4. Create a method with output parameters.
  5. Overload a method.
  6. Declare a class to be static.

So far I created a class. In the Main() method, instantiate that class. I apologize in advance if the answer is on here. Point me to the direction if it is. Thank you.

in my main() program:

class Program
{
    static void Main(string[] args)
    {
        MathMethod mathMethod = new MathMethod(); //Instantiate

    }
}

my MathMethod.cs:

class MathMethod
{
    public void Operator()
    {
        int num1 = 6;
        int num2 = 9;
    }
    public void Output(int number1, int number2)
    {
        int value = 
    }

Solution

  • I'm not going to do your assignment but I'm happy to provide pointers for you to assemble into a solution for your assignment

    The assignment text contains some confusing phrasing. I'd say "call the method on that number" should read "call the method, passing that number" - calling a method "on" is something different, and somewhat implies that you're expected to write an extension method (which I can't quite believe is a requirement for an assignment of this level)

    It also asks things of you that I'm not sure I'd ask of a new learner but hey ho, we are where we are

    For the most part I'd say that your particular problem here has been answered by Piotr; their answer adds rather than your requirement to divide but I'm sure you can work that one out. Also, their answer takes both operands to the operation but your assignment asks you to create a method that takes just one number, and halves it so your method won't have 3 parameters like Piotr's does, it will have 2

    In the Main() method, instantiate that class.

    This is done

    Have the user enter a number.

    Use a Console.WriteLine to prompt the user to do something, then use a Console.ReadLine to read their typing into a string variable

    You'll also need to convert the string to an int; take a look at int.Parse which is easy to use.

    You can look at upgrading to int.TryParse later, which is a more robust option that doesn't crash if the user enters garbage that can't be interpreted as a number. Amusingly/interestingly, TryParse works in the same way as what your assignment is asking you; it uses an out parameter to make the parsed number available. The reason why it uses an out is because it also returns a boolean indicating success or not so you can act accordingly; this is a more valid use case for out than what your assignment asks you to consider because TryParse has a genuine need to return two things (the bool of whether it succeeded and the value it parsed if it did succeed). Your requirement is more artificial and uses an out for the sake of academic point; please don't take away from it that out is a good thing to be used often. It (and it's sibling ref) are terrible things that we try to minimize usage of wherever possible.

    Once this assignment is over, strive to avoid ever having to use out again until you're fully aware of the implications; once newbies start to think "this is how I return multiple things from a method" they start putting out everywhere. No; the "a method can only return one thing" is perfectly possible to work with, you just make the one thing a method can return an object that holds the multiple things you want to return and then return one of that object. This is Object Oreinted programming after all!

    I use out so seldom I don't recall a particular use case in the last 10 years of coding I've done, other than academically contrived examples on SO, also imploring people not to use it

    Call the method on that number.

    It means call the method, passing the parsed number as the argument

    Display the output to the screen.

    Piotr's demonstrated this

    Create a method with output parameters.

    This seems like a tip for how you should achieve step 2; you'll have already done this by the time you get here

    Overload a method.

    Overloading is providing two or more methods in the same class, that are named the same. The compiler chooses which one to use based on some difference it finds in the argument

    This is an example of set of overloads:

    void Halve(int number, out int result)
    
    void Halve(int number, int howManyTimes, out int result)
    
    void Halve(double number, out double result)
    

    Overloads can differ by number of arguments, type of arguments, or both. Incidentally, the first two above are what I would perhaps recommend for your assignment; provide a method Halve that divides by 2 just once, and then provide another method that takes an additional number that is how many times to do the divide.

    For extra style points you can have the Halve that divides just once call the method that divides N times, passing 1 for the N argument

    Declare a class to be static.

    Oof, I do wish educators wouldn't promote static. It's such a pain in the ass because it undoes all the good work we're doing trying to get you to think in terms of multiple instances of the same type of object. It also makes me wonder more about the earlier mentioned extension method

    I'd "bypass" this one by making the Program class that holds your Main method as static