Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Test program below: } } else if (i == -1) { RecursionIntro.printsparsetable(13, 20); System.setOut(out); String expected = 13 15 15 18 16 21 20 24

image text in transcribedimage text in transcribed

Test program below:

image text in transcribedimage text in transcribedimage text in transcribed

} } else if (i == -1) { RecursionIntro.printsparsetable(13, 20); System.setOut(out); String expected = "13 15 15 18 16 21 20 24 "; if (!Arrays.equals(expected.split(" ? "), ba.toString().split(" ? "))) { System.out.println("For printsparsetable(13, 20), expected: " + expected + " , got: " + ba); } else { stGenScore ++; } } else { RecursionIntro.printsparsetable(starts[i], ends[i]); System.setOut(out); byte[] digest = md5.digest(Arrays.toString(ba.toString().split(" ? ")).getBytes()); if (!Arrays.equals(md5s[i], digest)) { System.out.println( "Didn't get expected output for printsparsetable(" + starts[i] + ", " + ends[i] + ")"); System.out.println("Saw this:"); System.out.println(ba); System.out.println("MD5 sum of output seen: " + Arrays.toString(digest)); } else stGenScore ++; } } ByteArrayOutputStream ba = new ByteArrayOutputStream(); System.setOut(new PrintStream(ba)); Random r = new Random(918273645); for (int i = 0; i

}

---------------This is what I have so far. But Im only getting 21/100----------------------

public class RecursionIntro {

public static int fibby(int n) { if (n == 0) return 1; return fibby(n / 4) + fibby(3 * n / 4); }

public static void tablegen(int start, int end) { long answer = fibby(start);

System.out.println(start + " " + answer);

if (start != end) { tablegen(start + 1, end); } }

public static void printsparsetable(int start, int end) { printsparsetable(start, end, 0); }

public static void printsparsetable(int start, int end, long lastPrinted) { long answer = fibby(start); if (answer != lastPrinted) { System.out.println(start + " " + fibby(start)); }

if (start != end) { printsparsetable(start + 1, end, answer); } }

public static void main(String[] args) { System.out.println("Table Gen:"); tablegen(0, 10); System.out.println("Sparse Table Gen:"); printsparsetable(0, 10);

}

//DONT KNOW WHERE TO PLACE THESE TWO PUBLIC STATICS BELOW--------

public static double median3(int[] is) { // TODO Auto-generated method stub return 0; }

public static long eduodd(long ns) { // TODO Auto-generated method stub return 0; }

}

For this assignment you will implement a few recursive methods in a Java class called RecursionIntro. Fothis assignment, there are some rules spelled out below: You may not make use of any Java classes other than incidental use of Strings, calls to System.out.println, and accessing an existing array passed to your method. The tester program will verify that your program contains no imports, no use of "new", and the only uses of. (dot) are within calls to System.out.println, accessing array.length, or followed by a digit (double literals) You may not use for or while. Any looping must be accomplished using recursion. The tester program will check for no "for"'s or "while"s and may be triggered by the word in a comment. You may not declare static or non-static member fields-only local or parameter variables allowed You may implement additional helper methods to use the "recursive driver" technique as we did with the find method in class. Part 1. Even digits up, odd digits down (30 pts) Implement the method public static long eduodd(long n). eduodd(n) returns a value which increases each of the even decimal digits of n by one and decreases each of the odd digits of n by one Leading one digits will disappear. The sign of eduodd(n) should be the same as n, unless n is negative and eduodd(n) is zero as seen in the last example below Please review the written practice recursion problems as seen in class. Table of examples: 27 36 987654321 -8443 896745230-9552 11121113 30002 eduodd(n) 1 0 Part 2a. Recursive definition (20 pts) Implement the method public static int fibby (int n). fibby is mathematically defined for nonnegative values in the following way fibby(0) 1 fibby(n) = fibby(In/4]) + fibby(13n/4) where n > 0 and lxmeans the floor of (round down) HINT: recall Java's integer division behavior. Table of examples: 4 10 20 24 100 fibby(n)-12 Part 2b. Sparse table generation (20 pts) Notice that for many values i, fibby(i) fibby(i+1). Implement the method public static void printsparsetable(int start, int end). Output using System.out.println all consecutive values of n and fibby(n) (just the two numeric values separated by a space) where n start and n S end However, skip a line of output if the previous row printed has the same fibby value import java.io.ByteArrayOutputStream; import java.io.File; import java.io.PrintStrean nport java-nio.file-Files import java.nio.file.Paths; import java.security.NessageDigest; import java.security.NoSuchAlgorithnException; import java.util.Arrays; mport java.util.Random; Test of CS 143 Assignment 2 by Martin Hock (Version of 8:23 PM 1/19/2019) You may only use this code as a student of Martin Hock, CS 143 Winter 2019 public class RecursionIntroTest public static void rowColunn (String source, int affset) int row 1; int col1; for (int i . 0; i for (int 1-2; 0 and lxmeans the floor of (round down) HINT: recall Java's integer division behavior. Table of examples: 4 10 20 24 100 fibby(n)-12 Part 2b. Sparse table generation (20 pts) Notice that for many values i, fibby(i) fibby(i+1). Implement the method public static void printsparsetable(int start, int end). Output using System.out.println all consecutive values of n and fibby(n) (just the two numeric values separated by a space) where n start and n S end However, skip a line of output if the previous row printed has the same fibby value import java.io.ByteArrayOutputStream; import java.io.File; import java.io.PrintStrean nport java-nio.file-Files import java.nio.file.Paths; import java.security.NessageDigest; import java.security.NoSuchAlgorithnException; import java.util.Arrays; mport java.util.Random; Test of CS 143 Assignment 2 by Martin Hock (Version of 8:23 PM 1/19/2019) You may only use this code as a student of Martin Hock, CS 143 Winter 2019 public class RecursionIntroTest public static void rowColunn (String source, int affset) int row 1; int col1; for (int i . 0; i for (int 1-2;

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

More Books

Students also viewed these Databases questions

Question

How does capitalism differ from socialism and communism?

Answered: 1 week ago