Question

In: Computer Science

private void btnCalculate_Click(object sender, System.EventArgs e) { decimal weightInPounds = 0m; try { weightInPounds = Convert.ToDecimal(txtPounds.Text);...

private void btnCalculate_Click(object sender, System.EventArgs e) { decimal weightInPounds = 0m; try { weightInPounds = Convert.ToDecimal(txtPounds.Text); if (weightInPounds > 0) { decimal weightInKilos = weightInPounds / 2.2m; lblKilos.Text = weightInKilos.ToString("f2"); } else MessageBox.Show("Weight must be greater than 0.", "Entry error"); txtPounds.Focus(); } catch(FormatException) { MessageBox.Show("Weight must be numeric.", "Entry error"); txtPounds.Focus(); } } (Refer to code example 7-1.) If the user enters 118 in the text box and clicks the Calculate button, what does the code do?

Solutions

Expert Solution

/* when calculate button clicks it saves lblKilos.Text with value 53.64 */

/* BELOW DOWN THERE IS COMMENTS FOR EVERY STATEMENT */

private void btnCalculate_Click(object sender, System.EventArgs e)
{    decimal weightInPounds = 0m;
   try {
           weightInPounds = Convert.ToDecimal(txtPounds.Text); // returns 118m
           if (weightInPounds > 0) { // true 118m > 0
               decimal weightInKilos = weightInPounds / 2.2m; // 118m/2.2m = 53.63636363636363
               lblKilos.Text = weightInKilos.ToString("f2"); // 53.64 // round to 2 decimal places
           }else
               MessageBox.Show("Weight must be greater than 0.", "Entry error");
           txtPounds.Focus(); // it highlights textbox
       } catch(FormatException) {
           MessageBox.Show("Weight must be numeric.", "Entry error");
           txtPounds.Focus();
       }
}


Related Solutions

1_ What is the output? private void btnMethods_Click(object sender, EventArgs e) { int arg1 = 2;...
1_ What is the output? private void btnMethods_Click(object sender, EventArgs e) { int arg1 = 2; double val = ValReturnMethod(arg1, 2.00); MessageBox.Show(val.ToString()); } private void ValReturnMethod(int val, double val2) { return val1 * val2 *25.50; } _____________________________ 2_ What is the output? private void btnMethods_Click(object sender, EventArgs e) { int arg1 = 4; double val = ValReturnMethod(ref arg1); MessageBox.Show(arg1.ToString()); } private double ValReturnMethod(ref int val1) { return val1 *25.50; } ______________________________ 3_ What is the output? private void btnMethods_Click(object sender,...
code in. c++ void seen ); If there is already a Word object in the Words...
code in. c++ void seen ); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. getNextWord(); Returns the next word of the list and sets the currentItem pointer to the next Word. This may return an empty string, “”, if...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] = 0;} catch(ArithmeticException e){array[2] = 0} // ? finally{ return 0;}} Could you please tell me why the line with the question mark will not report an error?
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the function void getDigit( intnum) so that it displays the digits in a given number. For example, the number, 1234 consist of 1, 2, 3 and 4. This is an exercise to demonstate the use of a recursive function and the use of recusrion in C++ */ #include #include using namespace std; int main() {      int num;      cout <<"Enter an integer: ";     ...
3. Assume that binary trees are defined as public class BTNode<E> { private E data; private...
3. Assume that binary trees are defined as public class BTNode<E> { private E data; private BTNode<E> left, right; } We say that a binary tree t1 is a prefix of a binary tree t2 if t2 can be made identical to t1 by removing zero or more leaves of t2. Write a method
void main() {     Grade g1; //object for grade     char h;     cout << "enter...
void main() {     Grade g1; //object for grade     char h;     cout << "enter character" ;     cin>>h; //getting value     g1.grade(h);     g1.print();    getch(); } create an output file (named “output.txt”), and save the character to this output file. Last, close this output file
1. In Object Oriented Programming The private object fields can be directly manipulated by outside entities....
1. In Object Oriented Programming The private object fields can be directly manipulated by outside entities. Group of answer choices A. True B. False 2. __________ programming is centered on the procedures or actions that take place in a program. Group of answer choices A. Class B. Object-oriented C. Menu-driven D. Procedural/ Structural 3. __________ programming encapsulates data and functions in an object. Group of answer choices A. Object-oriented B. Class C. Menu-driven D. Procedural 4. The data in an...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
Try the following: get some stuff: a small ball (or some kind of object that will...
Try the following: get some stuff: a small ball (or some kind of object that will roll - a golf ball or marble or toy car is great, but an empty soup can will do in a pinch) get a tape measure (a yardstick or a ruler will also work. You can also stretch a piece of string and mark off ruler lengths on the string to get the total length.) around ten coins Measure the distance from a tabletop...
(Sort ArrayList) Write the following method that sorts an ArrayList:   public static <E extends Comparable<E>> void...
(Sort ArrayList) Write the following method that sorts an ArrayList:   public static <E extends Comparable<E>> void sort(ArrayList<E> list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT