Skip to main content.

Assign 1: Writing and Debugging Simple Java Programs

Objective: Identify and fix the compiler and logic errors in a program and to write simple programs using some of Java's available class, simple control structures, and arrays.

Due: Before the next class (Friday)

Set Up

Create a directory for assign1 within your cs209 directory.

Part 0: Bookmark the Java API

Bookmark the Java API. Also bookmark the course schedule page, if you haven't already.

Part 1: Fixing a Program

Save Assign1.java into your assign1 directory.

This program contains at least 5 errors. Some are compiler errors that the compiler will catch. Others are logic errors that the compiler won't catch.

Comment out the original code and note the cause of the error. Then, correct the code.

You may want to run this program with multiple command-line arguments. Specifically, try running the program with and without cow as a command-line argument.

Save your output in files called cow1, cow2, etc.

Example run: java Assign1 cat dog bird

The length of command-line args is 3
You have more than 2 arguments.
arg[0] is cat with length 3
arg[1] is dog with length 3
arg[2] is bird with length 4
The sorted command-line arguments are 
    bird
    cat
    dog
You don't have a cow, man.

Part 2: String Practice (25 pts)

Your Java program (ReverseString) should take in a string from the command line (look at example using command-line arguments) and print the string backwards.

Use a String as an accumulator for the reverse of the original string. Go through the original string character by character, from the end of the string to the beginning. (Hint: What String method should you use?)

Alternatively, for 3 points extra credit, use a StringBuilder to accumulate the String, since the StringBuilder is more efficient.

Example output for java ReverseString stars:

stars backwards is srats.

If the user does not enter a string on the command line, print a useful error message.

Example output for java ReverseString:

Error: Need a command-line argument.

Save the output from several runs of the program in the file reverse.out. After running the program the first time, use >> to put additional output into the same file. For example:

java ReverseString stars > reverse.out
java ReverseString banana >> reverse.out

View reverse.out before submitting to make sure that it contains all of your test runs.

Part 3: Computing Olympics Gymnastic Score (35 pts)

The current system of scoring an Olympic gymnast is based on two separate panels of scores. The A panel judges the requirements, difficulty, and connections of a routine. The scoring starts at zero and then adds points accordingly. The B panel judges the execution of a routine, and the scoring starts at 10 with points deducted accordingly for execution and for any applicable violations such as stepping out of bounds or being over the time limit.

For each panel of six judges, the lowest and highest scores are dropped (to prevent judges from biasing the results) and then averaged. The two panels are then added together for the final score. A very good score will range in the 15s and 16s.

Your task: Write a Java class called OlympicScores. Assume an average difficulty score of 6.0 (which should be a class constant; where are class constants defined?). Represent the judges' execution scores as an array of doubles. You cannot assume that the array is sorted from initialization. (Look at the Arrays class and this example for help.) Calculate the average score, as described above. Display the judges' scores and the final score in an appropriate format.

Note that there is no input for this problem. You're just setting the values in the array.

Example output from executing java OlympicScores

Gymnastics Score
----------------
        Judges Execution Scores: 8.7 8.0 9.2 8.3 8.7 8.4
        Average Execution Score: 8.525
        Average Difficulty Score: 6.0
        The Final Score: 14.525

Save the output from one of your runs in the file scores.out.

Turning in Your Assignment

Copy your assign1 directory into your turnin directory, i.e., /csdept/local/courses/cs209/turnin/yourusername.

You can use the script: turnin.sh assign1

If that doesn't work, try: /csdept/local/courses/cs209/shared/turnin.sh assign1

If you don't want the abstraction of the script, you can use this: cp -r assign1 /csdept/local/courses/cs209/turnin/yourUserName/

There is no printed part of this assignment.

Grading (100 pts)

You will be evaluated based on