Question
I need help with this java activity. I'm really not understanding how to do it. I was thinking using a switch statement because I don't
I need help with this java activity. I'm really not understanding how to do it. I was thinking using a switch statement because I don't really understand using recursion. Can anyone help? It is a test case question which I am just learning on my own.
package com.el.onboarding.junit.romanNumerals.services;
public class HinduToRomanNumeralConverter {
public String convert(int numberToConvert) {
int number = 1;
number = Math.min(3999, Math.max(1, number)); // wraps number between 1-3999
String asRomanNumerals = "";
// Array including numerals in ascending order
String RN = "IVXLCDM";
int i = 0; // Index used to keep track which digit we are translating
while (number > 0) {
switch(number % 10) {
case 1: asRomanNumerals = String.valueOf(RN.charAt(i)) + asRomanNumerals; break;
case 2: asRomanNumerals = String.valueOf(RN.charAt(i)) + String.valueOf(RN.charAt(i)) + asRomanNumerals; break;
case 3: asRomanNumerals = String.valueOf(RN.charAt(i)) + String.valueOf(RN.charAt(i)) + String.valueOf(RN.charAt(i)) + asRomanNumerals; break;
case 4: asRomanNumerals = String.valueOf(RN.charAt(i)) + String.valueOf(RN.charAt(i+1)) + asRomanNumerals; break;
case 5: asRomanNumerals = String.valueOf(RN.charAt(i+1)) + asRomanNumerals; break;
case 6: asRomanNumerals = String.valueOf(RN.charAt(i+1)) + String.valueOf(RN.charAt(i)) + asRomanNumerals; break;
case 7: asRomanNumerals = String.valueOf(RN.charAt(i+1)) + String.valueOf(RN.charAt(i)) + String.valueOf(RN.charAt(i)) + asRomanNumerals; break;
case 8: asRomanNumerals = String.valueOf(RN.charAt(i+1)) + String.valueOf(RN.charAt(i)) + String.valueOf(RN.charAt(i)) + String.valueOf(RN.charAt(i)) +asRomanNumerals; break;
case 9: asRomanNumerals = String.valueOf(RN.charAt(i)) + String.valueOf(RN.charAt(i+2)) + asRomanNumerals; break;
}
number = (int) number / 10;
i += 2;
}
return asRomanNumerals;
}}
_____________________________________________________________________________________________________________
// HinduToRomanNumeralConverterTests.java package com.el.onboarding.junit.romanNumerals.services;
import static org.assertj.core.api.Assertions.*;
import java.util.Arrays; import java.util.Collection;
import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters;
import com.el.onboarding.junit.romanNumerals.services.HinduToRomanNumeralConverter;
@RunWith(Parameterized.class) public class HinduToRomanNumeralConverterTests {
@Parameters public static Collection
private int input; private String expectedOutput;
public HinduToRomanNumeralConverterTests(int input, String expectedOutput) { this.input = input; this.expectedOutput = expectedOutput; }
@Test public void testConversion() { // Arrange HinduToRomanNumeralConverter converter = new HinduToRomanNumeralConverter();
// Act String result = converter.convert(input);
// Assert assertThat(result).isEqualTo(expectedOutput); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started