Could you please help me on this. This is the contunation of the JAVA program yo
ID: 3887680 • Letter: C
Question
Could you please help me on this. This is the contunation of the JAVA program you helped me last week.
1. Based on the below JAVA program Use a method named "outlist" to output the array.
void outlist (int [] numberlist, int listsize) and you would invoke it using
outlist(numbers, numitems);
2. Once your method for outputting the list works, add a third integer parameter to the argument list. If the value passed in is 0, the output is as it always was (from beginning to end). If the value is a 1, then output the list in reverse order.
So, if outlist(numbers, numitems, 0) output a list (for example) 1 4 2 5,
Then outlist(numbers, numitems, 1) would output 5 2 4 1.
You'll need a loop that starts at the end of the list, and works its way back to the beginning.
3. Modify the search operation to count how many times the item is found in the array (should return an answer from 0 up to numitems).
4. Find and display the largest item in the list.
import java.util.*;
public class ShuffleArray {
public static void shuffle(int[] a,int n) { // Calling a Static Method to swap
Random ran = new Random(); // Generating Random Numbers
ran.nextInt();
for (int i = 0; i < n; i++) {
int c = i + ran.nextInt(n - i); // Calling the Element positions
swap(a, i, c); // Calling Swap Function
}
}
private static void swap(int[] a, int i, int c) {
int help = a[i];
a[i] = a[c];
a[c] = help;
}
public static void main(String[] args) {
int[] a = new int[10] ;
Scanner sc=new Scanner(System.in);
int i;
System.out.println("Enter Elements One After the Other");
for(i=0;i<10;i++)
{
int x=sc.nextInt(); // Accepting Numbers
if(x<=0) // If Negative or 0 break
break;
else
a[i]=x; // Accepting Number
}
shuffle(a,i); // Calling Shuffled Code
System.out.println("The Shuffled List is");
for (int j=0;j<i;j++) { // Displaying
System.out.println(a[j]);
}
System.out.println("Enter a value");
int val=sc.nextInt(); // Accepting a Value for Search
boolean b=false;
for (int j=0;j<i;j++) {
if(a[j]==val) // If Found
{
b=true;
break;
}
}
if(b)
System.out.println("In the List");
else
System.out.println("Not in the List");
}
}
Instruction for above program:
Write a well structured and internally commented program according to the following specs
Create an array that can hold up to 10 integers.
Run through a loop asking the user to enter a positive integer (max of 10 times), but allow the input to stop early if the user enters a 0 or a negative number. Keep track of how many items are in the array and output that number after the input is complete.
Output the list.
Randomly reorganize the list (see the Shuffle program VideoNote we looked at in class Monday).
Ask the user to enter a single integer and output "IN THE LIST" or "NOT IN THE LIST" accordingly.
Output all of the values in the reorganized list.
That's it.
Sample data:
If the user enters 5 6 8 33 51 0
The array should have the values 5 6 8 33 and 51 in it, and you'd output something like:
There are 5 values in the list.
The list is: 5 6 8 33 51
The shuffled list is: 8 51 33 5 6
What value would you like to find? 7 <-- user entered the 7
7 is NOT IN THE LIST.
Explanation / Answer
import java.lang.*;
import java.util.*;
import java.math.*;
public class ShuffleArray {
public static void shuffle(int[] a,int n) { // Calling a Static Method to swap
Random ran = new Random(); // Generating Random Numbers
ran.nextInt();
for (int i = 0; i < n; i++) {
int c = i + ran.nextInt(n - i); // Calling the Element positions
swap(a, i, c); // Calling Swap Function
}
}
private static void swap(int[] a, int i, int c) {
int help = a[i];
a[i] = a[c];
a[c] = help;
}
public static void outlist(int [] numberlist, int listsize,int flag){ // Displaying Output to Console
if(flag == 0){
System.out.println("The list is:");
for(int i=0; i<listsize;i++){
System.out.print(numberlist[i]+" "); // As it was
}
System.out.println(" ");
}
else if (flag == 1){
System.out.println("List in Reverse Order: ");
for(int i=0; i<listsize;i++){
System.out.print(numberlist[listsize-i-1]+" "); // reverse order iterating from last element to first.
}
System.out.println(" ");
}
}
public static void main(String[] args) {
int[] a = new int[10] ;
Scanner sc=new Scanner(System.in);
int i;
System.out.println("Enter Elements One After the Other");
for(i=0;i<10;i++)
{
int x=sc.nextInt(); // Accepting Numbers
if(x<=0) {// If Negative or 0 break
System.out.println("There are "+i+" values in the list. "); // Displaying the length of array.
break;
}
else
a[i]=x; // Accepting Number
}
outlist(a,i,0); // Calling Outlist Code with flag = 0
outlist(a,i,1); // calling outlist code with flag = 1
shuffle(a,i); // Calling Shuffled Code
System.out.println("The Shuffled List is");
for (int j=0;j<i;j++) { // Displaying
System.out.print(a[j]+" ");
}
System.out.println(" What value would you like to find?");
int val=sc.nextInt(); // Accepting a Value for Search
int count = 0; // To count the number of occurrence of that no.
for (int j=0;j<i;j++) {
if(a[j]==val) // If Found
{
count++; // Counting Number of Occurrence
}
}
if(count>0){
System.out.println(val+" Is In the List");
System.out.println("Number of occurrence: "+count);
}
else
System.out.println(val+" Is Not in the List");
}
}