Design and implement a Java program for programming exercise 7.15, page 279 (nam
ID: 3937315 • Letter: D
Question
Design and implement a Java program for programming exercise 7.15, page 279 (name it Eliminate Duplicates), as specified in the problem statement. Follow the instructions in the problem statement to design your method. Design the main method of your program such that it allows the user to re-run the program with different sets of inputs. Use the givens ample run to test your code. Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable. (Computing gcd) Write a method that returns the gcd of an unspecified number of integers. The method header is specified as follows: Public static int gcd(int...numbers) Write a test program that prompts the user to enter five numbers, invokes the method to find the gcd of these numbers, and displays and gcd. (Eliminate duplicates) Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: Public static int [] eliminate duplicates (int[] list) Write a test program that reads in ten integers, invokes the method, and displays the result. Here is the sample run of the program: (Execution time) Write a program that randomly generates an array of 100,000Explanation / Answer
Please follow the code and comments for description :
CODE :
import java.util.Scanner;
public class EliminateDuplicates { // class to run the code
public static int[] removeDuplicates(int[] list) { // method to remove the duplicates and return the data
int endData = list.length;
for (int i = 0; i < endData; i++) { // iterate over the data array
for (int j = i + 1; j < endData; j++) {
if (list[i] == list[j]) { // check for the data if equal
int leftShift = j; // shift the data
for (int k = j + 1; k < endData; k++, leftShift++) { // check for the next element
list[leftShift] = list[k];
}
endData--; // decrement the index
j--;
}
}
}
int[] resData = new int[endData]; // save the data to a list
for (int i = 0; i < endData; i++) {
resData[i] = list[i];
}
return resData; // return the data
}
public static void main(String[] args) { // driver method
Scanner sc = new Scanner(System.in); // scanner class to get the data
int arrData[] = new int[10]; // local varaibles
int resultData[] = new int[10];
System.out.print("Enter Ten Numbers : "); // prompt for the user
String str = sc.nextLine();
String[] array = str.split(" "); // split the data
for (int i = 0; i < array.length; i++) {
arrData[i] = Integer.parseInt(array[i]); // save as integer
}
resultData = removeDuplicates(arrData); // call the method to remove the duplicates
System.out.print("The Distinct Numbers are : ");
for (int j = 0; j < resultData.length; j++) {
System.out.print(resultData[j]);
System.out.print(" "); // print the data to console
}
System.out.println(""); // new line character
}
}
OUTPUT :
Enter Ten Numbers : 1 2 3 2 1 6 3 4 5 2
The Distinct Numbers are : 1 2 3 6 4 5
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.