In: Computer Science
Create a new Java project using NetBeans, giving it the name L-14. In java please
Read and follow the instructions below, placing the code statements needed just after each instruction. Leave the instructions as given in the code.
Declare and create (instantiate) an integer array called List1 that holds 10 places that have the values: 1,3,4,5,2,6,8,9,2,7.
Declare and create (instantiate) integer arrays List2 and List3 that can hold 10 values.
Write a method called toDisplay which will display the contents of the List1 (on one line with spaces between each value)
Write a method called toDisplayWithIndex which will display the contents of the List1 (on separate lines with index, a space, a colon, a space and the value. ( 1 : 3 )
Call the method toDisplay for List1 in the main
Call the method toDisplayWithIndex for List1in the main
Assign the value 15 to the first and 23 to the last elements of the array List1
Display the new contents of the array List1 using your method toDisplay
Write a method called makeRandom which will fill the array with random integers from 1 to 1000.
Invoke makeRandom in the main to adjust List2 with these random values.
Display List2 using toDisplayWithIndex
Write a method called makeWithInput which will fill the array with numbers inputted by the user.
Invoke makeWithInput in the main to load List3
Display the contents of the List3 array using your method toDisplay
Write a method called makeWithIndex which will fill the array List1 with the value of the index (List[0] = 0 List[1] =1 etc )
Invoke makeWithIndex in the main to adjust List1 with these index values.
Invoke toDisplayWithIndex for List1
=======================
Write a method called calcSum that will use a for loop to find and return the sum of all elements in the array
Display the sum for all 3 arrays
Write the method calcAvg which will calculate the average of the array.
Display the average for all 3 arrays
Write a method called getSmallest that will use a loop to find and return the index of the smallest element in the array List2 and List3
Display the index of the smallest element and the value of the smallest element in List2 and List3
Write a method called shuffleArray which will shuffle the values in the array. Use your book as a reference!
Shuffle List1 then call the method toDisplayWithIndex
Write a method called bubbleSort . Use your book as a reference!
Sort List2 then call the method toDisplayWithIndex
Copy your final working output and the source code to a WORD doc.
Editable Code:
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class JavaProg
{
static void toDisplay(int List1[])
{
for(int i=0;i<List1.length;i++)
System.out.print(List1[i] + " ");
}
static void toDisplayWithIndex(int List1[])
{
for(int i=0;i<List1.length;i++)
System.out.println(i + " : " + List1[i] + " ");
}
static void makeRandom(int List2[])
{
Random random = new Random();
for(int i=0;i<List2.length;i++)
{
int randomNum = random.nextInt((1000 - 1) + 1) + 1;
List2[i]=randomNum;
}
}
static void makeWithInput(int List3[])
{
Scanner sc = new Scanner(System.in);
for(int i=0;i<List3.length;i++)
{
System.out.print("Enter List3[" + (i) + "] ");
List3[i]=sc.nextInt();
}
}
static void makeWithIndex(int List1[])
{
for(int i=0;i<List1.length;i++)
List1[i]=i;
}
static void calcSum(int List[])
{
int sum=0;
for(int i=0;i<List.length;i++)
sum = sum+List[i];
System.out.print(sum);
}
static void calcAvg(int List[])
{
int sum=0,avg;
for(int i=0;i<List.length;i++)
sum = sum+List[i];
avg = sum/List.length;
System.out.print(avg);
}
static void getSmallest(int List[])
{
int small = List[0];
int large = List[0];
int index=0;
for(int i=1; i< List.length; i++)
{
if(List[i] > large)
large = List[i];
else if (List[i] < small)
{
index = i;
small = List[i];
}
}
System.out.print(small);
System.out.print(" (Index is: " + index + ")");
}
static void shuffleArray(int List1[])
{
Random rnd = ThreadLocalRandom.current();
for (int i = List1.length - 1; i > 0; i--)
{
int ind = rnd.nextInt(i + 1);
int a = List1[ind];
List1[ind] = List1[i];
List1[i] = a;
}
}
static void bubbleSort(int List2[])
{
int n = List2.length;
int temp = 0;
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(List2[j-1] > List2[j])
{
temp = List2[j-1];
List2[j-1] = List2[j];
List2[j] = temp;
}
}
}
}
public static void main(String[] args)
{
int List1[] = {1,3,4,5,2,6,8,9,2,7};
int List2[] = {5,8,9,6,7,4,3,5,4,2};
int List3[] = {6,9,8,7,4,2,1,5,6,3};
toDisplay(List1);
System.out.println("\n");
toDisplayWithIndex(List1);
System.out.println();
List1[0]=15;
List1[9]=23;
toDisplay(List1);
System.out.println();
makeRandom(List2);
System.out.println();
toDisplayWithIndex(List2);
System.out.println();
makeWithInput(List3);
System.out.println();
toDisplay(List3);
System.out.println();
makeWithIndex(List1);
System.out.println();
toDisplayWithIndex(List1);
/*System.out.println();
toDisplay(List1);
toDisplay(List2);
toDisplay(List3);*/
System.out.println("\nCalculating Sum");
System.out.print("\nSum of List1: ");
calcSum(List1);
System.out.print("\nSum of List2: ");
calcSum(List2);
System.out.print("\nSum of List3: ");
calcSum(List3);
System.out.println("\n\nCalculating Average");
System.out.print("\nAverage of List1: ");
calcAvg(List1);
System.out.print("\nAverage of List2: ");
calcAvg(List2);
System.out.print("\nAverage of List3: ");
calcAvg(List3);
System.out.println("\n\nGet Smallest");
System.out.print("\nSmallest of List1: ");
getSmallest(List1);
System.out.print("\nSmallest of List2: ");
getSmallest(List2);
System.out.print("\nSmallest of List3: ");
getSmallest(List3);
shuffleArray(List1);
System.out.println("\n");
toDisplayWithIndex(List1);
bubbleSort(List2);
System.out.println("\n");
toDisplayWithIndex(List2);
}
}