Your job is to design a test plan to test the following static method that conve
ID: 3801878 • Letter: Y
Question
Your job is to design a test plan to test the following static method that converts a given NaturalNumber into a String with commas in a format similar to the one used by the method printWithCommas you implemented in a previous lab:
/**
* Converts the given {@code NaturalNumber} to a {@code String} with commas.
*
* @param n
* the number
* @return the {@code String} with commas
* @ensures toStringWithCommas = [String representation of n with commas]
*/
public static String toStringWithCommas(NaturalNumber n) {...}
/**
* Converts the given {@code NaturalNumber} to a {@code String} with commas.
*
* @param n
* the number
* @return the {@code String} with commas
* @ensures toStringWithCommas = [String representation of n with commas]
*/
public static String toStringWithCommas(NaturalNumber n) {...}
Explanation / Answer
Test.java
public class Test {
public static void main(String[] args) {
int n = 5;
System.out.println(toStringWithCommas(n));
}
public static String toStringWithCommas(int n) {
String s = "";
for(int i=1; i<=n; i++){
s = s + i+ ",";
}
s = s.substring(0, s.length()-1);
return s;
}
}
output:
1,2,3,4,5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.