Question

In: Computer Science

write the “largerComponents” method that takes in two integer arrays and returns true or false if...

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:

  • Does the solution compile?
  • Does the solution have your name in the comments?
  • Does the solution have a high-level solution description (150-300 words) in the comments?
  • If the first array has the values {5,4,3} and the second array has the value {2,1,0} does the method return true?
  • If the first array has the values {1,2,3} and the second array has the value {1,2,3} does the method return false?
  • If the first array has the values {10,9,8} and the second array has the value {1,2,3,4,5,6} does the method return false?

Solutions

Expert Solution

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:-

  • If the first array has the values {5,4,3} and the second array has the value {2,1,0} does the method return true?

  • If the first array has the values {1,2,3} and the second array has the value {1,2,3} does the method return false?

  • If the first array has the values {10,9,8} and the second array has the value {1,2,3,4,5,6} does the method return false?

HAPPY LEARNING


Related Solutions

Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these 2 Arrays are equal or not, and also if the elements are all even numbers. Describe under what conditions these 2 Arrays would be considered equal.
Write a method that takes two Sorted Arrays of different sizes and merges them into one...
Write a method that takes two Sorted Arrays of different sizes and merges them into one sorted array, and use the method to write a full recursive Merge Sort Algorithm.
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT