Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following questions be solved using only a recursive method . Coding language : Java Questions /** * * @param n * @param d: single

The following questions be solved usingonly a recursive method.

Coding language : Java

Questions

/** * * @param n * @param d: single digit integer * @return the number of occurrences of digit d in integer n, * except when target (d) appears twice in a row, * the second one counts twice * * For example, * * if countWeighted(8818, 8) returns 4. * * Explanation: * * First 8 (from left) contributes 1 * Second 8 contributes 2 (since there is another 8 immediately to its left) * Third 8 contributes 1 * * Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), * while divide (/) by 10 removes the rightmost digit (126 / 10 is 12). */ public int countWeighted(int n, int d) { return 0; //to be completed }

/** * * @param str * @param tokens * @return true if str contains all characters from token, false otherwise * Note if tokens contains multiple occurrences of a particular character, * then str must have at least as many occurrences of that character. */ public boolean contains(String str, String tokens) { return false; //to be completed }

Test Cases

@Test public void testCountWeighted() { assertEquals(5, testObject.countWeighted(888, 8)); assertEquals(3, testObject.countWeighted(88, 8)); assertEquals(2, testObject.countWeighted(808, 8)); assertEquals(1, testObject.countWeighted(8, 8));

assertEquals(5, testObject.countWeighted(707070707, 7)); assertEquals(4, testObject.countWeighted(707070707, 0)); assertEquals(17, testObject.countWeighted(777777777, 7)); }

@Test public void testContainsStringString() { assertTrue(testObject.contains("man", "nma")); assertTrue(testObject.contains("superman", "amen")); assertTrue(testObject.contains("superman", "")); assertTrue(testObject.contains("", "")); assertFalse(testObject.contains("environmentally prudent scheme", "pool")); assertTrue(testObject.contains("environmentally prudent scheme", "pedestrian")); assertFalse(testObject.contains("environmentally prudent scheme", "xoxo")); assertFalse(testObject.contains("environmentally prudent scheme", "scheme!")); assertFalse(testObject.contains("environmentally prudent scheme", "Scheme")); //uppercase 'S' }

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

Survey of Accounting

Authors: Carl S Warren

5th Edition

9780538489737, 538749091, 538489731, 978-0538749091

Students also viewed these Programming questions