Skip to content
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

Exercise3.4 #49

Open
wants to merge 4 commits into
base: bootcamp_roberta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions exercise3.4/RomanNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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(String l) {
for (RomanNumber r : RomanNumber.values()) {
if (r.toString().equals(l)) {
return r.value;
}
}

throw new IllegalArgumentException("Not a romand symbol!");

}
}
109 changes: 109 additions & 0 deletions exercise3.4/RomanNumberConversion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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";
}
StringBuilder resul = new StringBuilder();
while (dec >= 1000) {
Copy link
Owner

Choose a reason for hiding this comment

The 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.append("M");
dec -= 1000;
}
while (dec >= 900) {
Copy link
Owner

Choose a reason for hiding this comment

The 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.append("CM");
dec -= 900;
}
while (dec >= 500) {
resul.append("D");
dec -= 500;
}
while (dec >= 400) {
resul.append("CD");
dec -= 400;
}
while (dec >= 100) {
resul.append("C");
dec -= 100;
}
while (dec >= 90) {
resul.append("XC");
dec -= 90;
}
while (dec >= 50) {
resul.append("L");
dec -= 50;
}
while (dec >= 40) {
resul.append("XL");
dec -= 40;
}
while (dec >= 10) {
resul.append("X");
dec -= 10;
}
while (dec >= 9) {
resul.append("IX");
dec -= 9;
}
while (dec >= 5) {
resul.append("V");
dec -= 5;
}
while (dec >= 4) {
resul.append("IV");
dec -= 4;
}
while (dec >= 1) {
resul.append("I");
dec -= 1;
}

return resul.toString();

}

// 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.substring(i, i + 1));

//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);

}

}
70 changes: 70 additions & 0 deletions exercise3.4/RomanNumberConversionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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);

}

/**
* 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);

}

}