CSC221 Lecture 3
CSC221 Lecture 3
Author
Tony Gaddis
At run time, when you click the addButton control, the names shown in the
image on the right are added to the nameListBox control.
6
The while loop
7
The while loop
8
Classwork 1: Using Loop to Calculate and account Balance
When you complete the application, it will allow the user to enter
an account’s starting balance into the startingBalTextBox control
and the number of months that the account will be left to earn
interest into the monthsTextBox control.
When the user clicks the calculateButton control, the application
calculates the account’s balance at the end of the time period.
The account’s monthly interest rate is 0.005, and the interest is
compounded monthly.
9
Solution
Design the form and use the
started names for all the
controls as depicted in the
diagram
10
private void calculateButton_Click(object sender, EventArgs e)
{
// Constant for the monthly interest rate.
const decimal INTEREST_RATE = 0.005m;
// Local variables
decimal balance; // The account balance
int months; // The number of months
int count = 1; // Loop counter, initialized with 1
Solution 11
// Add one to the loop counter.
count = count + 1;
} // end of while.
// Display the ending balance.
endingBalanceLabel.Text = balance.ToString("c");
} // second if ends here.
else {
// Invalid number of months was entered.
MessageBox.Show("Invalid value for months.");
}
} // first if ends here.
else {
// Invalid starting balance was entered.
MessageBox.Show("Invalid value for starting balance.");
}
} // function ends here.
Solution (Cont’d) 12
The ++ and -- operator
13
The for loop
15
Classwork 2 - The for loop
Your friend Amanda just inherited a European sports car from her uncle. Amanda lives in
the United States, and she is afraid she will get a speeding ticket because the car’s
speedometer works in kilometers per hour.
She has asked you to write a program that displays a table of speeds in kilometers per
hour with their values converted to miles per hour.
The formula for converting kilometers per hour to miles per hour is:
16
Classwork 2 (Cont’d) - The for loop
In the formula, MPH is the speed in miles per hour and KPH
is the speed in kilometers per hour.
A combo box is like a list box that has been combined with a text
box.
There are three different styles of combo boxes:
Drop-down combo box
Simple combo box
Drop-down list combo box.
You can select a combo box’s style with its DropDownStyle property.
20
With the drop-down list combo box
style, the user may not type text
With the simple style of combo This is the default setting for the directly into the combo box. An item
box, the list of items does not drop combo box Drop Down style must be selected from the list. At run
down but is always displayed. property. time, a drop-down list combo box
As with the drop-down combo box, At run time, a drop-down combo box appears, as shown in the image on the
this style allows the user to select an like the one shown in the middle left in the rightmost figure. When the
item from the list or type text appears. When the user clicks the user clicks the down arrow, a list of
directly into the text box area. down arrow, a list drops down as items appears, as shown in the image
shown in the image on the right in the on the right in the figure.
figure.
“Remove Item” removes an item from the ListBox while Use the “Items” property to fill up
“Clear” removes all the item in the ListBox at once. a ComboBox with items
28
Random Numbers
The .NET Framework provides a class named Random that you can use in C# to generate
random numbers. First you create an object from the Random class with a statement such
as this:
Random rand = new Random();
29
The Next Method
Once you have created a Random object, you can call its Next method to get a random
integer number. The following code shows an example:
// Declare an int variable.
int number;
// Create a Random object.
Random rand = new Random();
// Get a random integer and assign it to number.
number = rand.Next();
30
The NextDouble Method
You can call a Random object’s NextDouble method to get a random floating-point
number between 0.0 and 1.0 (not including 1.0). The following code shows an example:
// Declare a Double variable.
double number;
// Create a Random object.
Random rand = new Random();
// Get a random number and assign it to number.
number = rand.NextDouble();
31
Classwork 4
(Random Number)
Create an application that simulates the tossing of a coin. Each time the user tosses the
coin, the application uses a Random object to get a random integer in the range of 0
through 1.
If the random number is 0, it means the tails side of the coin is up, and if the random
number is 1, it means the heads side is up.
The application displays an image of a coin showing either heads or tails, depending on the
value of the random number.
32
Solution
33
private void tossButton_Click(object sender, EventArgs e)
{
// Variable to indicate which side is up
int sideUp;
// Create a Random object.
Random rand = new Random();
// Get a random integer in the range of 0 through 1, 0 means tails up, 1 means heads up.
sideUp = rand.Next(2);
// Display the side that is up.
if (sideUp == 0){
// Display tails up.
tailsPictureBox.Visible = true;
headsPictureBox.Visible = false;
}
else{
// Display heads up.
headsPictureBox.Visible = true;
tailsPictureBox.Visible = false;
}
}
Solution 34
Review Questions
Question 1
If you know a vehicle’s speed and the amount of time it has traveled, you can calculate the
distance it has traveled as follows:
For example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles.
Create an application with a form similar to the one shown in the figure on the next slide.
The user enters a vehicle’s speed and the number of hours traveled into text boxes. When the
user clicks the Calculate button, the application should use a loop to display in a list box the
distance the vehicle has traveled for each hour of that time period.
Figure for Question 1 37
Question 2
Create an application that displays a table of the Celsius temperatures 0–20 and their
Fahrenheit equivalents. The application should use a loop to display the
temperatures in a list box.
38
Question 3
World Population
The United States Census Bureau (USCB) had estimated the current world population to be
around 7 billion in the current year.
The world population is, at present, growing at a rate of around 1.14 percent per year.
Create an application that projects the estimated world population over the coming years.
The application allows the user to enter the number of years to be projected, and calculates
the total global population at that period of time.
39
Question 4
40
Question 5
Dice Simulator
Create an application that simulates rolling a pair of dice. When the user clicks a button,
the application should generate two random numbers, each in the range of 1 through 6, to
represent the value of the dice.
If the user’s guess is lower than the random number, the program should display
“Too low, try again.”
42
Question 6 (Cont’d)
If the user guesses the number, the application should congratulate the user and
then generate a new random number so the game can start over.
43