In: Computer Science
public class AllEqual { // You must define the allEqual method, which will return // true if either: // 1.) The given array contains fewer than two elements, or... // 2.) All elements of the array are equal to each other. // As a hint, you only need to compare the first element // to all subsequent elements for this check. // // TODO - define your code below this comment // // DO NOT MODIFY parseStrings! public static int[] parseStrings(String[] strings) { int[] retval = new int[strings.length]; for (int x = 0; x < strings.length; x++) { retval[x] = Integer.parseInt(strings[x]); } return retval; } // DO NOT MODIFY main! public static void main(String[] args) { int[] argsAsInts = parseStrings(args); boolean areEqual = allEqual(argsAsInts); System.out.println("Are equal: " + areEqual); } }
Below is the complete program(including allequal function) -
public class AllEqual
{
public static boolean allEqual(int n[])
{
int k=n.length;
boolean b=false;
if(k<2)
{
b=true;
}
for(int i=1;i<k;i++)
{
if(n[0]==n[i])
{
b=true;
}
}
return b;
}
public static int[] parseStrings(String[] strings)
{
int[] retval = new int[strings.length];
for (int x = 0; x < strings.length; x++) {
retval[x] = Integer.parseInt(strings[x]);
}
return retval;
}
// DO NOT MODIFY main!
public static void main(String[] args)
{
int[] argsAsInts = parseStrings(args);
boolean areEqual = allEqual(argsAsInts);
System.out.println("Are equal: " + areEqual);
}
}
Below is the only allEqual() function that needs to be copied to the actual program (in above program, everything is mentioned) (this function is subpart of it) -
public static boolean allEqual(int n[])
{
int k=n.length;
boolean b=false;
if(k<2)
{
b=true;
}
for(int i=1;i<k;i++)
{
if(n[0]==n[i])
{
b=true;
}
}
return b;
}