ulrail.gif

Homework 1

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Assignments | Exams
Lectures | Course Staff | Submit

br-2a.gif   br-2c.gif brrail.gif
spacer.gif

spacer.gif

Due: 11:59 PM, Tuesday, September 11th

Purpose

The goal of this assignment is practice writing expressions, loops, and conditionals in Java.

Before Lab

Read this document carefully, and make sure you have read up to and including chapter 1.3 in your textbook.

In Lab: Color 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 reads in three integers red, green, and blue from the command line and prints out equivalent CMYK parameters. The mathematical formulas for converting from RGB to an equivalent CMYK color are:

MATH

Hint: Math.min(x, y) returns the minimum of x and y. Here is an example run:

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


If red, green, and blue are all 0, the resulting color should be black.

Turning in your work

Use the CS101 submission pages to submit RGBtoCMYK.java.

Post-Lab

You will write four more short programs that use conditionals and loops.
  1. Distinct values. Write a program Distinct.java that takes three integer command line parameters a, b, and c, and prints out the number of distinct values (1, 2, or 3) among them.
    % java Distinct 876 5309 5309
    There are 2 distinct values
    % java Distinct 17 17 17
    There is 1 distinct value
  2. Average value. Write a program Average.java that takes an integer N as a command line argument and uses Math.random() to generate N random numbers betwen 0.0 and 1.0, and then prints their average value. Use a while loop. Do not print all the numbers; only print the average. What do you expect the average to approach when N is large?
    % java Average 5
    0.5920401639695192
  3. Checkerboard. Write a program Checkerboard.java that reads an integer, N from the command line, and prints out a two dimensional N-by-N checkerboard pattern with alternating spaces and asterisks, like the following 4-by-4 pattern. Use two nested for loops.
    % java Checkerboard 4
    * * * *
     * * * *
    * * * *
     * * * *
  4. Ordinals. Write a program Ordinals.java that takes a command line argument N and prints out the first N ordinals, separated by commas. Make sure to look at the example run below and handle the end of the list correctly. Also make sure that your program works properly when N=1 and when N=0.
    % java Ordinals 23
    1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd and 23rd
    Hint: consider using (i % 10) and (i % 100) to determine when to use "st", "nd", "rd", or "th".

Submission

Go to the CS101 submission page and submit all four of your programs.

spacer.gif
spacer.gif footer-middle.gif spacer.gif