Write a function called va1 that takes as input an integer array g1 and its leng
ID: 3599555 • Letter: W
Question
Write a function called va1 that takes as input an integer array g1 and its length m1, and returns true if the number of even elements in the array is more than 5. Test your code in a main function.
2. Write a class called Circle that has the input arguments of the above function as the private members, and a new version of the above function as a public method. The new version of the va1 function should have only 1 input argument. Add any more accessor and mutators methods if needed, and test your class in a main function.
3. Add an improved version of the the function called va2 that returns true if the number of odd numbers is twice more than the number of even numbers.
Explanation / Answer
import java.util.Scanner;
public class EvenOddFunc {
public static void main(String[] args) {
//using scanner class to allow user to enter input array value through command prompt
Scanner userInput = new Scanner(System.in);
// split the line by single space
String[] luserInput = userInput.nextLine().split(" ");
// create new int array with same size of lUserinput array
int[] inputArray=new int[luserInput.length];
int index=0;
//convert sting array to int array
for(String i:luserInput)
{
Integer lInteger=new Integer(i);
inputArray[index]=lInteger.intValue();
index++;
}
//Case 1 : Write a function called va1 that takes as input an integer array g1 and its length m1, and returns true if the number
//of even elements in the array is more than 5. Test your code in a main function.
boolean isEven=va1(inputArray,inputArray.length);
System.out.println("Given Array Has Even Numbers More Than 5 is :"+isEven);
//Case 2 :Write a class called Circle that has the input arguments of the above function as the private members, and a new version of the above function as a public method. The new version of the va1 function should have only 1 input argument.
//Add any more accessor and mutators methods if needed, and test your class in a main function.
Circle lWithInputArrayAndInputLength=new Circle(inputArray,inputArray.length);
System.out.println("Given Array Has Even Numbers More Than 5 is :"+lWithInputArrayAndInputLength.va1());
Circle lWithoutInputArray=new Circle(null,inputArray.length);
System.out.println("Given Array Has Even Numbers More Than 5 is :"+lWithoutInputArray.va1(inputArray));
Circle lWithoutConsutrutorArg=new Circle();
System.out.println("Given Array Has Even Numbers More Than 5 is :"+lWithoutInputArray.va1(inputArray,inputArray.length));
//Case 3:Add an improved version of the the function called va2 that returns true
//if the number of odd numbers is twice more than the number of even numbers.
Circle lOddNumbersTwiceMoreThanEven=new Circle();
System.out.println("Given Array Has Even Numbers More Than 5 is :"+lOddNumbersTwiceMoreThanEven.va2(inputArray,inputArray.length));
}
/**
* Takes inputarray and length and return true if evencount is more than 5
*
* @param inputArray
* @param length
* @return
*/
private static boolean va1(int[] inputArray, int length) {
int evenCount=0;
for(int i=0;i<=length-1;i++)
{
if(inputArray[i]%2==0)
{
++evenCount;
}
}
if(evenCount>5)
{
return true;
}
return false;
}
}
public class Circle {
private int[] inputArray;
private int inputArrayLen;
Circle(int[] inputArray, int inputArrayLen) {
super();
this.inputArray = inputArray;
this.inputArrayLen = inputArrayLen;
}
Circle()
{
}
//without arguments function getting input array and length by constructor of circle
/**
* Takes zero arguments and return true if evencount is more than 5
* @return
*/
public boolean va1() {
int evenCount=0;
for(int i=0;i<=inputArrayLen-1;i++)
{
if(inputArray[i]%2==0)
{
++evenCount;
}
}
if(evenCount>5)
{
return true;
}
return false;
}
//with input array argument of function and length by constructor of circle
/**
* Takes inputarray and return true if evencount is more than 5
* @param inputArray
* @return
*/
public boolean va1(int[] inputArray) {
this.inputArray=inputArray;
int evenCount=0;
for(int i=0;i<=inputArrayLen-1;i++)
{
if(inputArray[i]%2==0)
{
++evenCount;
}
}
if(evenCount>5)
{
return true;
}
return false;
}
//without constructor arguments ,function having input array and length
/**
* Takes inputarray and length and return true if evencount is more than 5
* @param inputArray
* @param length
* @return
*/
public boolean va1(int[] inputArray,int length) {
this.inputArray=inputArray;
this.inputArrayLen=length;
int evenCount=0;
for(int i=0;i<=inputArrayLen-1;i++)
{
if(inputArray[i]%2==0)
{
++evenCount;
}
}
if(evenCount>5)
{
return true;
}
return false;
}
/**
* Takes inputarray and length and return true if odd count is more than twice of even number
* @param inputArray
* @param length
* @return
*/
public boolean va2(int[] inputArray, int length) {
int evenCount=0;
int oddCount=0;
for(int i=0;i<=length-1;i++)
{
if(inputArray[i]%2==0)
{
++evenCount;
}
else
{
++oddCount;
}
}
if(oddCount>evenCount*2)
{
return true;
}
return false;
}
}
Please like it if it solved your problem!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.