Expand All Collapse All


Triangle
Should I write one single complex boolean expression to check for valid triangles?
No. That would make your program difficult to read, debug, and maintain. Instead, break up a complicated expression into individual components. For example, define a boolean variable arePositive to be true if a, b, and c are all positive, and false otherwise. Then, you can use this boolean variable (and, perhaps, others) to define a boolean variable isValidTriangle that checks for valid triangles.
What does System.out.println(x) do if x is of type boolean?
It prints either true or false, according to the value of x.
Will the last command-line argument always correspond to the longest side?
No, do not make this assumption.
FahrenheitToCelsius
How do I round a floating-point number x to the nearest integer?
There are a few viable approaches: Since the assignment specification doesn't tell you what to do when the fractional part is 0.5, you are free to round it either up or down to the nearest integer.
ColorContrast
How do I round a floating-point number x to the nearest integer?
There are a few viable approaches: Since the assignment specification doesn't tell you what to do when the fractional part is 0.5, you are free to round it either up or down to the nearest integer.
What is the class TextBackgroundDrawer.java for?
This class allows you to visualize how two colors interact by drawing the text "Hello World!" over a background. To use it, you will need to compile it

    ~/Desktop/conditionals> javac-introcs TextBackgroundDrawer.java
    
and then execute it with six integer command-line arguments: three RGB values for the text color and three RGB values for the background color. A sample execution is below,

    ~/Desktop/conditionals> java-introcs TextBackgroundDrawer 28 54 114 27 252 235
    
Hello World! written using high contrast text and background colors.
I'm getting lots of checkstyle errors like The numeric literal '0.03928' appears more than once. Define a constant variable to refer to the number '0.03928'.
What's wrong with my code?
We consider many repetitions of the same literal to be bad style for a couple of reasons: first, it is quite error-prone and the bugs it introduces can be very hard to find. (One might spend hours -- or days! -- trying to fix what looks like a logical error, only to realize that the 73th appearance of 0.03928 is mistyped as 0.03298.)

Second, the code may become difficult to read and maintain: if you ever need to change a constant in one of the formulas, it's much easier to change it only in one place. By declaring a variable like LUMINANCE_THRESHOLD, assigning it the value 0.03928 and referring to the variable instead of the literal, your code becomes easier to update and more readable.