C# .NET Exercise Answers
Exercise A
int answerSubtract;
int numberOne = 12;
int numberTwo = 4;
answerSubtract = 50 - numberOne - numberTwo;
MessageBox.Show(answerSubtract.ToString());
Exercise B
Your first answer should be 150. Because the variables were set up as int, however, your second answer should be 100. Try changing the int variables to float and you'll get an answer of 150. This is the answer you would expect because 75 divided by 50 is 1.5. 1.5 multiplied by 100 is 150. If you set your variables up as int then the .5 of 1.5 gets chopped off, leaving just 1 to multiplied by 100.
Exercise C
private void btnPoint_Click(object sender, EventArgs e) {
txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
}
Exercise D
View the code for the calculator: calculator1.txt
Exercise E
View the code for the calculator: calculator2.txt
Exercise F
int age;
age = int.Parse(textBox1.Text);
if (age < 17) {
MessageBox.Show("Still a youngster.");
}
else if (age >= 16 && age <= 24) {
MessageBox.Show("Fame beckons!");
}
else if (age >=25 && age < 40 ) {
MessageBox.Show("There's still time.");
}
else {
MessageBox.Show("Oh dear, you've probably missed it!");
}
Exercise G
Extra points if you spotted that that the loopEnd could be i < (loopEnd + 1)
Exercise H
int answer = 0;
int loopStart;
int loopEnd;
loopStart = int.Parse(textBox1.Text);
loopEnd = int.Parse(textBox2.Text);
int multiplyBy = int.Parse(textBox3.Text);
listBox1.Items.Clear();
for ( int i = loopStart; i <= loopEnd; i++) {
answer = multiplyBy * i;
listBox1.Items.Add( i + " times " + multiplyBy + " = " +
answer.ToString() );
}
Exercise I
The letter count never gets beyond zero because of this line:
letter = strText.Substring(0, 1);
Substring(0, 1) means start at the first character in the string (character 0) and grab 1 character. You need to use the loop variable to move through the text:
letter = strText.Substring(i, 1);
Exercise J
MULTIPLY BUTTON:
int number1;
int number2;
int returnValue = 0;
number1 = int.Parse(textBox1.Text);
number2 = int.Parse(textBox2.Text);
returnValue = Multiply(number1, number2);
MessageBox.Show(returnValue.ToString());
MULTIPLY METHOD
private int Multiply(int firstNumber, int secondNumber) {
int answer;
answer = firstNumber * secondNumber;
return answer;
}
DIVIDE BUTTON
int number1;
int number2;
int returnValue = 0;
number1 = int.Parse(textBox1.Text);
number2 = int.Parse(textBox2.Text);
returnValue = Divide(number1, number2);
MessageBox.Show(returnValue.ToString());
DIVIDE METHOD
private int Divide(int firstNumber, int secondNumber) {
int answer;
answer = firstNumber / secondNumber;
return answer;
}
Exercise K
array[0] doesn't get used because the for loop is starting at 1.
Exercise L
There are lots of ways to do this, but the answer below follows on from the previous question. It sets the i variable to 0 on the first line of the loop. We then use (i + 1) inside the loop. Can you see why we'd need to do this?
listBox1.Items.Clear();
int[ ] aryTimes;
aryTimes = new int[10];
int times = int.Parse(textBox1.Text);
for (int i = 0; i != (aryTimes.Length); i++)
{
aryTimes[i] = (i + 1) * times;
listBox1.Items.Add(times + " times " + (i + 1) + " = " +
aryTimes[i]);
}
Exercise M
string emailAddress = "enquiry@me.co.uk";
string result = "";
result = emailAddress.Substring(11, 5);
if (result == "co.uk") {
MessageBox.Show("Email Address OK " + result);
}
else {
MessageBox.Show("Bad Email Address " + result);
}
Exercise N
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
if (listBox1.SelectedIndex == 1) {
loadListBox();
}
if (listBox1.SelectedIndex == 0) {
loadListBoxCheques();
}
}
private void loadListBox() {
listBox2.Items.Clear();
listBox2.Items.Add("American Express");
listBox2.Items.Add("Discover");
listBox2.Items.Add("Mastercard");
listBox2.Items.Add("Visa");
}
private void loadListBoxCheques() {
listBox2.Items.Clear();
listBox2.Items.Add("Business Cheque");
listBox2.Items.Add("eCheque");
listBox2.Items.Add("Personal Cheque")
}
Exercise O
View the Happy Birthday Class: Birthday Class
Exercise P
private void labelUpdate() {
label4.Text = "Record " + (inc + 1) + " of " + MaxRows;
}
Call it from the NavigateRecords method and the Form Load event.