Class PairOfDicePrivate m_die1, m_die2 As IntegerDim randomNum As New Random()Public ReadOnly Property Die1() As IntegerGetReturn m_die1End GetEnd PropertyPublic ReadOnly Property Die2() As IntegerGetReturn m_die2End GetEnd PropertyPublic ReadOnly Property SumOfFaces() As IntegerGetReturn m_die1 + m_die2End GetEnd PropertySub Roll()m_die1 = randomNum.Next(1, 7)m_die2 = randomNum.Next(1, 7)End SubEnd 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 SubPrivate 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 SubClass CashRegisterPrivate m_balance As DoublePublic ReadOnly Property Balance() As DoubleGetReturn m_balanceEnd GetEnd PropertySub Add(ByVal amount As Double)'Make sure balance does not go negative for negative amounts.If m_balance + amount >= 0 Thenm_balance += amountEnd IfEnd SubSub Subtract(ByVal amount As Double)'Make sure balance does not go negative.If m_balance - amount >= 0 Thenm_balance = m_balance - amountEnd IfEnd SubEnd Class
Exercises 11.2
1.
Sub btnDisplay_Click(...) Handles btnDisplay.ClickDim 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 lastStudentAddedIf students(i).CalcSemGrade = "A" ThenlstGrades.Items.Add(String.Format(fmtStr, students(i).Name, _students(i).SocSecNum, _students(i).CalcSemGrade))End IfNextEnd Sub
7.
Class CashRegisterPrivate m_balance As DoubleEvent AttemptToOverdraw(ByVal amt As Double)Public ReadOnly Property Balance() As DoubleGetReturn m_balanceEnd GetEnd Property
[Page 723]
Sub Add(ByVal amount As Double)'Make sure balance does not go negative for negative amounts.If m_balance + amount >= 0 Thenm_balance += amountElseRaiseEvent AttemptToOverdraw(m_balance - amount)End IfEnd SubSub Subtract(ByVal amount As Double)'Make sure balance does not go negative.If m_balance - amount >= 0 Thenm_balance = m_balance - amountElseRaiseEvent AttemptToOverdraw(m_balance - amount)End IfEnd SubEnd 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 frmRregisterDim tollBooth As New FastTrackRegister()Private Sub btnProcess_Click(...) Handles btnProcess.Click'Process the vehicleIf radCar.Checked ThentollBooth.ProcessCar()ElsetollBooth.ProcessTruck()End If'Display the revenue and number of vehiclestxtRevenue.Text = FormatCurrency(tollBooth.Balance)txtNumVehicles.Text = CStr(tollBooth.Count)End SubEnd Class
[Page 724]
Class CashRegisterPrivate m_balance As DoubleSub Deposit(ByVal amount As Double)m_balance += amount 'Add the amount to the balance.End SubSub WithDrawal(ByVal amount As Double)m_balance = m_balance - amount 'Subtract the amount from balance.End SubPublic ReadOnly Property Balance() As DoubleGetReturn m_balanceEnd GetEnd PropertyEnd ClassClass FastTrackRegisterInherits CashRegisterPrivate m_count As IntegerPublic ReadOnly Property Count() As IntegerGetReturn m_countEnd GetEnd PropertySub ProcessCar()m_count += 1 'Process a car: $1.00.Deposit(1)End SubSub ProcessTruck()m_count += 1 'Process a truck: $2.00.Deposit(2)End SubEnd Class