Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following conditional statement for which int variables side1, side2, and side3 have previously been declared and assigned values. It will help us determine

Consider the following conditional statement for which int variables side1, side2, and side3 have previously been declared and assigned values. It will help us determine whether sticks with lengths side1, side2, and side3 can be used to make a triangle because the sum of the lengths of any two sides must be greater than the length of the third side.

boolean triangle = true; if (side1 + side2 <= side3) { triangle = false; } if (side1 + side3 <= side2) { triangle = false; } if (side2 + side3 <= side1) { triangle = false; }

System.out.println("Do we have a triangle? " + triangle); Answer the following questions.

For each of the following two cases, what is printed out?

a) the value of side1 is 27, the value of side2 is 12, and the value of side3 is 41

b) the value of side1 is 8, the value of side2 is 12, and the value of side3 is 6

If any of the sides have a negative or zero value, we can never form a triangle. Describe how you would modify the code to print out "No triangle is possible" when that happens.

Challenge: Attempt only after completing the remainder of the exam: Rewrite the entire code, including what you added in the previous question, to use the keyword if as infrequently as possible.

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

Transport Operations

Authors: Allen Stuart

2nd Edition

978-0470115398, 0470115394

Students also viewed these Programming questions