data class to process an array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

data class to process an array

I am asked to write a class that take an array of double numbers then sort values and smooth them by the average .this is my attempt but I am not sure about sort and smooth methods. public class Data { private double [] values; int largestPos; //a constructor with 2 arguments public Data(double [] array, int actSize) { values = array; } // method to find the position of largest value public int largest(int strtIndex) { double largestPosition = values[strtIndex]; int largestPos = strtIndex; for (int i = strtIndex ; i < values.length ; i++) if (values[i] > largestPosition) { largestPosition = values[i]; } return largestPos; } // method to sort the array value from largest to smallest public void sort() { for (int i = 0 ; i < values.length;i++) { double temp = values[0]; values[0]= values[largestPos] ; values[largestPos] = temp; } } // method that modify values of the array public void smooth() { double smoothed; for (int i = 0; i<values.length ; i++) { if (i == 0){ smoothed = (values[0]+values[1])/2; values[0] = smoothed; } if (i == values.length-1) { smoothed = (values[values.length-1]+values[values.length])/2; values[i] = smoothed; } else { smoothed = (values[i-1]+values[i]+values[i+1])/3; values[i]= smoothed; } } } //method to print the array values public void print() { for (int i = 0 ;i< values.length ;i++) System.out.println(values[i]); } }

25th Mar 2019, 5:20 AM
Student
Student  - avatar
1 Answer
+ 3
Can you please provide a sample input and output so that I can understand better and may help you out!!
28th Mar 2019, 1:34 AM
Ananya Pandey
Ananya Pandey - avatar