tags:

views:

333

answers:

3

I am taking a programming class in C# and here is my first assignment, and already I am in need of help. I think I can handle the problem I just need help getting started.

Create an Average class with a public data member to collect the sum of the integer entries and a public data member to hold the double average of the sum of the 10 integers.

  • Public class method to get each integer number input.
  • Public class method to calculate the average of the numbers input.
  • Public class method to output the average of the numbers input.
  • Main( ) function that instantiates one Average object, and includes a for loop to take 10 inputs and calls to the calculation and output method.

The next chapter covers looping, however, you should be able to build a for-loop to accomplish the 10 inputs.

namespace Assignment1_White
{

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int num1;
        int num2;
        int num3;
        int num4;
        int num5;
        int num6;
        int num7;
        int num8;
        int num9;
        int num10;
        int answer;

        num1 = int.Parse(num100.Text);
        num2 = int.Parse(num200.Text);
        num3 = int.Parse(num300.Text);
        num4 = int.Parse(num400.Text);
        num5 = int.Parse(num500.Text);
        num6 = int.Parse(num600.Text);
        num7 = int.Parse(num700.Text);
        num8 = int.Parse(num800.Text);
        num9 = int.Parse(num900.Text);
        num10 = int.Parse(num1000.Text);



        answer = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 +num10)/10;
        MessageBox.Show(answer.ToString());

   }
}

I got this far but now I am stuck on error checking. Currently it will divide by 10 everytime. I need a way to add an if statement.

+5  A: 

Hi,

  • First, read up on classes. There are many tutorials such as this one.
  • Learn what a method is, and how they are implemented. (example tutorial)
  • Learn how to use ForEach loops (example tutorial)
  • Put it all together :)

Post any difficulties, with code you've written, and you can get specific help for that part. It helps a lot to break down the problem into smaller parts. Reading the entire problem in one go will become confusing, but when you break it down you'll see that it's easier to understand the different aspects.

keyboardP
@keyboardP happy 3000th rep
Nathan Koop
Thanks Nathan! Nice, round figure....not sure if I want to change it now :D
keyboardP
+2  A: 

If what you need is help translating the paragraph into actual requirements (the comments are right- write your own code and then post for help if/when it doesn't work) here's how I would go about translating that:

It says you need a class, so write that down on a piece of paper. Draw a line under it. Then it says it needs a data member (often "requirement-ese" for a field or property, to collect the sum of a seriese of ints. So write that down directly below the line you just drew. Something like 'sumOfInts' or something like that. You can come back to it later. Next you need a data member to hold the double average, so write that down, too: Average. That completes the required data members, so draw a new line. Next, you've had one method defined to calculate the average. So write that down: something like: `CalculateAverage(int sum, int items). I just pointed to a tricky part you'll need to consider: you don't just want the sums, you want to know how many items there are.

Now you should have something like this

    Class: Average
  ------------------
    Sum (Int)
    Average (Double)
  ------------------
    CalculateAverage(Int, Int) (Returns double)

Congratulations, you've just created your first UML Class specificaiton (ish).

With that written down, you should be able to start tackling the actual coding portion of the assigment.

Once you've got code written, if it doesn't work, post a new question listing your specific difficulties, and we'll be happy to assist.

AllenG
private void button1_Click(object sender, EventArgs e) { int num1 = 0; int num2 = 0; int sum; sum = num1 + num2; MessageBox.Show(sum.ToString); }Heres what I got
randywhite30
I get an error MessageBox.Show(sum.ToString);
randywhite30
I'm going to need way more than that. Consider editing your question to add your code so far, or accepting an answer on this question, and creating a new question containing your code. I can tell you right now, though, that to get the messagebox to show, you need parens after ToString (`MessageBox.SHow(sum.ToString());`)
AllenG
+6  A: 

Sometimes an effective approach is to start by working out what your method signatures need to look like (this is commonly achieved using interfaces; I'm guessing you haven't gotten there in your course yet, though).

Here are a couple of examples.

Assignment

Public class method to get each integer number input.

Hint

The description indicates that the method will get input; this typically takes the form of a parameter to the method.

public void CollectInput(double input);

Assignment

Public class method to calculate the average of the numbers input.

Hint

The description mentions calculating a value; such a method will almost certainly return the result of its calculation. Presumably the values involved in the calculation are stored by the Average class internally (the values input by CollectInput above need to go somewhere, after all).

public double CalculateAverage();

Actually, I'm going to stop right there.

Hopefully this answer is enough to get you started and heading in the right direction.

Dan Tao
+1 for pointing out the bad design. Calculating averages and formatted output should not be part of the same class.
Ben Voigt
@Ben Voigt: Indeed -- of course, when dealing with a customer, flawed requirements can be discussed and, depending on the customer, modified appropriately. In an academic environment, on the other hand... I'm doubtful.
Dan Tao
Right, and hopefully the professor explained that everything is being rolled into one class for simplicity's sake, but in a real project there would be different classes responsible for calculation vs output. If he hasn't, we make that point for him. If randywhite comes away from this question with a working program for his assignment, we've failed. If he comes away with a good understanding of how to complete the assignment, we've marginally succeeded. Much better that he comes away with experience applying the software engineering process (spec, design, implement, test/debug/verify).
Ben Voigt