Java Program recursion to create an arraylist1 2 2 3 4 6 8 1

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.

Solution

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);
}
}

 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 ev
 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 ev

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site