Write the code above and the ones below in netbeans IDE 8.1 3. (Eliminate duplic
ID: 3925467 • Letter: W
Question
Write the code above and the ones below in netbeans IDE 8.1
3. (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[] eliminateDuplicates(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:
Enter ten numbers: 1 2 3 2 1 6 3 4 5 2 [Enter]
The distinct numbers are: 1 2 3 6 4 5
4. (Sort students) Write a program that prompts the user to enter the number of students, the students’ names, and their scores, and prints student names in decreasing order of their scores.
Explanation / Answer
Please follow the code and comments for description :
a)
CODE :
import java.util.Scanner;
public class MeanSD {
public static double deviation (double[] x) { // method to return the deviation
double sum = 0, mean = 0, deviation = 0; // required initialisations
double[] temp = new double[10]; // temporary array
for (int i = 0; i < 10; i++) //calculate the standard deviation
{
temp[i] = Math.pow((x[i] - mean), 2); // assigning the value to the array
sum += temp[i]; // adding up the values
}
mean = sum / 10; // getting the mean
deviation = Math.sqrt(mean); // calculating the deviation
return deviation; // returning the deviation
}
public static double mean (double[] x) { // method to return themean of the values entered
double sum = 0, mean; // required initialisations
for (int i = 0; i < 10; i++) //Take input in the array
{
sum += x[i]; //sum of all elements
}
mean = sum / 10; // calculating the mean
return mean; // returning the mean value
}
public static void main(String[] args) { // driver method
System.out.println("Enter the 10 numbers."); // prompt to enter the data
Scanner in = new Scanner(System.in); // scanner class to get the data from the user
double[] arr = new double[10]; // array that saves the data
double sum = 0, mean = 0, deviation = 0; // required initialisations
for (int i = 0; i < 10; i++) //Take input in the array
{
System.out.print("Enter a number : "); // prompt to enter the number
arr[i] = in.nextDouble(); // getting the data from the console
}
mean = mean(arr); // calling the method to return the mean value
System.out.println("Mean : " + mean); //Display mean of all elements
deviation = deviation(arr); // calling the method to return teh deviation value
System.out.println("Deviation : " + deviation); // display the result
}
}
OUPTPUT :
Enter the 10 numbers.
Enter a number : 1
Enter a number : 2
Enter a number : 3
Enter a number : 4
Enter a number : 5
Enter a number : 6
Enter a number : 7
Enter a number : 8
Enter a number : 9
Enter a number : 10
Mean : 5.5
Deviation : 6.2048368229954285
b)
CODE :
import java.util.Scanner;
public class MyDuplicateElements {
public static int[] eliminateDuplicates(int[] input) { // method to remove the duplicates
int j = 0; // required initialisations
int i = 1;
//return if the array length is less than 2
if (input.length < 2) { // checking for the input data
return input; // returning the data
}
while (i < input.length) { // iterating over the data int the list
if (input[i] == input[j]) { // checking for the array data elements
i++; // incrementing the index value
} else {
input[++j] = input[i++];
}
}
int[] output = new int[j + 1]; // resultant array
for (int k = 0; k < output.length; k++) { // iterating over the loop
output[k] = input[k]; // assigning the data
}
return output; // returning the result
}
public static void main(String a[]) { // driver method
Scanner in = new Scanner(System.in); // scanner class to get the data from the user
int[] array = new int[10];
System.out.println("Enter the Elements Please : "); // prompt to enter the data
for (int i = 0; i < 10; i++) //Take input in the array
{
System.out.print("Enter a number : "); // prompt to enter the number
array[i] = in.nextInt(); // getting the data from the console
}
int[] output = eliminateDuplicates(array); // calling the method to return the duplicate free elements
System.out.println("The Duplicate Free Array is : ");
for (int i : output) {
System.out.print(i + " ");
}
}
}
OUPTPUT :
Enter the Elements Please :
Enter a number : 1
Enter a number : 2
Enter a number : 2
Enter a number : 6
Enter a number : 4
Enter a number : 3
Enter a number : 3
Enter a number : 4
Enter a number : 7
Enter a number : 8
The Duplicate Free Array is :
1 2 6 4 3 4 7 8
c)
CODE :
import java.util.*;
public class SortingtheStudents { // class to run the code
public static void main(String[] args) { // driver class
Scanner input = new Scanner(System.in); // scanner class to get the data
System.out.print("Enter the total number of Students: "); // prompt to enter the data
int totalStudents = input.nextInt(); // getting the data
String[] stNames = new String[totalStudents]; // array to save the data
int[] scoreData = new int[totalStudents];
for (int i = 0; i < totalStudents; i++) { // iterating over the loop
System.out.print("Enter the Student's Name: "); // student name enter
stNames[i] = input.next();
System.out.print("Enter the Student's Score: "); // prompt to enter the score
scoreData[i] = input.nextInt();
}
descendSort(stNames, scoreData); // calling the method
System.out.println("The Order of the Students based on the Scores is : ");
System.out.println(Arrays.toString(stNames)); // printing the result
}
public static void descendSort(String[] stNames, int[] scoreData) { // method to sort the data
for (int i = scoreData.length - 1; i >= 1; i--) { // iterating over the loop
String temp; // required initialisations
int currentMax = scoreData[0];
int currentMaxIndex = 0;
for (int j = 1; j <= i; j++) { // iterating over the loop to get the data
if (currentMax > scoreData[j]) { // checking the conditions
currentMax = scoreData[j];
currentMaxIndex = j; // assigning the values
}
}
if (currentMaxIndex != i) {
temp = stNames[currentMaxIndex];
stNames[currentMaxIndex] = stNames[i];
stNames[i] = temp;
scoreData[currentMaxIndex] = scoreData[i];
scoreData[i] = currentMax; // assigning the values
}
}
}
}
OUPTPUT :
Enter the total number of Students: 3
Enter the Student's Name: John
Enter the Student's Score: 98
Enter the Student's Name: Carter
Enter the Student's Score: 58
Enter the Student's Name: MArk
Enter the Student's Score: 65
The Order of the Students based on the Scores is :
[John, MArk, Carter]
Hope this is helpful.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.