Write a main method that invokes each of these methods - use a variety of input
ID: 3741227 • Letter: W
Question
Write a main method that invokes each of these methods - use a variety of input to make sure you account for all conditions. 1. public static double investmentValue double amount, double interest, int numYears) Write a recursive method that takes as parameters an initial investment amount, an annual interest rate, and a number of years. The method should return the value of the investment after the given number of years, assuming that the interest is compounded annually. (For example, if the initial investment is 1000 and the interest rate is 10 percent, then after one year the investment will be worth 1100, after two years 1210, after three years 1331, etc) 2. public static String changePi(String str) Given a string, compute recursively (no loops) a new string where all appearances of "pi" have been replaced by "3.14". changePi("????") %2.14x" changePi("pipi") -> "3.143.14" changePi("pip") "3.14p" 3. sumDigits Method sumDigits takes a non-negative integer and returns the sum of its base- 10 digits. For example, sumDigits (1234) returns 12+3+ 4 10S. Note that it is easy to split an integer into two smaller pieces by dividing by 10 (e.g., 1234/10-123, and 1234 % 10Explanation / Answer
Please find my implementation of Q3.
Please repost others in separate post.
public class Test {
public static int sumDigits(int n) {
// base case
if( n == 0)
return 0;
else // recursive call
return ( n%10 + sumDigits(n/10));
}
public static void main(String[] args) {
System.out.println(sumDigits(12345));
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.