Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design and implement a Java method (name itgetValues(...)) in a class and name itDistinctValuesYourname. The method takes a single-dimensional array of integer values as a

Design and implement a Java method (name itgetValues(...)) in a class and name itDistinctValuesYourname. The method takes a single-dimensional array of integer values as a parameter and returns another single-dimensional array containing only distinct values in the original (passed) array.

 

Next, test program in the main method asks the user to enter 10 integer values into a single-dimensional array, then calls methodint[] getDistinct(int[] inputArray) in classDistinctValuesYourname to copy distinct numbers into another array (which will be returned to the caller method such as main method), and then displays the output distinct numbers as shown below.

Design the main methodsuch that it allows the user to re-run the program with different sets of inputs (as in previous assignments, using a sentinel loop). Document your code, and organize and space the outputs properly usingescape characters as shown in the following sample tests. The sample test data below shows only the output labels.Remember that the user enters all numbers on one line one per line.

//First test:

Input Numbers: 5 2 1 5 2 3 2 1 5 9 User input

Distinct Numbers:5 2 1 3 9

Retry? (Y/N):Y

//Second test:

Array before processing: -5 21 2 0 2 -3 2 21 5 2

Array after processing: -5 21 2 0 -3 5

Retry? (Y/N):Y

//Third test:

Array before processing: 45 2 1 45 2 45 2 1 45 2

Array after processing: 45 2 1

Retry? (Y/N):Y

//Fourth test:

Array before processing: 0 0 0 0 0 0 0 0 0 0

Array after processing: 0

Retry? (Y/N):Y

//Fifth test:

Array before processing: 1 2 3 4 5 6 7 8 9 10

Array after processing: 1 2 3 4 5 6 7 8 9 10

Retry? (Y/N):N

Program Terminated.

Hint:

use a new array with the same size(10) as the input array. After you copied the distinct numbers, the number of active elements can be less than the array size (like the above examples).

If you return this new array, the main() method needs to print the elements. But, the main() is only able to get the array size. Not the number of active elements. So, the main() will print 0s for the remaining empty array slots.

To overcome this problem, when you check input numbers and copy distinct numbers, you need to count how many numbers are copied to the new array.

After you copy&count all, please create another new array with the new size (You already counted the number of active elements), and copy distinct values and return this new array. Now, main can print only the distinct numbers.

Step by Step Solution

3.52 Rating (162 Votes )

There are 3 Steps involved in it

Step: 1

Here is the Java code that meets the requirements DistinctValuesYournamejava import javautilScanner public class DistinctValuesYourname public static ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Process Dynamics And Control

Authors: Dale E. Seborg, Thomas F. Edgar, Duncan A. Mellich

2nd Edition

471000779, 978-0471000778

More Books

Students also viewed these Programming questions