Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the two functions incremental and doubling. Each function takes 2 arguments, a character c and a number size, and returns a String made up

Implement the two functions incremental and doubling. Each function takes 2 arguments, a character c and a number size, and returns a String made up of size repetitions of the character c. The difference between the two functions is that incremental builds up the result one char at a time, but appending the char onto the end of the string you are building up which doubling starts with a String consisting of a single c character and keeps appending the string to itself, thus doubling its size until the string is at least as big as required and then uses the substring method form the String class to cut it down to the required size. Run the JUnit tests in to make sure your functions are working correctly.

// MAIN FILE //

package hw7;

public class StringExperiments { /** * Constructs a {@code String} of length {@code size} composed entirely * of the character {@code c}. This function creates the {@code String} * by building it up one character at a time. * @param c the character to use in the {@code String} * @param size the length of the {@code String} * @return a {@code String} consisting of {@code length} {@code c}s. */ public static String incremental(char c, int size) { return null; } /** * Constructs a {@code String} of length {@code size} composed entirely * of the character {@code c}. This function creates the {@code String} * by concatenating the temporary string to itself so as to double the * length of the {@code String} every time until the temporary {@code String} * is at least as long as {@code length}. It then calls the substring method * on the temporary {@code String} to make it the right length. * @param c the character to use in the {@code String} * @param size the length of the {@code String} * @return a {@code String} consisting of {@code length} {@code c}s. */ public static String doubling(char c, int size) { return null; } public static void main(String[] args) { // TODO // Write code to conduct doubling experiments on the two functions. // And collect data for your write-up. } }

// JUNIT TEST //

package hw7;

import static org.junit.Assert.*;

import org.junit.Test;

public class StringExperimentTest {

@Test public void smallTests() { assertEquals("", StringExperiments.incremental('*', 0)); assertEquals("", StringExperiments.doubling('*', 0)); assertEquals("*", StringExperiments.incremental('*', 1)); assertEquals("*", StringExperiments.doubling('*', 1)); assertEquals("**", StringExperiments.incremental('*', 2)); assertEquals("**", StringExperiments.doubling('*', 2)); assertEquals("***********", StringExperiments.incremental('*', 11)); assertEquals("***********", StringExperiments.doubling('*', 11)); } @Test public void largeTestIncremental() { String s = StringExperiments.incremental('x', 1029); assertEquals(1029, s.length()); for(int i = 0; i < 1029; i++) assertEquals('x', s.charAt(i)); s = StringExperiments.incremental('x', 71029); assertEquals(71029, s.length()); for(int i = 0; i < 71029; i++) assertEquals('x', s.charAt(i)); } @Test public void largeTestDoublingl() { String s = StringExperiments.doubling('x', 1029); assertEquals(1029, s.length()); for(int i = 0; i < 1029; i++) assertEquals('x', s.charAt(i)); s = StringExperiments.doubling('x', 71029); assertEquals(71029, s.length()); for(int i = 0; i < 71029; i++) assertEquals('x', s.charAt(i)); }

}

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

x = X1+2X3, x2 = X2 - 2X3, x3 = -2X1+2X2 + X3

Answered: 1 week ago

Question

Demonstrate three aspects of assessing group performance?

Answered: 1 week ago