Java Program recursion to create an arraylist(1 2 2 3 4 6 8 10 12 15) A test dri
ID: 3798303 • Letter: J
Question
Java Program recursion to create an arraylist(1 2 2 3 4 6 8 10 12 15) A test driver to test as well!1. Write a recursive method nEven that returns how many even numbers are contained For ex. nEven(values) would return 5(the even numbers are 2 2 4 6 8 and 10)
2. Write a recursive method that contains (int target) that returns true if list contains target and false otherwise. For example, list contains (10, values) would return false and list contains (15, values) would return true.
Java Program recursion to create an arraylist(1 2 2 3 4 6 8 10 12 15) A test driver to test as well!
1. Write a recursive method nEven that returns how many even numbers are contained For ex. nEven(values) would return 5(the even numbers are 2 2 4 6 8 and 10)
2. Write a recursive method that contains (int target) that returns true if list contains target and false otherwise. For example, list contains (10, values) would return false and list contains (15, values) would return true.
Java Program recursion to create an arraylist(1 2 2 3 4 6 8 10 12 15) A test driver to test as well!
1. Write a recursive method nEven that returns how many even numbers are contained For ex. nEven(values) would return 5(the even numbers are 2 2 4 6 8 and 10)
2. Write a recursive method that contains (int target) that returns true if list contains target and false otherwise. For example, list contains (10, values) would return false and list contains (15, values) would return true.
Explanation / Answer
import java.util.*;
public class HelloWorld{
public void createArrayList(ArrayList<Integer> ll)
{
int n=Integer.parseInt(System.console().readLine());
if(n!=-1)
{
ll.add(n);
createArrayList(ll);
}
return;
}
public int nEven(ArrayList<Integer> ll,int index)
{
int count=0;
if(index>=ll.size()) return 0;
nEven(ll,index+1);
if(ll.get(index)%2==0)
{
return count+1;
}
return ;
}
public static void main(String []args){
HelloWorld hh = new HelloWorld();
ArrayList<Integer> ll=new ArrayList<Integer>();
hh.createArrayList(ll);
for(int tmp:ll)
System.out.println(tmp);
int tt=hh.nEven(ll,0);
System.out.println(tt);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.