Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a static method called majorityZeros, which takes an integer array as

ID: 3682165 • Letter: 1

Question

1.

Create a static method called majorityZeros, which takes an integer array as an input parameter. It should check to see if the array contains more zeros than non-zero values. If the array contains strictly more zeros, then majorityZeros should return true. Otherwise majorityZeros should return false.



public static boolean majorityZeros (int[] data) {

2. Create a static method called negToZero, which takes an integer array, and returns a new integer array in which all negative entries in the input array have been changed to zero. The non-negative entries should remain unchanged.



public static int [ ] negToZero (int [ ] theArray) {

Explanation / Answer

MajorityZerosDemo.java
/**
*
* @author CHEGG
*/
class MajorityZeros {

public boolean majorityZeros(int[] data) {//This method takes data array as parameter and return boolean variable
boolean MajorityZerosstatus = false;
int cnt = 0;//for counting no of ero's
for (int i = 0; i < data.length; i++) {
if (data[i] == 0) {
cnt++;
}
}
if (cnt > data.length - cnt) {
MajorityZerosstatus = true;
}
System.out.println("no of zero's are greater than non zero's in an array is :" + MajorityZerosstatus);
return MajorityZerosstatus;

}
}

public class MajorityZerosDemo {//main class

public static void main(String args[]) {
MajorityZeros z = new MajorityZeros();//creating Majorityero class object
int[] data = new int[]{1, 0, 0, 2, 0, 0};
z.majorityZeros(data);//calling method
}
}

output

no of zero's are greater than non zero's in an array is :true

NegativetoZerosDemo.java


/**
*
* @author CHEGG
*/
class NegativetoZeros {

public int[] negToZero (int[] theArray) {//negToero is a method which converts negative numbers into ero's
int[] noNegs = new int[ theArray.length ];//creating noNegs array
for( int i = 0; i< noNegs.length; i++) {
if( theArray[i] < 0 )
noNegs[i] = 0;//if number presen t in array is -ve then converts in to zeros
else
noNegs[i] = theArray[i];
}
for( int i = 0; i< noNegs.length; i++)
{
System.out.print(noNegs[i]+",");
}

return noNegs;
}
}

public class NegativetoZerosDemo {//main class

public static void main(String args[]) {
NegativetoZeros n= new NegativetoZeros();
System.out.println("converting negative no' in an array into zero's");
int[] theArray=new int[]{1,-1,-3,-45,566,78,9};
n.negToZero(theArray);//calling negToZero method
}
}

output

:
1,0,0,0,566,78,9