All pastes #389273 Raw Edit

Something

public text v1 · immutable
·published 2007-03-10 16:49 UTC
rendered paste body
Mod 1. Write the method arrayToString. Write a method named arrayToString.
a. The method must take two parameters: (1) an array of elements of type double and (2) a
number of type int representing the number of elements actually inserted into the array.
b. The method must return a string that lists the elements of the array as one line of text,
with a blank space separating the successive elements of the array. This string must be comprised
of just the elements actually inserted into the array. The numbers of type double should display
3 decimal positions.
c. The returned string is intended for display purposes, to be printed to the Command
Prompt window or a JOptionPane dialog or the NetBeans Output pane by the calling
method.

6. Mod 2. Replace existing code within main for-loop with a call to arrayToString
method.
a. Modify the existing program code so that the new arrayToString method is called within
the main for-loop to display the list of elements in each popup dialog after the text “Array
elements:”. In other words, there is existing code that creates a string of text to list the array
elements entered thus far (in the previous passes through the loop body). You are to replace this
existing code with a call to the new method instead. Pass the array and element count into the
new method as parameters.
b. As a result of your modification, the four lines of code shown below will be replaced by
one line of code. The new one line will call the arrayToString method and assign the return
value to variable arrayAStr.
arrayAStr = "";
for (int j = 0; j < countA; j++) {
arrayAStr += A[j] + " ";
}

7. Mod 3. Modify the code that displays the final array information so that it uses
the arrayToString method.
a. Modify the existing program code that follows after the main for-loop. The
arrayToString method must be called in the code after the for-loop finishes (below the forloop).
This code displays the final list of array elements.
i. On the first line, the output display should continue to show the total
number of elements in the array.
ii. On the second line, the display should show the phrase “The array elements
are:”, followed by the list of the actual elements, all on one line. That is, the display
of the final list of array elements in the Output pane of NetBeans should still look
the same as in the window capture above, with the same explanatory label, but the
code must call the method arrayToString.
iii. On the third line, the output should continue to display the sum of the
array elements.

8. Mod 4. Add code to display a JOptionPane dialog after the for-loop.
a. The dialog should display all of the output listed above as part of Mod 3.
b. This same output string should be displayed to both the NetBeans Output pane and the
newly added JOptionPane dialog.

9. Mod 5 Add code for the selection sort method. The method must take a double array as
a parameter. It has no return. See the code for the selection sort method in your text in
Chapter 6. Place the code appropriately in this class.

10. Mod 6. Create and populate array B.
a. In the main method of the program, declare and create a second array named B. When
declaring the new array B, the size of the array should be specified using the same symbolic
constant as used for the existing array A. Do not declare another constant. That is, use the same
unmodifiable (final) variable as is currently used to declare the size of array A.
b. Add another for-loop to populate the new array B. The new for-loop should get each array
element for array B from the user using a JOptionPane popup dialog, just as for array A.
c. Each popup dialog must list the elements entered thus far for array B, just as for array A.
d. Use the arrayToString method, as for array A, and display array B in a JOptionPane
dialog.

11. Mod 7. Display the number of elements, contents, and sum for array B.
a. After the populating array B (described above), add code to display the following (just
like for array A):
o The number of elements in the array.
o The list of the elements in the array. Be sure to invoke the method arrayToString.
o The sum of the elements in the array. Be sure to invoke the method arraySum.
b. Add code to display the results in a JOptionPane dialog as well as in the Output pane of
NetBeans, just like for array A.

12. Mod 8. Present a final display of all the program results (output) in a JOptionPane
dialog in addition to the Output pane of NetBeans. Invoke the method selectionSort for both
arrays, A and B. In this final JOptionPane dialog, display all the information that was previously
presented on both arrays including the number of elements, list of elements (now in sorted order),
and sum of elements for each of arrays A and B. Be sure to invoke methods arrayToString and
arraySum for each array.

13. Format the source code properly:
a. Reformat the Java code so that it has proper indentation and vertical and horizontal
alignment.
b. Be sure to have a blank line right above each method definition.