In: Computer Science
A incomplete definition of a class Temperature is given below: public class Temperature {
private double value[] = {36.5, 40, 37, 38.3}; }
[6]
(i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of the method as the answers to this part.
(ii) Write another class TestTemperature in a separate file with a method main() to test the class Temperature. In main(), create a Temperature object temperature and print its values by calling toString(). Run the program. Copy the content of the file and the output showing the message as the answers to this part.
(iii) Add a method decreasePercent(int index, double percent) to the Temperature class which decreases the element with subscript index of the value array by the percentage percent without returning anything. Also add a method getValue(int index) to return value[index]. Copy the content of the methods as the answers to this part.
(iv) Add another method countNotLessThan(double threshold) to the Temperature class which returns the number of values which are not less than threshold. Copy the content of the method as the answers to this part.
(v) Add another method minimumIndex() to the Temperature class which returns the index of the minimum value in the array. Copy the content of the method as the answers to this part. You can assume there is only one minimum value.
(vi) Add another method trimmedMean() to the Temperature class which returns the average value which excludes the (unique) largest value and (unique) smallest value in the calculation. You should use minimumIndex() to get the index of the minimum value. Note that this method should work without any modifications when the number of values, which is at least three, is changed. Copy the content of the method as the answers to this part.
(vii) Perform the tasks listed below in main() of TestTemperature:
* print the second value from the left, decrease it by 10% using the method
decreasePercent(), and print the new value;
* calling countNotLessThan()to count the number of values not less than 37 and then
print it;
* print the index of the minimum value and the trimmed mean.
Run the program. Copy the content of the class and the output as the answers to this part.
class Temperature { private double value[] = {36.5, 40, 37, 38.3}; /*** * * @return the string of each value that appear on a line in the ascending order of their indexes. */ @Override public String toString() { StringBuilder result = new StringBuilder(); for (double v : value) { result.append(v).append("\n"); } return result.toString(); } /*** * * @param index * @param percent * decreases the element with subscript index of the value array by the percentage percent without returning anything */ void decreasePercent(int index, double percent) { this.value[index] = this.value[index] - (this.value[index] * percent / 100); } /*** * * @param index * @return value at given index */ double getValue(int index) { return this.value[index]; } /*** * * @param threshold * @return the number of values which are not less than threshold */ int countNotLessThan(double threshold) { int c = 0; for (double v : value) { if (v >= threshold) { c++; } } return c; } /*** * * @return the index of the minimum value in the array */ int minimumIndex() { double minimum = value[0]; int minIndex = 0; for (int i = 0; i < value.length; i++) { if (value[i] <= minimum) { minIndex = i; minimum = value[i]; } } return minIndex; } private int maxIndex() { double max = value[0]; int index = 0; for (int i = 0; i < value.length; i++) { if (value[i] >= max) { index = i; max = value[i]; } } return index; } /*** * * @return the average value which excludes the (unique) largest value and (unique) * smallest value in the calculation */ double trimmedMean() { int sum = 0; int minIndex = minimumIndex(); int max_Index = maxIndex(); //find sum for (int i = 0; i < value.length; i++) { //skip min , max values if (i != minIndex && i != max_Index) { sum += value[i]; } } return sum / (value.length - 2); //returns avg } } class TemperatureTest { public static void main(String[] args) { Temperature temperature = new Temperature(); System.out.println(temperature.toString()); //testing to string System.out.println("Second Vale From Left: " + temperature.getValue(1)); temperature.decreasePercent(1, 10); System.out.println("Second Vale From Left After decreasing: " + temperature.getValue(1)); System.out.println("Number of values not less than 37 : " + temperature.countNotLessThan(37)); System.out.println("Minimum value: " + temperature.getValue(temperature.minimumIndex())); System.out.println("Trimmed Mean value: " + temperature.trimmedMean()); } } |