Assign 1: Debugging and Writing Java Programs
Objectives:
- To practice debugging simple Java programs
- To practice using the Java API
- To get more familiar with features of the Java programming language
Due: Wednesday before class
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.
Bookmark the Java API
Bookmark the Java 21 API.
Part 1: Debugging a Program (30 pts)
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 for the latest language features we've learned.
The given Debug1.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. Then, correct the code. 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. Strive for the fewest changes (both in number and size of changes) that fix the problem.
Save your output (using the >
output redirection)
in a file called debugged.out
. Add this file to your
repository.
FAQ -- not to be read until after you've attempted the assignment completely and are now stuck.
Part 2: Leveraging the Java API (30 pts)
Your Java class (FileExtensionFinder
) should take in
a file name (a String) as user input and display the file extension,
lowercased. For example, when you run your program using java
FileExtensionFinder, it should look something like:
Enter your filename: myfile.txt Your file is a(n) txt file.
Best Development Process: Wait to implement getting user input until last in your development process. Instead, to start, hardcode the String variable that represents the filename and write your code to find the extension. Then, hardcode a different String to test that your code still works with that new value. Then and only then, add getting user input.
There are a variety of ways to solve this problem. Think about
your algorithm and what tools (Java classes/methods--check out the
String
API)
are available to you. You
should not use any control structures for
this problem, in part because we're not worried about error handling at this point.
The goal is to get you familiar with using the Java API. (Using control structures
for this problem will be less efficient.)
Use print
statements to check/verify your work at intermediate steps. Remove
them for your final submission.
More example output:
Enter your filename: intro.pptx.pdf Your file is a(n) pdf file.
Enter your filename: Paper.DOCX Your file is a(n) docx file.
At this point in the term, you don't need to worry about error handling, such as if there is no filename given or if there is no "." in the filename.
Since the program is getting user input, we can't just write to an output file like we did for the last assignment. I will execute your code using my test cases.
Cleaning up your code
Great! You got it working! Before you submit, 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? Is the indentation appropriate?
Make those changes and commit your cleaned up code.
Part 3: Debugging a(nother) Program (60)
Don't attempt until after Monday's class.
Debug2.java
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 explain the cause of the error. Then, correct the code. Strive for the smallest number of changes and smallest amount of change to fix the problem.
You may want to run this program with multiple command-line
arguments. For example, try running the program with and
without cow
as a command-line argument.
Demonstrate that your fixed code works correctly. Save good
examples of your output in files
called cow1
, cow2
, etc. Add all your test
output files to the repository.
Example run with expected output: java Debug2 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.
Submission
Submit your assignment by pushing the code 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 [re?]clone your project in a new directory, and then make sure that you can compile and run your programs. (This is, afterall, the process that I'll use.)
Grading (130 pts)
You will be evaluated based on
- (30 pts) Part 1: Debugging a program
- Catch, fix, comment all errors; program gives expected output
debugged.out
has the expected output
- (30 pts) Part 2: Correctness of your program
- identifies and displays the extension of the filename
- gets filename as user input
- (60 pts) Part 3: Debugging a program
- (50 pts) Catch, fix, comment all errors;
- (10 pts) Good test cases shown in output
- (10 pts) Parts 1-3: Proper style, indentation, documentation of Java programs