Title: Project 1 Discussion and Documentation
1Project 1 Discussion and Documentation
2Algorithm for Main class
3Structure of Report class
-inFile -grantTotal -genreCount -currentBook,
nextBook -newGenre -maxTotalGenre -maxGenre
FileInput
Fields
Book
Report object
printReport -processBook -processGenre -columnHea
dings -padL -padR
Methods
4Algorithm for printing a report (Report.java)
Open file
Print footer of the report
processing a book
5Algorithm for process a book and a genre
currentBook record just read Current genre
genre of the current book
Start a new genre and go back to processing a new
genre
6Documentation
- Source code documentation is a fundamental
engineering practice critical to efficient
software development. Regardless of the intent of
its author, all source code is eventually reused,
either directly, or just through the basic need
to understand it. In either case, the source code
documentation acts as a specification of behavior
for other engineers. -
Without documentation, they are forced to get
the information they need by making dangerous
assumptions, scrutinizing the implementation, or
interrogating the author. These alternatives are
unacceptable. (http//www.literateprogramming.com/
quotes_sc.html)
7Some examples
- Mysql 5.0 server documentation
- Documentation for String class in Java
- Documentation for JOptionPane class in Java
- GNU documentation
8Questions for us
How can we generate a similar document for our
own class?
How can we make our code more readable?
9Javadoc tool
- generating Application Program Interface (API)
documentation in HTML format from doc comments in
source code. - Resource
- http//java.sun.com/j2se/javadoc/
-
10What we focus on in this course
- How to write simple doc comments for Javadoc?
- How to run Javadoc
11Template for Class Definition
Import Statements
Class Comment
Class Name
public class
Data Members
Methods (incl. Constructor)
12Template for Class Definition
Import Statements
Class Comment
A doc comment must precede a class, data
members, constructor or method declaration
public class
Class Name
Data Member Comments
Data Members
Methods Comments
Methods (incl. Constructor)
13Class comment
- First sentence should be a summary sentence,
containing a concise but complete description of
a class. - The remaining of the class comments should talk
about the purpose of this class
14Example
- String class in Java
- JOptionPane class in Java
- Our Lab 2
- / This class provides various routines to
convert metric measurements to U.S. units and
vice versa. /
15Data member comments
- / data members description
- /
- public ltdatatypegt ltfield namegt
- Note The documentation is only generated for
those methods/fields that are declared as public
16Example Our Lab 2
- /A factor to convert inches to centimeters /
- public static final double INCHES_TO_CENTIMETERS
2.54 -
- / A factor to convert centimeters to inches /
- public static final double CENTIMETERS_TO_INCHES
1 / INCHES_TO_CENTIMETERS -
- / A factor to convert feet to inches /
- public static final double FEET_TO_INCHES 12.0
17Result
18Result
19Constructor/Method comments
- Purpose what this method is doing
- Data (parameters, what they are) and
- Details description of data (parameters name,
their data types)
20Example
- / Converts a given length in inches to
equivalent centimeters. - _at_param inches the length expressed in
inches - _at_return length expressed in centimeters
- /
- public double inchesToCentimeters( double inches
) -
- //end inchesToCentimeters
21Results
22Writing Doc comments
- Block tags
- _at_param (classes, interfaces, methods and
constructors only) - _at_return (methods only)
- _at_exception (_at_throws is a synonym added in
Javadoc 1.2) - _at_author (classes and interfaces only, required)
- _at_version (classes and interfaces only, required.
23Required tags
- _at_param ltname of parametergt ltdescriptiongt
- For example
- public void writeToFile(String fileName)
- _at_param fileName this represents the name of the
file created for writing purpose.
24Require tags
- _at_throws ltexceptiongt ltdescriptiongt
- Example
- public void writeToFile(String filename, int
array) throws IOException - _at_throw IOException if any error happens during
the process of writing to a file.
25Required tags
- _at_author ltname of authorgt
- _at_version ltversion numbergt, ltdategt
26Required tags
- _at_return ltdescription of the item returnedgt
- Note Omit this for constructors and void methods
- Example
- public void writeToFile(String fileName) ???
- public boolean readFromFile(String fileName)
- _at_return a boolean value (true/false) if there is
no error/or any errors during reading process.
27Example
- /
- What do I write here?????
- /
- public void readFromFile(String filename) throws
IOException - / Assuming data read is stored in an integer
array /..
28Example
- /
- Purpose read data from a file given a file
name and store data in an integer array - _at_param filename represents the name of file
from which data is read - _at_throw IOException if any error happens during
the process of reading data from this file. - /
- public void readFromFile(String filename) throws
IOException - ..
29How to run Javadoc
- Step 1 Make sure you have javadoc installed
- Step 2 Command line mode
- change to the source directory
- type
- javadoc -d ltdocumentation directorygt package
name - Or
- javadoc -d ltdocumentation directorygt ltjava file
namesgt
30Lab 3
- Step 1 Open eclipse and your MetrixConverter.java
class. - Make sure this class is declared as public.
- Insert comments for feetAndInchesToCentimeters
method - Step 2 Open your Main.java (done in Lab2)
- Insert class comment for Main.java
- Insert data member comments (if applicable)
- Insert method comments for other methods (if
applicable) - Step 3 Using javadoc, generate documentation for
this two classes
31Documentation (continue)
How can we generate a similar document for our
own class?
How can we make our code more readable?
32Documentation
- Comments along the code
- Create meaningful variable names
- Example
- public class DataProcessing
-
- Vs
- public class DiscountedLoan
-
-
33Documentation
- Create meaningful variable names
- int h1, h2
-
- for (int i0 iltlengthName i)
-
-
- Define constants
- public static final double INCHES_TO_CENTIMETERS
2.54
34Arrays
- Array is a collection of data values of the same
data type. - Example
- int number // This is an array of integers
- double gpa // This an array of double
-
-
35Array Declaration
- Format
- ltdata typegt ltarray_namegt
- OR
- ltdata typegt ltarray_namegt
- data type double, int, float, string
- Example
- double gpa
- Or
- double gpa
36Arrays
- The amount of memory allocated to store an array
depends on the size and type of values in the
array - Size number of elements in the array
- Type of values data type of each element in the
array - An individual value in an array is called array
element - Example
- int number new int5
- data type integer (4 bytes)
- size 5
- memory 20 bytes
-
Array is a reference data type. Array is NOT an
object
37Arrays
- Example
- int number new int5
number0
number
6
1
2
3
0
4
38Arrays
- Elements of an array are indexed from zero to
number of elements in an array -1 - length a public constant represents the size of
an array. - Example
- sum 0
- for (int i0 iltnumber.length i)
- sum numberi
39Fixed size array declaration
- Size of an array is pre-determined.
- Example
- int number new int5
- Problems
- What if we have more than pre-determined size of
an array? - Underutilization of space.
40Variable-size array declaration
- In Java, we can declare an array of different
size every time we run a program - Example
- int size
- int number
-
- inputStr JOptionPane.showInputDialog(null,"Plea
se enter the number of elements ") - size Integer.parseInt(inputStr)
- number new intsize
41On the fly review
- Given a one dimensional array myArray, what is
the correct way of getting the number of elements
in this array. - a. myArray.length
- b. myArray.length - 1
- c. myArray.size
- d. myArray.size 1
42On the fly review
- Given a one dimensional array myArray, what is
the correct way of getting the number of elements
in this array. - a. myArray.length
- b. myArray.length - 1
- c. myArray.size
- d. myArray.size 1
43On the fly review
- Array is a collection of data values of the
different data types - a. True
- b. False
44Arrays for Objects
// An array for the names of the distinct
private Loan loanArray new Loan5
- Note No Loan objects are created. Only a
container for Loan. - Each location is initialized to null.
loanArray
NULL
45Arrays of Objects
- private Loan loanArray new Loan5
- for (i0 ilt5 i)
- loanArrayi new Loan()
-
loanArray
46Indexing an Array
- int number new int 5
- number0 2
- number13
- number24
- number3 6
- number4-1
47Initializing Arrays
... // Differentially number the
assignments. private int number 1, 2, 1,
5, 1, private final int totalNumber
number.length
- No new required.
- Terminating semicolon.
- Optional final comma.
48Further Initializer Examples
String suitNames "Spades", "Hearts",
"Diamonds", "Clubs" Point vertices
new Point(0,0), new Point(0,1), new
Point(1,1), new Point(1,0), YearlyRainfall
y2k new YearlyRainfall( new
int10,10,8,8,6,4,4,0,4,4,7,10,)
49Passing Array to Methods
- When an array is passed to a method, only its
reference is passed. A copy of the array is not
created in the method. - That means
- we pass the identifier for that array which in
fact is a reference to a start address of the
array. -
50Passing Array to Methods
- Assuming changeArrayValue is one method of a
class named ArrayClass - public void changeArrayValue(int arrayChanged)
- for (int i0 ilt arrayChanged.length-1 i)
- arrayChangedi 1
-
- We call this method as
51Passing Array to Methods
- ArrayClass anObj new ArrayClass()
- int number5 new int5
- for(int i0 iltnumber.length i)
- number0 i
- anObj.changeArrayValue(number)
52Passing Array to Methods
- for(int i0 iltnumber.length i)
- number0 i
- anObj.changeArrayValue(number)
number
number
53On the fly review
- The amount of memory allocated to store an array
depends on - a. the name of the array
- b. the size of the array
- c. the size and type of values in the array
- d. none of the above
54On the fly review
- The amount of memory allocated to store an array
depends on - a. the name of the array
- b. the size of the array
- c. the size and type of values in the array
- d. none of the above
55On the fly review
- Arrays are
- a. variable-length entities.
- b. fixed-length entities.
- c. data structures that contain up to 10 related
data items. - d. used to draw a sequence of lines, or rays.
56On the fly review
- Arrays are
- a. variable-length entities.
- b. fixed-length entities.
- c. data structures that contain up to 10 related
data items. - d. used to draw a sequence of lines, or rays.
57The Arrays Class
- Defined in the java.util package.
- Contains static methods for manipulating arrays
- binarySearch search for a value.
- equals compare the contents of two arrays.
- fill fill an array with a particular value.
- sort sort the contents of an array.
58Multi-Dimensional Arrays
- Arrays of multiple dimensions are possible.
- 2D grid, for a board game such as chess.
- 3D cube structure, etc.
- Multi-dimensional arrays are regarded as being
arrays of arrays. - Non-rectangular structures are possible.
592D Array Construction
final int numRows 10, numCols 5 double
matrix new doublenumRowsnumCols char
hiddenWord 'd', 'g', 'i', 'b' ,
'e', 'i', 'u', 'm' , 't', 'a', 's', 'a'
,
60Review
- Arrays make it possible to group related items in
a single object. - An array's length is fixed on construction.
- Arrays may have multiple dimensions.
61Multidimensional Arrays
- Multidimensional arrays
- Tables with rows and columns
- Two-dimensional array
- m-by-n array
62Two-dimensional array with three rows and four
columns.
63Multidimensional Arrays (Cont.)
- Arrays of one-dimensional array
- Declaring two-dimensional array b22
- int b 1, 2 , 3, 4
- 1 and 2 initialize b00 and b01
- 3 and 4 initialize b10 and b11
- int b 1, 2 , 3, 4, 5
- row 0 contains elements 1 and 2
- row 1 contains elements 3, 4 and 5
64Multidimensional Arrays (Cont.)
- Two-dimensional arrays with rows of different
lengths - Lengths of rows in array are not required to be
the same - E.g., int b 1, 2 , 3, 4, 5
65Multidimensional Arrays (Cont.)
- Creating two-dimensional arrays with
array-creation expressions - Can be created dynamically
- 3-by-4 array
- int b b new int 3 4
- Rows can have different number of columns
- int b
- b new int 2 // create 2 rows b 0
new int 5 // create 5 columns for row 0
b 1 new int 3 // create 3 columns for
row 1
66Multidimensional Arrays (Cont.)
- Common multidimensional-array manipulations
performed with for statements - Many common array manipulations use for
statements - E.g.,
- for ( int column 0 column lt a 2 .length
column ) a 2 column 0 -
67On the fly review
- In array items, which expression below retrieve
the value at row 3 and column 5? - a. items 2 . 4
- b. items 2 4
- c. items 2 4
- d. items 2, 4
68On the fly review
- In array items, which expression below retrieve
the value at row 3 and column 5? - a. items 2 . 4
- b. items 2 4
- c. items 2 4
- d. items 2, 4
69On the fly review
- An array with m rows and n columns is NOT
- A. An m-by-n array. B. An n-by-m array.
- C. A two-dimensional array.
-
- 1. A and C.
- 2. A.
- 3. B.
- 4. C.
70On the fly review
- An array with m rows and n columns is NOT
- A. An m-by-n array. B. An n-by-m array.
- C. A two-dimensional array.
-
- 1. A and C.
- 2. A.
- 3. B.
- 4. C.