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

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: ";     ...
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...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
(20 pts) In the following code inside a try block, what kind of object is passed...
(20 pts) In the following code inside a try block, what kind of object is passed to the Scanner constructor? Scanner file = new Scanner ( new File (“test.txt”)); (20 pts) What is the output of this code sequence? Scanner parse = new Scanner (“A 1 B 2 C 3 D”); while (parse.hasNext())     System.out.print(parse.next()); (20 pts) What is wrong with following code? try { Scanner file = new Scanner(new File(“c:\docs\data.txt”)); int n = 0; while (file.hasNext()) { String s...
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
A sample of soil was tested; the results were void ratio e = 1.024 moisture content...
A sample of soil was tested; the results were void ratio e = 1.024 moisture content w = 33.4% specific gravity of solids Gs = 2.75 Using a phase diagram, determine the following: a) The soil's bulk, dry and saturated unit weights, and degree of saturation; b) The volume of air void in 1m^3 of this soil; and c) The weight of water required to fully saturate the 1m^3 soil
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT