Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set up three methods in C# that takes a user input and does math on the input?

This is a beginner's assignment, so I understand people not wanting to just give me the answer. But if possible, I'd appreciate being pointed in the right direction, as I'm very stuck on this prompt:

In a console app project, in a class file, I need to create an object called "Operator." It has to have three methods linked to it, one each for addition, subtraction and division. The program file must ask the user to type an integer, which is then passed through each method and then returned as a result to the console. They didn't specify what to add/subtract/divide by, so I'm going to use Method 1 to add "4" to user input, Method 2 to subtract "3",and last method to divide by "1".

The object is supposed to have its own cs file, and the methods are supposed to be created and called within the program file. I know there's a way to put both class and method in the same file, but for this assignment they want them separate.

I assumed the operator object shouldn't have any properties, since it doesn't have characteristics that I can think of; it's just an abstract object that does things with numbers. So I set up a really simple operator object with nothing in it but the class name:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace threemethods
{
    public class Operator
    {

    }
}

I realize this is likely wrong so far, but I don't know what else needs to be in the class file since the methods will all be in the program file. Now, where I'm really struggling is the program. I don't know where to put everything because none of examples I've seen involve user input, and most are not done externally. I know Console.WriteLine("Pick a number") will probably go in the main method, and addition/subtraction/division will be separate methods below the main one. But other than this, I have no idea the template that this should follow. I'm also unsure how to "pass" the result from one method to the next until all methods have been used. Again, I'm not expecting it to be done for me, but I really need guidance on how to approach this in order to get me going. Even a general outline of where things should go would really help. My program code is so much of a mess right now that I can't share it, as it makes no sense.

Thank you so much!

To Amir:

Thank you so much for your help! Could you explain more on how to format the Console code you've written in order to call the methods? The reason I'm confused is that we are taught to use this format for the program.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace threemethods
{
    class Program
    {
     static void Main(int[] args)

And then the main method underneath that. So, I'm not sure how to correctly integrate your console code when the format the school gives me is a little different. If I place your console code within a program file, can you tell me what code I should add to yours in order to make the program work? For example, what do I write at the top of the file before the Console code?

Thank you so much!! Sorry if what I said is confusing!

like image 865
FreddieMercury Avatar asked Jan 28 '23 09:01

FreddieMercury


1 Answers

Update:

This is cs file about Console:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        public int Data { get; set; }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter input:");
            string line = Console.ReadLine();

            var operatorObject = new Operator(); //You have to add reference, If is not.
            var result = operatorObject.GetAdd(data);

            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

Now in this Operator.CS file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace threemethods
{
    public class Operator
    {
        public double GetAdd(int data)
        {
             data = data + 4;
             return GetSubtract(data);
        }    

        private  double GetSubtract(double data)
        {
             data = data - 3;
             return GetDivide(data);
        }

        private  double GetDivide(double data)
        {
             return data / 3;
        }
    }
}

For do this issue, you have to set private 2 last method, and work with one of them like my sample.

With this sample, you can call just one of the methods and develop so easy.

If you want I can create more sample for you.

like image 156
AmirReza-Farahlagha Avatar answered Feb 08 '23 17:02

AmirReza-Farahlagha