CSCI.UA0101: Homework 3

Section 001 (Honors): 30 points Section 002: 30 points + 0 bonus Updates: 2/21 @ 18:00pm: Requirements for Exercise 3 have been changed in regards to return values and javadoc clarification. 2/23 @ 1:00pm: A bug in the code for printChars() in Helper.java was corrected. Instructions: Answer the questions stated in the following exercises by submitting plain-text or Java source code (.txt and .java extensions respectively) as stated in the exercises through NYU Classes. No word (.doc/.docx/etc.) or rich-text (.rtf) files please (in your text editor do "Save file as plain-text"). Java programs should be just the Java source code (not your .class files or Eclipse workspaces) unless otherwise stated. In all files submitted include a documentation header using comments which include: /** * [Description of the program] * @author: [Your Name] */ Points will be deducted for bad coding style (up to 25% of your grade), so: - use good variable/function names (self-documenting code) - avoid hardcoding values - provide comments for complex code blocks For more details regarding good coding style guidelines see the grading criteria.

Exercise 1 - Javadocs/Functions

Points: 10 Harry Halfway is a decent coder, but he tends to get distracted and only write about half his code. You can download his code Helper.java and a matching test class Tester.java. Javadocs: Javadocs are an important part of software development. In fact, documentation is so important that a documentation system (javadocs) is built-in to the java language. Having proper documentation for functions allows developers to quickly and effectively utilize your code. All of the Java API is produced using javdoc comments that are stored in the java runtime code itself. Javadocs start with /** and end with */ similar to regular block comments. As programmers we should utilize this type of feature whenever we write new classes (we'll learn more about classes as we go along, but for now you can think of them as program components), or new functions. Javadocs support most standard HTML tags such as <code> (used for code references), <pre> (used for pre-formatted text), <b> (bold), <i> (italic), etc. In addition to stanard java tags such as "@author" and "@version" for Class documentation, and "@param" and "@return" for method/function documentation. You should get in the habit of providing this documentation (and will do so throughout this exercise). In Eclipse, you can test javadocs by selecting "Project -> Generate Javadocs" from the Eclipse menu. You may need to find the 'javadoc.exe' or 'javadoc' program. It would have been included as part of the JDK you installed earlier in Homework0. Next select the project source files to run javadoc against and click "Finish". You'll want to fix any warnings/errors produced when generating the javadocs. The docs will be created as a new directory called 'doc' in your Eclipse project's workspace directory (the same location as your 'src' (your .java files) and 'bin' (your .class files) are stored in). Open the index.html file located in the 'doc' directory in the browser to see the documentation generated. You'll also notice that Eclipse will display the javadoc information whenever you mouse over a function call to those functions (just like it does with built-in java functions). Importing the files: If you create a new Eclipse project you can add the java files mentioned (Helper/Tester) to that project by using the File -> Import -> General -> File System -> Next. Then choose directory where you downloaded the files -> Click next. Then select the files downloaded. Make sure that the files show up in appropriate project and in the 'src' directory. If not, move them to that location. Running the code: You'll notice that the functions in Helper.java can be accessed in Tester.java by specifying the ClassName.FunctionName(...). Also, you'll notice that Tester.java provides a main(...) while the Helper class does not. This means that only the Tester class can be run since a main is required to run as a program. When you try to run the project you'll have to have the Tester.java file selected. What to modify: You'll be writing your code in Helper.java and then test it by running the Tester.java program. The Tester.java file shouldn't need to be modified. If there are problems with your code, Tester will print out which functions have problems. If you look in the Helper.java code, you'll notice several TODO comments. Replace these comments with the code/comments that should take their place. Remember that when documenting functions, you should use javadocs that specify
  1. a description of that program
  2. @param tags for each parameter
  3. an @return tag for any data that is returned
For more information on Javadocs (other available tags, examples of it's usage, etc.), see Oracle's Javadoc Documentation. Save and submit your program (just the source code) as Helper.java.

Exercise 2 - Nested Loops

Points: 10 Being able to nest programming logic is an important part of programming and allows us to perform complex tasks. In this program, you are required to write a program that creates and prints a matrix (set of numbers organized in rows and columns) using a nested loop structures (a loop inside a loop). Your program should, ask the user for the size of a matrix to print. Using a nested loop structure, print the matrix with all values equal to the row being printed (starting with 1's). Sample input/output: Size of matrix to print: 5 Matrix: 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 Save and submit your program (just the source code) as Matrix.java.

Exercise 3 - Function based Matrix

Points: 10 In this assignment you will be taking advantage of writing functions (often called a 'method' in java) to help simplify the problem. Functions provide many benefits:
  1. It allows us to simplify the amount of work each block of code has to do. Allowing us to more easily write and test code.
  2. It allows us to have a limited scope of knowledge as once a function is written, we don't have to understand how it works, but what it does.
  3. It allows us to write code once and re-use it as often or from as many programs as wanted.
Rewrite Matrix.java using at least two functions that have the following specification. /** * Prints <code>number</code> <code>count</code> times with a space in between * each number and returns the total of all numbers printed. * * Sample output of calling printNumbers(3, 5): * <pre> * 3 3 3 3 3 * </pre> * This function call would return the value 15. * * @param number to print multiple times * @param times is the number of times to print <code>number</code> * @return the total of all the numbers printed */ int printNumbers(int number, int times) /** * Prints a matrix with all values equal to the row being printed (starting with 1's). This function should return the total of all values printed. * * Sample output of calling printMatrix(5): * <pre> * 1 1 1 1 1 * 2 2 2 2 2 * 3 3 3 3 3 * 4 4 4 4 4 * 5 5 5 5 5 * </pre> * This function call would return the value 75. * * @param size of matrix (in columns and rows) to print * @return the total accumulated value of all values printed. */ int printMatrix(int size) Save and submit your program (just the source code) as MatrixFun.java.