UNDER CONSTRUCTION

This assignment is optional. Its sole purpose is to ensure that you can write simple Java programs, use algs4.jar, and submit them to the Coursera autograder.


  1. Install our Java programming environment (optional).  Install our novice-friendly Java programming environment on your computer by following these step-by-step instructions for Mac OS X, Windows, or Linux. On each assignment, use the Project from the menu at top.

    As part of these instructions, you will write, compile, and execute the program HelloWorld.java.

    ~/Desktop/hello> javac HelloWorld.java
    
    ~/Desktop/hello> java HelloWorld
    Hello, World
    


  2. Command-line arguments. Write a program HelloGoodbye.java that takes two names as command-line arguments and prints hello and goodbye messages as shown below (with the names for the hello message in the same order as the command-line arguments and with the names for the goodbye message in reverse order).
    ~/Desktop/hello> javac HelloGoodbye.java
    
    ~/Desktop/hello> java HelloGoodbye Kevin Bob
    Hello Kevin and Bob.
    Goodbye Bob and Kevin.
    
    ~/Desktop/hello> java HelloGoodbye Alejandra Bahati
    Hello Alejandra and Bahati.
    Goodbye Bahati and Alejandra.
    


  3. Using algs4.jar. Under construction. Write a program RandomWord.java that reads a sequence of words from standard input and prints one of those words uniformly at random. Do not store the words in an array or list. Instead, use Knuth’s method: when reading the ith word, select it with probability \(1 \,/ \, i\) to be the champion, replacing the previous champion. After reading all of the words, print the surviving champion.
    ~/Desktop/hello> javac-algs4 RandomWord.java
    
    ~/Desktop/hello> java-algs4 RandomWord
    heads tails
    tails
    
    ~/Desktop/hello> java-algs4 RandomWord
    heads tails
    heads
    
    ~/Desktop/hello> more animals8.txt
    ant bear cat dog
    emu fox goat horse
    
    ~/Desktop/hello> java-algs4 RandomWord < animals8.txt
    emu
    
    ~/Desktop/hello> java-algs4 RandomWord < animals8.txt
    bear
    
    Use the following library functions from algs4.jar:

    In order to access these library functions, you must do the following two things:


Web submission. Submit a ZIP file containing only HelloWorld.java, HelloGoodbye.java, and RandomWord.java. Your submission may not call library functions except those in java.lang and the ones in algs4.jar enumerated above.


This assignment was developed by Bob Sedgewick and Kevin Wayne.
Copyright © 2021.