Question
Here is the starting file. So far from the starting file, I have written the following code. I am not sure how to do the
Here is the starting file.
So far from the starting file, I have written the following code. I am not sure how to do the last 3 methods. If anyone could help it would be greatly appreciated!
import java.io.*; public class Recursion { public static void main(String[] args) { //TRISTARS int rows = 5; System.out.format("A triangle with %d rows contains %d stars ", rows, triStars(rows)); //SUMDIGITS int number = 12345; System.out.format("The sum of the digits in the number %d = %d ", number, sumDigits(number)); //COUNT7S number = 713274772; System.out.format("There are %d occurrences of the digit 7 in the number %d ", count7s(number), number); //COUNT8S number = 82338828; System.out.format("There are %d occurrences of the digit 8 in the number %d ", count8s(number), number); } //count stars in a triangle using # of rows as input. static int triStars(int rows) { if(rows == 1) return 1; return rows + triStars(rows-1); } //given a number return the sum of the digits. static int sumDigits(int n) { if(n == 0) return 0; return n%10 + sumDigits(n/10); } //given a number compute the number of 7's in that number static int count7s(int n) { if(n == 0) return 0; else if(n%10 == 7) return 1+count7s(n/10); else return count7s(n/10); } //given a number count the number of8 but if an 8 has another 8 to its immediate left //count it as 2. //the number 8802388 will return a count of 6. static int count8s(int n) { if(n == 0) return 0; else if(n%100 == 88) return 3+count8s(n/100); else if(n%10 == 8) return 1+count8s(n/10); else return count8s(n/10); } //compute base to the power n static int powerN(int base, int n) { return 0; } // return true only if the array is sorted static boolean isSorted(int array[], int i, int count ) { return false; }
// return true if string is palindrome static boolean isPalindrome(String s, int lo, int hi ) { return false; } } // END CLASS Recursion
WRITING CURSIVE METHODS N Y W H E R E Here is your starter file: Recursion You must write the code for the recursive methods.NO LOOPS ALLOWED The counting the 8s problem has a twist. If two eights appear beside each other the second one counts as TWO eights. This is why for the counting 8s problem, there are only 4 actual 8s in the number BUT the correct answer is 5. Here is the output of a the program Command Prompt C: Users\tim Desktop java Solution A tringle with 5 rows contains 15 stars The sum of the digits in the number 12345 15 There are 4 occurances of the digit 7 in the number 713274772 There are 5 occurances of the digit 8 in the number 82338828 2 to the power 8 256 array: 78 12 20 21 22 37 41 55 60 65 74 83 84 87 is SORTED Stanleyyelnats IS a palindrome C: Users tim Desktop
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started