Example printbinary7 prints 111 example printbinary12

Info icon This preview shows pages 6–11. Sign up to view the full content.

View Full Document Right Arrow Icon
– Example: printBinary(7) prints 111 – Example: printBinary(12) prints 1100 – Example: printBinary(42) prints 101010 Write the method recursively and without using any loops. 1 8 0 4 0 1 0 1 2 4 value 1 2 16 32 1 10 place
Image of page 6

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
7 Case analysis • Recursion is about solving a small piece of a large problem. What is 69743 in binary? • Do we know anything about its representation in binary? Case analysis: • What is/are easy numbers to print in binary? • Can we express a larger number in terms of a smaller number(s)? Suppose we are examining some arbitrary integer N . • if N 's binary representation is 1001010101 1 •(N / 2) 's binary representation is 1001010101 •(N % 2) 's binary representation is 1
Image of page 7
8 printBinary solution // Prints the given integer's binary representation. // Precondition: n >= 0 public static void printBinary(int n) { if (n < 2) { // base case; same as base 10 System.out.println(n); } else { // recursive case; break number apart printBinary(n / 2); printBinary(n % 2); } } – Can we eliminate the precondition and deal with negatives?
Image of page 8

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
9 printBinary solution 2 // Prints the given integer's binary representation. public static void printBinary(int n) { if (n < 0) { // recursive case for negative numbers System.out.print("-"); printBinary(-n); } else if (n < 2) { // base case; same as base 10 System.out.println(n); } else { // recursive case; break number apart printBinary(n / 2); printBinary(n % 2); } }
Image of page 9
10 Exercise • Write a recursive method isPalindrome accepts a String and returns true if it reads the same forwards as backwards.
Image of page 10

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern