Question:
Method and Class documentation in Java?
CLH22101
2013-01-24 10:30:21 UTC
Ok so I have completed this program. I was to put the numbers in order from largest to smallest. My teacher has been saying that I am lacking in my comments throughout the program. Can you please add in method documentation and other comments throughout? I want to make sure I can get full credit for this because obviously I am lacking in my comments.

public class ThreeNumberSort
{
public static void main(String[] args)
{
String[][] data =
{
{"102", "89", "56"},
{"103", "90", "57"},
{"104", "91", "58"},
{"105", "92", "59"},
{"107", "50", "20"},
{"108", "60", "10"}
};

for (int i = 0; i < data.length; i++)
{
System.out.println( sortDataNums( data[i] ) );
}
}

private static String sortDataNums(String[] d)
{
int[] values = new int[d.length];
for (int i = 0; i < values.length; i++)
{
values[i] = Integer.parseInt(d[i]);
}

for (int i = 0; i < values.length - 1; i++)
{
for (int j = i + 1; j < values.length; j++)
{
if (values[i] < values[j])
{
int temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
String sorted = "";
for (int i = 0; i < values.length; i++)
{
sorted += "" + values[i] + ",";
}
return sorted.substring(0, sorted.lastIndexOf(","));
}
}
/**
Output:
102,89,56
103,90,57
104,91,58
105,92,59
107,50,20
108,60,10
*/
Three answers:
Matthew
2013-01-24 12:11:51 UTC
public class ThreeNumberSort

{

public static void main(String[] args)

{

// test data

String[][] data =

{

{"102", "89", "56"},

{"103", "90", "57"},

{"104", "91", "58"},

{"105", "92", "59"},

{"107", "50", "20"},

{"108", "60", "10"}

};



for (int i = 0; i < data.length; i++)

{

System.out.println( sortDataNums( data[i] ) );

}

}

// Precondition: String is numbers

// Postcondition: Numbers are sorted in a descending order

private static String sortDataNums(String[] d)

{

int[] values = new int[d.length];

for (int i = 0; i < values.length; i++)

{

values[i] = Integer.parseInt(d[i]);

}



for (int i = 0; i < values.length - 1; i++)

{

for (int j = i + 1; j < values.length; j++)

{

if (values[i] < values[j]) // actual sorting algorithm

{

int temp = values[i];

values[i] = values[j];

values[j] = temp;

}

}

}

String sorted = "";

for (int i = 0; i < values.length; i++) // loop to place numbers in string

{

sorted += "" + values[i] + ","; // Place numbers in the string

}

return sorted.substring(0, sorted.lastIndexOf(","));

}

}

/**

Output:

102,89,56

103,90,57

104,91,58

105,92,59

107,50,20

108,60,10

*/



Oh by the way, I'd recommend testing this program for a data set or two that are actually out of order. All the ones you used are already in order.
anonymous
2013-01-25 11:53:32 UTC
Matthew and Godfather, you both missed about Java @ Documentation. Please see here how to add Comments that can be used by javadoc.exe to create automatically the documentation for your Classes and Methods:



A doc comment is written in HTML and must precede a class, field, constructor or method declaration. It is made up of two parts -- a description followed by block tags. See the full list of JAVA DOC TAGS:



http://www.tutorialspoint.com/java/java_documentation.htm



Tag

@author

@deprecated

{@docRoot}

@exception

{@inheritDoc}

{@link}

{@linkplain}

@param

@return

@see

@serial

@serialData

@serialField

@since

@throws

{@value}

@version
godfatherofsoul
2013-01-24 20:12:12 UTC
Did you write this program? Commenting is easy:



// This is a one line comment



/*

This is a

multi-line

comment.

*/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...