-
Notifications
You must be signed in to change notification settings - Fork 164
Exercise3.4 #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: bootcamp_roberta
Are you sure you want to change the base?
Exercise3.4 #49
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * To change this license header, choose License Headers in Project Properties. | ||
| * To change this template file, choose Tools | Templates | ||
| * and open the template in the editor. | ||
| */ | ||
| package romannumberconversion; | ||
|
|
||
| /** | ||
| * | ||
| * @author roberta | ||
| */ | ||
| public enum RomanNumber { | ||
|
|
||
| I(1), | ||
| V(5), | ||
| X(10), | ||
| L(50), | ||
| C(100), | ||
| D(500), | ||
| M(1000); | ||
|
|
||
| private final int value; | ||
|
|
||
| RomanNumber(int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return this.value; | ||
| } | ||
|
|
||
| /** | ||
| * Devuelve el valor numérico de la letra romana | ||
| * | ||
| * @param l | ||
| * @return | ||
| */ | ||
| public static int parse(char l) { | ||
| switch (l) { | ||
| case 'I': | ||
| return I.value; | ||
| case 'V': | ||
| return V.value; | ||
| case 'X': | ||
| return X.value; | ||
| case 'L': | ||
| return L.value; | ||
| case 'C': | ||
| return C.value; | ||
| case 'D': | ||
| return D.value; | ||
| case 'M': | ||
| return M.value; | ||
| default: | ||
| throw new IllegalArgumentException("Not a romand symbol!"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * To change this license header, choose License Headers in Project Properties. | ||
| * To change this template file, choose Tools | Templates | ||
| * and open the template in the editor. | ||
| */ | ||
| package romannumberconversion; | ||
|
|
||
| /** | ||
| * | ||
| * @author roberta | ||
| */ | ||
| public class RomanNumberConversion { | ||
|
|
||
| private String romanNumber; | ||
|
|
||
|
|
||
|
|
||
| public static String decToRoman(int dec) { | ||
| if (dec <= 0) { | ||
| return " There isn't zero in Roman number System"; | ||
| } | ||
| String resul = ""; | ||
| while (dec >= 1000) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. En vez de hacer esto no podemos hacer una division? Por ejemplo si yo divido por 1000 el cociente entero me dice cuantas veces deberia poner determinada letra que me decis? |
||
| resul += "M"; | ||
| dec -= 1000; | ||
| } | ||
| while (dec >= 900) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creo firmemente que a esto se le puede pegar una vuelta de rosca por que sino la complejidad algoritmica se va a la nubes |
||
| resul += "CM"; | ||
| dec -= 900; | ||
| } | ||
| while (dec >= 500) { | ||
| resul += "D"; | ||
| dec -= 500; | ||
| } | ||
| while (dec >= 400) { | ||
| resul += "CD"; | ||
| dec -= 400; | ||
| } | ||
| while (dec >= 100) { | ||
| resul += "C"; | ||
| dec -= 100; | ||
| } | ||
| while (dec >= 90) { | ||
| resul += "XC"; | ||
| dec -= 90; | ||
| } | ||
| while (dec >= 50) { | ||
| resul += "L"; | ||
| dec -= 50; | ||
| } | ||
| while (dec >= 40) { | ||
| resul += "XL"; | ||
| dec -= 40; | ||
| } | ||
| while (dec >= 10) { | ||
| resul += "X"; | ||
| dec -= 10; | ||
| } | ||
| while (dec >= 9) { | ||
| resul += "IX"; | ||
| dec -= 9; | ||
| } | ||
| while (dec >= 5) { | ||
| resul += "V"; | ||
| dec -= 5; | ||
| } | ||
| while (dec >= 4) { | ||
| resul += "IV"; | ||
| dec -= 4; | ||
| } | ||
| while (dec >= 1) { | ||
| resul += "I"; | ||
| dec -= 1; | ||
| } | ||
|
|
||
| return resul; | ||
|
|
||
| } | ||
| // Returns a string containing the entered Roman numeral in numeral form. | ||
| public String romanToDec(String s) { | ||
|
|
||
|
|
||
|
|
||
| int resul = 0; | ||
| int last_digit = 0; | ||
| int current_digit = 0; | ||
|
|
||
| for (int i = 0; i < s.length(); i++) { | ||
| current_digit = RomanNumber.parse(s.charAt(i)); | ||
|
|
||
|
|
||
| //This is the tricky part. | ||
| //If the last number is smaller than the curren number, subtract the last number from the current number | ||
| //Otherwise, just add the current number. We must also skip the first number from this rule simply because | ||
| //e.g. someone enters 1799 in which case it would subtract 1 from 7 | ||
|
|
||
| if (last_digit < current_digit && last_digit != 0) { | ||
| current_digit -= last_digit; | ||
| resul -= last_digit; | ||
| resul += current_digit; | ||
| last_digit = current_digit; | ||
| current_digit = 0; | ||
| } else { | ||
| last_digit = current_digit; | ||
| resul += current_digit; | ||
| current_digit = 0; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| return String.valueOf(resul); | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * To change this license header, choose License Headers in Project Properties. | ||
| * To change this template file, choose Tools | Templates | ||
| * and open the template in the editor. | ||
| */ | ||
|
|
||
| package romannumberconversion; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import static org.junit.Assert.*; | ||
|
|
||
| /** | ||
| * | ||
| * @author roberta | ||
| */ | ||
| public class RomanNumberConversionTest { | ||
|
|
||
| public RomanNumberConversionTest() { | ||
| } | ||
|
|
||
| @BeforeClass | ||
| public static void setUpClass() { | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void tearDownClass() { | ||
| } | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Test of decToRoman method, of class RomanNumberConversion. | ||
| */ | ||
| @Test | ||
| public void testDecToRoman() { | ||
| System.out.println("decToRoman"); | ||
| int dec = 5; | ||
| String expResult = "V"; | ||
| String result = RomanNumberConversion.decToRoman(dec); | ||
| assertEquals(expResult, result); | ||
| // TODO review the generated test code and remove the default call to fail. | ||
| //fail("The test case is a prototype."); | ||
| } | ||
|
|
||
| /** | ||
| * Test of toArabic method, of class RomanNumberConversion. | ||
| */ | ||
| @Test | ||
| public void testToArabic() { | ||
| System.out.println("toArabic"); | ||
| String s = "XXI"; | ||
| RomanNumberConversion instance = new RomanNumberConversion(); | ||
| String expResult = "21"; | ||
| String result = instance.romanToDec(s); | ||
| assertEquals(expResult, result); | ||
| // TODO review the generated test code and remove the default call to fail. | ||
| // fail("The test case is a prototype."); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
en ves de tener un switch se podria tener un bucle while que vaya comprando e iterando y corte cuando encuentra el elemento. Existe un metodo que se llama enumClass.values()
public static RomanNumber getRomanValueFromChar(Char i) {
for (RomanNumber romanNumber : RomanNumber.values()) {
if (romanNumber.getValue().equals(i)) {
return romanNumber;
}
}
// throw an IllegalArgumentException or return null
throw new IllegalArgumentException("the given letter doesn't match any Roman Number.");
}