Objectives .To implement a sort with Generic abilities Project setup Use NetBean
ID: 3761435 • Letter: O
Question
Objectives .To implement a sort with Generic abilities Project setup Use NetBeans to create the project mp08 and rename the suggested main class to Main Refer to the appropriate "how to" tutorialsavailable on the course website, for instructions on how to create a project and manage its files Language features used classes Generics Problem Description In this mp you are going to implement a sorting algorithm described belovw An array of integers or characters, can be sorted not by comparison but by distribution based on the value itself. Given an array of 200 numbers in the range 1-100, you will count the frequency of occurrence of each number and store these tallies in an array frequency. The array frequency will be of size 100 + 1, since index [01 will not be used if the numbers range from 1-100. A similar argument can be made for characters, since characters are integers internally (ASCII) and thus the usable range would then be [65]-[90] Each index of the frequency array represents one of the 100 generated numbers, and the element itself represents that number's frequency. So, if index [4] has the value 5, it means that the number 4 occurred 5 times. Similarly if index [65] has the value 3, it means that the character 'A' occurred 3 times To sort the array you then simply traverse the frequency array and display each index the designated number of times as given by its element's value. In the above reference, you would display the number 4, five times and the character 'A 3 times Implementation requirements (1) The array frequency is of size 101 (2) Create two arrays to hold Integers and Characters values. Name these arrays intList and charList respectively (3) Populate each array with 200 randomly generated integers (1-100) and characters ('A'-'Z') respectively (4) Add the generic method sort.. that performs the sorting, following the described algorithm above (5) Add additional methods to carry out the individual tasks (6) Output the arrays as shown in the screen capture belowExplanation / Answer
Below is the required JAVA code with comments for explanation
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int[] frequency = new int[101];
int[] intArray = new int[200];
char[] charArray = new char[200];
final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random rNo = new Random();
int i = 0, j = 0, cnt = 0;
char ch = '';
/* Filling the arrays with random numbers & characters */
for(i=0; i<200; i++) intArray[i] = (rNo.nextInt(99)+1);
for(i=0; i<200; i++) charArray[i] = alphabet.charAt(rNo.nextInt(25));
/* Initialising the array frequency[] */
for(i=0; i<101; i++) frequency[i] = 0;
/* Dealing with Integer array */
System.out.print("Original integer array:");
for(i=0; i<200; i++)
{
if(i%10 == 0)System.out.println("");
System.out.print(intArray[i] + " ");
frequency[intArray[i]] = frequency[intArray[i]] + 1;
}
System.out.println("Sorted integer array:");
cnt=0;
for(i=1; i<=100; i++)
{
for(j=0; j<frequency[i]; j++)
{
if(cnt%10 == 0)System.out.println("");
System.out.print(i + " ");
cnt++;
}
}
/* Reinitialising array frequency[] */
for(i=0; i<101; i++) frequency[i] = 0;
/* Dealing with Character array */
System.out.print("Original character array:");
for(i=0; i<200; i++)
{
if(i%10 == 0)System.out.println("");
System.out.print(charArray[i] + " ");
frequency[charArray[i]] = frequency[charArray[i]] + 1;
}
System.out.println("Sorted character array:");
cnt=0;
for(i=65; i<=90; i++)
{
for(j=0; j<frequency[i]; j++)
{
if(cnt%10 == 0)System.out.println("");
ch = (char)i;
System.out.print(ch + " ");
cnt++;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.