Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given an ArrayList of Integer objects, return its size. apcsaListSize([]) 0 apcsaListSize([0, 2]) 2 apcsaListSize([1]) 1 Given an ArrayList of Integers called lst, add a

  • Given an ArrayList of Integer objects, return its size.
    • apcsaListSize([]) 0
    • apcsaListSize([0, 2]) 2
    • apcsaListSize([1]) 1

  • Given an ArrayList of Integers called lst, add a new element num at the end of the ArrayList. Return the ArrayList as output.

HINT

The add method with one parameter will add to the end of the ArrayList. Easy-peasy!

apcsaListAddEnd([3, 4, 5], 2) [3, 4, 5, 2]

apcsaListAddEnd([5, 0, 3], 0) [5, 0, 3, 0]

apcsaListAddEnd([3, 5, 6], 1) [3, 5, 6, 1]

  • Given an ArrayList of Integers called lst, add a new element to the end, the value of which is the value of the largest element in the ArrayList. Return the ArrayList as output.

HINT

You will need to use a loop here.

apcsaListAddLargestValue([3, 4, 5]) [3, 4, 5, 5]

apcsaListAddLargestValue([1, 2, 4, 5, 6, 10]) [1, 2, 4, 5, 6, 10, 10]

apcsaListAddLargestValue([5, 0, 3]) [5, 0, 3, 5]

  • Given an ArrayList of Integers called lst, add a new element to the end, the value of which is the index of the largest element in the ArrayList. Return the ArrayList as output.

If the largest element in the List occurs more than once, only add one element to the end of the list with the index of where the largest element occurs first (lowest index).

HINT

You will need to use a loop here, but the methodology will be slightly different from the last problem.

apcsaListAddLargestIndex([3, 4, 5]) [3, 4, 5, 2]

apcsaListAddLargestIndex([1, 2, 4, 5, 6, 10]) [1, 2, 4, 5, 6, 10, 5]

apcsaListAddLargestIndex([5, 0, 3]) [5, 0, 3, 0]

  • The method removeLowerVals() runs through an integer arraylist and removes any value that is less than the minimum value specified.

removeLowerVals([1, 2, 5, 6, 7, 8, 1, 2], 5) [5, 6, 7, 8]

removeLowerVals([1, 1, 1, 1, 5, 5, 5, 5, 5], 3) [5, 5, 5, 5, 5]

removeLowerVals([1, 2, 3, 4, 5, 6, 7, 8, 9], 6) [6, 7, 8, 9]

  • Given an ArrayList of Strings, return a new List of Strings where each element contains the first character of the same-index input String.

Precondition: every element of lst will have length > 0.

Note, if you arereturning multiple Strings you will have to return a List instead of ArrayList. Don't blame me, this issue has to do (I think) with a problem in CodingBat that I couldn't find my way around. I suggest code utilizing the following structure:

List out = new ArrayList();

HINT

You will need a loop for this. Also remember to declare and instantiate (and return) a new ArrayList.

apcsaListFirstCharacters(["Hello", "world"]) ["H", "w"]

apcsaListFirstCharacters(["I", "am", "legend"]) ["I", "a", "l"]

apcsaListFirstCharacters(["Crunchy chewy", "Awesome", "Nice and sweet", "Delightful and delicious", "Yummy treat"]) ["C", "A", "N", "D", "Y"]

  • Given an ArrayList of Strings, come up with and return a new List of Strings where each output String at index n is made up of all the characters with index n from the input Strings, in the same order they appeared in the input.

Note, if you arereturning multiple Strings you will have to return a List instead of ArrayList. Don't blame me, this issue has to do (I think) with a problem in CodingBat's problem-authoring system that I couldn't find my way around. If you need to come up with a new ArrayList as your output, I suggest declaring it as follows:

List out = new ArrayList();

apcsaListOrderedCharacters(["hello", "world", "today"]) ["hwt", "eoo", "lrd", "lla", "ody"]

apcsaListOrderedCharacters(["whistle", "while", "you", "work"]) ["wwyw", "hhoo", "iiur", "slk", "te", "l", "e"]

apcsaListOrderedCharacters([]) []

  • Given a String input str with length > 0, return an ArrayList of all the characters in the String, as single-character Strings in the order they were encountered in the original. The output must have no repeats, meaning that if a character occurs more than once in the input it will only occur once in the output, based on the first time it appears in the input.

Note, if you arereturning multiple Strings you will have to return a List instead of ArrayList. Don't blame me, this issue has to do (I think) with a problem in CodingBat that I couldn't find my way around. I suggest code utilizing the following structure:

List out = new ArrayList();

HINT

You will need a loop for this. Also remember to declare and instantiate (and return) a new ArrayList. Also, the List interface has a handy method called contains() which can save you from having to use a nested loop. Neat!

apcsaListJustCharacters("hi") ["h", "i"]

apcsaListJustCharacters("Hello") ["H", "e", "l", "o"]

apcsaListJustCharacters("a man a plan a canal panama") ["a", " ", "m", "n", "p", "l", "c"]

  • Given an ArrayList of Integers, find out for each element whether there are any later elements with the same value. Then "complete" those pairs by moving the second element with that matched value to be after the first in the list. Any elements that cannot be completed are to be removed. Return the modified ArrayList.

If you are thinking, "I could just sort this and return it," you have good instincts but think it through - there are several problems.

WARNING: Integer objects should not be compared using == toother Integer objects, as doing so will evaluate a reference comparison rather than a value comparison. Perhaps you want to use the equals() method, or the Integer method intValue() instead?

apcsaListCompletePairs([3, 5, 3]) [3, 3]

apcsaListCompletePairs([1, 4, 14, 4, 1, 14]) [1, 1, 4, 4, 14, 14]

apcsaListCompletePairs([3, 9, 9, 3, 4, 3]) [3, 3, 9, 9]

  • Given an ArrayList of Integers, make passes forward through the list, moving any elements with values >=0 and < lst.size() into new index positions corresponding with their values. By "move," I mean remove them from their original location and insert them at a location such that, after removal/insertion is complete, the element is located at an index numbered the same as the element value. Keep doing this until a pass through the list either does not move any elements, or puts them back into an order indistinguishable from the order before the pass. Return the modified list.

For example, look at the third example input of [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]. After one or more passes this will become [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]. Processing through that will move the back half of the elements to the front of the ArrayList, but because of the repeated values the ArrayList looks the same after that pass as it did before the pass. So, there's no need for an additional pass and we can stop processing. This sounds like a good case for an outer while loop, since you really don't know how many passes you are going to end up making in advance.

Note that some numbers may repeat in the input!

apcsaListElementsToIndexes([0, 2, 1]) [0, 1, 2]

apcsaListElementsToIndexes([3, 4, 5, 6, 7]) [5, 6, 7, 3, 4]

apcsaListElementsToIndexes([0, 1, 2, 3, 4, 0, 1, 2, 3, 4]) [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]

Could someone please help me with this? Each bullet point is a SEPERATE question with SEPERATE code!

Step by Step Solution

3.31 Rating (163 Votes )

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Java Programming, Comprehensive Version

Authors: Y. Daniel Liang

10th Edition

133761312, 978-0133761313

More Books

Students also viewed these Programming questions