Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Provide the answers in the text box below the question. Consider the code segment below assuming that the variables x, y and z are integer

image text in transcribed
image text in transcribed
Provide the answers in the text box below the question. Consider the code segment below assuming that the variables x, y and z are integer values. Given values for the variables x,y and z, the code segment correctly determines whether x,y and z are all equal, whether onlyx andy are equal, whether onlyx and z are equal, whether onlyy and z are equal or whether x,y and z are all different. Code Segment: 1. if (x == y && y == z) 2. System.out.println("x,y and zequal"); 3. else if (x ==y) 4. System.out.println("only x and y are equal"); 5. else if (x == z) 6. System.out.println("only x and z are equal"); 7. else if (y == z) 8. System.out.println("only y and z are equal"); 9. else 10. System.out.println("x,y and z are all different"); (A) Show that this code segment is suboptimal by giving a set of values for the variables x, yandz that causes a relational expression in the code segment to be evaluated unnecessarily. Use integer values between-5 and 5 for the set of values. Using the line numbers in the code, give a detailed explanation of which relational expressions are evaluated and the order in which they are evaluated up to and Time left 0.45:39 when the unnecessary evaluation cuplain which one is unnecessarvevaluatedad 10 AM ADO O type here to search DOLL 2-2 (A) Show that this code segment is suboptimal by giving a set of values for the variables x, y and z that causes a relational expression in the code segment to be evaluated unnecessarily. Use integer values between -5 and 5 for the set of values. Using the line numbers in the code, give a detailed explanation of which relational expressions are evaluated and the order in which they are evaluated up to and including when the unnecessary evaluation occurs. Explain which one is unnecessarily evaluated and why. [10 points) (B) Suppose E denotes the average number of relational expressions that is evaluated per outcome (output) of a code segment involving if-statement(s). Complete the chart below and then calculate E for the code segment above. (10 points] Chart Outcome Number of Relational Expressions Evaluated Ky and Z are equal only and y are equal only and are equal only y and are equal X Y and Z are all different (C) Give an optimal version of the code segment above that eliminates the potential for unnecessary evaluations of relational expressions. Do not use any additional relational operators or arithmetic operators, such as etc, in this version of the code segment. (10 points) (D) Write and complete a chart similar to the one given in this exercise then calculate for the optimal version of the code segment that you wrote in (C). [10 points) Provide the answers in the text box below the question. Consider the code segment below assuming that the variables x, y and z are integer values. Given values for the variables x,y and z, the code segment correctly determines whether x,y and z are all equal, whether onlyx andy are equal, whether onlyx and z are equal, whether onlyy and z are equal or whether x,y and z are all different. Code Segment: 1. if (x == y && y == z) 2. System.out.println("x,y and zequal"); 3. else if (x ==y) 4. System.out.println("only x and y are equal"); 5. else if (x == z) 6. System.out.println("only x and z are equal"); 7. else if (y == z) 8. System.out.println("only y and z are equal"); 9. else 10. System.out.println("x,y and z are all different"); (A) Show that this code segment is suboptimal by giving a set of values for the variables x, yandz that causes a relational expression in the code segment to be evaluated unnecessarily. Use integer values between-5 and 5 for the set of values. Using the line numbers in the code, give a detailed explanation of which relational expressions are evaluated and the order in which they are evaluated up to and Time left 0.45:39 when the unnecessary evaluation cuplain which one is unnecessarvevaluatedad 10 AM ADO O type here to search DOLL 2-2 (A) Show that this code segment is suboptimal by giving a set of values for the variables x, y and z that causes a relational expression in the code segment to be evaluated unnecessarily. Use integer values between -5 and 5 for the set of values. Using the line numbers in the code, give a detailed explanation of which relational expressions are evaluated and the order in which they are evaluated up to and including when the unnecessary evaluation occurs. Explain which one is unnecessarily evaluated and why. [10 points) (B) Suppose E denotes the average number of relational expressions that is evaluated per outcome (output) of a code segment involving if-statement(s). Complete the chart below and then calculate E for the code segment above. (10 points] Chart Outcome Number of Relational Expressions Evaluated Ky and Z are equal only and y are equal only and are equal only y and are equal X Y and Z are all different (C) Give an optimal version of the code segment above that eliminates the potential for unnecessary evaluations of relational expressions. Do not use any additional relational operators or arithmetic operators, such as etc, in this version of the code segment. (10 points) (D) Write and complete a chart similar to the one given in this exercise then calculate for the optimal version of the code segment that you wrote in (C). [10 points)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

=+decisionNature and what in Nature is impacted by your decision?

Answered: 1 week ago