Chapter 11


[Page 720 (continued)]

Exercises 11.1

1.

Any negative grade will be recorded as 0, and any grade greater than 100 will be recorded as 100.

3.

Remove the keyword WriteOnly from the Final property block, and add the following Get property procedure to it:

Get   Return m_midterm End Get


5.

The properties MidGrade and Final are write only.

7.

The property SocSecNum is initialized to the value 999-99-9999.


[Page 721]
9.

The keyword New is missing from the third line of the event procedure.

11.

The statement nom = m_name is not valid.

13.

The property MidGrade is write only; it can be set, but cannot return a value.

15.

Country: Canada Capital: Ottawa Pop: 31 million


17.

Create a formLoad event procedure with the following lines, and change 20 to in the btnMove_Click event procedure:

round.Xcoord = picCircle.Width - 40 round.Ycoord = picCircle.Height - 40


21.

Class PairOfDice   Private m_die1, m_die2 As Integer   Dim randomNum As New Random()   Public ReadOnly Property Die1() As Integer     Get       Return m_die1     End Get   End Property   Public ReadOnly Property Die2() As Integer     Get       Return m_die2     End Get   End Property   Public ReadOnly Property SumOfFaces() As Integer     Get       Return m_die1 + m_die2     End Get   End Property   Sub Roll()     m_die1 = randomNum.Next(1, 7)     m_die2 = randomNum.Next(1, 7)   End Sub End Class


25.

Dim register As New CashRegister() Private Sub btnAdd_Click(...) Handles btnAdd.Click   'Add an amount to the balance.   register.Add(CDbl(txtAmount.Text))   'Display the new result.   txtBalance.Text = FormatCurrency(register.Balance)   txtAmount.Clear()   txtAmount.Focus() End Sub Private Sub btnSubtract_Click(...) Handles btnSubtract.Click   'Subtract an amount from the balance.   register.Subtract(CDbl(txtAmount.Text)) 
[Page 722]
'Display the new result. txtBalance.Text = FormatCurrency(register.Balance) txtAmount.Clear() txtAmount.Focus() End Sub Class CashRegister Private m_balance As Double Public ReadOnly Property Balance() As Double Get Return m_balance End Get End Property Sub Add(ByVal amount As Double) 'Make sure balance does not go negative for negative amounts. If m_balance + amount >= 0 Then m_balance += amount End If End Sub Sub Subtract(ByVal amount As Double) 'Make sure balance does not go negative. If m_balance - amount >= 0 Then m_balance = m_balance - amount End If End Sub End Class


Exercises 11.2

1.

Sub btnDisplay_Click(...) Handles btnDisplay.Click   Dim fmtStr As String = "{0,-20}{1,-15}{2,-1}"   lstGrades.Items.Clear()   lstGrades.Items.Add(String.Format(fmtStr, "Student Name", _                                  "SSN", "Grade"))   For i As Integer = 1 To lastStudentAdded     If students(i).CalcSemGrade = "A" Then       lstGrades.Items.Add(String.Format(fmtStr, students(i).Name, _                         students(i).SocSecNum, _                         students(i).CalcSemGrade))     End If   Next End Sub


7.

Class CashRegister   Private m_balance As Double   Event AttemptToOverdraw(ByVal amt As Double)   Public ReadOnly Property Balance() As Double     Get       Return m_balance     End Get   End Property 
[Page 723]
Sub Add(ByVal amount As Double) 'Make sure balance does not go negative for negative amounts. If m_balance + amount >= 0 Then m_balance += amount Else RaiseEvent AttemptToOverdraw(m_balance - amount) End If End Sub Sub Subtract(ByVal amount As Double) 'Make sure balance does not go negative. If m_balance - amount >= 0 Then m_balance = m_balance - amount Else RaiseEvent AttemptToOverdraw(m_balance - amount) End If End Sub End Class


Exercises 11.3

1.

4

3.

64

5.

Can move Has jointed limbs and no backbone

7.

The keyword Overrides should be Inherits.

9.

The Hi function should be declared with the Overridable keyword in class Hello and with the keyword Overrides keyword in class Aussie.

11.

The Hi function should be declared with the Overrides keyword in class Cowboy.

13.

The Hello class should be declared with the MustInherit keyword, and the function Hi should be declared with the MustOverride keyword.

15.

The Hello class should be declared with the MustInherit keyword, not MustOverride.

19.

Public Class frmRregister   Dim tollBooth As New FastTrackRegister()   Private Sub btnProcess_Click(...) Handles btnProcess.Click     'Process the vehicle     If radCar.Checked Then       tollBooth.ProcessCar()     Else       tollBooth.ProcessTruck()     End If     'Display the revenue and number of vehicles     txtRevenue.Text = FormatCurrency(tollBooth.Balance)     txtNumVehicles.Text = CStr(tollBooth.Count)   End Sub End Class 
[Page 724]
Class CashRegister Private m_balance As Double Sub Deposit(ByVal amount As Double) m_balance += amount 'Add the amount to the balance. End Sub Sub WithDrawal(ByVal amount As Double) m_balance = m_balance - amount 'Subtract the amount from balance. End Sub Public ReadOnly Property Balance() As Double Get Return m_balance End Get End Property End Class Class FastTrackRegister Inherits CashRegister Private m_count As Integer Public ReadOnly Property Count() As Integer Get Return m_count End Get End Property Sub ProcessCar() m_count += 1 'Process a car: $1.00. Deposit(1) End Sub Sub ProcessTruck() m_count += 1 'Process a truck: $2.00. Deposit(2) End Sub End Class





An Introduction to Programming Using Visual Basic 2005
Introduction to Programming Using Visual Basic 2005, An (6th Edition)
ISBN: 0130306541
EAN: 2147483647
Year: 2006
Pages: 164

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