From 390bcf715cb5c31ac1df5403aa85f9fef1441cd6 Mon Sep 17 00:00:00 2001 From: tonkeran Date: Fri, 30 Sep 2016 16:53:35 +0300 Subject: [PATCH 1/3] Project started --- src/RomanNumerals.java | 90 +++++++++++++++++++++++++++++++++++- tests/TestRomanNumerals.java | 15 +++++- 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index 20904f0..beb4718 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -1,8 +1,94 @@ +import sun.security.util.Length; public class RomanNumerals { + public int convertToInteger(String romanNum) { - // To be Implemented - return 0; + + int Resultnum = 0; + switch(romanNum){ + case "I": + Resultnum = 1; + break; + case "II": + Resultnum = 2; + break; + case "III": + Resultnum = 3; + break; + case "IV": + Resultnum = 4; + break; + case "V": + Resultnum = 5; + break; + case "VI": + Resultnum = 6; + break; + case "VII": + Resultnum = 7; + break; + case "VIII": + Resultnum = 8; + break; + case "IX": + Resultnum = 9; + break; + } + + return Resultnum; + } + + public int get_Roman_Number(String romanum){ + + int RomanNumAsinteger = 0; + + if(romanum.charAt(0) == 'M'){ + RomanNumAsinteger = 1000; + }if(romanum.charAt(1) == 'C' || romanum.charAt(1) == 'D'){ + RomanNumAsinteger = RomanNumAsinteger + this.gethundreds(romanum); + } + + return RomanNumAsinteger; + } + + public int gethundreds(String romanum){ + int length = romanum.length(); + int hundreds = 0; + + if(romanum.charAt(1) == 'C'){ + hundreds = 100; + length = length - 1; + }else if(romanum.charAt(2) == 'C'){ + hundreds = 200; + length = length - 2; + }else if(romanum.charAt(3) == 'C'){ + hundreds = 300; + length = length - 3; + }else if(romanum.charAt(2) == 'D'){ + hundreds = 400; + length = length - 2; + }else if(romanum.charAt(2) == 'M'){ + hundreds = 900; + length = length - 2; + }else if(romanum.charAt(1) == 'D'){ + hundreds = 500; + length = length - 1; + }else if(romanum.charAt(2) == 'C'){ + hundreds = 600; + length = length - 2; + }else if(romanum.charAt(3) == 'C'){ + hundreds = 700; + length = length - 3; + }else if(romanum.charAt(4) == 'C'){ + hundreds = 800; + length = length - 4; + } + + return hundreds; + + } + + } diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index 5d1de75..8a11eac 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -5,8 +5,19 @@ public class TestRomanNumerals { @Test - public void test() { - fail("Not yet implemented"); + public void ConvertNumberlength1() { + + RomanNumerals romannumerals = new RomanNumerals(); + + assertEquals(6, romannumerals.get_Roman_Number("VI")); + } + + @Test + public void ConvertNumber1284() { + + RomanNumerals romannumerals = new RomanNumerals(); + + assertEquals(6, romannumerals.get_Roman_Number("MCCLXXXIV")); } } From a8b95bdbdce88e6448ed3b47e318a4a55b9502d0 Mon Sep 17 00:00:00 2001 From: tonkeran Date: Fri, 7 Oct 2016 16:52:48 +0300 Subject: [PATCH 2/3] Project --- src/RomanNumerals.java | 89 +++++++++++++++++++++++++++++++----- tests/TestRomanNumerals.java | 6 +-- 2 files changed, 80 insertions(+), 15 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index beb4718..db8bf36 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -2,6 +2,10 @@ public class RomanNumerals { + int length; + int length1; + + public int convertToInteger(String romanNum) { int Resultnum = 0; @@ -43,52 +47,113 @@ public int convertToInteger(String romanNum) { public int get_Roman_Number(String romanum){ int RomanNumAsinteger = 0; + int Charsleft = 0; if(romanum.charAt(0) == 'M'){ RomanNumAsinteger = 1000; + Charsleft = Charsleft + 1; }if(romanum.charAt(1) == 'C' || romanum.charAt(1) == 'D'){ RomanNumAsinteger = RomanNumAsinteger + this.gethundreds(romanum); } + int Length4 = romanum.length() - length - Charsleft - 1; + Charsleft = romanum.length() - Length4; + + if(romanum.charAt(Charsleft) == 'X' || romanum.charAt(Charsleft) == 'L'){ + RomanNumAsinteger = RomanNumAsinteger + this.getTens(romanum, Length4); + }if(romanum.charAt(0) == 'I' || romanum.charAt(0) == 'V'){ + RomanNumAsinteger = convertToInteger(romanum); + } + + String number = null; + int length3 = length1 + Charsleft; + + while(length3 != romanum.length()){ + number = romanum.charAt(length3) + number; + length3++; + } + + if(length3 == romanum.length()){ + RomanNumAsinteger = RomanNumAsinteger + convertToInteger(number); + } + return RomanNumAsinteger; } public int gethundreds(String romanum){ - int length = romanum.length(); + int hundreds = 0; - if(romanum.charAt(1) == 'C'){ + if(romanum.charAt(1) == 'C' && romanum.charAt(2) != 'C'){ hundreds = 100; - length = length - 1; - }else if(romanum.charAt(2) == 'C'){ + length = length + 1; + }else if(romanum.charAt(2) == 'C' && romanum.charAt(3) != 'C'){ hundreds = 200; length = length - 2; }else if(romanum.charAt(3) == 'C'){ hundreds = 300; - length = length - 3; + length = length + 3; }else if(romanum.charAt(2) == 'D'){ hundreds = 400; - length = length - 2; + length = length + 2; }else if(romanum.charAt(2) == 'M'){ hundreds = 900; - length = length - 2; + length = length + 2; }else if(romanum.charAt(1) == 'D'){ hundreds = 500; - length = length - 1; + length = length + 1; }else if(romanum.charAt(2) == 'C'){ hundreds = 600; - length = length - 2; + length = length + 2; }else if(romanum.charAt(3) == 'C'){ hundreds = 700; - length = length - 3; + length = length + 3; }else if(romanum.charAt(4) == 'C'){ hundreds = 800; - length = length - 4; + length = length + 4; } return hundreds; } - + public int getTens(String romanum, int length2){ + int tens = 0; + length1 = length2; + + if(romanum.charAt(length1) == 'X' && romanum.charAt(length1 + 1) != 'X'){ + tens = 10; + length1++; + }if(romanum.charAt(length1 + 1) == 'X' && romanum.charAt(length1 + 2) != 'X'){ + tens = 20; + length1 = length1 + 2; + }if(romanum.charAt(length1 + 2) == 'X'){ + tens = 30; + length1 = length1 + 3; + }if(romanum.charAt(length1 + 2) == 'L'){ + tens = 40; + length1 = length1 + 2; + }if(romanum.charAt(length + 2) == 'C'){ + tens = 90; + length1 = length1 + 2; + } + + if(romanum.charAt(length1) == 'L'){ + tens = 50; + length1++; + }if(romanum.charAt(length1 + 2) == 'X' && romanum.charAt(length1 + 3) != 'X'){ + tens = 60; + length1 = length1 + 2; + }if(romanum.charAt(length1 + 3) == 'X' && romanum.charAt(length1 + 4) != 'X'){ + tens = 70; + length1 = length1 + 3; + }if(romanum.charAt(length1 + 4) == 'X'){ + tens = 80; + length1 = length1 + 4; + } + + return tens; + + } + } diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index 8a11eac..abe94dc 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -13,11 +13,11 @@ public void ConvertNumberlength1() { } @Test - public void ConvertNumber1284() { + public void ConvertNumber1334() { RomanNumerals romannumerals = new RomanNumerals(); - assertEquals(6, romannumerals.get_Roman_Number("MCCLXXXIV")); + assertEquals(1134, romannumerals.get_Roman_Number("MCCCLXXXIV")); } -} +} \ No newline at end of file From b3cef3fa1f830135e76f2b78d8b3f719b611022c Mon Sep 17 00:00:00 2001 From: Toni Date: Sat, 8 Oct 2016 18:48:50 +0300 Subject: [PATCH 3/3] Project Finished --- src/RomanNumerals.java | 65 ++++++++++++++++++------------------ tests/TestRomanNumerals.java | 22 ++++++++++-- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/src/RomanNumerals.java b/src/RomanNumerals.java index db8bf36..31697f4 100644 --- a/src/RomanNumerals.java +++ b/src/RomanNumerals.java @@ -56,25 +56,26 @@ public int get_Roman_Number(String romanum){ RomanNumAsinteger = RomanNumAsinteger + this.gethundreds(romanum); } - int Length4 = romanum.length() - length - Charsleft - 1; - Charsleft = romanum.length() - Length4; + int Length4 = romanum.length() - length - Charsleft; + Length4 = romanum.length() - Length4; - if(romanum.charAt(Charsleft) == 'X' || romanum.charAt(Charsleft) == 'L'){ + if(romanum.charAt(Length4) == 'X' || romanum.charAt(Length4) == 'L'){ RomanNumAsinteger = RomanNumAsinteger + this.getTens(romanum, Length4); }if(romanum.charAt(0) == 'I' || romanum.charAt(0) == 'V'){ RomanNumAsinteger = convertToInteger(romanum); - } + }else{ - String number = null; - int length3 = length1 + Charsleft; + Length4 = Length4 + length1; + StringBuilder builder = new StringBuilder(); - while(length3 != romanum.length()){ - number = romanum.charAt(length3) + number; - length3++; - } + while(Length4 <= romanum.length() - 1){ + builder.append(romanum.charAt(Length4)); + Length4++; + } - if(length3 == romanum.length()){ - RomanNumAsinteger = RomanNumAsinteger + convertToInteger(number); + if(Length4 == romanum.length()){ + RomanNumAsinteger = RomanNumAsinteger + convertToInteger(builder.toString()); + } } return RomanNumAsinteger; @@ -89,7 +90,7 @@ public int gethundreds(String romanum){ length = length + 1; }else if(romanum.charAt(2) == 'C' && romanum.charAt(3) != 'C'){ hundreds = 200; - length = length - 2; + length = length + 2; }else if(romanum.charAt(3) == 'C'){ hundreds = 300; length = length + 3; @@ -121,35 +122,33 @@ public int getTens(String romanum, int length2){ int tens = 0; length1 = length2; - if(romanum.charAt(length1) == 'X' && romanum.charAt(length1 + 1) != 'X'){ + if(romanum.charAt(length1) == 'X' || romanum.charAt(length1) == 'X' && romanum.charAt(length1 + 1) != 'X'){ tens = 10; - length1++; - }if(romanum.charAt(length1 + 1) == 'X' && romanum.charAt(length1 + 2) != 'X'){ + length1 = 1; + }else if(romanum.charAt(length1 + 1) == 'X' && romanum.charAt(length1 + 2) != 'X'){ tens = 20; - length1 = length1 + 2; - }if(romanum.charAt(length1 + 2) == 'X'){ + length1 = 2; + }else if(romanum.charAt(length1 + 2) == 'X' && romanum.charAt(length1) != 'L'){ tens = 30; - length1 = length1 + 3; - }if(romanum.charAt(length1 + 2) == 'L'){ + length1 = 3; + }else if(romanum.charAt(length1 + 2) == 'L' && romanum.charAt(length1 + 1) == 'X'){ tens = 40; - length1 = length1 + 2; - }if(romanum.charAt(length + 2) == 'C'){ + length1 = 2; + }else if(romanum.charAt(length1 + 2) == 'C'){ tens = 90; - length1 = length1 + 2; - } - - if(romanum.charAt(length1) == 'L'){ + length1 = 2; + }else if(romanum.charAt(length1) == 'L' && romanum.charAt(length1 + 1) != 'X'){ tens = 50; - length1++; - }if(romanum.charAt(length1 + 2) == 'X' && romanum.charAt(length1 + 3) != 'X'){ + length1 = 1; + }else if(romanum.charAt(length1 + 1) == 'X' && romanum.charAt(length1 + 2) != 'X'){ tens = 60; - length1 = length1 + 2; - }if(romanum.charAt(length1 + 3) == 'X' && romanum.charAt(length1 + 4) != 'X'){ + length1 = 2; + }else if(romanum.charAt(length1 + 2) == 'X' && romanum.charAt(length1 + 3) != 'X'){ tens = 70; - length1 = length1 + 3; - }if(romanum.charAt(length1 + 4) == 'X'){ + length1 = 3; + }else if(romanum.charAt(length1 + 3) == 'X'){ tens = 80; - length1 = length1 + 4; + length1 = 4; } return tens; diff --git a/tests/TestRomanNumerals.java b/tests/TestRomanNumerals.java index abe94dc..0590960 100644 --- a/tests/TestRomanNumerals.java +++ b/tests/TestRomanNumerals.java @@ -13,11 +13,27 @@ public void ConvertNumberlength1() { } @Test - public void ConvertNumber1334() { + public void ConvertNumberlength2() { RomanNumerals romannumerals = new RomanNumerals(); - assertEquals(1134, romannumerals.get_Roman_Number("MCCCLXXXIV")); + assertEquals(8, romannumerals.get_Roman_Number("VIII")); + } + + @Test + public void ConvertNumber1384() { + + RomanNumerals romannumerals = new RomanNumerals(); + + assertEquals(1384, romannumerals.get_Roman_Number("MCCCLXXXIV")); + } + + @Test + public void ConvertNumber1210() { + + RomanNumerals romannumerals = new RomanNumerals(); + + assertEquals(1210, romannumerals.get_Roman_Number("MCCX")); } -} \ No newline at end of file +}