answer.
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
Blizzard
1 month ago
10

In this exercise, you will create a couple of helper methods for ArrayLists in a class called ArrayListMethods.

Computers and Technology
1 answer:
oksian1 [797]1 month ago
5 0

Answer:

package lab1;

import java.util.ArrayList;

import java.util.Iterator;

public class ArrayListMethodsTester

{

public static void main(String[] args)

{

ArrayList<String> stringArray = new ArrayList<String>();

stringArray.add("This");

stringArray.add("is");

stringArray.add("an");

stringArray.add("ArrayList");

stringArray.add("of");

stringArray.add("Strings");

System.out.println("Displaying the arraylist:");

ArrayListMethods.print(stringArray);

System.out.println("\nCondensing the ArrayList:");

ArrayListMethods.condense(stringArray);

ArrayListMethods.print(stringArray);

System.out.println("\nDuplicating the ArrayList:");

ArrayListMethods.duplicate(stringArray);

ArrayListMethods.print(stringArray);

}

}

class ArrayListMethods

{

static void print(ArrayList stringArray)

{

Iterator it = stringArray.iterator();

while (it.hasNext())

System.out.println(it.next() + " ");

}

static void condense(ArrayList stringArray)

{

int length=stringArray.size();

ArrayList cloneArray=(ArrayList) stringArray.clone();

stringArray.clear();

StringBuilder sb=new StringBuilder("");

for (int i = 0; i < (length/2); i++)

{

sb.append(cloneArray.get(i));

sb.append(" ");

}

stringArray.add(sb);

StringBuilder sb1=new StringBuilder("");

for (int i = (length/2); i < length; i++)

{

sb1.append(cloneArray.get(i));

sb1.append(" ");

}

stringArray.add(sb1);

}

static void duplicate(ArrayList stringArray)

{

for (int i = 0; i < stringArray.size(); i+=2)

{

stringArray.add(i+1,stringArray.get(i));

}

}

}

Explanation:

  • Calculate the size of the arraylist to duplicate it into a secondary arraylist.
  • Clear the original array to prepare for new contents.
  • Implement a loop to process half of the original array.
  • Employ another loop to handle the remaining half of the array and concatenate all into a unified string.
You might be interested in
Represent decimal number 8620 in (a) BCD, (b) excess-3 code, (c)2421 code, and (d) as a binary number
maria [879]

The decimal number 8620 can be expressed in the following ways:

a = 1000 0110 0010 0000

b = 1011 1001 0101 0011

c = 1110 1100 0010 0000

d = 10000110101100

I hope these representations answer your question and prove useful.

4 0
1 month ago
Write an if-else statement to describe an integer. Print "Positive even number" if isEven and is Positive are both true. Print "
Harlamova29_29 [932]

Answer:

Below is the explanation for the C code.

Explanation:

#include <stdio.h>

#include <stdbool.h>

int main(void) {

int userNum;

bool isPositive;

bool isEven;

scanf("%d", &userNum);

isPositive = (userNum > 0);

isEven = ((userNum % 2) == 0);

if(isPositive && isEven){

  printf("Positive even number");

}

else if(isPositive &&!isEven){

  printf("Positive number");

}

else{

  printf("Not a positive number");

}

printf("\n");

return 0;

}

6 0
1 month ago
Need 2.5 Code Practice Answers
Natasha_Volkova [897]

Answer:

import random

random.seed(1,10) # Note: seed takes one argument, so this line has an error

a = random.randint(1, 10)

b = random.randint(1, 10)

print("Calculate: " + str(a) + " X " + str(b) + "?")

ans = int(input("Your answer: "))

if ans == a * b:

print("Well done!")

else:

print("That's wrong!")

Explanation:

4 1
1 month ago
Read 2 more answers
Write an expression to print each price in stock_prices. Sample output with inputs: 34.62 76.30 85.05
8_murik_8 [892]

Answer:

Below is the Python code:

stock_prices = input().split() #this takes input and separates it into a list

for price in stock_prices: #this loops through each stock price

   print("$",price) #this outputs each stock price prefixed by a dollar sign

Explanation:

The logic behind the program is clearly outlined in the attached comments. To illustrate the program's workings, let's consider an example:

Imagine the user inputs the stock_prices values of

34.62 76.30 85.05

The input() method captures user input, while the split() method divides the input string, providing a list of strings as:

['34.62', '76.30', '85.05']

Following that, the loop statement for price in stock_prices: iterates through each item in the list, and print("$",price) displays each value from the list on the output screen with a dollar sign, as follows:

$ 34.62                                                                                                            

$ 76.30                                                                                                            

$ 85.05    

4 0
1 month ago
Define a function below called average_strings. The function takes one argument: a list of strings. Complete the function so tha
Natasha_Volkova [897]

Answer:

def average_strings(lst):

   total_length = 0

   avg = 0

   for s in lst:

       total_length += len(s)

   avg = total_length/len(lst)

   return avg

Explanation:

Define a function named average_strings that takes a single argument, lst

Initialize both total length and avg variables

Utilize a for loop to iterate through lst, accumulating the length of each string into total length

Once the loop ends, compute the average by dividing the total length by the number of elements in lst

Return the computed average.

6 0
1 month ago
Other questions:
  • . Electricians will sometimes call ______ "disconnects" or a "disconnecting means."
    12·1 answer
  • 6. Write pseudo code that will perform the following. a) Read in 5 separate numbers. b) Calculate the average of the five number
    6·1 answer
  • An author is preparing to send their book to a publisher as an email attachment. The file on their computer is 1000 bytes. When
    6·1 answer
  • Write a loop that reads strings from standard input, where the string is either duck or goose. the loop terminates when goose is
    7·1 answer
  • Write a program whose input is two integers and whose output is the two integers swapped. Ex: If the input is: 3 8 the output is
    5·1 answer
  • Which of the following is true? a. Pseudocode is used to describe an algorithm. b. Pseudocode is not an actual computer programm
    11·1 answer
  • RADIAC instruments that operate on the ionization principle are broken down into three main categories based on what?
    15·1 answer
  • Which generation of programming languages provides programmers with a visual environment for coding programs?
    12·1 answer
  • Which of the following is opened when the Find command is clicked?
    12·1 answer
  • An administrator has been working within an organization for over 10 years. He has moved between different IT divisions within t
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!