Lab 3-2: Creating a Class

Lab 3-2: Creating a Class

In this lab, you will create a Fraction class that models a fractional number. You will implement properties that represent the numerator and the denominator. You will also implement methods to reduce the fraction and convert the instance of the fraction class to a String or a Double. Finally you will build a small Windows Forms application to test your class.

Before You Begin

There are no prerequisites to completing this lab.

Estimated lesson time: 30 minutes

Exercise 3.3: Creating the Fraction Class

In this exercise, you will create the Fraction class and implement its properties and members.

To Create the Fraction Project

  1. From the File menu, choose New and then Project. In the New Project dialog box, choose a Windows Forms project, name it Fraction and click OK. The new project opens to the Windows Forms designer.

  2. From the Project menu, choose Add Class. Name the class Fraction, and click OK.

  3. Just under the class declaration, add private variables to hold the values for the numerator and the denominator, as shown here:

    Visual Basic .NET

    Private mNumerator As Integer Private mDenominator As Integer

    Visual C#

    private int mNumerator; private int mDenominator;

  4. In the body of the class, add properties to represent the numerator and the denominator, as shown in the following code. These properties should be read/write properties that set and return the values stored in the private fields you added in Step 2.

    Visual Basic .NET

    Public Property Numerator() As Integer Get Return mNumerator End Get Set(ByVal Value As Integer) mNumerator = Value End Set End Property Public Property Denominator() As Integer Get Return mDenominator End Get Set(ByVal Value As Integer) mDenominator = Value End Set End Property

    Visual C#

    public int Numerator { get { return mNumerator; } set { mNumerator = value; } } public int Denominator { get { return mDenominator; } set { mDenominator = value; } }

  5. Next add a method to reduce fractions when necessary. This method should be Private because it will not be accessible outside the class.

    Visual Basic .NET

    Private Sub Reduce() Dim i As Integer For i = Denominator To 2 Step -1 If Numerator Mod i = 0 And Denominator Mod i = 0 Then Numerator = Numerator / i Denominator = Denominator / i Exit For End If Next End Sub

    Visual C#

    private void Reduce() { int i; for(i = Denominator; i > 1; i--) { if (Numerator % i == 0 && Denominator % i == 0) { Numerator = Numerator / i; Denominator = Denominator / i; break; } } }

  6. Create a constructor that will initialize the values of the Numerator and Denominator properties, and reduce the fraction if necessary. Note that Visual C# classes begin with a default constructor that takes no parameters. You will have to replace that constructor with the one shown in this example:

    Visual Basic .NET

    Public Sub New(ByVal Numerator As Integer, ByVal Denominator As Integer) mNumerator = Numerator mDenominator = Denominator 'Calls reduce to reduce the fraction if necessary Reduce() End Sub

    Visual C#

    public Fraction(int Numerator, int Denominator) { mNumerator = Numerator; mDenominator = Denominator; // Calls reduce to reduce the fraction if necessary Reduce(); }

  7. Finally add ToString and ToDouble methods to convert your class to a String (string) and Double (double), respectively. Note that since your class implicitly inherits from System.Object, you must use the Overrides (override) keyword to override the implementation of ToString inherited from the Object class. The Overrides (override) keyword is further discussed in Chapter 4.

    Visual Basic .NET

    Public Function ToDouble() As Double Return Numerator / Denominator End Function Public Overrides Function ToString() As String Return Numerator.ToString & "/" & Denominator.ToString End Function

    Visual C#

    public double ToDouble() { return (double)Numerator / (double)Denominator; } public override string ToString() { return Numerator.ToString() + "/" + Denominator.ToString(); }

  8. From the Build menu, choose Build All to build and save your project.

Exercise 3.4: Testing the Fraction Class

In this exercise, you will build a simple Windows Forms application that will accept values for the numerator and denominator of a fraction, create a new instance of the Fraction class, and display this class converted to a string and a double.

  1. In Solution Explorer, right-click Form1 and choose View Designer. The Designer for Form1 opens.

  2. Add the controls listed in Table 3.7 to the form, and set the text property as indicated.

    Table 3-7. Controls for Form1

    Control

    Text

    Label1

    Numerator

    Label2

    Denominator

    Label3

    Fraction as String

    Label4

    (no text)

    Label5

    Fraction as Double

    Label6

    (no text)

    TextBox1

    (no text)

    TextBox2

    (no text)

    Button1

    Simplify and Display Fraction

    When you have added controls to your form, position them appropriately. Figure 3.2 demonstrates one example of how this could be done.

    figure 3-2 testing the fraction class.

    Figure 3-2. Testing the Fraction class.

    The two text boxes will be used to enter values for the numerator and denominator of an instance of the Fraction class, which will be created and displayed when Button1 is pressed.

  3. In the designer, double-click Button1 to open the code window to the click event handler. In the click event handler, add code to create a new instance of the Fraction class and display the value on the form, as shown below:

    Visual Basic .NET

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click ' Try and Catch are statements used for handling errors. ' They will be discussed further in Chapter 5 Try Dim numerator As Integer Dim denominator As Integer 'Retrieves values from the two textboxes. numerator = Integer.Parse(TextBox1.Text) denominator = Integer.Parse(TextBox2.Text) ' Creates a new instance of the Fraction class Dim aFraction As New Fraction(numerator, denominator) ' Displays the Fraction as a fraction and as a string Label4.Text = aFraction.ToString Label6.Text = (aFraction.ToDouble).ToString Catch ex As Exception MessageBox.Show("Please check the values and try again!") End Try End Sub

    Visual C#

    private void button1_Click(object sender, System.EventArgs e) { // Try and Catch are statements used for handling errors. // They will be discussed further in Chapter 5 try { int numerator; int denominator; numerator = int.Parse(textBox1.Text); denominator = int.Parse(textBox2.Text); // Creates a new instance of the Fraction class Fraction aFraction = new Fraction(numerator, denominator); // Displays the Fraction as a fraction and as a string label4.Text = aFraction.ToString(); label6.Text = (aFraction.ToDouble()).ToString(); } catch { MessageBox.Show("Please check the values and try again!"); } }

  4. Press F5 to build and test your application.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[.  .. ]0-316
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[. .. ]0-316
ISBN: 735619263
EAN: N/A
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net