0. Hello, World

This assignment introduces Java programming and the workflow for preparing and submitting COS 126 assignments. You will use IntelliJ to write, compile, and run Java programs, and TigerFile to submit your work.

Getting Started

Before you begin, complete the following setup and policy steps:

If you get stuck, post on Ed, attend office hours, or visit the COS Lab TAs. Don’t be afraid to ask for help!

Restrictions

Follow these rules to support the intended learning goals and preserve anonymous grading:

  • For each assignment, you may use only Java features introduced in COS 126 so far.
  • For example, on this assignment, you may call System.out.print(), Integer.parseInt(), Math.sqrt(), and StdAudio.play(), but you may not use loops or conditionals.
  • Do not include your name, NetID, or email in any submitted file except acknowledgments.txt.

Hello, World

Write a program HelloWorld.java that prints “Hello, World.” If you completed the IntelliJ setup, you have already done this step.

~/Desktop/hello> javac HelloWorld.java

~/Desktop/hello> java HelloWorld
Hello, World

Personalized Greeting

Write a program Greeting.java that takes three command-line arguments: a greeting and two names. It prints two lines. The first line uses the names in the given order; the second line in the reverse order.

Below are some sample executions. Match the output exactly, including punctuation.

~/Desktop/hello> javac Greeting.java
~/Desktop/hello> java Greeting Hi Sonia Samuel
Hi Sonia and Samuel.
Hi Samuel and Sonia.

~/Desktop/hello> java Greeting Ciao Kellia Yacoub
Ciao Kellia and Yacoub.
Ciao Yacoub and Kellia.

~/Desktop/hello> java Greeting γεια Alexandria Homer
γεια Alexandria and Homer.
γεια Homer and Alexandria.

~/Desktop/hello> Greeting "Nǐ hǎo" Ahmed Hagop
Nǐ hǎo Ahmed and Hagop.
Nǐ hǎo Hagop and Ahmed.
Q.How do I pass a greeting or name that contains spaces as a single command-line argument?
A.
Spaces normally separate command-line arguments. To include a space, wrap the entire argument in double quotes.

For more multilingual examples, see HelloWorldMultilingual.java.

Hello, World in Many Voices

Write a program SayHelloWorld.java that takes the name of a WAV file as a command-line argument and plays it using StdAudio.play().

Details:

  • StdAudio.play() takes a filename, such as "HelloWorld.wav", and plays the audio file.
  • The filename may include a directory path, such as people/KevinWayne.wav.
  • When using the command line, compile and run with javac-introcs and java-introcs. These commands tell Java where to find our course libraries, such as StdAudio.
  • The people, google and siri directories contain many WAV files of ‘Hello, World’ in different voices and languages, including recordings and synthetic audio (from Google Translate and Apple Siri).

Below are some sample executions:

~/Desktop/hello> javac-introcs SayHelloWorld.java

~/Desktop/hello> java-introcs SayHelloWorld people/KevinWayne.wav


~/Desktop/hello> java-introcs SayHelloWorld people/AloeBlacc.wav


~/Desktop/hello> java-introcs SayHelloWorld google/Spanish.wav


~/Desktop/hello> java-introcs SayHelloWorld siri/Kyoko.wav

Record yourself saying “Hello, World” (any language) and save the audio file in your device’s native format:

  • Mac OS X: Voice Memos app → Record. Drag the recording to a folder to save the file.
  • iPhone: Voice Memo app → Record. Transfer the file to your laptop.
  • Windows: Sound Recorder → Record → Save.
  • Android: Use an app that allows you to record audio. Transfer the file to your laptop.

Then convert it to WAV format (e.g., using Zamzar) and name the file MyHelloWorld.wav.

Great Circle Distance

The great-circle distance is the length of the shortest path between two points \( (x_1, y_1) \) and \( (x_2, y_2) \) on the surface of a sphere, where the path is constrained to be along the surface.

great-circle distance between two points

Write a program GreatCircle.java that takes four double command-line arguments \(x_1\), \(y_1\), \(x_2\), and \(y_2\)—the latitude and longitude (in degrees) of two points on the surface of the earth—and prints the great-circle distance (in kilometers) between them.

Use the following formula based on the spherical law of cosines:

\[ distance \; = \; r \cdot \arccos \left ( \sin x_1 \sin x_2 \, + \, \cos x_1 \cos x_2 \cos(y_1 - y_2) \right ) \]

where \(r = \text{6,371.0}\) is the mean radius of the Earth (in kilometers).

Below are some sample executions. Your output should match exactly, except possibly in the last digit.

~/Desktop/hello> javac GreatCircle.java

~/Desktop/hello> java GreatCircle 40.35 74.65 48.87 -2.33   // Princeton to Paris
5902.927099258562 kilometers

~/Desktop/hello> java GreatCircle 30.0 15.0 30.0 75.0       // for debugging
5706.280401668459 kilometers
Q.The command-line arguments are given in degrees but Java’s trigonometric functions use radians. What can I do?
A.
Use code like radians = Math.toRadians(degrees) to convert from degrees to radians.
Q.Which other math library functions will I need?
A.
You will also need Math.sin(), Math.cos(), and Math.acos().`.
Q.I’m getting the wrong result. How can I check my formula?
A.

Create a small example that you can work out by hand. Then verify that each intermediate term matches.

great circle trace

Context: Although the Earth is not a perfect sphere, this formula is a good approximation to the true distance.

RGB to CMYK Conversion

Colors are represented in different formats, including RGB and CMYK.

  • RGB format is used for screens and web graphics. It specifies each component (red, green, blue) on an integer scale from 0 to 255.
  • CMYK format is used for print publishing. It specifies each component (cyan, magenta, yellow, black) on a real scale from 0.0 to 1.0

Write a program RGBtoCMYK.java that converts from RGB format to CMYK format. The program takes three integer command-line arguments: red, green, and blue (each between 0 and 255). It prints the RGB components, followed by the corresponding CMYK components.

Use the following conversion formulas:

\[ \begin{aligned} white & \;\; = \; \max \left ( { \frac{red}{255}, \; \frac{\textit{green}}{255}, \; \frac{blue}{255}} \right ) \\[.2in] cyan & \;\; = \; \left ( white - \frac{red}{255} \right ) \;\;\;\, \div \; white \\[.2in] magenta & \;\; = \; \left ( white - \frac{green}{255} \right ) \; \div \; white\\[.2in] yellow & \;\; = \; \left ( white - \frac{blue}{255} \right ) \;\;\, \div \; white\\[.2in] black & \;\; = \; 1 - white \end{aligned} \]

Restriction: You may assume the three RGB arguments are not all zero (to ensure $white$ is nonzero).

Below are some sample executions. Your output format must match the examples exactly, including whitespace and punctuation.

~/Desktop/hello> javac RGBtoCMYK.java

~/Desktop/hello> java RGBtoCMYK 75 0 130   # indigo
red     = 75
green   = 0
blue    = 130
cyan    = 0.423076923076923
magenta = 1.0
yellow  = 0.0
black   = 0.4901960784313726

~/Desktop/hello> java RGBtoCMYK 255 143 0   # Princeton orange
red     = 255
green   = 143
blue    = 0
cyan    = 0.0
magenta = 0.4392156862745098
yellow  = 1.0
black   = 0.0
Q.How do I compute the maximum of three numbers?
A.
Java does not include a built-in function for that. However, Math.max(x, y) returns the maximum of x and y.
Q.How do I align the equals signs in the output?
A.
We will learn about formatted printing later in the course. For now, add the appropriate number of spaces in the string you print, before each equals sign.

Readme

Complete readme.txt to describe your work. Use the provided template and answer every question.

Acknowledgments

Complete acknowledgments.txt using the provided template. It contains two sections:

  • List the names and dates of anyone who helped you with this assignment. When you attend office hours or a Lab TA session, introduce yourself and write down the person’s name.


    1/23/26 Alan Turing (lab TA)
    Helped me cast the result of Math.round() to an int.
    
    1/24/26 Grace Hopper (faculty)
    Explained how to use the StdAudio library.
    
  • Include the Student Acknowledgment of Original Work from Rights, Rules, Responsibilities. Add your digital signature as /s/ followed by your full name.


    This programming assignment represents my own work in
    accordance with University regulations. /s/ Ada Lovelace
    

Submission

Log in to TigerFile with your OIT NetID and upload the required files.

  • HelloWorld.java
  • Greeting.java
  • SayHelloWorld.java
  • GreatCircle.java
  • RGBtoCMYK.java
  • MyHelloWorld.wav
  • readme.txt
  • acknowledgments.txt

Then click Check Submitted Files to check your work. Fix every reported issue (including issues in readme.txt and acknowledgments.txt) and resubmit.

  • You may submit files individually or in a batch.
  • You may click Check Submitted Files at most 20 times per assignment.
  • We will not grade your submission until acknowledgments.txt is submitted. If you plan to submit late, do not submit that file until you are finished and ready for grading.

Grading breakdown

Item Points
HelloWorld.java 4
Greeting.java 8
SayHelloWorld.java 8
GreatCircle.java 8
RGBtoCMYK.java 8
readme.txt 2
Course Entry Survey 2
Total 40

You must complete the Collaboration Policy Quiz with a perfect score to receive credit for any programming assignment.