define a class called utilities.... Define a class called Utilities which has th
ID: 3687422 • Letter: D
Question
define a class called utilities....
Define a class called Utilities which has the following public methods: Define a method called count that takes two parameters named data and cutoff. The data parameter represents an array of integers. The cutoff parameter is an integer value. The method will return the number of entries in the array with a value less or equal to the cutoff parameter. Define a method called filter that takes two parameters named data and cutoff. The data parameter represents an array of integers. The cutoff parameter is an integer value. The method will return a new array of integers where elements of the data array with a value greater than the cutoff value are not present. Feel free to use the count method we described earlier, even if you have not implemented the method. The following driver provides an example dealing with the methods you are expected to define.Explanation / Answer
UtilitiesDriver.java
public class UtilitiesDriver {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Utilities util = new Utilities();
int[] values = {10, 1, 8, 13, 2, 15, 18};
System.out.println("Count: "+util.count(values, 8));
int[] result = util.filter(values, 8);
for(int i=0; i<result.length; i++){
System.out.print(result[i]+ " ");
}
}
}
Utilities.java
public class Utilities {
public Utilities(){
}
public int count(int[] values, int cutoff){
int countNum = 0;
for(int i=0; i<values.length; i++){
countNum++;
if(values[i] == cutoff){
break;
}
}
return countNum;
}
public int[] filter(int[] values, int n){
int temp[] = new int[count(values, n)];
int count = 0;
for(int i=0; i<values.length; i++){
if(n >= values[i]){
temp[count] = values[i];
count++;
}
}
return temp;
}
}
Output:
Count: 3
1 8 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.