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.
Triangle.java
that takes three int
command-line arguments prints true
if they constitute the side lengths of some
triangle, and false
otherwise.
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
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:
Next, write a program~/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.
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:
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.
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:
Contrast ratio: 2.9406909147122167
.These colors do not provide sufficient color contrast.
These colors may not provide sufficient color contrast.
These colors provide sufficient color contrast.
~/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.
~/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.
~/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.
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 |
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".