COS 126

Assignment 0
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. Your goal this week is to learn to use DrJava for editing, compiling, and executing programs.

Hello, World.  The purpose of this step is to set up your computer for developing Java programs.

Programming.  Your job is to write four short programs on your own.

  1. We'll assume that you've already created, compiled, and executed HelloWorld.java by following the instructions in the previous part of the assignment.

  2. 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. 
    

  3. 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
    

  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) 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.   Write a program ElectionResults.java that reads in four int command-line arguments, the votes for Newt Gingrich, Ron Paul, Mitt Romney, and Rick Santorum in that order and prints out the total number of votes. It then prints whether each candidate was the winner, and finally what percentage of the votes the winner received. When calculating the percentage, use a double variable. When printing the percentage convert that variable to an int. Round to the nearest integer. For example, if the variable is 0.4862 the percent is 49%. (If the fractional part is exactly .5, round up. E.g., If the variable is 0.425 the output is 43%.)

    So, for example (using the data from South Carolina, Iowa, and New Hampshire):

    %  java ElectionResults 243153 77993 167280 102057      // SC
    Total number of votes in this state: 590483
    Newt Gingrich is the winner? true
    Ron Paul is the winner? false
    Mitt Romney is the winner? false
    Rick Santorum is the winner? false
    The winner won this state with 41% of the vote.
    
    % java ElectionResults 16251 26219 30015 30007          // Iowa
    Total number of votes in this state: 102492
    Newt Gingrich is the winner? false
    Ron Paul is the winner? false
    Mitt Romney is the winner? true
    Rick Santorum is the winner? false
    The winner won this state with 29% of the vote.
    
    % java ElectionResults 22921 55455 95669 22708          // NH
    Total number of votes in this state: 196753
    Newt Gingrich is the winner? false
    Ron Paul is the winner? false
    Mitt Romney is the winner? true
    Rick Santorum is the winner? false
    The winner won this state with 49% of the vote.
    
    

    If two or more candidates are tied for top place, all those candidates tying for first place should report winner as true.

    % java ElectionResults 25 25 25 25          // go crazy NJ
    Total number of votes in this state: 100
    Newt Gingrich is the winner? true
    Ron Paul is the winner? true
    Mitt Romney is the winner? true
    Rick Santorum is the winner? true
    The winner won this state with 25% of the vote.
    

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. You will need to read the COS 126 Collaboration Policy in order to answer the related questions in your readme file.

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

Browsing the course website.  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.

Submitting the assignment.  The final part of the assignment is to submit HelloWorld.java, HiFour.java, Ordered.java, GreatCircle.java, ElectionResults.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. 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 or email us. We also recommend reading the checklist, which provides some clarifications and answers to frequently asked questions.