Professional Documents
Culture Documents
Click Start, point to All Programs, click Microsoft Visual Studio 2010, and
then click Microsoft Visual Studio 2010.
You can import a code snippet from the Tools menu, where you click Code Snippets
Manager. In the Code Snippets Manager dialog box, in the Language drop-down,
click Visual Basic, and then click Add. Browse to the D:\Labfiles\Lab03\Snippets
folder, click Select Folder, and then click OK.
b. In the Code Snippets Manager dialog box, in the Language drop-down, click
Visual Basic.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3&F 1/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
a. If the Task List is not already visible, on the View menu, point to Other
Windows, and then click Task List.
b. If the Task List is displaying User Tasks, in the drop-down list, click
Comments.
2. Use the Task List window to navigate to the TODO: Exercise 1, Task 2 task.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3&F 2/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
End Function
End Class
4. In the FindGCDEuclid method, add code that calculates and returns the greatest
common divisor (GCD) of the values specified by the parameters a and b by
using Euclid's algorithm.
ByVal b As Integer) As
Integer
If a = 0 Then Return b
While b <> 0
If a > b Then
a=a-b
Else
b=b-a
End If
End While
1. Use the Task List window to navigate to the TODO: Exercise 1, Task 3 task.
This task is located in the MainWindow.xaml.vb file. This is the code-behind file for a
WPF window that you will use to test the FindGCDEuclid method and display the
results.
2. Remove the TODO: Exercise 1, Task 3 comment, add code to call the static
FindGCDEuclid method of the GCDAlgorithms class, and display the results in
the ResultEuclidLabel control. In the method call, use the firstNumber and
secondNumber variables as arguments (these variables contain values that the
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3&F 4/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
user enters in the WPF window). Finally, the result should be formatted as the
following example.
Euclid: result
Hint: Set the Content property of a label control to display data in a label. Use
the
...
If sender Is FindGCD3Button Then ' Euclid for two integers
' Invoke the FindGCD method and display the result
Me.ResultEuclidLabel.Content =
String.Format("Euclid: {0}",
GCDAlgorithms.FindGCDEuclid(firstNumber,
secondNumber))End If
...
6. In the second text box, type 345 and then click Find GCD (2 Integers). The result
of 23 should be displayed, as the following screen shot shows.
7. Use the window to calculate the GCD for the values that are specified in the
following table, and verify that the results that are displayed match those in the
table.
0 0 0
0 10 10
25 10 5
25 100 25
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3&F 6/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
26 100 2
27 100 1
2. In the GCDAlgorithms class, create a unit test for the FindGCDEuclid method.
Create a new Test Project named GCD Test Project project to hold the unit test.
c. In the New Test Project dialog box, in the Enter a name for your new
project, type GCD Test Project and then click Create.
3. In the GCD Test Project project, in the GCDAlgorithmsTest.vb file, locate the
FindGCDEuclidTest method.
4. In the FindGCDEuclidTest method, set the a variable to 2806, set the b variable
to 345, set the expected variable to 23, and then remove the
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3&F 7/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
...
<TestMethod()> _
Public Sub FindGCDEuclidTest()
Dim a As Integer = 2806 ' TODO: Initialize to an appropriate
value
Dim b As Integer = 345 ' TODO: Initialize to an appropriate
value
Dim expected As Integer = 23 ' TODO: Initialize to an
appropriate value
Dim actual As Integer
actual = GCDAlgorithms.FindGCDEuclid(a, b)
Assert.AreEqual(expected, actual)
End Sub
...
5. Open the Test View window and refresh the display if the unit test is not listed.
a. On the Test menu, point to Windows, and then click Test View.
Note: If the You have made changes to your tests dialog box
appears, click OK.
6. Run the FindGCDEuclidTest test and verify that the test ran successfully.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3&F 8/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
a. In the Test View window, right-click FindGCDEuclidTest, and then click Run
Selection.
b. In the Test Results window, verify that the FindGCDEuclidTest test passed.
Note: If the You have made changes to your tests dialog box appears,
click OK.
a. If the Task List is not already visible, on the View menu, point to Other
Windows, and then click Task List.
b. If the Task List is displaying User Tasks, in the drop-down list box click
Comments.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3&F 9/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
2. Use the Task List window to navigate to the TODO: Exercise 2, Task 2 task.
...
Public Class GCDAlgorithms
...
Public Shared Function FindGCDEuclid(ByVal a As
Integer, ByVal b As Integer, ByVal c
As Integer) As Integer
4. In the new method, add code that uses the original FindGCDEuclid method to
find the GCD for the parameters a and b. Store the result in a new variable
named d.
...
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 10/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
5. Add a second call to the original FindGCDEuclid method to find the GCD for
variable d and parameter c. Store the result in a new variable named e.
...
Public Shared Function FindGCDEuclid(ByVal a As Integer, ByVal b
As Integer, ByVal c As
Integer) As Integer
Dim d As Integer = FindGCDEuclid(a, b)
Dim e As Integer = FindGCDEuclid(d, c)End Function
...
...
Public Shared Function FindGCDEuclid(ByVal a As Integer, ByVal b
As Integer, ByVal c As
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 11/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
Integer) As Integer
Dim d As Integer = FindGCDEuclid(a, b)
Dim e As Integer = FindGCDEuclid(d, c)
...
Public Shared Function FindGCDEuclid(ByVal a As Integer, ByVal b
As Integer, ByVal c As
Integer, ByVal d As Integer) As Integer
Dim e As Integer = FindGCDEuclid(a, b, c)
Dim f As Integer = FindGCDEuclid(e, d)
Return f
End Function
...
...
Public Shared Function FindGCDEuclid(ByVal a As Integer, ByVal b
As Integer, ByVal c As
Integer, ByVal d As Integer, ByVal e As Integer) As Integer
Dim f As Integer = FindGCDEuclid(a, b, c, d)
Dim g As Integer = FindGCDEuclid(f, e)
Return g
End Function
...
At the end of this task, the GCDAlgorithms class should resemble the following code
example.
...
Public Class GCDAlgorithms
...
Public Shared Function FindGCDEuclid(ByVal a As Integer,
ByVal b As Integer, ByVal c
As Integer) As Integer
Dim d As Integer = FindGCDEuclid(a, b)
Dim e As Integer = FindGCDEuclid(d, c)
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 13/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
Return e
End Function
Return f
End Function
Return g
End Function
...
1. Use the Task List window to navigate to the TODO: Exercise 2, Task 3 task.
This task is located in the code for the WPF window that you can use to test your
code.
2. Remove the TODO: Exercise 2, Task 3 comment, locate the ElseIf sender Is
FindGCD3Button Then block and modify the statement that sets the Content
property of the ResultEuclidLabel control to "N/A" as follows.
a. Call the FindGCDEuclid overload that accepts three parameters and pass
the variables firstNumber, secondNumber, and thirdNumber as arguments.
b. Display the results in the ResultEuclidLabel label control. The result should
be formatted as the following code example shows.
Euclid: result
...
ElseIf sender Is FindGCD3Button Then ' Euclid for three integers
Me.ResultEuclidLabel.Content =
String.Format("Euclid: {0}",
GCDAlgorithms.FindGCDEuclid(
firstNumber,
secondNumber,
thirdNumber)) Me.ResultSteinLabel.Content =
"N/A"
...
3. Locate the ElseIf sender Is FindGCD3Button Then block, the ElseIf sender Is
FindGCD4Button Then block, and the ElseIf sender Is FindGCD5Button Then
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 15/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
block, and modify the statements that set the Content property of the
ResultEuclidLabel control to "N/A". Call the appropriate FindGCDEuclid
overload by using the firstNumber, secondNumber, thirdNumber,
fourthNumber, and fifthNumber variables as arguments. Display the results in
the ResultEuclidLabel label control.
...
Private Sub FindGCDButton_Click(ByVal sender As Object, ByVal e
As RoutedEventArgs)
...
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 16/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
Me.ResultSteinLabel.Content = "N/A"
ElseIf sender Is FindGCD5Button Then ' Euclid for five
integers
Me.ResultEuclidLabel.Content =
String.Format("Euclid: {0}",
GCDAlgorithms.FindGCDEuclid(firstNumber,
secondNumber,
thirdNumber, fourthNumber, fifthNumber))
Me.ResultSteinLabel.Content = "N/A"
End If
End Sub
...
Press Ctrl+F5.
7. Use the window to calculate the GCD for the values that are specified in the
following table and verify that the results that are displayed match those in the
table.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 17/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
2806 345 0 0 0 23
0 0 0 0 0 0
0 0 0 0 1 1
12 24 36 48 60 12
13 24 36 48 60 1
14 24 36 48 60 2
15 24 36 48 60 3
16 24 36 48 60 4
0 24 36 48 60 12
a. If the Task List is not already visible, on the View menu, point to Other
Windows, and then click Task List.
b. If the Task List is displaying User Tasks, in the drop-down list box click
Comments.
2. Use the Task List window to navigate to the TODO: Exercise 2, Task 4 task.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 18/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
3. Remove the TODO: Exercise 2, Task 4 comment and add a test method named
FindGCDEuclidTest1.
...
<TestMethod()>
Public Sub FindGCDEuclidTest1()
End Sub
...
<TestMethod()>
Public Sub FindGCDEuclidTest1()
Dim a As Integer = 7396
Dim b As Integer = 1978
Dim c As Integer = 1204
Dim expected As Integer = 86End Sub
5. Declare a variable named actual, and assign it the result of a call to the
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 19/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
<TestMethod()>
Public Sub FindGCDEuclidTest1()
Dim a As Integer = 7396
Dim b As Integer = 1978
Dim c As Integer = 1204
Dim expected As Integer = 86
Dim actual As Integer = GCDAlgorithms.FindGCDEuclid(a,
b, c)End Sub
6. Call the shared AreEqual method of the Assert class, and pass the expected
and actual variables as arguments.
<TestMethod()>
Public Sub FindGCDEuclidTest1()
Dim a As Integer = 7396
Dim b As Integer = 1978
Dim c As Integer = 1204
Dim expected As Integer = 86
Dim actual As Integer = GCDAlgorithms.FindGCDEuclid(a, b, c)
Assert.AreEqual(expected, actual)End Sub
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 20/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
7. Repeat steps 46 to create two more test methods to test the other
FindGCDEuclid method overloads. Create test methods named
FindGCDEuclidTest2 and FindGCDEuclidTest3. Use the values 7396, 1978,
1204, and 430 for the FindGCDEuclidTest2 method, and the values 7396, 1978,
1204, 430, and 258 for the FindGCDEuclidTest3 method. The result should be
86 in both cases.
...
<TestClass()> _
Public Class GCDAlgorithmsTest
...
<TestMethod()>
Public Sub FindGCDEuclidTest1()
Dim a As Integer = 7396
Dim b As Integer = 1978
Dim c As Integer = 1204
Dim expected As Integer = 86
Dim actual As Integer = GCDAlgorithms.FindGCDEuclid(a, b,
c)
Assert.AreEqual(expected, actual)
End Sub
<TestMethod()>
Public Sub FindGCDEuclidTest2()
Dim a As Integer = 7396
Dim b As Integer = 1978
Dim c As Integer = 1204
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 21/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
<TestMethod()>
Public Sub FindGCDEuclidTest3()
Dim a As Integer = 7396
8. Open the Test View window and refresh the display if the unit test is not listed.
a. On the Test menu, point to Windows, and then click Test View.
b. If the You have made changes to your tests dialog box appears, click OK.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 22/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
Note: If the You have made changes to your tests dialog box appears,
click OK.
2. At the end of the GCDAlgorithms class, remove the TODO: comment and
declare a shared Public method named FindGCDStein. The method should
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 23/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
accept two integer parameters named u and v and return an integer value.
...
Public Class GCDAlgorithms
...
Public Shared Function FindGCDStein(ByVal u As Integer,
ByVal v As Integer) As
Integer
3. In the FindGCDStein method, add the code in the following code example,
which calculates and returns the GCD of the values that are specified by the
parameters u and v by using Stein's algorithm. You can either type this code
manually, or use the Mod03Stein code snippet. To use the Mod03Stein snippet,
add a blank line, type Mod03Stein, and then press TAB.
Note: For the purposes of this exercise, it is not necessary for you to
understand this code. However, if you have time, you may like to
compare this method with the algorithm that is described in the exercise
scenario. Note that this code uses the left-shift (<<) and right-shift (>>)
operators to perform fast multiplication and division by 2. If you left-shift
an integer value by one place, the result is the same as multiplying the
integer value by 2. Similarly, if you right-shift an integer value by one
place, the result is the same as dividing the integer value by 2. In addition,
the Or operator performs a bitwise OR operation between two integer
values. Consequently, if either u or v is zero, the expression u And v is a
fast way of returning the value of whichever variable is nonzero, or zero if
both are zero. Similarly, the And operator performs a bitwise AND
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 24/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
' Step 1.
' gcd(0, v) = v, because everything divides zero,
' and v is the largest number that divides v.
' Similarly, gcd(u, 0) = u. gcd(0, 0) is not typically
' defined, but it is convenient to set gcd(0, 0) = 0.
If u = 0 OrElse v = 0 Then
Return u Or v
End If
' Step 2.
' If u and v are both even, then gcd(u, v) = 2gcd(u/2, v/2),
' because 2 is a common divisor.
Do While ((u Or v) And 1) = 0
u >>= 1
v >>= 1
k += 1
Loop
' Step 3.
' If u is even and v is odd, then gcd(u, v) = gcd(u/2, v),
' because 2 is not a common divisor.
' Similarly, if u is odd and v is even,
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 25/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
' Step 4.
' If u and v are both odd, and u v,
' then gcd(u, v) = gcd((u v)/2, v).
' If both are odd and u < v, then gcd(u, v) = gcd((v u)/2,
u).
' These are combinations of one step of the simple
' Euclidean algorithm,
' which uses subtraction at each step, and an application
' of step 3 above.
' The division by 2 results in an integer because the
' difference of two odd numbers is even.
Do
While (v And 1) = 0 ' Loop x
v >>= 1
End While
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 26/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
' Step 5.
' Repeat steps 34 until u = v, or (one more step)
' until u = 0.
' In either case, the result is (2^k) * v, where k is
' the number of common factors of 2 found in step 2.
Loop While v <> 0
u <<= k
Return u
End Function
Stein: result
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 27/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
Press Ctrl+F5.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 28/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
8. At the end of the GCDAlgorithmsTest class, locate the TODO: Exercise 3, Task
2 comment, remove the comment, and then add a test method named
FindGCDSteinTest.
...
<TestMethod()>
Public Sub FindGCDSteinTest()
End Sub...
<TestMethod()>
Public Sub FindGCDSteinTest()
Dim u As Integer = 298467352
Dim v As Integer = 569484
Dim expected As Integer = 4End Sub
10. Declare a variable named actual and assign it the result of a call to the
FindGCDStein method call. Use the variables u and v as arguments.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 29/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
<TestMethod()>
Public Sub FindGCDSteinTest()
Dim u As Integer = 298467352
Dim v As Integer = 569484
Dim expected As Integer = 4
Dim actual As Integer = GCDAlgorithms.FindGCDStein(u,
v)End Sub
11. Call the shared AreEqual method of the Assert class, and pass the expected and
actual variables as arguments.
<TestMethod()>
Public Sub FindGCDSteinTest()
Dim u As Integer = 298467352
Dim v As Integer = 569484
Dim expected As Integer = 4
Dim actual As Integer = GCDAlgorithms.FindGCDStein(u, v)
Assert.AreEqual(expected, actual)End Sub
12. Open the Test View window and refresh the display if the unit test is not listed.
a. On the Test menu, point to Windows, and then click Test View.
b. If the You have made changes to your tests dialog box appears, click OK.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 30/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
13. Run the FindGCDSteinTest test and verify that the test ran successfully.
a. In the Test View window, right-click the FindGCDSteinTest test, and then
click Run Selection.
2. In the GCDAlgorithms class, locate the FindGCDEuclid method that accepts two
parameters and modify the method signature to take a reference parameter
named time of type Long.
...
Public Shared Function FindGCDEuclid(ByVal a As Integer, ByVal b
As Integer, ByRef time
As Long) As Integer
...
End Function
...
3. At the start of the FindGCDEuclid method, add code to initialize the time
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 31/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
parameter to zero, create a new Stopwatch object named sw and start the stop
watch.
The Stopwatch class is useful for timing code. The Start method starts an internal
timer running. You can subsequently use the Stop method to halt the timer, and
establish how long the interval was between starting and stopping the timer by
querying the ElapsedMilliseconds or ElapsedTicks properties.
...
Public Shared Function FindGCDEuclid(ByVal a As Integer, ByVal b
As Integer, ByRef time
As Long) As Integer
time = 0
Dim sw As New Stopwatch()
sw.Start() ...
End Function
...
4. At the end of the FindGCDEuclid method, before the Return statement, add
code to stop the Stopwatch object, and set the time parameter to the number
of elapsed ticks of the Stopwatch object.
...
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 32/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
6. Modify the FindGCDStein method to include the time reference parameter and
add code to record the time each method takes to run. Note that the
FindGCDStein method contains two Return statements, and you should record
the time before each one.
...
Public Shared FindGCDStein(ByVal u As Integer, ByVal v As
Integer, ByRef time As Long) As
Integer
time = 0
Dim sw = new Stopwatch()
sw.Start() Dim k As Integer = 0
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 33/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
' Step 1.
' gcd(0, v) = v, because everything divides zero, and
' v is the largest number that divides v.
' Similarly, gcd(u, 0) = u. gcd(0, 0) is not typically
' defined, but it is convenient to set gcd(0, 0) = 0.
If u = 0 OrElse v = 0 Then
sw.Stop()
time = sw.ElapsedTicks return u Or v
End If ... sw.Stop()
time = sw.ElapsedTicks
Return u
End Function
...
a. For calling the Euclid algorithm, create a variable named timeEuclid of type
Long.
b. For calling the Stein algorithm, create a variable named timeStein of type
Long
c. Format the results displayed in the labels as the following code example
shows.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 34/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
[Euclid]
Euclid: result, Time (ticks): result
[Stein]
Stein: result, Time (ticks): result
...
If sender Is FindGCDButton Then ' Euclid for two integers and
graph
Dim timeEuclid As Long
Dim timeStein As Long Me.ResultEuclidLabel.Content =
String.Format("Euclid: {0}, Time (ticks): {1}",
GCDAlgorithms.FindGCDEuclid(firstNumber,
secondNumber, timeEuclid), timeEuclid)
Me.ResultSteinLabel.Content = String.Format("Stein:
{0}, Time (ticks): {1}",
GCDAlgorithms.FindGCDStein(firstNumber,
secondNumber, timeStein), timeStein)...
9. Comment out the code that calls the overloaded versions of the FindGCDEuclid
method.
...
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 35/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
11. Modify the FindGCDEuclidTest and FindGCDSteinTest methods to use the new
method signatures. Comment out the methods FindGCDEuclidTest1,
FindGCDEuclidTest2, and FindGCDEuclidTest3.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 36/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
<TestClass()> _
Public Class GCDAlgorithmsTest
...
<TestMethod()> _
Public Sub FindGCDEuclidTest()
Dim a As Integer = 2806
Dim b As Integer = 345
Dim time As Long Dim expected As Integer = 23
Dim actual As Integer
actual = GCDAlgorithms.FindGCDEuclid(a, b, time)
Assert.AreEqual(expected, actual)
End Sub
...
<TestMethod()>
Public Sub FindGCDSteinTest()
Dim u As Integer = 298467352
Dim v As Integer = 569484
Dim time As Long Dim expected As Integer = 4
Dim actual As Integer = GCDAlgorithms.FindGCDStein(u, v,
time)
Assert.AreEqual(expected, actual)
End Sub
End Class
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 37/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
Press Ctrl+F5.
Note: The bigger the difference between the two values, the more
efficient Stein's algorithm becomes compared with Euclid's. If you have
time, try experimenting with different values.
16. Open the Test View window and refresh the display if the unit test is not listed.
a. On the Test menu, point to Windows, and then click Test View.
17. Run the FindGCDEuclidTest and FindGCDSteinTest methods and verify that
the tests ran successfully.
...
If sender Is FindGCDButton Then ' Euclid and Stein for two
integers and graph
...
DrawGraph(timeEuclid, timeStein)...
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 39/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
Press Ctrl+F5.
1. In the MainWindow class, locate the DrawGraph method and add the following
three optional parameters.
...
Private Sub DrawGraph(ByVal euclidTime As Long, ByVal steinTime
As Long,
Optional ByVal orient As Orientation =
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 40/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
Orientation.Horizontal,
Optional ByVal colorEuclid As String = "Red",
Optional ByVal colorStein As String = "Blue")
...
End Sub
...
3. Locate the Use optional color parameters comment, and modify the
assignment of the bEuclid and bStein variables to use the optional parameters
in the method signature. To do this, you will need to use the BrushConverter
class and the ConvertFromString instance method as shown in the following
code example.
...
Private Sub DrawGraph(ByVal euclidTime As Long, ByVal
steinTime As Long, Optional ByVal orient As Orientation =
Orientation.Horizontal,
Optional ByVal colorEuclid As String = "Red",
Optional ByVal colorStein As String = "Blue")
...
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 41/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
Press Ctrl+F5.
These list boxes are already included in the user interface; they appear in the lower
part of the window. The user can select the values in these list boxes to change the
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 42/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
...
If sender Is FindGCDButton Then ' Euclid for two integers and
graph
...
If Me.ChartOrientationListBox.SelectedIndex = 0 Then
orient = Orientation.Vertical
Else
orient = Orientation.Horizontal
End If
DrawGraph(timeEuclid, timeStein,
orient:=orient,
colorStein:=selectedSteinColor,
colorEuclid:=selectedEuclidColor)
...
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 43/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
Press Ctrl+F5.
5. In the Euclid list, select Green, in the Stein list, select Black, in the Orientation
list, select Horizontal, and then click Find GCD (2 Integers). The graph should
be displayed with the specified colors and direction.
\Starter folder.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 44/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
double-click MainWindow.xaml.
This is a different application from the one that the previous exercises have used. It is
a WPF application that enables a user to enter the coefficients for four simultaneous
equations that contain four variables (w, x, y, and z), and then uses Gaussian
Elimination to find a solution for these equations. The results are displayed in the
lower part of the screen.
This file contains a class named Gauss that provides a method named
SolveGaussian. This method takes two arrays as parameters.
An array of Double values containing the result of each equation specified by the
user (the value to the right of the equal sign).
The method returns an array of Double values that will be populated with the values
of w, x, y, and z that provide the solutions to these equations.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 45/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
2. In the Gauss class, locate the TODO: Exercise 5, Task 2 comment. Remove this
comment and declare a shared Private method named DeepCopy1D. The
method should accept and return a Double array.
The SolveGaussian method will make a copy of the arrays passed in as parameters to
avoid changing the original data that the user provided.
...
Private Shared Function DeepCopy1D(ByVal arr() as Double)
As Double()
End Function...
3. In the DeepCopy1D method, add code to create a deep copy of the one-
dimensional array that was passed into the method. Your code should perform
the following tasks.
a. Create and initialize an array with the same number of columns as the array
that was passed in.
b. Copy the values in the array that was passed as a parameter into the new
array.
...
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 46/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
...
Private Shared Function DeepCopy2D(ByVal arr(,) As Double)
As double(,)
End Function...
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 47/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
5. In the DeepCopy2D method, add code to create a deep copy of the two-
dimensional array that was passed into the method. Your code should do the
following.
a. Create and initialize an array with the same number of columns and rows as
the array that was passed in.
b. Copy the values in the array that was passed in as the parameter into the
new array.
...
Private Shared Function DeepCopy2D(ByVal arr(,) As Double) As
double(,)
' Get dimensions
Dim columns As Integer = arr.GetLength(0)
Dim rows As Integer = arr.GetLength(1)
...
...
Public Shared Function SolveGaussian(ByVal coefficients(,) As
Double, ByVal rhs(,) As
Double) As Double()
' TODO: Exercise 5, Task 3
' Make deep copies of the coefficients and rhs arrays
Dim a(,) As Double = DeepCopy2D(coefficients)
Dim b() As Double = DeepCopy1D(rhs) ...
End Function
...
2. Locate the Convert the equation to triangular form comment and add code
to convert the equations represented by the copies of the coefficients and rhs
arrays into triangular form.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 49/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
...
' TODO: Exercise 5, Task 3
' Convert the equations to triangular form
Dim x, sum As Double
In the Gauss class, in the SolveGaussian method, locate the Perform the back
substitution and return the result comment, and then add code to perform back
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 50/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
substitution. To do this, you will need to work back from the equation with one
unknown and substituting the values calculated at each stage to solve the
remaining equations.
...
' TODO: Exercise 5, Task 4
' Perform the back substitution and return the result
b(numberOfEquations 1) = b(numberOfEquations 1) /
a(numberOfEquations - 1,
numberOfEquations 1)
Return b...
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 51/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
MainWindow.xaml.vb.
2. In the MainWindow class, locate the TODO: Exercise 5, Task 5 comment, and
add code to call the SolveGaussion method. Use the coefficients and rhs
variables as parameters and set the answers array to the result.
...
Private Sub SolveButton_Click(ByVal sender As Object, ByVal e As
RoutedEventArgs)
...
' TODO: Exercise 5, Task 5
' Invoke the SolveGaussian method
answers = Gauss.SolveGaussian(coefficients, rhs) ...
End Sub
...
Press Ctrl+F5.
4. In the MainWindow window, enter the following equations, and then click
Solve.
w x y z Result
2 1 -1 1 8
-3 -1 2 1 -11
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 52/53
10/4/2014 Lab Answer Key: Module 3: Declaring and Calling Methods
-2 1 -2 0 -3
3 -1 2 -2 -5
w=4
x = 17
y = 11
z=6
5. Experiment with other equations. Note that not all systems of equations have a
solution. How does your code handle this situation?
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=20&FontSize=3& 53/53