Hey so I have create a class FunWithIntegers. Add a method to the class called GreaterThanFive. Have that method accept an array of int. The method should allocate a new array that contains only the values from the first array that are greater than 5 and return it.
public class FunWithIntegers
{
private static int counter;
public FunWithIntegers()
{
counter = 0;
}
public static int[] GreaterThanFive(int[] values)
{
int[] vals = new int[values.length];
for(counter = 0; counter < values.length; counter++)
{
for(int j = 0; j < counter; j++)
{
if(values[j] > 5)
{
vals[counter] = values[j];
}
}
}
return vals;
}
public int m()
{
return counter;
}
}
public class IntegerTester
{
public static void main(String[] args)
{
FunWithIntegers fun = new FunWithIntegers();
/*int[] values = new int[6];
int[] greater = fun.GreaterThanFive(values);
for(int i = 0; i < values.length; i++)
System.out.println(values[i]);
*/
int[] values2 = {1, 7, 9, 10};
int[] greater2 = fun.GreaterThanFive(values2);
int[] values3 = {5, 5, 5, 150};
int[] greater3 = fun.GreaterThanFive(values2);
for(int i = 0; i < values2.length; i++){
System.out.println(greater2[i]);
}
for(int j = 0; j < values3.length; j++){
System.out.println(greater3[j]);
}
//System.out.println(fun.m());
}
}
I keep getting this:
0
0
7
9
0
0
7
9
But I want to get this:
7
9
10
150
it doesn't matter if there are 0's instead of other numbers.
and I can't change any of the method, I need to used nested loop.