Return a new array with all the 0s that are in data remove

// Return a new array with all the 0\'s that are in
         // data removed. If data contains 0, 3, 0, 0, 5, 9
         // then an array containing 3, 5, 9 is returned.
         public static int[] noZeros(int[] data)

-------------------------------------------------

         // Return the number of duplicate pairs in an array a.
         // Example: if a contains elements 2, 5, 1, 5, 2, 5
         // the return value would be 4 (one pair of 2\'s three pairs of 5\'s.
         public static int dups(int[] a)

--------------------------------------------------

         // Return an array containing the sign (1, -1 or 0)
         // of each element of x.
         // For example if x contains elements 2, -5, 0, 7,
         // then return a new array containing 1, -1, 0, 1.
         static int[] signs(int[] x)

--------------------------------------------------

         // Return a new array that is the same as data
         // except that all the Strings are in upper case.
         public static String[] upperCopy(String[] data)

---------------------------------------------------

         // Modify the input array so that all of
         // the Strings in data are in upper case.
         public static void upper(String[] data)

Solution

Here is the code for the given functions:

class StringsAndArraysStatistics
{
// Return a new array with all the 0\'s that are in
// data removed. If data contains 0, 3, 0, 0, 5, 9
// then an array containing 3, 5, 9 is returned.
public static int[] noZeros(int[] data)
{
int count = 0;
for(int i = 0; i < data.length; i++)
if(data[i] != 0)
count++;
int[] nonZeros = new int[count];
int start = 0;
for(int i = 0; i < data.length; i++)
if(data[i] != 0)
nonZeros[start++] = data[i];
return nonZeros;
}
//-------------------------------------------------
// Return the number of duplicate pairs in an array a.
// Example: if a contains elements 2, 5, 1, 5, 2, 5
// the return value would be 4 (one pair of 2\'s three pairs of 5\'s.
public static int dups(int[] a)
{
int count = 0;
for(int i = 0; i < a.length; i++)
for(int j = 0; j < a.length; j++)
if(a[i] == a[j])
{
count++;
break;
}
return count;
}
//--------------------------------------------------
// Return an array containing the sign (1, -1 or 0)
// of each element of x.
// For example if x contains elements 2, -5, 0, 7,
// then return a new array containing 1, -1, 0, 1.
static int[] signs(int[] x)
{
int[] newArray = new int[x.length];
for(int i = 0; i < x.length; i++)
if(x[i] < 0)
newArray[i] = -1;
else if(x[i] > 0)
newArray[i] = 1;
else
newArray[i] = 0;
return newArray;
}
//--------------------------------------------------
// Return a new array that is the same as data
// except that all the Strings are in upper case.
public static String[] upperCopy(String[] data)
{
String[] newData = new String[data.length];
for(int i = 0; i < data.length; i++)
newData[i] = data[i].toUpperCase();
return newData;
}
//---------------------------------------------------
// Modify the input array so that all of
// the Strings in data are in upper case.
public static void upper(String[] data)
{
for(int i = 0; i < data.length; i++)
data[i] = data[i].toUpperCase();
}
}

// Return a new array with all the 0\'s that are in // data removed. If data contains 0, 3, 0, 0, 5, 9 // then an array containing 3, 5, 9 is returned. public s
// Return a new array with all the 0\'s that are in // data removed. If data contains 0, 3, 0, 0, 5, 9 // then an array containing 3, 5, 9 is returned. public s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site