COS 126

Assignment 0: Hello, World
Programming Assignment


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. You will learn to use the DrJava editor for writing, compiling, and executing programs, and the Dropbox system for submitting your solutions.

Installing the course software.  The introcs package includes everything that you need for this course: the Java compiler and runtime, the DrJava development environment, the course libraries, and tools checkstyle-introcs and findbugs-introcs that you can use to review your work.

If you have problems installing the software, check with the preceptors or lab TAs; you can report issues on Piazza where we can determine what advice to give everyone.

Programming.  Your job is to write additional short programs on your own. We'll assume that you have already created, compiled, and executed HelloWorld.java by following the instructions in the previous part of the assignment.

  1. Command-line arguments. Read Section 1.1 of the textbook. Modify UseArgument.java to make a program HiFour.java that takes four names as command-line arguments and prints out a proper sentence with the names in the reverse of the order given, so that, for example,
    % java HiFour Alice Bob Carol Dave
    
    outputs
    Hi Dave, Carol, Bob, and Alice. 
    

  2. Integer variables. Read Section 1.2 (through page 22) of the textbook. Write a program SumThree.java that reads in three integer command-line arguments. The program then outputs all three numbers and their sum in the form of an equation.
    % java SumThree 2 5 8
    2 + 5 + 8 = 15
    

  3. Boolean and integer variables. Restriction: You must not use if for this problem — you can use it starting next week. Write a program Ordered.java that reads in three int 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
    

  4. Floating-point numbers and the 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 out the great-circle distance between them (in nautical miles. A nautical mile is 1/60 of a degree.) using the following formula derived from the spherical law of cosines:
    Great circle distance formula
    This formula uses degrees, whereas Java's trigonometric functions use radians. Use Math.toRadians() and Math.toDegrees() to convert between the two.
    % java GreatCircle 40.35 74.65 48.87 -2.33      // Princeton to Paris 
    3185.1779271158425 nautical miles  
    

  5. 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. Input three integers, red, green, and blue, from the command line. (The input values will never all be zero.) 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
    red     = 75
    green   = 0
    blue    = 130
    cyan    = 0.423076923076923
    magenta = 1.0
    yellow  = 0.0
    black   = 0.4901960784313726
    

Program style and format.  Now that your program is working, go back and look at the program itself. Did you include a header that will tell the reader who wrote the program, and what it does? (Instructions for compiling or running the program are optional. The latter is especially useful when there are command-line arguments.) For full marks, your program should not only work correctly, but should conform to the recommended style and format so that it is easy to follow. Read Booksite Appendix B: Writing Clear Code. Follow the guidelines in the Programming Style section of the checklist.

Writeup.  With each assignment you must submit a text file named readme.txt that is a narrative description of your work. Some weeks there are also analysis questions to complete. We provide a readme.txt that you should use as a template. Download this file and answer all questions in the space provided. We recommend editing this file in DrJava.

Treasure Hunt  The next part of the assignment is to browse the COS 126 website. The address is: http://www.princeton.edu/~cos126. It is essential that you understand what's where and how to get to it. Part of this assignment is to do the Treasure Hunt on Blackboard, found under "Assignment 0 Materials".

Collaboration Policy Quiz.  As indicated in the readme template, part of this week's assignment is to complete the Collaboration Policy Quiz. Read the COS 126 Collaboration Policy, then access the quiz on the "Assignment 0 Materials" area of Blackboard. You can re-take it as many times as you like, and you must re-do the quiz until you get all questions correct.

Questionnaire.  Part of this assignment is to fill out the following brief questionnaire.

Submitting the assignment.  The final part of the assignment is to submit HelloWorld.java, HiFour.java, SumThree.java, Ordered.java, GreatCircle.java, RGBtoCMYK.java, and readme.txt via the Web submission system called Dropbox. To do this, click the Assignments link from the course website; click the Submit link for that assignment; and (if you aren't already logged in) login using your OIT NetID and email password. Upload the required files, and click the Check All Submitted Files button. Your programs should compile without errors or warnings; if not, fix the problem and resubmit the appropriate files. (The dropbox runs a few basic checks; passing it doesn't guarantee your program is 100% correct.) Remember that every file you submit needs to have your name, netID, and precept number.

Getting help. If anything is unclear, don't hesitate to drop by office hours, post a question on Piazza or email us. We also recommend reading the checklist, which provides some clarifications and answers to frequently asked questions.

Challenge for the bored (not extra credit). Try to solve Ordered without using any of the four ordered-comparison operators (< <= > >=). Try to solve RGBtoCMYK without using Math.max(). You don't need to use if for either one. Even if you solve these, please submit your original simpler solutions, which are less likely to have bugs and are more transparent to read.