Factorizing Quadratic Equations (practice and solver)

 

Overview

 

There are several different methods for solving Quadratic equations. This application focuses on solving by Factorizing.

The application is primarily a revision aid, being comprised of a practice app. and a solver app.


**Figure 1. Manually solving practice questions.
**


**Figure 2. Solving programmatically.
**

The program consists of one Form and two UserControls:

The bracket UC is a dual purpose control...:

...and  is shown in Figure 1, where it is used for input, and in Figure 2 (the lower control) in readonly mode as an output control.

The equation UC...:

 ...is an input control which can be seen in Figure 2 (the top control).

The code – The bracket UC

 

Both the bracket control (in input mode) and the equation control are restricted in the input they will accept. I haven’t included the input restricting code here.

The code includes a Boolean [readonly] Property and four String Properties for retrieving and setting text in the Labels and TextBoxes.  The Paint event is handled to draw highlighting lines below the TextBoxes.

This UC also has two Public methods,  alignControls which moves the controls to accommodate different text, and reset which resets the control’s text back to the default values.

 

 

Public Class bracket 

    Private _readonly As Boolean = False
    Public Property [readonly]() As Boolean
        Get
            Return _readonly
        End Get
        Set(ByVal value As Boolean)
            _readonly = value
            TextBox1.ReadOnly = value
            TextBox2.ReadOnly = value
        End Set
    End Property 
 

    Public Property a1() As String
        Get
            Return Label1.Text
        End Get
        Set(ByVal value As String)
            Label1.Text = value
        End Set
    End Property  

    Public Property a2() As String
        Get
            Return Label4.Text
        End Get
        Set(ByVal value As String)
            Label4.Text = value
        End Set
    End Property  

    Public Property i1() As String
        Get
            Return TextBox1.Text
        End Get
        Set(ByVal value As String)
            TextBox1.Text = value
        End Set
    End Property  

    Public Property i2() As String
        Get
            Return TextBox2.Text
        End Get
        Set(ByVal value As String)
            TextBox2.Text = value
        End Set
    End Property 

    Private Sub bracket_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawLine(Pens.Black, TextBox1.Left - 1, TextBox1.Bottom - 4, TextBox1.Left - 1, TextBox1.Bottom + 1)
        e.Graphics.DrawLine(Pens.Black, TextBox1.Left - 1, TextBox1.Bottom + 1, TextBox1.Right + 1, TextBox1.Bottom + 1)
        e.Graphics.DrawLine(Pens.Black, TextBox1.Right + 1, TextBox1.Bottom - 4, TextBox1.Right + 1, TextBox1.Bottom + 1)
        e.Graphics.DrawLine(Pens.Black, TextBox2.Left - 1, TextBox2.Bottom - 4, TextBox2.Left - 1, TextBox2.Bottom + 1)
        e.Graphics.DrawLine(Pens.Black, TextBox2.Left - 1, TextBox2.Bottom + 1, TextBox2.Right + 1, TextBox2.Bottom + 1)
        e.Graphics.DrawLine(Pens.Black, TextBox2.Right + 1, TextBox2.Bottom - 4, TextBox2.Right + 1, TextBox2.Bottom + 1)
    End Sub 

    Public Sub alignControls()
        TextBox1.Left = Label1.Right + 6
        Label2.Left = TextBox1.Right + 6
        Label4.Left = Label2.Right - 2
        TextBox2.Left = Label4.Right + 6
        Label3.Left = TextBox2.Right + 6
        Me.Invalidate()
    End Sub 

    Public Sub reset()
        Label1.Text = "(x"
        Label4.Text = "(x"
        TextBox1.Clear()
        TextBox2.Clear()
    End Sub  
  

 

The code – The equation UC

 

This UC has three String Properties, which expose the TextBoxes’ Text Properties. It also contains a Public reset method which resets the input control’s text back to the default values.

  

Public Class equation 

    Public Sub reset()
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
    End Sub 

    Public Property a() As String
        Get
            Return TextBox1.Text
        End Get
        Set(ByVal value As String)
            TextBox1.Text = value
        End Set
    End Property 

    Public Property b() As String
        Get
            Return TextBox2.Text
        End Get
        Set(ByVal value As String)
            TextBox2.Text = value
        End Set
    End Property 

    Public Property c() As String
        Get
            Return TextBox3.Text
        End Get
        Set(ByVal value As String)
            TextBox3.Text = value
        End Set
    End Property 

End Class

 

 

The code – The Form

 

The Form code is organized into five distinct regions:

Ø  common variables

 

These are Form level and used by the practice part of the application.

 

      Dim a As Integer

      Dim b As Integer

      Dim c As Integer

 

      Dim r As New Random 

      Dim i1 As Integer

      Dim i2 As Integer

  

      Dim solutions As String = "" 

Ø  practise

 

The practice part of the application formulates random equations, and checks your answers.

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

      Label4.Text = "" 

            Dim a1 As Integer

            Dim a2 As Integer

            a1 = r.Next(-9, 13)

            a2 = r.Next(-9, 13)

            a = a1 * a2

            While a = 0

                  a1 = r.Next(-9, 13)

                  a2 = r.Next(-9, 13)

                  a = a1 * a2

            End While 

            Do

                  i1 = r.Next(-9, 13)

                  i2 = r.Next(-9, 13)

                  b = a1 * i2 + i1 * a2

                  c = i1 * i2

            Loop While b = 0 Or c = 0 

            '(3x - 2)(x + 3)

      Label3.Text = String.Format("Solve:{0}{1}x² {2}x {3} = 0", Environment.NewLine, a, If(b > 0, "+" & b.ToString, b.ToString), If(c > 0, "+" & c.ToString, c.ToString))

            Bracket1.a1 = String.Format("({0}x", If(a1 = 1, "", a1.ToString))

            Bracket1.a2 = String.Format("({0}x", If(a2 = 1, "", a2.ToString))

            Bracket1.i1 = ""

            Bracket1.i2 = ""

            Bracket1.alignControls() 

            solutions = String.Format("x = {0} or x = {1}", simplify(i1, a1), simplify(i2, a2)) 

      End Sub

 

      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Dim int1, int2 As Integer

            If Integer.TryParse(Bracket1.i1.TrimStart("+"c), int1) AndAlso Integer.TryParse(Bracket1.i2.TrimStart("+"c), int2) Then

                  If i1 = int1 AndAlso i2 = int2 Then

                        MsgBox("Correct answer", MsgBoxStyle.Information)

                        Label4.Text = solutions

                  Else

                        MsgBox("Try again", MsgBoxStyle.Exclamation)

                  End If

            End If

      End Sub

 

Ø  solve

The solve part of the application allows you to specify a Quadratic Equation and programmatically solve that equation.

 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

            Bracket2.reset()

            Label5.Text = "" 

            Dim quadratic, linear, constant As Integer

            If Integer.TryParse(Equation1.a, quadratic) AndAlso Integer.TryParse(Equation1.b, linear) AndAlso Integer.TryParse(Equation1.c, constant) Then

                  Dim aDivisors As List(Of Point) = getDivisors(quadratic)

                  Dim cDivisors As List(Of Point) = getDivisors(constant) 

                  '(3x - 2)(x + 3)

                  For x1 As Integer = aDivisors.Count - 1 To 0 Step -1

                        For x2 As Integer = 0 To cDivisors.Count - 1

                              If aDivisors(x1).X * cDivisors(x2).Y + cDivisors(x2).X * aDivisors(x1).Y = linear Then

                                    Bracket2.a1 = String.Format("({0}x", If(aDivisors(x1).X = 1, "", aDivisors(x1).X.ToString))

                                    Bracket2.a2 = String.Format("({0}x", If(aDivisors(x1).Y = 1, "", aDivisors(x1).Y.ToString))

                                    Bracket2.i1 = If(cDivisors(x2).X > 0, "+" & cDivisors(x2).X.ToString, cDivisors(x2).X.ToString)

                                    Bracket2.i2 = If(cDivisors(x2).Y > 0, "+" & cDivisors(x2).Y.ToString, cDivisors(x2).Y.ToString)

                                    Bracket2.alignControls()

                                    Label5.Text = String.Format("x = {0} or x = {1}", simplify(cDivisors(x2).X, aDivisors(x1).X), simplify(cDivisors(x2).Y, aDivisors(x1).Y))

                                    Return

                              End If

                        Next

                  Next

            End If

      End Sub 

      Private Function getDivisors(ByVal x As Integer) As List(Of Point)

            Dim divisors As New List(Of Point)(Enumerable.Range(If(x > 0, -x, x), Math.Abs(x) * 2).Except(New Integer() {0}).Where(Function(n) x Mod n = 0).Select(Function(n) New Point(n, (x \ n))).ToArray)

            Return divisors

      End Function 

      Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

            Bracket2.reset()

            Equation1.reset()

            Label5.Text = ""

      End Sub

Ø  common methods

Common methods contains just one Function which is used for simplifying fractions.

 

Private Function simplify(ByVal n As Integer, ByVal d As Integer) As String

            If d = 1 Or d = -1 Then Return n.ToString

            Dim hcd As Integer = Enumerable.Range(2, Math.Max(Math.Abs(n), Math.Abs(d))).LastOrDefault(Function(x) n Mod x = 0 AndAlso d Mod x = 0)

            If hcd > 0 Then

                  n \ hcd

                  d \ hcd 

                  If d = 1 Or d = -1 And n < 0 Then

                        Return (n \ d).ToString

                  ElseIf d = -1 And n > 0 Then

                        Return "-" & (n \ d).ToString

                  End If 

                  Return String.Format("{2}{0}/{1}", Math.Abs(n), Math.Abs(d), If((n / d) < 0, "-", ""))

            Else

                  Return String.Format("{2}{0}/{1}", Math.Abs(n), Math.Abs(d), If((n / d) < 0, "-", ""))

            End If

            Return ""

      End Function

      

 

Ø  form

 

The general Form code handles the TabPage Paint events and adds some extra functionality to the application.

 

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

            If e.KeyCode = Keys.F2 Then

                  If TabControl1.SelectedIndex = 0 Then

                        MsgBox(String.Format("{0}, {1}", i1, i2))

                  End If

            ElseIf e.KeyCode = Keys.F3 Then

                  If TabControl1.SelectedIndex = 0 Then

                        TabControl1.SelectedIndex = 1

                        Bracket2.reset()

                        Equation1.reset()

                        Equation1.a = a.ToString

                        Equation1.b = If(b > 0, "+" & b.ToString, b.ToString)

                        Equation1.c = If(c > 0, "+" & c.ToString, c.ToString)

                  End If

            End If

      End Sub 

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            TabControl1.SelectedIndex = 0

            Button1.PerformClick()

            Me.KeyPreview = True

      End Sub 

      Private Sub TabPage1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TabPage1.Paint

            For c As Integer = 0 To Me.Width Step 25

                  e.Graphics.DrawLine(Pens.LightGray, c, 0, c, Me.Height)

            Next
            

            For r As Integer = 0 To Me.Height Step 25

                  e.Graphics.DrawLine(Pens.LightGray, 0, r, Me.Width, r)

            Next

      End Sub 

      Private Sub TabPage2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TabPage2.Paint

            For c As Integer = 0 To Me.Width Step 25

                  e.Graphics.DrawLine(Pens.LightGray, c, 0, c, Me.Height)

            Next

            For r As Integer = 0 To Me.Height Step 25

                  e.Graphics.DrawLine(Pens.LightGray, 0, r, Me.Width, r)

            Next

      End Sub 

      Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

            If e.KeyCode = Keys.Up Then

                  TextBox1.SelectedText = "²"

            End If

      End Sub

**See Also **

Download here