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 DrJava editor for
writing, compiling, and executing Java programs, and Dropbox
for submitting your work electronically.
Setting up a Java programming environment.
In this step, you will set up a Java programming environment, either on your
computer or on an OIT public cluster machine.
-
Installing on your own computer.
Follow these step-by-step instructions
for your operating system
[
OS X ·
Windows ·
Linux
].
- Working on an OIT public cluster machine.
Follow the same step-by-step instructions
[
OS X ·
Windows
],
but go straight to the Create the program instructions; the
necessary software is pre-installed.
To save files on your H: drive, you must first
enable your Unix account.
If you are familiar with your operating system, you should be able to finish this
part on your own. If you encounter difficulties, please consult the
undergraduate lab TA schedule
and go to Lewis Library 121 for assistance. Don't be afraid to ask for help.
Programming.
In the previous step, you
created, compiled, and executed HelloWorld.java.
Next, you will write
two
four
short programs on your own.
- Hello, World.
Type in everyone's first
program—HelloWorld.java.
% java HelloWorld
Hello, World
You will need to make one minor edit: change the header
to match the format prescribed in the Assignment FAQ.
- Strings and command-line arguments.
Write a program HiFour.java that
takes four first names as command-line arguments
and prints a proper sentence with the names in the
reverse of the order given. Here are two sample executions:
% java HiFour Alice Bob Carol Dave
Hi Dave, Carol, Bob, and Alice.
% java HiFour Alejandro Bahati Chandra Deshi
Hi Deshi, Chandra, Bahati, and Alejandro.
- Integers.
Read Section 1.2 (through page 23) of the textbook.
Write a program SumThree.java
that takes three int command-line arguments and
prints the three integers and their sum in the form of an equation.
% java SumThree 2 5 8
2 + 5 + 8 = 15
% java SumThree -2 5 -8
-2 + 5 + -8 = -5
- Integers and booleans.
Write a program Ordered.java
that takes three int command-line arguments,
x, y, and z.
Define a boolean variable 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.
% java Ordered 10 17 49
true
% java Ordered 49 17 10
true
% java Ordered 10 49 17
false
Restriction: You may not use if statements on this assignment.
- 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 the great-circle distance (in nautical miles) between them.
Use the following formula, which is derived from the spherical law of cosines:
\(
\textrm{distance} = 60 \arccos(\sin x_1 \sin x_2 + \cos x_1 \cos x_2 \cos(y_1 - y_2))
\)
This formula uses degrees, whereas Java's trigonometric functions use
radians. Use Math.toRadians() and Math.toDegrees() to convert
between the two.
For reference, a nautical mile is 1/60 of a degree of an arc along a
meridian of the Earth (which is approximately 1.151 miles).
% java GreatCircle 40.35 74.65 48.87 -2.33 // Princeton to Paris
3185.1779271158425 nautical miles
% java GreatCircle 48.87 -2.33 40.35 74.65 // Paris to Princeton
3185.1779271158425 nautical miles
- Type conversion.
Several different models 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 from RGB format
to CMYK format. Your program should take
three int command-line arguments
(red, green, and blue)
and print the corresponding CMYK values using these formulas:
\(
\begin{align*}
\textrm{white} \;&=\; \max\{\, \textrm{red}/255, \, \textrm{green}/255, \, \textrm{blue}/255 \, \} \\
\textrm{cyan} \;&=\; \Bigl( \textrm{white}-\frac{\textrm{red}}{255} \Bigr) \;/\; \textrm{white} \\
\textrm{magenta} \;&=\; \Bigl(\textrm{white}-\frac{\textrm{green}}{255}\Bigr) \;/\; \textrm{white} \\
\textrm{yellow} \;&=\; \Bigl(\textrm{white}-\frac{\textrm{blue}}{255}\Bigr) \;/\; \textrm{white} \\
\textrm{black} \;&=\; 1 - \textrm{white}
\end{align*}
\)
% java RGBtoCMYK 75 0 130 // indigo
red = 75
green = 0
blue = 130
cyan = 0.423076923076923
magenta = 1.0
yellow = 0.0
black = 0.4901960784313726
|
% 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
|
Restriction: You may not use if statements on this assignment, but
you may assume that the inputs are not all simultaneously zero.
Hint. Recall that Math.max(x, y) returns the maximum
of x and y.
For full credit, your programs should not only work correctly for all valid inputs,
but they should be easy to read.
You are expected to follow these style guidelines in this course.
In particular, you must include a header (name, NetID, precept number,
and a brief description of what the program does) in every file you submit,
using the prescribed header format.
More things to do.
- Writeup.
Submit a text file named readme.txt
that is a narrative description of your work.
Each week, we provide a
readme.txt
file that you should download and use as a template, answering all
questions in the space provided.
- Questionnaire.
Complete the following brief questionnaire.
- Collaboration policy quiz.
Read the
course collaboration policy
and take a short quiz on the policy (available in Blackboard).
You must repeat the quiz until you answer all questions correctly;
otherwise, you will not receive credit on any programming assignment.
- Treasure hunt.
Browse the COS 126 website
and complete the Treasure Hunt (available in Blackboard).
It is essential that you understand what's where and how to get to it.
- Submitting the assignment.
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;
login using your OIT NetID (if necessary); and upload the specified files.
Finally, click the Check All Submitted Files button:
This will compile and execute your programs,
alerting you to potential problems before we grade your work.
You should fix any problems and resubmit.
Getting help.
If anything is unclear, don't hesitate to drop by office hours or
post a question on Piazza.
We also recommend reading the
checklist, which provides
some clarifications and answers to frequently asked questions.
Challenge for the bored (not extra credit).
Redo Ordered.java without using the comparison operators
(<, <=, >, and >=).
Redo RGBtoCMYK.java without using Math.max().
Do not use if statements for either one.
Even if you solve these, please submit your
original solutions, which are likely to be simpler and easier to read.