So Ive been instructed to write a class that generates an array (with return), sorts the generated array, reverses the sorted array (with return), and then check the sorted to see if any 2 adjacent #'s are the same (return True if there are, or False if there aren't).
I've managed to write everything out so far from what I can tell however my last set of instructions are to fill main with print statements that call on the lower methods I wrote to be displayed in the order written.
I managed to get the original randomly generated array to be called and printed, however I've had no luck at all on calling any of the other methods like I did the first and have tried everything I can think of to get them to just print their results with the provided "This is the blah blah blah:"
If anyone could point me in the right direction as far as being able to call the other methods to print their results, along with each print out to include a basic statement like "The Random array is: ", "The Sorted array is: ", "The Reversed array is: ", "It is (TRUE/FALSE) this array has adjacent duplicates." I would be very much appreciative. Nothing I've tried at this point has worked and im at a complete standstill. I've been trying to work friends who are familiar with java as well but we seem to be stuck too. This is what I've written so far...
import java.util.*;
public class SortedSequence
{
public static void main(String[] args)
{
int[] randomNumbers = new int[20];
randomNumbers = generateRandom(20);
printArray(randomNumbers);
// This is where Im getting stuck at. Everything I've tried makes a compile error
}
public static int[] generateRandom(int n)
{
int[] genNumbers = new int[n];
Random rand = new Random();
for (int i = 0; i < genNumbers.length; i++)
{
int bubble = rand.nextInt(100);
genNumbers[i] = bubble;
}
return genNumbers;
}
public static void sortArray(int[] genNumbers)
{
Arrays.sort(genNumbers);
}
public static int[] reverse(int[] x)
{
int[] sortArray = new int[x.length];
for (int i = 0; i < x.length; i++) {
sortArray[i] = x[x.length - 1 -i];
}
return sortArray;
}
public static boolean adjacentDuplicates(int[] boo)
{
boolean duplicates = false;
for (int i = 0; !duplicates && i < boo.length-1; i++)
if (boo[i] == boo[i+1]);
{
duplicates = true;
}
return duplicates;
}
public static void printArray(int[] print)
{
for (int i = 0; i < print.length; i++) {
System.out.print(print[i] + " "); }
System.out.println();
}
}
I compiled the code, and I'm not getting any errors whatsoever.
I added a
printArray(reverse(randomNumbers));to the main class, like so:and upon running the program, it returned:
It looks like it's an issue with your compiler, or how you were calling that class.
Answers to subsequent questions.
Here's what you needed for the sorted class. The Arrays.sort() method takes the array and manually moves them around inside it. :
Also, you had the return for it set as void, not int[].
Adjacent Duplicates
I honestly have no idea what the hell was going on in the adjacentDuplicates. Looks like the if and for methods did nothing, since you put a
;directly after the).Your code needs to come between the end of the
)and the;, or you need to use{}after the for loop declaration (shown below).Also, Using those nextline brackets is terrible for readability, and I would advise against it. Here's what the adjacentDuplicates method should look like.
Running that
Now, here's how we can print all that out and get readable info:
Test run:
Here's the full class. Listed again at bottom.
Newline Stuff
It appears that you want it formatted differently. I'll let you have the enjoyment of getting to do that, but do note:
Will print this and not add a newline at the end
Will print the string and add a newline.
So
Is exactly like running
Both of which would print:
Here's all of that code