COS 126

Assignment 0
Programming Assignment

Due: 11:55pm

The purpose of this assignment is to introduce you to programming in Java and familiarize you with the mechanics of preparing and submitting assignment solutions. Your goal this week is to learn to use DrJava for editing programs, javac for compiling them, java for executing them, and Moodle for submitting them.

Hello, World.  In this step, you will setup an environment for developing Java programs.

Programming.  In this part, your job is to write four short programs. We'll assume that you've already created, compiled, and executed HelloWorld.java by following the instructions in the previous part of the assignment. Before completing this part, you should read Sections 1.1 and 1.2 in the textbook.

  1. Using strings. Write a program HiTwo.java that takes two names as command-line arguments and prints out the greeting specified below.
    % java HiTwo Alice Bob
    Hi, Alice! Have you met my friend Bob?
    

  2. Boolean and integer variables. Write a program Ordered.java that reads in three integer command line arguments, x, y, and z. Define a boolean variable isOrdered 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. Print out the variable isOrdered using System.out.println(isOrdered).
    % java Ordered 10 17 49
    true
    
    % java Ordered 49 17 10
    true
    
    % java Ordered 10 49 17
    false
    

  3. Floating point numbers and the Math library. Write a program StdGaussian.java that prints out a random value z from a standard Gaussian (normal) distribution using the polar form of the Box-Muller formula:
    z = (-2 ln u)1/2 sin(2 π v)
    where u and v are real numbers between 0 and 1 generated by Math.random(). Note that Math.PI is the mathematical constant π,   Math.log(x) is the natural logarithm of x, Math.sqrt(x) is the square root of x, and Math.sin(x) is the sine of x in radians.
    % java StdGaussian
    0.9500138326397604
    
    % java StdGaussian
    -0.6001188427428142
    

  4. Type conversion. There are a number of different formats for representing color. RGB format specifies the level of red (R), green (G), and blue (B) on an integer scale from 0 to 255: It is the primary format for LCD displays, digital cameras, and web pages. CMYK format specifies the level of cyan (C), magenta (M), yellow (Y), and black (K) on a real scale of 0.0 to 1.0: It is the primary format for publishing books and magazines. Write a program RGBtoCMYK.java that takes three integer command-line arguments red, green, and blue (not all 0), and prints out equivalent CMYK parameters. The mathematical formulas for converting from RGB to an equivalent CMYK color are:
    RGB to CMYK formula

    Hint. Math.min(x, y) returns the minimum of x and y.

    % java RGBtoCMYK 75 0 130       // indigo
    cyan    = 0.4230769230769229
    magenta = 1.0
    yellow  = 0.0
    black   = 0.4901960784313726
    

Writeup.  With each assignment you must submit a text file named readme.txt that is a narrative description of your work. We provide a readme.txt that you should use as a template. Download this file and answer all questions in the space provided.

Browsing the course website.  The next part of the assignment is to browse the COS 126 Web pages. The address is:

http://www.princeton.edu/~cos126
Cruise around the COS 126 Web site; it is essential that you understand what's where and how to get to it. Be sure to fill out the online questionnaire. Also make sure you find the Assignment 0 checklist page, and also be sure to read the COS 126 Collaboration Policy and answer the related questions in your readme file.

Submitting the assignment.  The final part of the assignment is to submit HiTwo.java, Ordered.java, StdGaussian.java, RGBtoCMYK.java, and readme.txt via the Web submission system. To do this, go to the course webpage and select the HelloWorld assignment under Social Activities. Be sure to hit the Run Script button to check your program against our Java compiler. It should compile without errors or warnings; if not, fix the problem and resubmit it. To submit a file, you must be officially registered for the course.