1 / 7

Chapter 3 Practice

Chapter 3 Practice. Practice with if statement. Write an if statement that assigns 0 to x when y is equal to 20. Write an if statement that multiply salary by 1.5 if hours is greater than 40. Write an if statement that assigns 30000 to fine if boolean variable add_swap is true.

lebeau
Télécharger la présentation

Chapter 3 Practice

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Practice

  2. Practice with if statement • Write an if statement that assigns 0 to x when y is equal to 20. • Write an if statement that multiply salary by 1.5 if hours is greater than 40. • Write an if statement that assigns 30000 to fine if boolean variable add_swap is true. • Write an if statement that assigns 0 to b and 1 to c if a is less than 10

  3. Practice with if statement • Write an if statement that displays “Has dollar sign” if String variable s contains the character ‘$’. • Write an if-else statement that assigns 20 to y if x is greater than 100. Otherwise, it assigns 0 to y. • Write an if-else statement that assigns 1 to x when y is equal to 100. Otherwise, it assigns 0 to x. • Write an if-else statement that assigns 0.1 to commision unless sales is greater than or equal to 50000, in which case it assigns 0.2 to commission. • Write an if-else statement that assigns 0 to b and assigns 1 to c if a is less than 10. Otherwise, it assigns -99 to b and 0 to c.

  4. Practice with boolean expression • One runner runs d1 meters in t1 seconds, another runner runs d2 meters in t2 seconds. Declare a boolean variable isFaster that has value true if the first runner runs faster than the second. • Given 3 integers a, b and c. Declare a boolean variable isTriangle that has value true if a, b, c can be the lengths of 3 sides of a triangle. • Given a circle with center at (0, 0) and radius r, and given a point (a, b). Declare a boolean variable isInside that has value true if the point (a, b) is inside the circle. • Given a circle with center at (0, 0) and radius r, and 3 integer coefficients a, b, c of equation of a line ax + by + c = 0. Declare a boolean variable isTangent that has value true if the line ax + by + c = 0 touches the circle at exactly one point.

  5. Practice with switch statement • Write a switch statement for variable ch of type char, based on its value as below: ch=‘A’: diplays “Excellent” ch=‘B’: diplays “Very Good” ch=‘C’: diplays “Good” ch=‘D’: diplays “Fair” ch=‘F’: diplays “Fail” Otherwise: displays “Not a grade”

  6. Standard 52-card Deck Heart Queen DiamondKing Heart Club DiamondJack Diamond Spade Heart Ace

  7. Practice with switch statement • A deck has 52 cards, numbered from 0 to 51 using the following way: Heart: Ace  King, numbered from 0 to 12. Diamond: Ace  King, numbered from 13 to 25. Club: Ace  King, numbered from 26 to 38. Spade: Ace  King, numbered from 39 to 51. Write a program that reads a number from 0 to 51, then displays the name of card. Example: 11  Heart Ten, 45  Spade Five, 38  Club Jack

More Related