Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I need help creating a program in JAVA applications. I am in the Introduction

ID: 3910821 • Letter: H

Question

Hi I need help creating a program in JAVA applications. I am in the Introduction to JAVA class.   

(Print distinct numbers) Write a program that reads in 10 numbers and displays the number of distinct numbers and the distinct numbers in their input order and separated by exactly one space(i.e., if a number apprears multiple times, it is displayed only once).(Hint: Read a number and store it to an array if it is new. If the number is alreadyin the array, ignore it.) After the input, the array contains the distinct numbers. The program should include a menu to prompt the user if they would like to continueHere is the sample run of the program:

Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2

The number of distinct numbers is 6

The distinct numbers are: 1 2 3 6 4 5

Would you like to continue (Y/N)?   

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// DistinctNumbers.java

import java.util.Scanner;

public class DistinctNumbers {

                public static void main(String[] args) {

                                /**

                                * Scanner to read user input

                                */

                                Scanner scanner = new Scanner(System.in);

                                /**

                                * A String variable to control the loop

                                */

                                String choice = "Y";

                                /**

                                * looping until user wish to stop

                                */

                                while (choice.equalsIgnoreCase("Y")) {

                                                /**

                                                * Defining an array to store distinct numbers

                                                */

                                                int distinctNumbers[] = new int[10];

                                                // number of distinct numbers found

                                                int distinctCount = 0;

                                                /**

                                                * prompting to enter 10 numbers

                                                */

                                                System.out.print("Enter 10 numbers: ");

                                                for (int i = 0; i < 10; i++) {

                                                                // getting the number

                                                                int num = scanner.nextInt();

                                                                // assuming it is distinct

                                                                boolean distinct = true;

                                                                /**

                                                                * looping through the distinctNumbers array to check if it is

                                                                * really distinct or not

                                                                */

                                                                for (int j = 0; j < distinctCount; j++) {

                                                                                if (distinctNumbers[j] == num) {

                                                                                                // already exists, assumption is wrong

                                                                                                distinct = false;

                                                                                                break;

                                                                                }

                                                                }

                                                                // if the number is still distinct, add to the array

                                                                if (distinct) {

                                                                                distinctNumbers[distinctCount] = num;

                                                                                distinctCount++;// incrementing the count

                                                                }

                                                }

                                                // to clear the next line character from the buffer

                                                scanner.nextLine();

                                                /**

                                                * Displaying the count, and the distinct numbers

                                                */

                                                System.out.println("The number of distinct numbers is "

                                                                                + distinctCount);

                                                System.out.print("The distinct numbers are: ");

                                                for (int i = 0; i < distinctCount; i++) {

                                                                System.out.print(distinctNumbers[i] + " ");

                                                }

                                                System.out.print(" Would you like to continue (Y/N) ");

                                                //getting choice to continue or quit

                                                choice = scanner.nextLine();

                                }

                }

}

/*OUTPUT*/

Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2

The number of distinct numbers is 6

The distinct numbers are: 1 2 3 6 4 5

Would you like to continue (Y/N) Y

Enter 10 numbers: 1 1 1 2 2 2 3 3 3 4

The number of distinct numbers is 4

The distinct numbers are: 1 2 3 4

Would you like to continue (Y/N) Y

Enter 10 numbers: 0 0 0 0 0 0 0 0 0 0

The number of distinct numbers is 1

The distinct numbers are: 0

Would you like to continue (Y/N) N