Hi, I need help with my java program on arrays I can get number 6 and number 7 t
ID: 673229 • Letter: H
Question
Hi, I need help with my java program on arrays I can get number 6 and number 7 to print the answer but um retty sure the methods are correct for both of them. I am just unsure on how to set up the statements for them to print.
I keep getting this error: The method copyEvens(int[], int) in the type Arrays is not applicable for the arguments (int[])
This is my java class:
public class Arrays {
/**
* @param args
*/
public static void main(String[] args) {
int [] a = {1, 2, 3};
int [] b = {3, 1, 2, 3, 5};
int [] c = {13, 6, 1, 2, 7};
int [] d = {1, 2, 3, 4, 5, 6};
int [] e = { 2, 3, 4, 5, 6, 7};
int[] ab = {2,5};
int[] bc = {2, 3, 4};
int[] cd = {4, 3, 2};
int[] dc = {3, 4, 5};
int[] ef = {2, 1, 2, 3, 4};
String[] fe = {"a", "bb", "b", "ccc"};
int[] fg = {6, 1, 2, 4, 5, 8};
// #1
boolean ex1 = firstLast3(a);
boolean ex2 = firstLast3(b);
boolean ex3 = firstLast3(c);
System.out.println(ex1);
System.out.println(ex2);
System.out.println(ex3);
//#2
System.out.println("");
System.out.println("");
System.out.println("Question number 2 ");
int ex4 = matchUp(b, c);
int ex5 = matchUp(d, e);
System.out.println(ex4);
System.out.println(ex5);
//#3
System.out.println("");
System.out.println("");
System.out.println("Question number 3 ");
boolean h = has23(ab);
System.out.println(h);
//#4
System.out.println("");
System.out.println("");
System.out.println("Question number 4 ");
boolean u = has2And3(dc);
System.out.println(u);
//#5
System.out.println("");
System.out.println("");
System.out.println("Question number 5 ");
int j = countEvens(ef);
System.out.println(j);
//6
//#7
System.out.println("");
System.out.println("");
System.out.println("Question number 7 ");
int[] l = copyEvens(fg);
System.out.print(l);
}
public static boolean firstLast3(int[] a ) {
if (a[0] == 3 || a[a.length - 1] == 3)
return true;
else
return false;
}
public static int matchUp( int[] a, int[] b ) {
int count = 0;
for ( int i = 0; i < a.length; i++)
if (a[i] == b[i])
count++;
return count;
}
//3
public static boolean has23(int[] nums)
{
if(nums[0] == 2 || nums[0] == 3)
return true;
return (nums[1] == 2 || nums[1] == 3);
}
//4
public static boolean has2And3(int[] nums)
{
if(nums[0] == 2 && nums[0] == 3)
return true;
return (nums[1] == 2 && nums[1] == 3);
}
//5
public static int countEvens(int[] nums)
{
int count = 0;
for(int i = 0; i < nums.length; i++)
{
if(nums[i] % 2 == 0)
count++;
}
return count;
}
//6
public static int wordsCount(String[] words, int length) {
int counts = 0;
for (int i = 0; i < words.length; i++) {
if (words[i].length() == length)
counts++;
}
return counts;
}
//7
public static int[] copyEvens(int[] nums, int count) {
int a = 0;
int[] array = new int[count];
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 == 0) {
array[a] = nums[i];
a++;
}
if (a == count)
return array;
}
return nums;
}
}
Explanation / Answer
You are calling number 7 method by using below statement
int[] l = copyEvens(fg);
but you should pass second parameter as well which is the count of elements as copyEvens function declaration expects 2 parameters:
public static int[] copyEvens(int[] nums, int count)
passing the second parameter to the function will work.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.