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
notka56 [123]
2 years 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:
nata0808 [166]2 years 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("Printing the arraylist:");

ArrayListMethods.print(stringArray);

System.out.println("\nArrayList is condensing:");

ArrayListMethods.condense(stringArray);

ArrayListMethods.print(stringArray);

System.out.println("\nArrayList is duplicating:");

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:

  • Get the length of the arraylist  to copy the array into another arraylist .
  • Empty the array so that new content can be stored in it .
  • Use a loop to half of array_list.
  • Use a loop from half of array_list to the end of arraylist and  append all into a single string.
You might be interested in
Which of the following is true with regard to defensive programming? Preconditions should never be visible to callers. Program c
Firlakuza [10]

Answer:

The correct point about defensive programming is that the "program code frequently assumes that input will be valid and that algorithms will behave as expected".

Explanation:

8 0
1 year ago
This program finds the sum and average of three numbers. What are the proper codes for Lines a and b?
Gennadij [26K]

Answer:

The Proper codes in Line a and Line b is given below

average=Sum/3  

print (" Average is = ", average)

Explanation:

In the given question it calculated the sum but the program does not calculate the average of the 3 numbers.The average of the 3 number is calculated by using average=Sum/3   statement so we add this code in Line a then After that print the value of average by using the print function so we add this code in Line b.

4 0
1 year ago
Read 2 more answers
there are four stage of the product life cycle. during which of these stages do you think is the best time for a company to purc
Alisiya [41]

Maturity Stage – During the maturity stage, the product is established and the aim for the manufacturer is now to maintain the market share they have built up. This is probably the most competitive time for most products and businesses need to invest wisely in any marketing they undertake. They also need to consider any product modifications or improvements to the production process which might give them a competitive advantage. i think this would be the best time for the company to purchase an emerging technology.

7 0
1 year ago
Read 2 more answers
Allows you to manually add an entry to the arp cache that resolves the ip address inetaddr to the physical address etheraddr. wh
Norma-Jean [14]
The answer in this question is that once the Adaptor or router received the destination IP address (even if we entered in the incorrect MAC address) the router or adapter would remove the IP address from the Ethernet frame and using ARP, would get the correct MAC address of the destination.
6 0
1 year ago
#Write a function called string_finder. string_finder should #take two parameters: a target string and a search string. #The fun
adoni [48]

Answer:

I am writing a Python program:

def string_finder(target,search): #function that takes two parameters i.e. target string and a search string

   position=(target.find(search))# returns lowest index of search if it is found in target string

   if position==0: # if value of position is 0 means lowers index

       return "Beginning" #the search string in the beginning of target string

   elif position== len(target) - len(search): #if position is equal to the difference between lengths of the target and search strings

       return "End" # returns end

   elif position > 0 and position < len(target) -1: #if value of position is greater than 0 and it is less than length of target -1

       return "Middle" #returns middle        

   else: #if none of above conditions is true return not found

       return "not found"

#you can add an elif condition instead of else for not found condition as:

#elif position==-1    

#returns "not found"

#tests the data for the following cases      

print(string_finder("Georgia Tech", "Georgia"))

print(string_finder("Georgia Tech", "gia"))

print(string_finder("Georgia Tech", "Tech"))

print(string_finder("Georgia Tech", "Idaho"))

Explanation:

The program can also be written in by using string methods.

def string_finder(target,search):  #method definition that takes target string and string to be searched

       if target.startswith(search):  #startswith() method scans the target string and checks if the (substring) search is present at the start of target string

           return "Beginning"  #if above condition it true return Beginning

       elif target.endswith(search):  #endswith() method scans the target string and checks if the (substring) search is present at the end of target string

           return "End" #if above elif condition it true return End

       elif target.find(search) != -1:  #find method returns -1 if the search string is not in target string so if find method does not return -1 then it means that the search string is within the target string and two above conditions evaluated to false so search string must be in the middle of target string

           return "Middle"  #if above elif condition it true return End

       else:  #if none of the above conditions is true then returns Not Found

           return "Not Found"

6 0
1 year ago
Other questions:
  • Refer to the exhibit. Which two network addresses can be assigned to the network containing 10 hosts? Your answers should waste
    7·1 answer
  • Using Amdahl’s Law, calculate the speedup gain of an application that has a 60 percent parallel component for (a) two processing
    11·1 answer
  • To play game, go inside the Grand Theft Auto V folder and right click and runGTAVLauncher as administrator.If you get any missin
    5·1 answer
  • A function defined beginning with void SetNegativesToZeros(int userValues[], ... should modify userValues such that any negative
    12·1 answer
  • Which expansion slot is used by an NVMe compliant device?
    9·1 answer
  • What is the average number of nodes accessed in search for a particular element in an unordered list? In an ordered list? In an
    6·1 answer
  • Create a different version of the program that: Takes a 3-digit number and generates a 6-digit number with the 3-digit number re
    14·1 answer
  • What do you call the two parts of lift that go down a mine
    13·2 answers
  • Define function print_popcorn_time() with parameter bag_ounces. If bag_ounces is less than 3, print "Too small". If greater than
    8·1 answer
  • Doug grew up on a large farm in southwest Wisconsin. As a college graduation gift, Doug’s father gave him several hundred acres
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!