The purpose of this assignment is to introduce you to programming with built-in data types (strings, integers, floating-point numbers, and booleans) and conditionals.


  1. Integers and booleans. Write a program Triangle.java that takes three int command-line arguments prints true if they constitute the side lengths of some triangle, and false otherwise.

    triangle with three sides marked a, b, c.

    The following two conditions are necessary and sufficient:

    ~/Desktop/conditionals> javac-introcs Triangle.java
    ~/Desktop/conditionals> java-introcs Triangle 3 4 5
    true
    ~/Desktop/conditionals> java-introcs Triangle 13 12 10
    true
    ~/Desktop/conditionals> java-introcs Triangle 20 4 12
    false
    ~/Desktop/conditionals> java-introcs Triangle 1 2 3
    false
    ~/Desktop/conditionals> java-introcs Triangle -3 -4 -5
    false
    


  2. Conversion of units of measurement. Several different units of measurement are used across the world. Some are standard in scientific contexts (e.g., Kelvin for temperature and the metric system for length), while others are common in some countries but virtually unknown elsewhere. (Some examples: stone, mil, arroba; and lots more!) Fahrenheit as a unit of temperature is another good example: although very common in the US, most of the world uses the Celsius scale. The two formulas below show how to convert from \(x\) degrees Fahrenheit to \(y\) degrees Celsius, and vice-versa: \[y = \frac{5}{9} \cdot (x - 32)\] \[x = \frac{9}{5} \cdot y + 32\]

    Write a program FahrenheitToCelsius.java that converts an integer value in Fahrenheit to Celsius using the formula above, rounding the answer to the nearest integer. Your program must take one int command-line argument \(x\) and print the corresponding temperature \(y\) in Celsius. It must also check if the input is smaller than absolute zero, \(-459.67^\circ\) F, and if so, print Invalid input: temperature smaller than absolute zero. instead of attempting to convert.

    Format the output as in the following sample executions:

    ~/Desktop/conditionals> javac-introcs FahrenheitToCelsius.java
    ~/Desktop/conditionals> java-introcs FahrenheitToCelsius 32    // freezing temperature of water
    32 Fahrenheit = 0 Celsius
    ~/Desktop/conditionals> java-introcs FahrenheitToCelsius 90
    90 Fahrenheit = 32 Celsius
    ~/Desktop/conditionals> java-introcs FahrenheitToCelsius 8    // lowest recorded temperature in Princeton
    8 Fahrenheit = -13 Celsius
    ~/Desktop/conditionals> java-introcs FahrenheitToCelsius -500
    Invalid input: temperature smaller than absolute zero.
    
    Next, write a program MetricToImperial.java that converts height measured in meters to feet and inches, the units used to measure a person's height in the US. (Your international friends will thank you!) Since a foot corresponds to 0.3048m (30.48cm) and an inch to 0.0254m (2.54cm), we can convert a height \(h\) expressed in meters to feet and inches as follows:

    1. Evenly divide \(h\) by 0.3048; the quotient \(f\) is the number of of (whole) feet. Call the remainder \(r\). (Note that \(h = 0.3048f + r\).)
    2. Divide \(r\) by 0.0254, rounding the answer to the nearest integer. The result \(i\) is the number of inches.

    Your program must take one double command-line argument \(h\) and print the corresponding height in feet and inches. It must also check if the input is negative, and if so, print Invalid input: height must be non-negative. instead of attempting to convert. Format the output as in the following sample executions:

    ~/Desktop/conditionals> javac-introcs MetricToImperial.java
    ~/Desktop/conditionals> java-introcs MetricToImperial 2.29    // Yao Ming's height, the tallest NBA player in history
    7 ft 6 in
    ~/Desktop/conditionals> java-introcs MetricToImperial 1.75    // average height of US men
    5 ft 9 in
    ~/Desktop/conditionals> java-introcs MetricToImperial 1.61    // average height of US women 
    5 ft 3 in
    ~/Desktop/conditionals> java-introcs MetricToImperial 1.5    // average height of Nepalese women 
    4 ft 11 in
    ~/Desktop/conditionals> java-introcs MetricToImperial -1
    Invalid input: height must be non-negative.
    


  3. Color Contrast. When presenting content, a crucial design principle is to ensure that users with a wider range of vision abilities can read text that is presented over some background. This can be done by ensuring sufficient color contrast between the text and background colors, as described by the book Teaching Accessible Computing.

    Now, in digital form, there exist several different formats to represent color. The primary format for web pages — known as the RGB format — specifies the level of red (R), green (G), and blue (B) on an integer scale from 0 to 255 inclusive. We will write a program ColorContrast.java that receives two colors in RGB format, computes their contrast ratio, and outputs a recommendation according to the Web Content Accessibility Guidelines.

    Your program must take six command line arguments: \(\texttt{redText}, \texttt{greenText}, \texttt{blueText}\) (red, green and blue text color levels) and \(\texttt{redBackground}, \texttt{greenBackground}, \texttt{blueBackground}\) (red, green and blue blackground color levels). It must perform the following steps:

    1. For each color level \(v \in \{\texttt{redText}, \texttt{greenText}, \texttt{blueText}, \texttt{redBackground}, \texttt{greenBackground}, \texttt{blueBackground}\}\), compute \(\texttt{luminance}_v\) using the following logic: \[\texttt{luminance}_v = \left\{\begin{array}{ll}\frac{v}{3,294.6}, &\text{if } \frac{v}{255} \leq 0.03928,\\ \left(\frac{\frac{v}{255} + 0.055}{1.055}\right)^{2.4}, &\text{otherwise.}\end{array}\right.\]
    2. Compute the relative luminance of the text and background using the following expressions:
    3. \[\texttt{luminanceText} = 0.2126 \cdot \texttt{luminance}_{\texttt{redText}} + 0.7152 \cdot \texttt{luminance}_{\texttt{greenText}} + 0.0722 \cdot \texttt{luminance}_{\texttt{blueText}}\] \[\texttt{luminanceBackground} = 0.2126 \cdot \texttt{luminance}_{\texttt{redBackground}} + 0.7152 \cdot \texttt{luminance}_{\texttt{greenBackground}} + 0.0722 \cdot \texttt{luminance}_{\texttt{blueBackground}}.\]
    4. Call \(c\) the lighter and \(c'\) the darker color between \(\texttt{luminanceText}\) and \(\texttt{luminanceBackground}\) (i.e., let \(c\) be the larger of the two, tiebreaking arbitrarily if they are equal). Calculate the contrast ratio
    5. \[\texttt{contrastRatio} = \frac{c + 0.05}{c' + 0.05}.\]
    6. Print the contrast ratio, formatted as Contrast ratio: 2.9406909147122167.
    7. Given the contrast ratio, output the following recommendations:
      • If \(\texttt{contrastRatio} < 4.5\), output These colors do not provide sufficient color contrast.
      • If \(4.5 \leq \texttt{contrastRatio} < 7\), output These colors may not provide sufficient color contrast.
      • If \(\texttt{contrastRatio} \geq 7\), output These colors provide sufficient color contrast.

    We show some sample executions below:

    ~/Desktop/conditionals> javac-introcs ColorContrast.java
    ~/Desktop/conditionals> java-introcs ColorContrast 255 143 0 255 255 255    // text is Princeton orange, background is white
    Contrast ratio: 2.2873364530973834
    These colors do not provide sufficient color contrast.
    
    Hello World! written with Princeton orange letters on a white background.
    ~/Desktop/conditionals> java-introcs ColorContrast 229 9 20 255 255 255    // text is Netflix red, background is white
    Contrast ratio: 4.79366807218479
    These colors may not provide sufficient color contrast.
    
    Hello World! written with Netfilx red letters on a white background.
    ~/Desktop/conditionals> java-introcs ColorContrast 28 54 114 27 252 235    // from the Teaching Accessible Computing book
    Contrast ratio: 8.884720491426096
    These colors provide sufficient color contrast.
    
    Hello World! written using high contrast text and background colors.

    Submission. Submit the Java files Triangle.java, FahrenheitToCelsius.java, MetricToImperial.java, and ColorContrast.java. Also submit a readme.txt file and answer the questions. You may not call library functions except those in java.lang (such as Integer.parseInt() and Math.pow()). Do not use Java features that have not yet been introduced in the course (such as arrays or functions).


    Grading.
    File Points
    Triangle.java 8
    FahrenheitToCelsius.java 8
    MetricToImperial.java 8
    ColorContrast.java 8
    readme.txt 8
    Total 40

    This assignment has been developed by Sebastian Caldas, Marcel Dall'Agnol and Kevin Wayne. The Color Contrast problem was based on:
    Alannah Oleson, Amy J. Ko, Richard Ladner (2024). Teaching Accessible Computing. https://bookish.press/, retrieved 5/24/2024.
    It is based on the "Color contrast checker" example assignment, in "Introductory Programming (CS1) + Accessibility".
    Copyright © 2024.
    Credits.