Homework 1: Hello, World

The goal of this homework is to familiarize you with programming in Java. In particular, you will practice:

Instructions

Task 1: Install IntelliJ On Your PC

Click the link below that matches your PC’s operating system, then follow steps 0 to 5 to install and use IntelliJ. However, download this zip file (hw1.zip) instead of the files linked in Step 1 – you will open the “hw1” folder instead of the “hello” folder.

Note that this version of IntelliJ is bundled with several course-required libraries, so you should follow these instructions even if you installed IntelliJ prior to this course.

Take screenshots of the Barnsley fern and colliding disks – you will include them with your submission.

Note to instructors: Download the lift-java files locally, in case they are too slow to download from Princeton.

Task 2: Reading and Exercises

Task 3: Implement Five Programs

HelloWorld.java

Write a program HelloWorld.java that prints the text “Hello, World”.

Hint: By following the instructions in Task 1, you should have already finished this program!

HiFour.java

This exercise demonstrates the use of the String data type and command-line arguments. Write a program HiFour.java that takes four first names as command-line arguments and prints a proper sentence with the names in the reverse of the order given.

Examples:

> java-introcs HiFour Alice Bob Carol Dave
Hi Dave, Carol, Bob, and Alice.

> java-introcs HiFour Alejandro Bahati Chandra Deshi
Hi Deshi, Chandra, Bahati, and Alejandro.

Hint: The command line arguments can accessed in the args array.

Hint: If you see the following message when running HiFour: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 you should check whether you forget to type a command-line argument when you executed the program.

Ordered.java

This exercise demonstrates the use of the int and boolean data types. Write a program Ordered.java that takes three integer command-line arguments, x, y, and z. Define a boolean variable whose value is true if the three values are either in strictly ascending order (x < y < z) or in strictly descending order (x > y > z), and false otherwise. Then, print this boolean value. You must not use any if statements (or switch statements).

Examples:

> java-introcs Ordered 10 17 49
true

> java-introcs Ordered 49 17 10
true

> java-introcs Ordered 10 49 17
false

Hint: The command-line arguments (args) are stored as String variables, but to compare numeric values you should use Integer variables. Review “Parsing command-line arguments” in the Java Cheatsheet for examples of type conversions.

Hint: If b is a boolean variable, then System.out.println(b) will print true or false, according to its value.

GreatCircle.java

This exercise demonstrates the use of the double data type and Java’s Math library. The great circle distance is the shortest distance between two points on the surface of a sphere if you are constrained to travel along the surface. Write a program GreatCircle.java that takes four double command-line arguments x1, y1, x2, and y2 (the latitude and longitude, in degrees, of two points on the surface of the earth) and prints the great circle distance (in nautical miles) between them. Use the following formula, which is derived from the spherical law of cosines:

\[\text{distance in degrees} = \text{arccos}\Big(\text{sin}(x_1) \text{sin}(x_2) + \text{cos}(x_1) \text{cos}(x_2) \text{cos}(y_1-y_2)\Big)\] \[\text{distance in nautical miles} = 60 \times \text{distance in degrees}\]

This formula uses degrees, whereas Java’s trigonometric functions use radians. Use Math.toRadians() and Math.toDegrees() to convert between the two. For reference, a nautical mile (approximately 1.151 regular miles) is 1/60 of a degree of an arc along a meridian of the Earth, and this is the reason for the 60 in the formula.

Examples:

> java-introcs GreatCircle 40.35 74.65 48.87 -2.33  	// Princeton to Paris
3185.1779271158425 nautical miles

> java-introcs GreatCircle 48.87 -2.33 40.35 74.65  	// Paris to Princeton
3185.1779271158425 nautical miles

Hint: The official Java reference for the Math class describes the methods you will need for this program.

Hint: Your output for GreatCircle may match the sample output in the assignment specification, except in the very last digit or two. Why is there this tiny discrepancy? Check whether you multiplied by 60 (to convert the arc to nautical miles) before or after you converted the angle from radians to degrees. Computers work with limited precision, so algebraically equivalent formulas can produce slightly different answers. We typically ignore such tiny discrepancies when grading.

RGBtoCMYK.java

This exercise demonstrates the use of type conversions. Several different formats are used to represent color. For example, the primary format for LCD displays, digital cameras, and 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. The primary format for publishing books and magazines — known as the CMYK format — specifies the level of cyan (C), magenta (M), yellow (Y), and black (K) on a real scale from 0.0 to 1.0 inclusive.

Write a program RGBtoCMYK.java that converts from RGB format to CMYK format. Your program must take three integer command-line arguments, red, green, and blue; print the RGB values; then print the equivalent CMYK values using the following mathematical formulas:

\[\text{white} = \text{max}\Big\{ \frac{\text{red}}{255}, \frac{\text{green}}{255}, \frac{\text{blue}}{255} \Big\}\] \[\text{cyan} = \Big(\text{white} - \frac{\text{red}}{255} \Big) \div \text{white}\] \[\text{magenta} = \Big(\text{white} - \frac{\text{green}}{255} \Big) \div \text{white}\] \[\text{yellow} = \Big(\text{white} - \frac{\text{blue}}{255} \Big) \div \text{white}\] \[\text{black} = 1 - \text{white}\]

CMYK is a subtractive color space, because its primary colors are subtracted from white light to produce the resulting color: cyan absorbs red, magenta absorbs green, and yellow absorbs blue.

You may not use if statements on this assignment, but you may assume that the command-line arguments are not all simultaneously zero.

For full credit, your programs must not only work correctly for all valid inputs, but they should be easy to read.

Examples:

// indigo
> java-introcs RGBtoCMYK 75 0 130
red     = 75
green   = 0
blue    = 130

cyan    = 0.423076923076923
magenta = 1.0
yellow  = 0.0
black   = 0.4901960784313726

// Princeton orange
> java-introcs RGBtoCMYK 255 143 0
red     = 255
green   = 143
blue    = 0

cyan    = 0.0
magenta = 0.4392156862745098
yellow  = 1.0
black   = 0.0

Hint: Recall that Math.max(x, y) returns the maximum of x and y.

Hint: In Java, dividing an integer by another integer always results in an integer. However, dividing an integer by a floating-point number results in a floating-point number.

Submit

Create a .zip file and upload it to Moodle. It should contain:

Acknowledgements

Thanks to Princeton’s COS126 materials, which served as a basis for this assignment.