Question

Language is in Java, please explain the answer using comments, Thank you. I always rate!!! PLEASE...

Language is in Java, please explain the answer using comments, Thank you. I always rate!!!

PLEASE DO NOT import any packages to solve the problem. PLEASE use classes and methods from java.lang.*, since they are automatically imported without import statement.

build a method called;

public static in[] multbygrp(int[] elems, int grpsize)

This method splits arr into subgroups of (equal) size groupSize, and multiply the contents of each subgroup. It returns the individual product in a new list.

If splitting can't be done properly, returns an empty array.

You can assume groupSize is greater than 0 and less than or equal to the length of arr

Input: [1, 2, -8, 2] , 2

Output: [2, -16]

Input: [1, 2, 3, 4] , 3

Output: []

Input: [5, 6, 7] , 1

Output: [5, 6, 7]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class MyClass {
  
public static int[] multbygrp(int[] elems, int grpsize)
{
int size = elems.length;
if(size%grpsize!=0)
{
return new int[0];
}
  
int grparray[] = new int[size/grpsize];
int k= 0;
for(int i =0;i<size;i+=grpsize)
{
int a =1;
for(int j=i;j<i+grpsize;j++)
{
a= a*elems[j];
}
grparray[k]=a;
k++;
}
return grparray;
}
  
public static void main(String args[]) {
int A[] = {1,2,-8,2};
int grpsize = 2;
int arr[] = multbygrp(A,grpsize);
System.out.println(Arrays.toString(arr));
}
}

Add a comment
Know the answer?
Add Answer to:
Language is in Java, please explain the answer using comments, Thank you. I always rate!!! PLEASE...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please check first question before you do this one Java please it's data sturcture classs using...

    please check first question before you do this one Java please it's data sturcture classs using java editor netbeans 2.   3. public static int indexOfLargest(int[] arr) – if arr is null then return -1; otherwise return the index of the largest value. If the largest value appears more than one time, then return the left-most index. Example: If arr = {9, -2, 3, 50, 4, 99, 11, 4, 5, 99, 3, 4, -6}; then the method returns 5 4. public...

  • Please draw a flow chart for each method please and thank you. Write a program that...

    Please draw a flow chart for each method please and thank you. Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, and output all integers less than or equal to that value. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 50 60 140 200 75 100 the...

  • please do in java and comments the code so i can understand Requirements: Create a Java...

    please do in java and comments the code so i can understand Requirements: Create a Java class named “MyRectangle2D.java”. Your class will have double two variables named x and y. These will represent the center point of your rectangle. Your class will have two double variables named width and height. These will represent the width and height of your rectangle. Create getter and setter methods for x, y, width, and height. Create a “no argument” constructor for your class that...

  • [IN JAVA] Finish the program. There are instructions in the comments. You may want to comment...

    [IN JAVA] Finish the program. There are instructions in the comments. You may want to comment out the function calls in main, and get one function to work at a time. Thanks in advance! Template import java.util.Scanner; public class Main{ public static Scanner kb = new Scanner(System.in); public static void main(String [] args) { String productName; double productPrice; int productQuantity; double shipping; int quantityDiscount; double finalTotal; productName = queryName(); productPrice = queryPrice(); productQuantity = queryQuantity(); System.out.println(); quantityDiscount = computeQuantityDiscount(productQuantity); shipping...

  • Can you please complete it in java and add comments explaining the program so I can understand it...

    Can you please complete it in java and add comments explaining the program so I can understand it. Test cases: Test case 2 input PROBLEM: Evaluate a prefix expression. The operands in the expression are single digit whole numbers. The operators are binary addition (+), subtraction (), and multiplication(*), and a trinary operator "switcher" (a). The a operator of a, b, and c returns b when a is positive; otherwise, it returns Example 1: * + 4 53 1 simplifies...

  • In java, please complete the methods in the class below. Add comments to identify your base...

    In java, please complete the methods in the class below. Add comments to identify your base case or base cases and the recursive steps in each of the recursive implementations. a. public String starsRecursive(int numOfLinesOfStars) takes the number of lines of stars to be in the returned string. The first line will have as many stars as numOfLinesOfStars. Each line after the first line will have one less star. The last line will have only one star. If the value...

  • CAN YOU PLEASE DO THIS IN JAVA! WILL LEAVE GOOD RATING THANK YOU Modify your program...

    CAN YOU PLEASE DO THIS IN JAVA! WILL LEAVE GOOD RATING THANK YOU Modify your program that reads grades from the user, so that it has a method that checks if a particular input is valid i.e. as long as the user types invalid input, the user should be given another chance to enter input (it would also be good to let the user know that their input is invalid). Moreover, only a valid grade should be used in computing...

  • Need help with a few questions! Using the JAVA language please. 1) exercises - branching Write...

    Need help with a few questions! Using the JAVA language please. 1) exercises - branching Write a method whatToWear(int temp) that takes a temperature and then outputs a string for what to wear in different weather conditions. There must be at least 3 categories. For example, whatToWear(90) might return “shorts” and whatToWear(30) might return “down coat” 2) Enum exercise • Define an enum Seasons, and rewrite the whatToWear method to use enums and switch statement 8) 2D array exercise 1....

  • in java no mathcall please ennas are out of date.Please sign was wkollieemail.cpeedu so we can...

    in java no mathcall please ennas are out of date.Please sign was wkollieemail.cpeedu so we can verily your subscription Sign In ASSIGNMENT Unit 9 Methods, Arrays, and the Java Standard Class Library Reimplementing the Arrays class (60 points) The Arrays class, which is also part of the Java standard class library, provides various class methods that perform common tasks on arrays, such as searching and sorting. Write a public class called MyArrays, which provides some of the functionality which is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT