c/c++ programming project - program-info.net · 9/28/2015 1 visual basic programming project long...
TRANSCRIPT
9/28/2015
1
VISUAL BASIC
PROGRAMMING PROJECT
Long Distance Call
Long Distance Call DESCRIPTION: Write a program that computes the cost of a long-distance call. The cost of the call is determined according to the following rate schedule:
a. Any call started between 8:00 am and 6:00 pm, Monday through Friday, is billed at a rate of $0.40 per minute.
b. Any call starting before 8:00 am or after 6:00 pm, Monday through Friday, is charged at a rate of $0.25 per minute.
c. Any call started on a Saturday or Sunday is charged at a rate of $0.15 per minute.
The input will consist of the day of the week, the time the call started, and the length of the call in minutes. The output will be the cost of the call. The time is to be input in 24-hour notation, so the time 1:30 pm is input as 13:30
The number of minutes will be input as a value of type Integer. (You can assume that the user rounds the input to a whole number of minutes.)
9/28/2015
2
Analyze the Problem
O Break it up into smaller pieces
O Identify the Inputs and Outputs
O Determine the Processing and any additional variables
O See if anything special needs to be done with the inputs or outputs
Identify the Inputs and Outputs DESCRIPTION:
Write a program that computes the cost of a long-distance call. The cost of the call is determined
according to the following rate schedule: a. Any call started between 8:00 am and 6:00 pm, Monday through Friday, is billed at a rate of $0.40 per minute. b. Any call starting before 8:00 am or after 6:00 pm, Monday through Friday, is charged at a rate of $0.25 per minute. c. Any call started on a Saturday or Sunday is charged at a rate of $0.15 per minute.
The input will consist of the day of the week, the time the call started, and the length of the call in minutes. The output will be the cost of the call.
INPUT PROCESSING OUTPUT
DayOfWeek TimeStarted LengthOfCall
CostOfCall
9/28/2015
3
Determine the Processing DESCRIPTION: Write a program that computes the cost of a long-distance call. The cost of the call is determined according to the following rate schedule:
a. Any call started between 8:00 am and 6:00 pm, Monday through Friday, is billed at a rate of $0.40 per minute. b. Any call starting before 8:00 am or after 6:00 pm, Monday through Friday, is charged at a rate of $0.25 per minute. c. Any call started on a Saturday or Sunday is charged at a rate of $0.15 per minute.
INPUT PROCESSING OUTPUT
DayOfWeek TimeStarted LengthOfCall
If DayOfWeek = Mo through Fr If time >= 800 and time <= 1800 Then rate = 0.40 else if time < 800 or time > 1800 rate = 0.25 Else If DayOfWeek = Sa or DayOfWeek = Su rate = 0.15 CostOfCall = rate * LengthOfCall
CostOfCall
Get The Inputs
The inputs for this program are just a little
complicated:
O DayOfWeek is a collection of RadioButtons. Only
one RadioButtion can be active at a time.
O TimeStarted is input like 7:35 an integer for the
hour, a colon character and an integer for minutes
O LengthOfCall in minutes. This is a simple one. You
input the value using an Integer.
9/28/2015
4
Sample Program Design
Rad
ioB
utt
on
s
Label used for the program's output. Set the AutoSize property to False
Name Each Control (You can use different names, but the names you choose must be consistent the entire program)
radSu
txtLengthOfCall
lblCostOfCall btnCompute
txtCallStart
radMo
radTu
radWe
radTh
radFr
radSa
9/28/2015
5
Create the Button's Event Handler
Display the Design view. Double-click the 'Compute' button to create the code for the Compute button Event Handler.
Define Constants for Billing Rates
Use constants to define the billing rates. Place the constants at the top of the program. Since the billing rates have digits past the decimal, use either the Double or Decimal data type.
9/28/2015
6
Define Variables Used in the Program
Since the hour and minute are separated by a colon character in the user's input (example 17:35 is entered for 5:35pm), the variables for hour and minute will be defined a little later.
Separate the Hour and Minute
The start time is entered by the user into the txtCallStart TextBox in 24-hour format. For example, 5:35pm is entered as 17:35. The TextBox contains a collection of characters. It does not contain numeric values. The program needs to find the colon character and set the hours to the characters before the colon, and the minutes to the characters after the colon. These two character String values can then be converted into Integer values for use by the program.
9/28/2015
7
Define Additional Variables
Define some additional variables that will be used for parsing (separating parts of a string) the Start Time string. Place these variables after the variables that have already been defined.
Find the Position of the Colon ":"
The String library in Visual Basic has several Methods (functions or subroutines) that can be used to manipulate strings. The InStr method is used to find the position of one string within another string. For Example, if the contents of txtCallStart.Text is "17:43"
Dim ColonPosition As Integer
ColonPosition = InStr(txtCallStart.Text, ":")
The variable ColonPosition would be set to a 3 after InStr executes.
17:43
9/28/2015
8
The Mid String Method
The Mid String method is used to extract one string from the middle of another string. There are two ways of calling the Mid string method:
1) With two arguments – The original string is the first argument and the starting position is the second argument. Mid returns all of the characters from the start position to the end of the string. 2) With three arguments – The original string is the first argument the starting position is the second argument, and the number of characters to be returned is the third argument.
The Mid String Method The Mid String method is used to extract one string from the middle of another string. There are two ways of calling the Mid string method:
1) With two arguments – The original string is the first argument and the starting position is the second argument. Mid returns all characters from the start position to the end of the string. 2) With three arguments – The original string is the first argument the starting position is the second argument, and the number of characters to be returned is the third argument.
NewString = Mid(OriginalString, StartPos, CharCount)
Optional
9/28/2015
9
Get the Hour value
The InStr method finds the position of the colon ":" and then Mid is used to get all of the characters up to but not including the ":" For Example, if the contents of txtCallStart.Text is "17:43"
Dim ColonPosition As Integer
Dim HourString As String
Dim Hour As Integer
ColonPosition = InStr(txtCallStart.Text, ":")
HourString = Mid(txtCallStart.Text, 1, ColonPosition - 1)
Hour = CInt(HourString)
17:43
Start Position Use -1 for up to but not including the colon
Get the Minute Value The Mid is used to get all of the characters after the colon ":" until the end of the string. For Example, if the contents of txtCallStart.Text is "17:43"
Dim ColonPosition As Integer Dim HourString As String Dim Hour As Integer Dim MinuteString As String Dim Minute As Integer
ColonPosition = InStr(txtCallStart.Text, ":") HourString = Mid(txtCallStart.Text, 1, ColonPosition - 1) Hour = CInt(HourString) MinuteString = Mid(txtCallStart.Text, ColonPosition + 1) Minute = Cint(MinuteString)
17:43
Use +1 to start after the colon. With only 2 parameters, Mid returns the rest of the string.
9/28/2015
10
Define Out of Range Exception
The InStr, Mid and Cint methods have built-in detection of when they are not able to process their input data. In these cases, they will throw an exception within a Try block that can detected by the Catch block. An out of range value, such as the 57th hour in a day (57:32) will not be caught because the value 57 is a legal integer even though it is out of range for the number of hours in a day. The program can use an If statement to detect out of range values and then use a Throw statement to exit out of the Try block and into the Catch block. The Exception must first be defined.
Dim IllegalTime As Exception = New Exception("Illegal Input Value")
If Hour < 0 Or Hour > 23 Then
Throw IllegalTime
End If
The Program So Far (Includes an Exception for out of range Hour or Minute)
9/28/2015
11
Input the Length Of Call Determine the Billing Rate
These are fairly straight forward. Make sure that you use the names of the RadioButtons the way they were defined when the form was being designed.
Compute and Display the Bill
These are fairly straight forward. Make sure that you use the names of the RadioButtons the way they were defined when the form was being designed.