Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the tester program import java.io.ByteArrayOutputStream; import java.io.File; import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Paths; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; public class RecursionTest {

Here is the tester program

import java.io.ByteArrayOutputStream; import java.io.File; import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Paths; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays;

public class RecursionTest {

public static void main(String[] args) throws NoSuchAlgorithmException { int[] ns = { 0, 1, 2, 3, 4, 5, 10, 20, 100, 1000, 10000, 100000000, 700000000, 800000000, 1000000000 }; int[] fibbys = { 1, 2, 3, 4, 6, 6, 11, 24, 111, 1125, 11497, 113764606, 791169897, 918443045, 1146120114 }; int[] scores = { 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 5, 5 }; int fibbyscore = 0; int tgscore = 0; int stgscore = 0; PrintStream out = System.out; MessageDigest md5 = MessageDigest.getInstance("MD5"); String source = null; try { source = new String(Files.readAllBytes(Paths.get("src" + File.separator + "RecursionIntro.java"))); } catch (Exception e) { System.out.println( "Couldn't find RecursionIntro.java! Run this from the same Eclipse project as RecursionIntro."); return; } if (source.matches("(?s).*\\sfor[\\s\\(].*")) { System.out.println( "Detected 'for' statement in your program! Please remove anything that resembles a 'for'."); System.exit(-1); } if (source.matches("(?s).*\\swhile[\\s\\(].*")) { System.out.println( "Detected 'while' statement in your program! Please remove anything that resembles a 'while'."); System.exit(-1); } if (source.matches("(?s).*\\simport\\s.*") || source.indexOf("import") == 0) { System.out.println("Detected 'import' statement in your program! Please remove any word 'import'."); System.exit(-1); }

for (int dotIndex = source.indexOf('.'); dotIndex != -1; dotIndex = source.indexOf('.', dotIndex + 1)) { if ("System.out.println".equals(source.substring(dotIndex - 6, dotIndex + 12)) || "System.out.println".equals(source.substring(dotIndex - 10, dotIndex + 8)) || "length".equals(source.substring(dotIndex + 1, dotIndex + 7))) { continue; } System.out.println("Bad dot! Context: " + source.substring(dotIndex - 10, dotIndex + 10)); System.exit(-1); } try { System.out.println("If you don't see a score, your program didn't get to the end!"); System.out.println("Testing fibby..."); for (int i = 0; i

}

Heres what I have so far:

public class RecursionIntro {

public static long 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 + "\t" + answer); if (start != end) { tablegen(start + 1, end); } }

public static void sparsetablegen(int start, int end) { // taking a value of 0 as last printed because fibby(0) = 1, hence 0 // can't be value any sparsetablegen(start, end, 0); }

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

// if this answer is not printed before print it if (answer != lastPrinted) { System.out.println(start + "\t" + fibby(start)); }

// if start is not equal to end go to next value if (start != end) { sparsetablegen(start + 1, end, answer); } }

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

}

}

My tester program is only giving me 20/100. Can anyone help fix the problem?

image text in transcribed

Please do not make use of any Java classes other than incidental use of Strings and calls to System.out printin. The tester program will verify that your program contains no imports and the only uses of (dot) are within calls to System.out.println. Please do not use for or while. Any looping must be triggered using recursion. The tester program will check for no "for"s or "while"s and may be triggered by the word in a comment. Aside from the extra credit, you should not need to use arrays, and the tester program will check that you do not declare static or non-static member fields only local variables allowed. You may implement additional methods to perform "recursive bootstrapping" as seen in class Part 1. Recursive definition (30 pts) Implement the method public static long fibby(int n). fibby is mathematically defined in the following way fibby(0) 1 fibby(n) fibby(In/4]) fibby(l3n/4J) where n 0 and LxJ means the floor of x (round down). (HINT: recall Java's integer division behavior) Part 2. Table generation (40 pts) mplement the method public static void tablegen(int start, int end). Output using n start and n s end. IMPORTANT: tablegen(start, end) should only make a total of (end start 1) calls to fibby (fibby will make recursive calls, of course Part 3. Sparse table generation (30 pts) mplement the method public static void sparsetablegen(int start, int end). Unlike tablegen, do not output n and fibby(n) if you already output a value of fibby equal to fibby(n)

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

Database Management With Website Development Applications

Authors: Greg Riccardi

1st Edition

0201743876, 978-0201743876

More Books

Students also viewed these Databases questions