Create a Java Application, called Unique. This Java Application will read in fiv
ID: 3805393 • Letter: C
Question
Create a Java Application, called Unique. This Java Application will read in five unique integer values between 1 and 20, displaying errors if the number is outside of the desired range or if the number is duplicate.
Use the Java Application template, Unique.java:
-Insert code as indicated by the comment blocks: /* */
-Create a static method ShowArrayList(), which has two parameters:
--String listName
--int numbers[]
-Accept a new entered number, make sure it is between 1 and 20 and verify that the number has not already been entered previously.
-Create two arrays of integers; one which contains the raw entered numbers and one which contains a sorted list of numbers
-As each unique number is entered, sort the unique array in incrementing order
Sample Output:
Unique - Application to retrieve 5 unique numbers between 1 and 20
Enter number: 19
Raw List : 19 0 0 0 0
Sort List : 19 0 0 0 0
Enter number: 12
Raw List : 19 12 0 0 0
Sort List : 12 19 0 0 0
Enter number: 2
Raw List : 19 12 2 0 0
Sort List : 2 12 19 0 0
Enter number: 9
Raw List : 19 12 2 9 0
Sort List : 2 9 12 19 0
Enter number: 5
Raw List : 19 12 2 9 5
Sort List : 2 5 9 12 19
Explanation / Answer
HI, Please find my implementation.
import java.util.Scanner;
public class Unique
{
/* Create a method called ShowArrayList() that accepts two parameters */
/* the first parameter contains a string specifying the text string */
/* the second parameter contains an integer array of variable length */
/* this method will print the array string, followed by the elements of the array */
public static void ShowArrayList(String s, int[] list){
System.out.print(s+" : ");
for(int i=0; i<list.length; i++)
System.out.print(list[i]+" ");
System.out.println();
}
public static void main( String args[] )
{
Scanner keyboard = new Scanner( System.in );
/* create two integer arrays; one to store the original numbers as they */
/* are entered and one to store the sorted numbers */
int count = 0; // number of uniques read
int entered = 0; // number of entered numbers
System.out.println( "Unique - Application to retrieve 5 unique numbers between 1 and 20" );
int raw[] = new int[5];
int sorted[] = new int[5];
while( entered < raw.length )
{
System.out.print( "Enter number: " );
/* Add code to read in a number from the keyboard */
int new_number = keyboard.nextInt();
// validate the Input
if (( new_number > 0 ) && ( new_number <= 20 ))
{
// flags whether this number already exists
boolean containsNumber = false;
/* Write a loop to determine if the new number entered is */
/* already in the array list, set containsNumber to true */
for(int i=0; i<entered; i++){
if(raw[i] == new_number)
containsNumber = true;
}
// Add the user input to the array only if the number is not already
// in the array
if ( !containsNumber )
{
raw[entered] = new_number;
sorted[entered] = new_number;
entered++;
/* Write a loop to sort the list from smallest to largest */
for (int i = 0; i < entered - 1; i++)
{
int index = i;
for (int j = i + 1; j < entered; j++)
if (sorted[j] < sorted[index])
index = j;
int smallerNumber = sorted[index];
sorted[index] = sorted[i];
sorted[i] = smallerNumber;
}
ShowArrayList( "Raw List ", raw );
// Print new list after insertion to make sure our sort works
ShowArrayList( "Sort List ", sorted );
}
else
{
System.out.printf( "%d has already been entered ", new_number );
}
}
else
{
System.out.println( "number must be between 1 and 20" );
}
}
} // end method getNumbers
} // end class Unique
/*
Sample run:
Unique - Application to retrieve 5 unique numbers between 1 and 20
Enter number: 4
Raw List : 4 0 0 0 0
Sort List : 4 0 0 0 0
Enter number: 199
number must be between 1 and 20
Enter number: 19
Raw List : 4 19 0 0 0
Sort List : 4 19 0 0 0
Enter number: 2
Raw List : 4 19 2 0 0
Sort List : 2 4 19 0 0
Enter number: 6
Raw List : 4 19 2 6 0
Sort List : 2 4 6 19 0
Enter number: 15
Raw List : 4 19 2 6 15
Sort List : 2 4 6 15 19
*/