Question

In: Computer Science

Create a new Java project using NetBeans, giving it the name L-14. In java please Read...

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.

Solutions

Expert Solution

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);
}
}


Related Solutions

in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
Create a Netbeans project with a Java main class. (It does not matter what you name...
Create a Netbeans project with a Java main class. (It does not matter what you name your project or class.) Your Java main class will have a main method and a method named "subtractTwoNumbers()". Your subtractTwoNumbers() method should require three integers to be sent from the main method. The second and third numbers should be subtracted from the first number. The answer should be returned to the main method and then displayed on the screen. Your main() method will prove...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder....
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder. 3. Import Scanner and Stacks from the java.util package. 4. Create a Stack object named basket. 5. The output shall: 5.1.Ask the user to input the number of fruits s/he would like to catch. 5.2.Ask the user to choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G for guava. 5.3.Display all the fruits that the...
Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
Step 1: Create a new Java project in NetBeans called “Wedding1” Step 2: Use appropriate data...
Step 1: Create a new Java project in NetBeans called “Wedding1” Step 2: Use appropriate data types to store the following information: The names of the bride and groom The total number of guests at the wedding The square footage of the location (must be accurate to 0.1 square feet). The names of each song in the DJ's playlist. You should use an ArrayList of Strings to store this, and the user should be able to enter as many song...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b. Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT