Evaluating and Writing Expressions that Contain the Ternary Conditional Operator
Evaluating Expressions
|
Grab a Piece of Paper! |
Determine the value of each expression. Verify your answer by running the code in the Java Playground. |
Writing Expressions
Your Turn
- Write program code to complete the provided tasks.
- Run the code in the Java Program to see if your code generates the desired result.
- Make necessary changes until you have satisfied the tasks.
Practice 7: Greeting Based on Time
- Write a ternary conditional operator statement that prints either
Good morningifhouris less than 12 orGood afternoonotherwise. - Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 8: Proper Display Based on Count
- Write a ternary conditional operator statement that prints either
itemifcountis 1 oritemsotherwise. - Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 9: Proper Display Based on Count
- Write a ternary conditional operator statement that calculates the price differently if you are a member or not. If
isMemberistruethe price is 90% of the original price. IfisMemberisfalsethe price is the original price. - Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.
Practice 10: Determine Tax Rate
- Write a nested ternary conditional operator statement that assigns
taxRatebased onincomeand the following rules:- if
incomeis less than 10000, thetaxRateis 10% - if
incomeis greater than or equal to 10000 and less than 20000, thetaxRateis 15% - if
incomeis greater than or equal to 20000 and less than 50000, thetaxRateis 20% - in all other cases, the
taxRateis 25%
- if
- Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
- Try the code in the Java Playground.