Beginner C# programming problems...*SOLVED

Status
Not open for further replies.

Kalsun...MK

Ars Scholae Palatinae
1,468
Okay, so I'm taking MIS_271 (which is basically intro to C#) this summer at Washington State University and am more than half way through the 6-week super-condensed summer term. I was doing okay at first and have some experience with programming (mostly just VB and HTML) but then about 2 weeks ago my girlfriend's mother passed away. Well more specifically she was in the ICU for nearly 3 months battling an auto immune disease (Good Pasture Syndrome) before they opted to remove her from the ventilator. So as a result I haven't been able to make it to class for two weeks as I've been with my gf trying to console her etc.

I emailed my instructor but apparently she is unwilling to grant me any leeway in allowing me to turn in my assignment late. She basically told me to drop the class or find out some way to make my program work. And I reaaaally can't afford to drop this class, financially or academically, as I'm in line to graduate next summer with my MIS degree but only as long as I get good grades in all of my classes from now till then...

I think my main problem is that the book that we're using--"Head First C#"--eats a big stinky genitalia when it comes to explaining what she wants us to do, or what to do to begin with. Its more of a "do-it" book than a "reference" book and that totally throws me off. I've searched and searched and am having trouble understanding alotta' things that she wants us to do. The C# Station website has given me a fair amount of information but not being able to make it to class for the last two weeks has really put me at a disadvantage. And I'm getting really frustrated. I'm hoping that one of you guys (or gals) could help me out with this assignment because its due tomorrow (Monday June 7) @ 5:59pm pacific time and I basically don't know what the F I'm doing. :scared: The assignment is:

For this assignment you will create an application starting with the following form:

This application will calculate the area of 2 rectangles. The first rectangle will have the are corresponding to the dimensions the user selects in the numericDropDown boxes (width and length). The second rectangle will have the area corresponding to a rectangle that has the double of the width that the user selected (but same length selected by the user).

1. Create a new project and create form according to figure above (or at least with the same controls.
2. Create a new public class which will be responsible to offer a method that calculates the rectangle area.
3. Create a public non-static method in the new class that will require two numeric values passed to it (i.e. the measurements of the width and of the length) and returns the resulting area. Of course, this method will have the calculation statement.
4. Create an event-handler for the calculate button in the form.
5. In the event-handler
o read the 2 values selected by the user into two different variables.
o Create 2 instances of the new class; so, two objects --> one will hold the small area result, the other will hold the large area result.
o The first object should call the new class' method passing to it the values selected by the user (which should now be held in two variables) and assign the result to a variable that will store the small area.
o The second object should call the new class' method passing to it the values selected by the user with the width value, however, doubled, and assign the result to a variable that will store the large area.
o Both results should be displayed back into the specific read-only textboxes


Now I know that alot of this is prolly' wrong but any help would be super appreciated. Like did I do anything right? I just got really stuck when it came to #5, and have no idea how to accomplish the event-handler requirements... :(

And here's what I have for Form1.cs (and I know alot of it is incorrect/incomplete):
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int width;
        int length;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            width = (int)widthNumericUpDown.Value;
            length = (int)LengthNumericUpDown.Value;
            Calculate smallArea = new Calculate();
            Calculate largeArea = new Calculate();
        }
    }
}

And for the "new" class calculate.cs that I created:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class Calculate
    {
        public int calculateArea(int widthNumericUpDown, int lengthNumericUpDown)
        {
            int width = widthNumericUpDown;
            int length = lengthNumericUpDown;

            int result = width * length;
            return result;
        
        }
    }
}
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Weird assignment, it's totally trivial and so the use of classes to do the calculation different is, frankly, bizarre.

note that all the class is asking you to do it output the following:

1) width * height
2) 2 *width * height

And for some reason making the method that calculates this an instance method on a class rather than a static method.

Note that asking for it to be done this way is _utterly_ idiotic. The calculation is stateless and thus could be written like this:

Code:
static class Geometry
{
    public static int CalculateRectangleArea(int width, int height)
    {
        // checked just to make sure it can't give an incorrect response
        // your class may not require this
        return checked (width * height); 
    }
}

This is still over kill for most situations, but does at leat make it clear why you are multiplying the two values when you do it.

I note your pressing concerns so tell you how to do it their (stupid) way.
Since they request that you make this an instance method you have little choice but to do the following:

Code:
class Geometry
{
    public int CalculateRectangleArea(int width, int height)
    {
        // checked just to make sure it can't give an incorrect response
        // your class may not require this
        return checked (width * height); 
    }
}

see what I did there?

Then assuming variables:


Code:
int width;
int height;
// values come from the text boxes which you already have

invocations of it change from:

Code:
int smallArea = Geometry.CalculateRectangleArea(width, height);
int largeArea = Geometry.CalculateRectangleArea(width * 2, height);

to

Code:
int smallArea = new Geometry().CalculateRectangleArea(width, height);
int largeArea = new Geometry().CalculateRectangleArea(width * 2, height);

See how utterly pointless it is.

Code:
int smallArea = new Geometry().CalculateRectangleArea(width, height);
int largeArea = new Geometry().CalculateRectangleArea(width * 2, height);

Nonetheless this is what is required by the question so be it.

Reading "Create 2 instances of the new class; so, two objects --> one will hold the small area result, the other will hold the large area result." seems to imply it should hold the area within the class, this is utterl moronic, and the latter points suggest it is actually unecessary so I ignore that.

I would say that, unless this exercise is then followed by something expanding on this in some way is is _massively_ flawed (and even then it it is still teaching you the wrong thing which is to over complicate your initial design). On that basis you should seriously question the utility of the course.

I would not normally write this much detail as an answer since it pretty much telling you how to do it but in this case I do it to illustrate that you shouldn't actually do it other than to jump through hoops.
 

Kalsun...MK

Ars Scholae Palatinae
1,468
So then
Code:
int smallArea = new Geometry().CalculateRectangleArea(width, height);
int largeArea = new Geometry().CalculateRectangleArea(width * 2, height);
is the code to input within the button_click event in order to create the new instances of the class?

I'm sorry for this lack of basic understanding, but the prof. basically talks from the book and then assigns assignments that have absolutely NOTHING to do with her lecture or her examples...

I mean usually in programming classes (or at least the couple of programming classes that I've taken) the in class assignments/examples had a direct [or at least somewhat indirect] correlation with the assignment that was due, which is apparently not at all how this class is taught...
 

Kalsun...MK

Ars Scholae Palatinae
1,468
or is it the code that allows...

o The first object should call the new class' method passing to it the values selected by the user (which should now be held in two variables) and assign the result to a variable that will store the small area.
o The second object should call the new class' method passing to it the values selected by the user with the width value, however, doubled, and assign the result to a variable that will store the large area.
 

Bhelyer

Well-known member
2,231
Kalsun...MK":2k7vcvqg said:
or is it the code that allows...

o The first object should call the new class' method passing to it the values selected by the user (which should now be held in two variables) and assign the result to a variable that will store the small area.
o The second object should call the new class' method passing to it the values selected by the user with the width value, however, doubled, and assign the result to a variable that will store the large area.

Yes. It creates the objects, then immediately calls upon the new instances for the results. It's crazy, but it's what they've asked you to do.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Code:
private void button1_Click(object sender, EventArgs e)
{
    width = (int)widthNumericUpDown.Value;
    length = (int)LengthNumericUpDown.Value;
    Calculate smallArea = new Calculate();
    Calculate largeArea = new Calculate();
}

Here you have the guts of it, you've done the calculation work once you actually call the calculation function (calculateArea in your code) and assign the result to some something.

You can assign the result to some temporary variables (your prof seems to like this tedious behaviour so I would continue to do this) you must simply then set the two 'result' text boxes to have those values. On the form you should have added two text boxes, lets call them smallAreaTextBox and largeAreaTextBox. you simply assign:

Code:
smallAreaTextBox.Text = smallArea;
largeAreaTextBox.Text = largeArea;

And you're done. Since you are in an event handler for the gui it is entirely legal to just update the values of the text boxes you should hopefully have been taught this cardinal rule.

Edit: for clarity I should add that smallArea and largeArea in the latter code section refer to simple int variables as the result of calling the calculate function, not the Calculate objects themselves as in your original code.

If your prof wasn't a jobsworth this would be entirely sufficient:
Code:
width = (int)widthNumericUpDown.Value;
length = (int)LengthNumericUpDown.Value;
smallAreaTextBox.Text = Geometry.CalculateRectangleArea(width, height);
largeAreaTextBox.Text = Geometry.CalculateRectangleArea(width * 2, height);

You must do the rigmarole of creating (pointless) instantiations of the calculator instances, and based on the way the assignment is worded assign various things to (pointless) temporary variables.

I've got to say I really would be making noises as to the the utility of this section of WSU's course based on this assignment. Even good universities sometimes get/use duff people (I've had to get a tutor replaced, an entire set of practicals rewritten and basically start a separate study group for a new course assigned to a professor out of his comfort zone and I wouldn't say my uni was poor) but you need to politely let the department know or you, and future students, are going to get shafted.
 
Status
Not open for further replies.