Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 data() { return Arrays.asList(new Object[][] { { 1, "I"}, { 2, "II" }, { 3, "III" }, { 4, "IV" }, { 5, "V" }, { 6, "VI" }, { 7, "VII" }, { 8, "VIII" }, { 9, "IX" }, { 10, "X" }, { 11, "XI" }, { 14, "XIV" }, { 15, "XV" }, { 16, "XVI" }, { 19, "XIX" }, { 20, "XX" }, { 30, "XXX" }, { 40, "XL", }, { 49, "XLIX" }, { 50, "L" }, { 51, "LI" }, { 59, "LIX" }, { 60, "LX" }, { 70, "LXX" }, { 80, "LXXX" }, { 90, "XC" }, { 91, "XCI" }, { 92, "XCII" }, { 93, "XCIII" }, { 94, "XCIV" }, { 95, "XCV" }, { 96, "XCVI" }, { 97, "XCVII" }, { 98, "XCVIII" }, { 99, "XCIX" }, { 100, "C" }, { 101, "CI" }, // This is complete and satisfactory { 110, "CX" }, { 127, "CXXVII" }, { 144, "CXLIV" }, { 200, "CC" }, { 300, "CCC" }, { 400, "CD" }, { 500, "D" }, { 549, "DXLIX" }, { 600, "DC" }, { 700, "DCC" }, { 800, "DCCC" }, { 900, "CM" }, { 1000, "M" }, { 2000, "MM" }, { 2222, "MMCCXXII" }, { 2345, "MMCCCXLV" }, { 3000, "MMM" }, // This is exceeds expectations }); }

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

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

Students also viewed these Databases questions

Question

Be familiar with the basic ways to manage capacity.

Answered: 1 week ago

Question

Be familiar with the five basic ways to manage demand.

Answered: 1 week ago