PLEASE HELP, LESS THAN ONE HOUR TO COMPLETE. CODE IS
DONE IN JAVA.
// Main.java: Java program to create and test the method
SumMultiplier
import java.util.*;
import java.io.*;
class Main {
/**
* method that takes as input an array of integers
* and returns a String "true" if any two elements of
the
* array can be multiplied to get a value greater
than
* double the sum of the elements in the array else
* return "false".
*
* Return data type for the method is String not int
*/
public static String SumMultiplier(int[] arr) {
// compute double the sum of the input array
int doubleSum = Arrays.stream(arr).sum() * 2;
// loop from start to second last element of the
array
for (int i=0;i<arr.length-1;i++){
// loop from element at index i+1 to end of the
array
for (int j=i+1;j<arr.length;j++){
// product of elements at indices i and j is greater than
doubleSum, return "true"
if(arr*arr[j] > doubleSum){
return "true";
}
}
}
return "false"; // no 2 elements of the array can be
multiplied to get a value greater than doubleSum, return
"false"
}
public static void main(String[] args) {
// test the method
// input to the method must be an array of integers not
String,
// so if you are taking input from user, convert the String
to array of integers and then pass the array to method
System.out.println("SumMultiplier([2, 2, 2, 2, 4, 1]): " +
SumMultiplier(new int[] {2, 2, 2, 2, 4, 1}));
System.out.println("SumMultiplier([1, 1, 2, 10, 3, 1, 12]):
"+SumMultiplier(new int[] {1, 1, 2, 10, 3, 1, 12}));
}
}
// end of Main.java
How do I do this?: // so if you
are taking input from user, convert the String to array of integers
and then pass the array to method
The user is inputting the data as follows, so it cannot be hard
coded into the code:
Input: new int[] {2, 2, 2, 2, 4, 1}
Input: new int[] {1, 1, 2, 10, 3, 1, 12}
Input must be exactly as written and must obtain "true" for
input 1 and "false" for input 2.
PLEASE HELP, LESS THAN ONE HOUR TO COMPLETE. CODE IS DONE IN JAVA. // Main.java: Java program to create and test the met
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
PLEASE HELP, LESS THAN ONE HOUR TO COMPLETE. CODE IS DONE IN JAVA. // Main.java: Java program to create and test the met
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!