Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java code level: beginner write a method sumList() public static Integer sumList(ArrayList list) This method calculates the sum of all Integer values in a given

java code

level: beginner

write a method

sumList()

public static Integer sumList(ArrayList list)

This method calculates the sum of all Integer values in a given ArrayList.

For example, if ArrayList list contains {1, 2, 3}, a call to sumList(list) should return 1+2+3, which is 6 as an Integer.

  • Return null if the list is empty or null.
  • Think about the base case.
    • When should the method terminate/end?
    • Under what condition should your method stop making recursive calls and return your expected value?
  • Think about the recursive case.
    • Make recursive calls to the method; make sure to call sumList in the method body.
      • How would the input have to change in the recursive call? What should your parameter argument be?
      • Keep in mind, you do not need to keep the original ArrayList content.
  • Again, iterative solution will not be accepted; do NOT use loop to solve this problem.

strToList()

public static ArrayList strToList(String word)

This method transforms a String into an ArrayList containing each character of the string in order.

For example, strToWord("word") will return an ArrayList of four characters: {w,o,r,d} .

strToWord("hello world") will return an

ArrayList: {h,e,l,l,o, ,w,o,r,l,d}

  • Return an empty list if the string is empty.
  • Return null if the string is null
  • Think about base case.
    • When should the method terminate/end?
    • Under what condition should your method stop making recursive calls and return your expected value?
  • Think about the recursive case
    • Make recursive calls to the method; make sure to call to strToList
      • How would the input have to change in the recursive call?
      • Keep in mind, you do not need to keep the original ArrayList content.
    • An iterative solution will not be accepted.

findAverage()

public static double findAverage(ArrayList list)

This method returns the average value in the given ArrayList.

For example, if ArrayList list is {1, 2, 3, 4}, findAverage(list) will return 2.5.

  • Return null if the list is empty or null.
  • Think about base case.
    • When should the method terminate/end?
    • Under what condition should your method stop making recursive calls and return your expected value?
    • Think about the recursive case
      • Make recursive calls to the method; make sure to make a function call to findAverage
      • How would the input have to change in the recursive call?
  • You do not need to keep the original content of the list.
  • An iterative solution will not be accepted; do NOT use a for loop.
  • Hint: You should use the idea you used in sumList because finding the average requires calculating the sum.
    • However, you cannot use sumList in this method directly. You are only allowed to make function calls to findAverage itself.
    • You cannot call the method sumList in findAverage
    • What operation do you have to do on the sum to find the average?

Step by Step Solution

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

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions

Question

Describe the new structures for the HRM function. page 676

Answered: 1 week ago