In: Computer Science
Java language
(a) Write code segments to perform the following:
(i) declare and create an integer array freqArray of size 8
(ii) declare and initialize an array weight (with suitable type) which contains 48.5, 80 and 68
(iii) declare a Mouse array of size 2 with name mouse and initialize it with Mouse objects using one statement
(b) 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.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
1)
int freqArray[]=new int[8];
____________________
2)
double weight[]={48.5,80,68};
____________________
3)
Mouse mouse[]={new Mouse(),new Mouse()};
____________________
4)
// Temperature.java
public class Temperature {
private double value[] = { 36.5, 40, 37, 38.3 };
public String toString() {
String str = "";
// This Logic will Sort the Array
of elements in Ascending order
double temp;
for (int i = 0; i <
value.length; i++) {
for (int j = i +
1; j < value.length; j++) {
if (value[i] > value[j]) {
temp = value[i];
value[i] = value[j];
value[j] = temp;
}
}
}
for (int i = 0; i <
value.length; i++) {
str += value[i]
+ "\n";
}
return str;
}
public void decreasePercent(int index, double
percent) {
value[index] = getValue(index) -
(getValue(index) * (percent / 100));
}
public double getValue(int index) {
return value[index];
}
public int countNotLessThan(double threshold)
{
int cnt = 0;
for (int i = 0; i <
value.length; i++) {
if (!(value[i]
< threshold)) {
cnt++;
}
}
return cnt;
}
public int minimumIndex() {
int minIndx = 0;
double min = value[0];
for (int i = 0; i <
value.length; i++) {
if (min >
value[i]) {
min = value[i];
minIndx = i;
}
}
return minIndx;
}
public double trimmedMean() {
double sum = 0;
double max = value[0], min =
value[0];
int maxIndx = 0;
int minIndx = minimumIndex();
for (int i = 0; i <
value.length; i++) {
if (max <
value[i]) {
max = value[i];
maxIndx = i;
}
}
int cnt = 0;
for (int i = 0; i <
value.length; i++) {
if (i != minIndx
&& i != maxIndx) {
cnt++;
sum += value[i];
}
}
return sum / cnt;
}
}
________________________
// TestTemperature.java
public class TestTemperature {
public static void main(String[] args) {
Temperature t=new Temperature();
System.out.println(t);
t.decreasePercent(2,10.0);
System.out.println("Index 2 Elemnt after 10%
reduce:"+t.getValue(2));
System.out.println("No of values not less than 37
:"+t.countNotLessThan(37));
System.out.println("Index of the minimum value
:"+t.minimumIndex());
System.out.println("Trimmed mean :"+t.trimmedMean());
}
}
______________________________
Output:
36.5
37.0
38.3
40.0
Index 2 Elemnt after 10% reduce:34.47
No of values not less than 37 :2
Index of the minimum value :2
Trimmed mean :36.75
_______________Could you plz rate me well.Thank
You