In this assignment, you will gain experience working with generics in Java. In particular, you will extend my binary search tree (BST) code from a data structure that just holds integers to a powerful container that can hold any kind of Comparable object under the sun.
A tangential benefit of this program is that you get to spend some time looking through some fairly straight-forward and well-structured BST code. It is always good to revisit how common data structures can be implemented as you ramp up your skills in different programming languages. You will also gain experience modifying someone elses code (something you will be doing a lot if you become a software developer) and adding comments to code.
Deliverables
GenericBST.java
Note! The capitalization and spelling of your filename matter!
Note! Code must be tested on Eustis, but submitted via Webcourses.
1. Problem Statement
Included with this assignment is a source file named BST.java. You must modify that file as follows:
- Change the class name to GenericBST and the file name to java.
- Modify the source code so that the class is generic (i.e., so that the binary search trees will be able to hold any type of data) with the restriction that only classes that implement Comparable may be stored in the BSTs. As with java (posted in Webcourses), you do not need to write a compareTo() method.
- The comments in java are practically non-existent. After you have read and absorbed the programs structure and functionality, add comments for the entire program. (See the commenting guidelines below.)
- Write a public static double difficultyRating() method that returns a double on the range of 1.0 (ridiculously easy) through 5.0 (insanely difficult) indicating how difficult you found this assignment.
- Write a public static double hoursSpent() method that returns a realistic and reasonable estimate (greater than zero) of the number of hours you spent working on this assignment.
To make this class generic, you will have to change the return types of some of the methods I have implemented. However, please do not change static methods to non-static methods, or private methods to public methods, and vice-versa.
2. Special Requirement: No Compile-Time Warnings (Super Important!)
Here are some special restrictions regarding compile-time warnings:
Your code must not produce any warnings when compiled on Eustis. For this particular assignment, its especially important not to have any -Xlint:unchecked warnings. Please note that some systems dont produce -Xlint:unchecked warnings! The safest way to check whether your code is producing such warnings is to compile and run your program on Eustis with the test-all.sh script.
You cannot use the @SuppressWarnings annotation (or any similar annotations) to suppress warnings in this assignment.
Please do not give your code to classmates and ask them to check whether their compilers generate compile-time warnings for your code. Remember, sharing code in this course is out of bounds for assignments.
3. General Commenting Guidelines
Here are some guidelines to follow when adding comments to GenericBST.java or when commenting your own code this semester: Keep comments brief. In general, try to avoid comments that exceed two or three lines. Note that the comments in my code in Webcourses are designed for educational reading. Theyre far more lengthy and verbose than anything you should be including in your own code.
In general, above each method signature, give a high-level overview of what that method does. Say something about its input parameters, expected output, and return value (if applicable).
Within your methods, give high-level comments that tell what different chunks of code do (rather than commenting every single line of code). However:
Avoid simply repeating things that are obvious in your code, thereby making someone read your code twice (once in English and once in Java). That is, avoid transliterating your code from Java to English.
Prefer comments that tell how or why something works, rather than simply saying what something does, unless youre explaining the behavior of a chunk of code in a way that makes your code easier for a reader to digest.
When writing comments, a good rule of thumb is this: Suppose you load up your code for an assignment five years from now, and youve lost the PDF for that assignment. What comments would help you quickly understand what you were doing and how each of the methods plays into your programs overall functionality? Try to use method names that are verb phrases, which therefore act as documentation of what your code is doing. (Avoid obscure method and variable names that would require us to add clarifying comments everywhere they appear.)
Dont copy and paste method descriptions from the assignment PDFs, as they sometimes contain nonstandard characters that will prevent your code from compiling. (Also, the method descriptions in the PDFs are often way too verbose. You should write shorter overviews for each of your methods.)
4. Style Restrictions (Same as in Program #1) (Super Important!)
Please conform as closely as possible to the style I use while coding in class. To encourage everyone to develop a commitment to writing consistent and readable code, the following restrictions will be strictly enforced:
Capitalize the first letter of all class names. Use lowercase for the first letter of all method names.
Any time you open a curly brace, that curly brace should start on a new line.
Any time you open a new code block, indent all the code within that code block one level deeper than you were already indenting.
Be consistent with the amount of indentation youre using, and be consistent in using either spaces or tabs for indentation throughout your source file. If youre using spaces for indentation, please use at least two spaces for each new level of indentation, because trying to read code that uses just a single space for each level of indentation is downright painful.
Please avoid block-style comments: /* comment */
Instead, please use inline-style comments: // comment
Always include a space after the // in your comments: // comment instead of //comment
The header comments introducing your source file (including the comment(s) with your name, course number, semester, NID, and so on), should always be placed above your import statements.
Use end-of-line comments sparingly. Comments longer than three words should always be placed above the lines of code to which they refer. Furthermore, such comments should be indented to properly align with the code to which they refer. For example, if line 16 of your code is indented with two tabs, and line 15 contains a comment referring to line 16, then line 15 should also be intended with two tabs.
Please do not write excessively long lines of code. Lines must be no longer than 100 characters wide. Avoid excessive consecutive blank lines. In general, you should never have more than one or two consecutive blank lines.
Please leave a space on both sides of any binary operators you use in your code (i.e., operators that take two operands). For example, use (a + b) c instead of (a+b)-c. (The only place you do not have to follow this restriction is within the square brackets used to access an array index, as in: array[i+j].) When defining or calling a method, do not leave a space before its opening parenthesis. For example:
use System.out.println(Hi!) instead of System.out.println (Hi!).
Do leave a space before the opening parenthesis in an if statement or a loop. For example, use use for (i = 0; i < n; i++) instead of for(i = 0; i < n; i++), and use if (condition) instead of if(condition) or if( condition ).
Use meaningful variable names that convey the purpose of your variables. (The exceptions here are when using variables like i, j, and k for looping variables or m and n for the sizes of some inputs.) Do not use var to declare variables.
5. Compiling and Testing GenericBST on Eustis (and the test-all.sh Script!)
Recall that your code must compile, run, and produce precisely the correct output on Eustis in order to receive full credit. Heres how to make that happen:
- To compile your program with one of my test cases:
javac GenericBST.java TestCase01.java
- To run this test case and redirect your output to a text file:
java TestCase01 > myoutput01.txt
- To compare your programs output against the sample output file Ive provided for this test case:
diff myoutput01.txt sample_output/TestCase01-output.txt
If the contents of myoutput01.txt and TestCase01-output.txt are exactly the same, diff wont print anything to the screen. It will just look like this:
[email protected]:~$ diff myoutput01.txt sample_output/TestCase01-output.txt [email protected]:~$ _
Otherwise, if the files differ, diff will spit out some information about the lines that arent the same.
- Ive also included a script, test-all.sh, that will compile and run all test cases for you. You can run it on Eustis by placing it in a directory with java and all the test case files and typing:
bash test-all.sh
Super Important: Using the test-all.sh script to test your code on Eustis is the safest, most sure-fire way to make sure your code is working properly before submitting.
Reviews
There are no reviews yet.