1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
artcher [175]
2 years ago
12

Write a static method called split that takes an ArrayList of integer values as a parameter and that replaces each value in the

list with a pair of values, each half the original. If a number in the original list is odd, then the first number in the new pair should be one higher than the second so that the sum equals the original number. For example, if a variable called list stores this sequence of values:
Computers and Technology
1 answer:
lorasvet [3.4K]2 years ago
5 0

Answer:

The method is as follows:

public static void split(ArrayList<Integer> mylist) {

   System.out.print("Before Split: ");

   for (int elem = 0; elem < mylist.size(); elem++) { System.out.print(mylist.get(elem) + ", ");    }

   System.out.println();

   for (int elem = 0; elem < mylist.size(); elem+=2) {

       int val = mylist.get(elem);

       int right = val / 2;

       int left = right;

       if (val % 2 != 0) {            left++;        }

       mylist.add(elem, left);

       mylist.set(elem + 1, right);    }        

   System.out.print("After Split: ");

   for (int elem = 0; elem < mylist.size(); elem++) { System.out.print(mylist.get(elem) + ", ");    }

}

Explanation:

This declares the method

public static void split(ArrayList<Integer> mylist) {

This prints the arraylist before split

<em>    System.out.print("Before Split: "); </em>

<em>    for (int elem = 0; elem < mylist.size(); elem++) { System.out.print(mylist.get(elem) + ", ");    } </em>

<em>    System.out.println(); </em>

This iterates through the list

   for (int elem = 0; elem < mylist.size(); elem+=2) {

This gets the current list element

       int val = mylist.get(elem);

This gets the right and left element

<em>        int right = val / 2; </em>

<em>        int left = right; </em>

If the list element is odd, this increases the list element by 1

       if (val % 2 != 0) {            left++;        }

This adds the two numbers to the list

       mylist.add(elem, left);

       mylist.set(elem + 1, right);    }      

<em>This prints the arraylist after split</em><em> </em>

<em>    System.out.print("After Split: "); </em>

<em>    for (int elem = 0; elem < mylist.size(); elem++) { System.out.print(mylist.get(elem) + ", ");    } </em>

<em>}</em>

You might be interested in
Which is the most likely reason why scientists change a model?
frozen [14]

C.) New discoveries make the current model inaccurate. Since science is all about observation and experimentation, it is logical that whenever new findings are gathered, scientific models must be modified to adapt to the information. As new ideas and concepts are uncovered, models should be updated to make them correct.

5 0
3 years ago
Read 2 more answers
Can you help please.
liberstina [14]
1. true
2.false
3.true
4. many - I'm not too certain of this answer
5. track changes
3 0
2 years ago
The PowerPoint view in which you can edit the slide master is called ______________.
nirvana33 [79]

Answer:

it's slide master view.

8 0
2 years ago
2) What is the value stored in the variable z by the statements below?
Dmitriy789 [7]

Answer:

7

Explanation:

Because the q.length is a inbuilt function in the programming which used to get the length of the array. In the array, there are 7 values are store. Therefore, the size 7 store in the variable z.

For example:

int[] array={1,2};

int x = array.length;

the answer of above code is 2, because the elements present in the array is 2.

 

5 0
2 years ago
A function defined beginning with void SetNegativesToZeros(int userValues[], ... should modify userValues such that any negative
Stels [109]

Answer:

   public static void setNegativesToZero(int userValues[]){

       System.out.println("Array before setting negatives to zero "+Arrays.toString(userValues));

       for(int i=0; i<userValues.length; i++){

           if(userValues[i]< 1){

               userValues[i] = 0;

           }

       }

       System.out.println();

       System.out.println("Array After setting negatives to zero "+Arrays.toString(userValues));

   }

Explanation:

Using Java programming Language, the method is created to receive an array of ints as parameter (as specified in the question)

Using a for loop, we iterate over the entire array checking for values that are negative (<0) and setting them to zero.

See a complete program below

import java.util.Arrays;

import java.util.Scanner;

public class num1 {

   public static void main(String[] args) {

   // Create an array and assign values

       int [] arr = {1,2,3,-1,1,2,-4};

   //Calling the method setNegativesToZero

   setNegativesToZero(arr);

   }

//Creating the Method

   public static void setNegativesToZero(int userValues[]){

       System.out.println("Array before setting negatives to zero "+Arrays.toString(userValues));

       for(int i=0; i<userValues.length; i++){

           if(userValues[i]< 1){

               userValues[i] = 0;

           }

       }

       System.out.println();

       System.out.println("Array After setting negatives to zero "+Arrays.toString(userValues));

   }

}

3 0
3 years ago
Other questions:
  • You're working at a large industrial plant and notice a tag similar to the one shown in the figure above. Which of the following
    5·1 answer
  • The ____ function sums the numbers in the specified range and then divides the sum by the number of cells with numeric values in
    14·1 answer
  • Which player type focuses on level progression?
    13·1 answer
  • If you have a windows 8, but want the features of windows 8 pro, purchase and install _______.
    7·1 answer
  • What is the benefit of an arch in a building design? It permits architects to build taller structures. It allows architects to c
    14·1 answer
  • Is 5g harmful to the body ?
    9·2 answers
  • What safety feature melts to protect a circuit? a. fuse b. diode c. three-prong plug d. transistor
    10·2 answers
  • Which of the following statements best illustrates how value was created for eggs? when a farmer sells a chicken who is no longe
    9·1 answer
  • The ___________ method returns the length of an array.
    11·2 answers
  • What should a pie chart represent?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!