So I've just been experimenting with ASP.NET MVC and I'm really starting to like the way everything has one single purpose (as far as the separation of purpose between the models, views and controllers) but I'm still having some trouble applying my basic knowledge to a somewhat practical purpose.
So, I was thinking of trying to create an ASP.NET MVC version of my original JavaScript polynomial root finder. It should be relatively simple enough. Obviously it's not going to solve for all roots of every single degree (at least at the moment) but I'm at least trying to get Quadratic-Quartic.
So, I have 1 Controller in addition to my HomeController
. This is called CalculateController
and will handle all of the controlling related to any pages dealing with calculating for roots of a certain type of Polynomial.
Therefore, I have 3 Views, 1 for each of the different kind of polynomial, Quadratic.aspx
, Cubic.aspx
and Quartic.aspx
.
Now, I was thinking I would need a Model here to handle my "business logic" in this case, the actual computing and algebraic calculating of the roots.
This is where I need some feedback.
Should I create one Calculate class? That is what I have at the moment. In the class, I declare 5 different instance string variables (I think that's the correct terminology):
public int aValue;
public int bValue;
public int cValue;
public int dValue;
public int eValue;
These will store the values of each of the terms. Now, is this a bad idea, considering that Quadratic and Cubic won't be using all of these instance fields? If so, then I could just create separate classes for each separate polynomial type.
OK, now, I also have to receive some input from my form in order to assign the inputted values into these variables.
I'm a little stumped about how I should do this.
I have a form here on my quadratic view:
<% using(Html.BeginForm("QuadraticForm", "Calculate")) %>
<% { %>
a: <%= Html.TextBox("quadraticAValue") %>
<br />
b: <%= Html.TextBox("quadraticAValue") %>
<br />
c: <%= Html.TextBox("quadraticAValue") %>
<br />
<input type="submit" id="quadraticSubmitButton" />
<% } %>
Very simple, very basic.
Now, how should assign these inputted integers into a new object of the Calculat
e class through a Quadratic()
method?
UPDATE:
OK, I have some more knowledge than before due to some research, so I'll leave the original question in its entirety above, and update below with some of the info I found out:
I'm going to have a separate class in the Models folder for each different type of polynomial. Currently, I'm experimenting with the QuadCalc.cs
file, the Model for the Quadratic Polynomials.
Here is the code I have so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace RootFinder.Models
{
public class QuadCalc
{
public double quadraticAValue { get; set; }
public double quadraticBValue { get; set; }
public double quadraticCValue { get; set; }
public void QuadCalculate(double quadraticdAValue, double quadraticBValue, double quadraticCValue, out double x1, out double x2)
{
//Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a
//Calculate the inside of the square root
double insideSquareRoot = (quadraticBValue * quadraticBValue) - 4 * quadraticdAValue * quadraticCValue;
if (insideSquareRoot < 0)
{
//There is no solution
x1 = double.NaN;
x2 = double.NaN;
}
else
{
//Compute the value of each x
//if there is only one solution, both x's will be the same
double sqrt = Math.Sqrt(insideSquareRoot);
x1 = (-quadraticBValue + sqrt) / (2 * quadraticdAValue);
x2 = (-quadraticBValue - sqrt) / (2 * quadraticAValue);
}
}
}
}
In the code above, I get and set the values of the forms (not really sure how the whole string
to double
thing is gonna work out, but we can worry about that later xD) and then use those as parameters of a method built around the oh so famous quadratic "formula".
Now, in my Calculate
controller, this is my code which applies to the Quadratics:
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Quadratic()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Cubic(QuadCalc boss)
{
return View();
}
Now, in the above code, instead of boss
what should I place here? I cannot leave it just omitted but I believe it should be the name of an object? Problem is, I never specified an object in my Model, should I do that?