Can someone help me write a JAVA code named makeDingDong ? The method will take three positive
Question:
Can someone help me write a JAVA code namedmakeDingDong?
The method will take three positive integer parametersa,b, andmaxNumber. The method will iterate through all the integers between 1 andmaxNumberandreturn a Stringas shown in the print statement below. The rules are:
- If the number is divisible bya(and not byb), it is replaced by"ding"
- If the number is divisible byb(and not bya), it is replaced by"dong"
- If the number is divisible by bothaandb, it is replaced by"dingdong"
A few rules and error checking:
- The integersaandbmust be between 2 and 15.
- Bothaandbmust be stricly less thanmaxNumber.
If any one of the two errors condition above is present, the method should print, respectively andEXACTLY:
- The first two inputs have to be between 2 and 15.
- The first two inputs have to be smaller than the third.
If both error conditions occur, the second one (aandbless thanmaxNumber) takes precedence.
Note there is no requirement about the relationship betweenaandb, but their order as arguments matters (acontrols the dings, andbthe dongs)
Final rule: Do not leave any trailing spaces at the end of your output String.
Here in an example:
System.out.println(makeDingDong(3, 7, 24));
yields
1 2 ding 4 5 ding dong 8 ding 10 11 ding 13 dong ding 16 17 ding 19 20 dingdong 22 23 ding
public class DingDong {
// Your method here
// Some test cases in the main method
public static void main(String[] args) {
// System.out.println(makeDingDong(1, 6, 22));
// System.out.println(makeDingDong(3, 17, 20));
// System.out.println(makeDingDong(2, 7, 6));
// System.out.println(makeDingDong(3, 3, 9));
// System.out.println(makeDingDong(3, 7, 24));
// Output:
// The first two inputs have to be between 2 and 15.
// The first two inputs have to be between 2 and 15.
// The first two inputs have to be smaller than the third.
// 1 2 dingdong 4 5 dingdong 7 8 dingdong
// 1 2 ding 4 5 ding dong 8 ding 10 11 ding 13 dong ding 16 17 ding 19 20 dingdong 22 23 ding
}
}
Basic Technical Mathematics
ISBN: 9780137529896
12th Edition
Authors: Allyn J. Washington, Richard Evans