snippetjavaMinor
Testing quicksort user input
Viewed 0 times
inputusertestingquicksort
Problem
I am testing a quicksort implementation. Arrays of random size with random data are passed to quicksort and the return time is averaged out. The user is prompted to enter values for
I tried breaking up the prompts for user input into several methods but things are getting ugly. Are there any guidelines to follow for when prompting the user for information? One thing I'm not sure about is when to prompt with a question mark vs a colon (e.g. "what is x?" vs "input x:").
```
public static void main(String[] args)
{
getInput();
}
public static void getInput()
{
int bound = getNumber("How many arrays should be generated for quicksort?");
int minVal = getNumber("Minimum value of element:");
int maxVal = getNumber("Maximum value of element:");
int minLength = getNumber("Minimum length of an array:");
int maxLength = getNumber("Maximum length of an array:");
boolean giveOutput = yes_no("Would you like to see the sorted and unsorted arrays?");
boolean checkCorrectness = yes_no("Check proccessed arrays to see if sorted correctly?");
long totalTime = 0, sumArraySizes = 0;
for(int i = 0; i < bound; i++)
{
int[] sample = generateRandomArray(minLength, maxLength, minVal, maxVal);
sumArraySizes += sample.length;
if(giveOutput)
System.out.println(Arrays.toString(sample));
long startTime = System.nanoTime();//was using System.currentTimeMillis(); but endTime and startTime would always be the same
qs(sample, 0, sample.length-1);
long endTime = System.nanoTime();
totalTime += endTime-startTime;
if(giveOutput)
System.out.println(Arrays.toString(sample)+"\n");
if(checkCorrectnes
- minimum length of array
- maximum length of array
- maximum value in array
- minimum value in array
- if the user wishes to see the sorted and unsorted arrays
- if the user wishes the program to check itself by testing to make sure the array was sorted properly
I tried breaking up the prompts for user input into several methods but things are getting ugly. Are there any guidelines to follow for when prompting the user for information? One thing I'm not sure about is when to prompt with a question mark vs a colon (e.g. "what is x?" vs "input x:").
```
public static void main(String[] args)
{
getInput();
}
public static void getInput()
{
int bound = getNumber("How many arrays should be generated for quicksort?");
int minVal = getNumber("Minimum value of element:");
int maxVal = getNumber("Maximum value of element:");
int minLength = getNumber("Minimum length of an array:");
int maxLength = getNumber("Maximum length of an array:");
boolean giveOutput = yes_no("Would you like to see the sorted and unsorted arrays?");
boolean checkCorrectness = yes_no("Check proccessed arrays to see if sorted correctly?");
long totalTime = 0, sumArraySizes = 0;
for(int i = 0; i < bound; i++)
{
int[] sample = generateRandomArray(minLength, maxLength, minVal, maxVal);
sumArraySizes += sample.length;
if(giveOutput)
System.out.println(Arrays.toString(sample));
long startTime = System.nanoTime();//was using System.currentTimeMillis(); but endTime and startTime would always be the same
qs(sample, 0, sample.length-1);
long endTime = System.nanoTime();
totalTime += endTime-startTime;
if(giveOutput)
System.out.println(Arrays.toString(sample)+"\n");
if(checkCorrectnes
Solution
Spaces
Put a space after
Put a space around a
And the same goes for
becomes:
if-statements
Add braces around if-statements:
Avoid nested if-statements such as:
Change that to:
(Consider throwing an exception for this by the way. A mistake is important stuff)
Naming
Whatever
Name your methods for inputting things as
Are there any guidelines to follow for when prompting the user for information? One thing I'm not sure about is when to prompt with a question mark vs a colon (e.g. "what is x?" vs "input x:").
I believe this can be answered better on User Experience. They probably have a question about it already.
I would think that a question mark should be used when asking a question, and a colon should be used when encouraging the user to enter something. Of course this doesn't say much though as all your user prompts can be modified to the other form. My opinion is that what you currently have is good though.
Other comments
-
Good choice to use
-
To make the existence of another line in your output clear, I would change
-
In Java, the convention is that
should be, according to the conventions:
-
Avoid multiple variable declarations on the same line
can be:
Put a space after
for and if:for(int i = 0; ifor (int i = 0; i
if(giveOutput)-->if (giveOutput)
Put a space around a
-:sample.length-1 --> sample.length - 1And the same goes for
+:System.out.println("Average time taken to sort array: "+totalTime/bound+" nanoseconds");becomes:
System.out.println("Average time taken to sort array: " + totalTime/bound + " nanoseconds");if-statements
Add braces around if-statements:
if (giveOutput) {
System.out.println(Arrays.toString(sample));
}Avoid nested if-statements such as:
if(checkCorrectness)
if(!isSorted(sample))
System.out.println("WARNING! mistake detected");Change that to:
if (checkCorrectness && !isSorted(sample)) {
System.out.println("WARNING! mistake detected");
}(Consider throwing an exception for this by the way. A mistake is important stuff)
Naming
Whatever
qs is doing (A quicksort I guess), the name is too short to be meaningful. Name it quicksort instead.Name your methods for inputting things as
input*:getNumber-->inputNumber
yes_no-->inputBoolean
Are there any guidelines to follow for when prompting the user for information? One thing I'm not sure about is when to prompt with a question mark vs a colon (e.g. "what is x?" vs "input x:").
I believe this can be answered better on User Experience. They probably have a question about it already.
I would think that a question mark should be used when asking a question, and a colon should be used when encouraging the user to enter something. Of course this doesn't say much though as all your user prompts can be modified to the other form. My opinion is that what you currently have is good though.
Other comments
-
Good choice to use
System.nanoTime();-
To make the existence of another line in your output clear, I would change
System.out.println(Arrays.toString(sample)+"\n"); to:if (giveOutput) {
System.out.println(Arrays.toString(sample));
System.out.println();
}-
In Java, the convention is that
{ goes on the same line, not on it's own line.public static int getNumber(String message)
{should be, according to the conventions:
public static int getNumber(String message) {-
Avoid multiple variable declarations on the same line
long totalTime = 0, sumArraySizes = 0;can be:
long totalTime = 0;
long sumArraySizes = 0;Code Snippets
System.out.println("Average time taken to sort array: "+totalTime/bound+" nanoseconds");System.out.println("Average time taken to sort array: " + totalTime/bound + " nanoseconds");if (giveOutput) {
System.out.println(Arrays.toString(sample));
}if(checkCorrectness)
if(!isSorted(sample))
System.out.println("WARNING! mistake detected");if (checkCorrectness && !isSorted(sample)) {
System.out.println("WARNING! mistake detected");
}Context
StackExchange Code Review Q#54567, answer score: 5
Revisions (0)
No revisions yet.