Machine Problem 1

Due Feb 8, on line

Part of the homework for CS:2820, Spring 2021
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Background

Start with the classic Java "Hello World" program:

// HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World.");
    }
}
This program ignores its command line arguments, but in order to make the Java compiler happy with the program, the program includes a declaration for the array of strings args. In the context of this declaration, args.length tells you how many elements there are in the array, which tells you how many strings were on the command line. For example, if the command line was
java HelloWorld this and that

Then

The Assignment

Make a Java program that:

Submission

On or before the end of Monday, Feb. 8, you must submit your program using the following shell command on the fastx machine:

~dwjones/submit xxxx Args.java

Substitute your section number for xxxx above, but do not alter the text dwjones.

The program will confirm that you successfully submitted your solution with the message "submiter: succesfull submission of xxxx/HawkID" with xxxx replaced with your section number and HawkID replaced with your HawkID.

You may submit as many times as you want. The last successful submission is the one that will count. As such, it is a good idea to submit as soon as you have code that sort-of works, submit again when you have code that works the way you like, and perhaps submit again when you bring your code up to the highest standard of readability.

Grading

Code that executes correctly is worth half credit (2.0 out of 5 points). The remaining credit is all for readability. Expect demerits for:

Student Questions

A student asked: Do we have to include our name in the file even if we are the only author?

Absolutely. It's like writing your name on your homework assignment when turned in on paper. Yes, we can do the detective work to relate your assignment back to your HawkID and from there connect it to your name, but we'd like to see your name in the author attribution.

Failure to include appropriate header comments in the file will be severely penalized.

A student asked: for the section we type in for submitting, is that the class section (0AAA) or the discussion section (0A01)?

The submit mechanism will give you an error message if you use the class section. It'll even list the legal section numbers, which are the discussion sections.

A student asked: I get this message "Error: Could not find or load main class Args.java"

First, to compile, you type javac Args.java
but to run it, you type just java Args

Second, Unix/Linux file names are case sensitive.
Args.java is not the same as args.java

A student asked: When I try to write in-line comments on my for loop, it gets too long. What's the best way to deal with this?

There are two obvious solutions, and they invite different text in the comment:

// for all items in the whazzit ...
for (long initialization; long termination rule; long increment code) {
    loop body;
}
for (long initialization; long termination rule; long increment code) {
    // item.thisone is one item in the whazzit ...
    loop body;
}

In the first alternative above, with the comment before the loop, the best wording for the comment refers to the entire loop, while in the second alternative, the comment focuses on what just this iteration does. It's rare to need both comments, because it's usually easily apparent to the reader who knows Java what one comment would be, given the other plus the loop header itself.

A student asked: You said "the Javadoc style illustrated in the course notes." Where is that?

Many code examples in the notes illustrate the use of Javadoc comments. The first use, and one that completely illustrates the necessary author and version attributions, is found in the Hello World program given in Lecture 2.

A student asked: You mentioned a format program we could run on our code.

Try this command on the FastX server:

~dwjones/format *.java

If you just have one Java source file, that will suffice. Otherwise, replace * with a specific file name. Note that this format checker checks for long lines, wonky indenting (mixed spaces and tabs) and things like that. It does not check Java style issues. If it gives no output, it saw no problems. If it gives output, it tells you what it sees that's odd.

I do not recommend any automatic style checkers to enforce something like the Sun-Oracle style guide, or any other style guide. The reason is, program style is for humans. Automatic enforcers tend to be rigid, not allowing for clear communication. There are plenty of cases where slavishly sticking to a rigid style makes code harder to read.

For comparison, consider mechanically enforced rules of English usage that try to taylor English to various reading levels. Such style checkers would never have given Winne the Pooh By A.A. Milne a pass — that book contains a 194-word run-on sentence! Milne's run-on sentence even contains the phrase "and the story went on and on, rather like this sentence," making it very clear that it is quite deliberate and carefully crafted.