COS 126

Assignment 0
Programming Assignment

Due: 11:00pm

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 them out as part of the greeting specified below.
    % java HiTwo Alice Bob
    Hi, Alice! Have you met my friend Bob?
    
    % java HiTwo Mickey Minnie
    Hi, Mickey! Have you met my friend Minnie?
    

  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. As you might remember, there is a trigonometric identity:
    sin 2x + cos 2x = 1.
    Write a program SinCos.java that reads in a double value x from the command line, computes
    sin 2x + cos 2x
    and prints out the result. Note that Math.sin(x) returns the sine of x and Math.cos(x) returns the cosine of x, where x is an angle measured in radians.
    % java SinCos 0.534
    1.0
    
    % java SinCos 1.0
    1.0
    

  4. Type conversion.   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. 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.

    Write a program RGBtoCMYK.java that converts RGB to CMYK. Take three integers red, green, and blue (not all zero) from the command line, and print the equivalent CMYK values using these formulas:

    RGB to CMYK formula

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

    % java RGBtoCMYK 75 0 130       // indigo
    cyan    = 0.423076923076923
    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, SinCos.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.