Assign 0: Getting Started with Java
Objectives:
- Make sure you can write Java programs in a text editor (e.g., Atom, Notepad++), compile the program/class file, and execute the program/class.
- Identify and fix the compiler and logic errors in a program and to write simple programs with print statements.
Due: Wednesday before class
Part 0: Set Up
Cloning the Repository
In your terminal, go to the directory where you are keeping your repositories for CSCI209. Clone the repository for this assignment.
README
In a text editor, open up the README file. Add your name to the README, specifying that you're the author/owner of the repository.
Practice on a Given Program
- In your terminal, view the contents of your repository. You
should see
First.java
- In your terminal, compile and run
First.java
:- javac First.java
Compiles the program intoFirst.class
List the contents of your directory to seeFirst.class
- java First
Runs the JVM, which executes the bytecode
- javac First.java
- View
First.java
in your favorite text editor, e.g., if your text editor isgedit
, you'd run
gedit First.java &. - Review the components of the program. Make sure you understand why the code outputs what it does.
Part 1: Your First Program
In your favorite text editor, create a Java class
called Intro
in a file named Intro.java
.
The Intro
class's main
method should print
out--on separate lines--your name, your favorite color, and your
favorite pop culture character. For example:
Sara Sprenkle Favorite Color: Purple Favorite Pop Culture Character: Tina Belcher
All the top-down design principles from CS111 and CS112 apply in every computer science course. Do not try to write the entire program in one attempt. Break the problem into manageable pieces. Branch/commit/merge at good checkpoints.
You should write only a little code at a time, then compile and execute it (see directions below) so that you find bugs quickly. You're getting used to the new Java syntax, so I'm sure you'll run into problems. If you run into problems, compare your code to the examples from class.
Make sure your program is well-documented, as discussed in class.
Compiling the Program
In a terminal, from the directory that
contains Intro.java
, compile your program
using javac Intro.java
If there are errors, fix those errors and re-compile until it compiles.
Executing the Program
In a terminal, from the directory that
contains Intro.java
, execute your program
using the command
java Intro
If the output is not what you expect, edit your program, recompile, and execute. Remembering to recompile before executing will be hard to get used to at first. Keep in mind some of the benefits of compiling: it catches syntax errors and can improve the efficiency of your executing program. Once compiled, you can run your program multiple times (which, granted, is not too interesting at this point.)
When you're sure your program is working correctly, execute your
program using the command
java Intro > output,
which writes the output from java Intro
into the
file output
.
Add your source code and output
to the repository.
If you are spending more than about 30 minutes on this program, something is wrong. Talk to me.
Part 2: Debugging A Program
Objective: The goal of this part of the assignment is to get you familiar with the required Java syntax, the compiler's error messages, and how to fix the errors so that the program compiles. Debugging is one of the most important skills you learn from programming, so it's good to practice, especially on a small program that you did not write. (Debugging your own code can be emotional; let's take that emotion out -- you're finding someone else's bugs!)
The given Assign0.java
program contains at least 4
errors. Some are compiler errors that the compiler will catch. Others
are logic errors that the compiler won't catch. The errors are
common--you'll likely make these mistakes yourself at some point in
time. You'll find those bugs faster in the future since you've
practiced debugging them.
Comment out the original code and note the cause of the error (e.g., the rule that is violated). Then, correct the code. It is not enough to say how you changed the code; you must explain the cause of the error. You prove to me that you know how to fix it by what you put in the code. The comments prove that you understand why the error occurred.
Aim for the fewest number of changes to get the code to work as expected. You could rewrite the code from scratch, but that's not the goal/point of the assignment.
Since you are inexperienced with Java, what are good ways to figure out the errors? You should have several different approaches to solving these problems (and that doesn't involve Web searches).
Rationale for requiring comments: I could
do a diff
between the original version of the code and
your version of the code to see what you changed. But, I want you to
show that you know why the error occured.
Saving the Final Output
After you're sure the program works correctly, save the output in a
file called debugged.out
, similar to how you did in the
last problem. Add the output file to your repository. Make sure your
working version of the code is in the repository.
Cleaning up your code
Great! You got it working! Before your final submission, review your code. Is your code written in an (relatively) understandable way? Any extra code that you no longer need? Do you have appropriate comments? Does the code's indentation look like the examples?
Submission
Submit your assignment by pushing the code, README.md, and output
files to GitHub. Note that the .class
files should not
be included in the repository. I set up the .gitignore
file to prevent you from adding the .class
file.
Checking your work
It is recommended that you confirm in the GitHub web interface that your final submission is in the repository. You can [re?]clone your project in a new directory, and then make sure that you can compile and run your programs. (This is, afterall, exactly the process that I'll use.)
Grading (70 pts)
You will be evaluated based on
- Part 1 (20 pts)
- Correctness of your program (e.g., no compiler or runtime errors, matches specification)
- Part 2 (40 pts)
- Catch, fix, comment all errors; program gives expected output
- Style/Indentation (10 pts)
- Parts 1--2: Proper style, indentation, documentation of Java programs