Question: IN JAVA, I need help making these methods: Here's the code it goes in: Count8 Method: public class Count8 { public static int count8(int n)
IN JAVA, I need help making these methods:


Here's the code it goes in: Count8 Method: public class Count8 { public static int count8(int n) { // TODO: return -1; } } DigitSum Method: public class DigitSum { public static int sumDigits(int n) { // TODO: return -1; } } Mutant Class: public class Mutant { public static int bunnies(int n) { // TODO: return -1; } } Sharing Method public class Sharing { private static String invert(String s) { StringBuilder sb = new StringBuilder(); for (char c: s.toCharArray()) { if (c == 'a') sb.append('b'); else sb.append('a'); } return sb.toString(); }
public static String thueMorse(int n) { // TODO: // B never gets to go first; we don't like B return "b"; } }
Inspired by http://codingbat.com/prob/p192383 Compute recursively the number of 8s in a non-negative number n. You can do this by determining whether the ones digit is an 8 , and then adding either 1 or 0 to the number of 8s in the rest of the number. Remember that you can isolate the ones digit using %10, and get the rest of the number by integer dividing it by 10 . Sample Results: count8(1) =0 count8(4) =0 count8(8) =1 count8(123) =0 count8(81818) =3 DigitSum.sumDigits (int n ) Recursively compute the sum of the digits in the non-negative integer n. The sum of the digits can be calculated by taking the ones digit and adding it to the sum of the rest of the digits. Sample Results: sumDigits (7)=7 sumDigits ()=0 sumDigits (123)=6 sumDigits (10203043023402345)=36 sumDigits (724)=13 Mutant.bunnies(int n ) Inspired by https://codingbat.com/prob/p107330 Recursively compute the number of ears present in a line of bunnies. The interesting thing about these bunnies is that every other bunny has 3 ears instead of 2 (the bunnies are lining up by the Springfield reactor to petition Mr. Burns for reparation). A line with zero bunnies has 0 ears, a line with one bunny has 3 ears, 2 bunnies have 5 ears, etc. Some hints: - You can assume that if the length of the list is odd, the front bunny will have 3 ears. - The total number of ears in the line of bunnies is the number of ears on the front bunny plus the number of ears on the rest of the line. Sample Results: mutantBunnies()=0 mutantBunnies (1)=3 mutantBunnies(2)=5 mutantBunnies (3)=8 mutantBunnies (4)=10 Sharing.thueMorse(int n ) Given a non-negative integer n, determine the way to fairly share 2n items using the ThueMorse fair sharing sequence (https://www.youtube.com/watch?v=prh72BLNjlk). If we only have one object (20,n==0), we will let person a have it. With two object(n==1), we will let a choose, followed by b, resulting in a sequence of ab. For 4 objects (n==2), we will generate the sequence by taking the result for n=1, and add to it the result of inverting the sequence for n=1:ab+ba=abba. We continue the pattern (for n=3, we take abba + baab = abbabaab ). Write a recursive method that generates the fair sharing sequence. In the code, a method that will invert a string of lower-case a's and b's has been provided for you (and is called invert). (Hint: if we say that previous =thueMorse(n1), the result for n is previous + invert (previous)) Sample Results: thueMorse (0)= "a" thueMorse (1)= "ab" thueMorse (2)= "abba" thueMorse (3)= "abbabaab" thueMorse (4) = "abbabaabbaababba
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
