(Note: Programming Problem 4 draws directly on Chapter 6, Programming Project 1, page 285.)
Nutritional Associates of New Jersey stresses to their patients the effects of caffeine on the body. After caffeine is absorbed into the body, 13% is eliminated from the body each hour. Assume a person drinks an 8-ounce cup of brewed coffee containing 130 mg of caffeine, and the caffeine is absorbed immediately into the body. See Figure 6.77 in the Visual Basic text, and write a program to compute the following values:
Suppose the person drinks a cup of coffee at 7:00 a.m. and then drinks a cup of coffee at the end of each hour until 7:00 a.m. the next day. How much caffeine will be in the body at the end of the 24 hours?
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.btnCalculate = New System.Windows.Forms.Button()
Me.Label1 = New System.Windows.Forms.Label()
Me.txt65mg = New System.Windows.Forms.TextBox()
Me.Label2 = New System.Windows.Forms.Label()
Me.txtOneCup = New System.Windows.Forms.TextBox()
Me.Label3 = New System.Windows.Forms.Label()
Me.txtHoursCup = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'btnCalculate
'
Me.btnCalculate.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
Me.btnCalculate.Location = New System.Drawing.Point(50, 12)
Me.btnCalculate.Name = "btnCalculate"
Me.btnCalculate.Size = New System.Drawing.Size(185, 37)
Me.btnCalculate.TabIndex = 0
Me.btnCalculate.Text = "Calculate Values"
Me.btnCalculate.UseVisualStyleBackColor = True
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
Me.Label1.Location = New System.Drawing.Point(28, 75)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(118, 42)
Me.Label1.TabIndex = 1
Me.Label1.Text = "One cup: When will 65 mg remain?"
'
'txt65mg
'
Me.txt65mg.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
Me.txt65mg.Location = New System.Drawing.Point(172, 75)
Me.txt65mg.Name = "txt65mg"
Me.txt65mg.ReadOnly = True
Me.txt65mg.Size = New System.Drawing.Size(100, 26)
Me.txt65mg.TabIndex = 2
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
Me.Label2.Location = New System.Drawing.Point(28, 120)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(118, 37)
Me.Label2.TabIndex = 1
Me.Label2.Text = "One cup: Quantity after 24 hours:"
'
'txtOneCup
'
Me.txtOneCup.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
Me.txtOneCup.Location = New System.Drawing.Point(172, 120)
Me.txtOneCup.Name = "txtOneCup"
Me.txtOneCup.ReadOnly = True
Me.txtOneCup.Size = New System.Drawing.Size(100, 26)
Me.txtOneCup.TabIndex = 2
'
'Label3
'
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
Me.Label3.Location = New System.Drawing.Point(28, 171)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(118, 48)
Me.Label3.TabIndex = 1
Me.Label3.Text = "Hours cups: Quantity after 24 hours:"
'
'txtHoursCup
'
Me.txtHoursCup.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
Me.txtHoursCup.Location = New System.Drawing.Point(172, 171)
Me.txtHoursCup.Name = "txtHoursCup"
Me.txtHoursCup.ReadOnly = True
Me.txtHoursCup.Size = New System.Drawing.Size(100, 26)
Me.txtHoursCup.TabIndex = 2
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 228)
Me.Controls.Add(Me.txtHoursCup)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.txtOneCup)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.txt65mg)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnCalculate)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Caffeine Absorption "
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents btnCalculate As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txt65mg As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtOneCup As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents txtHoursCup As System.Windows.Forms.TextBox
End Class
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'calculate The number of hours required until 65 mg (one-half the original amount) remain in the body.
Dim numberHours As Integer = 0
Dim amount As Double = 130
While amount >= 65
amount -= amount * 13 / 100
numberHours += 1
End While
txt65mg.Text = numberHours.ToString()
'The amount of caffeine in the body 24 hours after the person drinks the coffee.
amount = 130
numberHours = 0
While numberHours < 24
amount -= amount * 13 / 100
numberHours += 1
End While
txtOneCup.Text = Math.Round(amount, 2).ToString() + " mg"
'c. Suppose the person drinks a cup of coffee at 7:00 a.m. the next day. How much caffeine will be in the body at the end of the 24 hours?
amount = 130
numberHours = 0
While numberHours < 24
amount -= amount * 0.13
amount += 130
numberHours += 1
End While
txtHoursCup.Text = Math.Round(amount, 2).ToString() + " mg"
End Sub
End Class
Comments
Leave a comment