In: Computer Science
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”.
here is a provided code
//Do not alter----------------------------------------------------------------------- public class Question01 { public static boolean largerComponents(int[] a, int[] b) { //----------------------------------------------------------------------------------- //Write your solution here }//Do not alter this //Write any additional helper properties or methods here //-------------------------------------------------------------------------------- //Test your code here. You may alter this as needed. public static void main(String[] args) { int[] test01 = {5,4,3}; int[] test02 = {2,1,0}; System.out.println(largerComponents(test01,test02)); } //-------------------------------------------------------------------------------- }//Do not alter this
Solution Tests:
COMPLETE CODE FOR THE FOLLOWING PROGRAM:-
// FILE: Question.java
// TITLE: Course Enrollments
// SUBMITTED BY: Your Name Goes Here
// PURPOSE:
// This program takes two arrays of integers and
//returns true or false if the first array’s components are strictly
//greater than the second array’s components. T
//he arrays must be the same size or else return false.
//Components meaning each value at a specific index.
// OVERALL METHOD:
// The list of general tasks is:
// 1. declare two integres array
// 2. pass them to largerComponents function
// 3. larger Components willl return false if two arrays
// are not equal in size if not
// it will compare every elments of array one to elements of array 2
// if array one elements are greater than array 2 elements
//print true else print false.
// FUNCTIONS:
//
// largerComponents
// It takes two arrays and return true if elements
// of array one is strictly greater than array two
public class Question01 {
// largerComponents function to check arrray one is strictly greater than two or not
public static boolean largerComponents(int[] a, int[] b)
{
//-------------------------------
int count=0;
//Write your solution here
//If both arrays are equal in length
if(a.length==b.length){
for(int i=0;i<a.length;i++)
{
if(a[i]>b[i]){
count++;
}
}
if(count==a.length){
return true;
}else{
return false;
}
}else{
//if both arrays are not equal in length
return false;
}
}//Do not alter this
//Test your code here. You may alter this as needed.
public static void main(String[] args)
{
//declaration of two arrays
int[] test01 = {5,4,3};
int[] test02 = {2,1,0};
//Calling largerComponents function
System.out.println(largerComponents(test01,test02));
}
//--------------------------------------------------------------------------------
}//
SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-
SAMPLE OUTPUT:-
HAPPY LEARNING