Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1.Write a program in Java with a recursive method sumDigits(int number) that takes a non-negative integer and returns the sum of its base-10 digits. For

1.Write a program in Java with a recursive method sumDigits(int number) that takes a non-negative integer and returns the sum of its base-10 digits. For example, sumDigits(1234) returns 1 + 2 + 3 + 4 = 10. (Note: that it is easy to split an integer into two smaller pieces by dividing by 10 (e.g., 1234/10 = 123, and 1234 % 10 = 4)). Demonstrate that your program works by printing to the console some sample numbers and the final result. Call the primary source file RecursiveSumDigits.java

2.Write a program in Java that reverses a string using a recursive method: String reverse (String text). For example, reverse(Hello!) returns the string !olleH. (Hint: Implement the recursive solution by removing the first character, reversing the remaining text, and combining the two) Demonstrate that your program works by printing to the console some sample texts and their reversed outputs. Call the primary source file RecursiveStringReverser.java

3. Write a method that adds all subsets of the letters in its first argument, str, to the ArrayList that is its second argument. For example, generateSubstrings("ABC", result) adds to result the strings: "", "A", "B", "C", "AB", "AC", "BC'', "ABC" The order of the strings does not matter. In your test code, you will probably want a helper method that prints all of the strings in an 2 ArrayList. You may use iteration in any way you see fit for this problem. Do not worry too much about eliminating all memory allocation in this method. Demonstrate that your program works by printing to the console the source string and the subset, followed by the result. Call the primary source file SubstringFinder.java

4.Spherical objects, such as cannonballs, can be stacked to form a pyramid with one cannonball at the top, sitting on top of a square composed of four cannonballs, sitting on top of a square composed of nine cannonballs, and so forth. Write a recursive method countCannonballs that takes as its argument the height of a pyramid of cannonballs and returns the number of cannonballs it contains. The prototype for the method should be as follows: public static int countCannonballs(int height) Demonstrate that your program works by printing to the console some examples of height and the resulting cannonballs, followed by the result. Call the primary source file CannonBallPyramid.java

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

Students also viewed these Databases questions

Question

How many states in India?

Answered: 1 week ago

Question

HOW IS MARKETING CHANGING WITH ARTIFITIAL INTELIGENCE

Answered: 1 week ago

Question

Different types of Grading?

Answered: 1 week ago

Question

Explain the functions of financial management.

Answered: 1 week ago