'
'SW506 VB.NET for Programmers II
'Spring 2006
'Guillermo V. Julca
'***********************************************************************************
Assignment I
Assigned: 1/30/2006
Due: 2/13/2006
In this Assignment, you will develop your own user defined Class type called Distance. You will then access it via a Console App. The Distance class is used to measure linear distances in feet and inches.
Add the following Properties to the class
Feet(long) : Read/Write, changes the Feet value
Inches(double) : Read/Write, changes the Feet and Inches values appropriately
Add the following methods to the class:
Display() :prints the object as (x feet, y inches). For example: 17' 3.25"
Display(string) :prints the object using the supplied formatting string
SetSize(long, double) :sets both the feet and inches
Swap(a as Distance, b as Distance) :exchanges the contents of a and b
Console App
The Console App uses the above class with the following Code:
Sub Main()
Dim d1 As Distance = New Distance
Dim d2 As Distance = New Distance
d1.Feet = 23
d1.Inches = 54.2
d2.SetSize(17, 12.25)
Console.Write("d1 = ")
d1.Display()
Console.WriteLine("")
Console.Write("d2 = ")
d2.Display()
Console.WriteLine("")
Distance.Swap(d1, d2)
d1.Display("d1 = {0} ft. {1} in." & vbCrLf)
d2.Display("d2 = {0} feet {1:N2} inches" & vbCrLf)
Console.WriteLine("d1 contains {0:N4} inches", d1.Feet * 12 + d1.Inches)
Console.Read()
End Sub
Console App - Output
The above code produces the following output:
d1 = 27' 6.2"
d2 = 18' 0.25"
d1 = 18 ft. 0.25 in.
d2 = 27 feet 6.20 inches
d1 contains 216.2500 inches
Additional Restrictions
Here are some additional requirements, before you steal somebody else's code and use it in your assignment.
1. Display() with no arguments, can have only one line of code in it.
2. Swap can only have 3 lines of code in it.
3. Remember that the value for inches can never be greater or equal to twelve. When that happens, you'll have to adjust both the feet and inches fields.
The Deliverables
I expect the following deliverables. I am your client, so please follow the client's requirements carefully.
Hard Copies
Please submit a hard copy printout of the following files :
1. Distance.vb
2. Module1.vb
Screen Shots
Please provide screenshots (need not be in color) of:
1. Console Output DOS Window
Files on Floppy Disk
Please submit a copy of the entire HW1 subdirectory that contains all the project/solution files.
'*************************************************************************************************************************
Public Class Distance
'Define a local
variable "lngFeet" to store the property "Feet" value.
Private
lngFeet As Long
'Define a local
variable "dblInches" to store the property "Inches" value.
Private
dblInches As Double
'Private variable
to store the integer division result from "dblInches" and 12
Private
intDeltaFeet As Integer
'Private variable
to store the remainder result that will get from the division
'operation
between "dblInches" variable value and 12
Private
dblRemainderInches As Double
'To define the
"Feet" property.
Public Property Feet() As Long
Get
Return
lngFeet
End Get
Set(ByVal value As Long)
lngFeet = value
End Set
End Property
'To define the
"Inches" property.
Public Property Inches() As Double
Get
Return
dblInches
End Get
Set(ByVal value As Double)
'Divides
number of inches by 12 and returns an integer result.
intDeltaFeet = value \ 12
'Divides
number of inches by 12 and returns only the remainder.
dblRemainderInches = value Mod 12
lngFeet += intDeltaFeet
'The
value for inches can never be greater or equal to 12
dblInches = dblRemainderInches
End Set
End Property
'Display function
with no arguments
Public Sub Display()
'This
function will print the object as (x feet, y incches). For example: 25'
4.25"
System.Console.Write("{0}' {1}" & Chr(34), lngFeet,
dblInches)
End Sub
'Display function
with a string argument
Public Sub Display(ByVal
strLine As String)
'This
function will print the object using the supplied formatting string.
Dim
bolThereIsCurly0 As Boolean
Dim
bolThereIsCurly1 As Boolean
bolThereIsCurly0 = False
bolThereIsCurly1 = False
If
(InStr(strLine, "{0",
CompareMethod.Text) > 0) Then
bolThereIsCurly0 = True
End If
If
(InStr(strLine, "{1",
CompareMethod.Text) > 0) Then
If
bolThereIsCurly0 = True Then
bolThereIsCurly1 = True
Else
System.Console.WriteLine("Please Enter a Valid Formatting String. . . ")
Exit
Sub
End
If
End If
If
bolThereIsCurly0 = True Then
If
bolThereIsCurly1 = True Then
System.Console.WriteLine(strLine, lngFeet, dblInches)
Else
System.Console.WriteLine(strLine, lngFeet)
End
If
Else
System.Console.WriteLine(strLine)
End If
End Sub
Public Sub SetSize(ByVal
lngFeetValue As Long,
_
ByVal
dblInchesValue As Double)
'This
function sets both the feet and inches.
'Divides
number of inches by 12 and returns an integer result.
intDeltaFeet = dblInchesValue \ 12
'Divides
number of inches by 12 and returns only the remainder.
dblRemainderInches = dblInchesValue Mod 12
lngFeet = lngFeetValue + CLng(intDeltaFeet)
dblInches = dblRemainderInches
End Sub
Public Shared Sub Swap(ByRef objDistance1 As
Distance, _
ByRef objDistance2 As
Distance)
'This
function will exchange the contents of "objDistance1" and
"objDistance2"
'To define a
temporary object which will store the features of the "objDistance1"
Dim
objAuxiliarDistance1 As Distance =
objDistance1 'Temporary
object
'To assign
the features of the "objDistance2" to "objDistance1"
objDistance1 = objDistance2
'To assign
the features of the "objAuxiliarDistance1" to
"objDistance2"
objDistance2 = objAuxiliarDistance1
End Sub
End Class
'*******************************************************************************************************
'
'SW506 VB.NET for Programmers II
'Spring 2006
'Guillermo V. Julca
'Assignment I
Module Module1
Sub
Dim d1 As Distance = New
Distance
Dim d2 As Distance = New
Distance
d1.Feet = 23
d1.Inches = 54.2
d2.SetSize(17, 12.25)
Console.Write("d1
= ")
d1.Display()
Console.WriteLine("")
Console.Write("d2
= ")
d2.Display()
Console.WriteLine("")
Distance.Swap(d1, d2)
d1.Display("d1
= {0} ft. {1} in.")
d2.Display("d2
= {0} feet {1:N2} inches")
Console.WriteLine("d1 contains {0:N4} inches", d1.Feet * 12
+ d1.Inches)
Console.Read()
End Sub
End Module