Skip to content
Closed
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
101 changes: 101 additions & 0 deletions Conversions/IntToRoman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
public class IntToRoman {

int value(int toPret){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a look on the code style conventions Between methods and { a space.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This methods you can write as static. Then you don't need the instanciate a object of type IntToRoman and you can use this method right away in you main method.

if(toPret>=1000){
return 1000;
}else if(1000>toPret && toPret>=900){
return 900;
}else if(900>toPret && toPret>=500){
return 500;
}else if(500>toPret && toPret>=400){
return 400;
}else if(400>toPret && toPret>=100){
return 100;
}else if(100>toPret && toPret>=90){
return 90;
}else if(90>toPret && toPret>=50){
return 50;
}else if(50>toPret && toPret>=40){
return 40;
}else if(40>toPret && toPret>=10){
return 10;
}else if(10>toPret && toPret>=9){
return 9;
}else if(9>toPret && toPret>=5){
return 5;
}else if(5>toPret && toPret>=4){
return 4;
}else if(4>toPret && toPret>=1){
return 1;
}else{
return 0;
}
}

String valStr(int a){
if(a==1000){
return "M";
}
if(a==900){
return "CM";
}
if(a==500){
return "D";
}
if(a==400){
return "CD";
}
if(a==100){
return "C";
}
if(a==90){
return "XC";
}
if(a==50){
return "L";
}
if(a==40){
return "XL";
}
if(a==10){
return "X";
}
if(a==9){
return "IX";
}
if(a==5){
return "V";
}
if(a==4){
return "IV";
}
if(a==1){
return "I";
}

return "";
}

StringBuffer str = new StringBuffer();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need StringBuffer you can simply use String.

public String intToRoman(int a) {

int toRepeat = value(a);
String corString = valStr(toRepeat);
for(int i =0;i<(a/toRepeat);i++){
str.append(corString);
}
if((a%toRepeat)>0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Between if and (...) one space. See

intToRoman(a%toRepeat);
return str.toString();

}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct indentation.



public static void main(String[] s){

int intVal = 11128;
System.out.println(new IntToRoman().intToRoman(intVal));

}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct the indentation.


}
51 changes: 51 additions & 0 deletions Conversions/RomanToInt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
public class RomanToInt {
int value(String a){
if(a.equals("I")){
return 1;
}else if(a.equals("V")){
return 5;
}else
if(a.equals("X")){
return 10;
}else
if(a.equals("L")){
return 50;
}else if(a.equals("C")){
return 100;
}else
if(a.equals("D")){
return 500;
}else
if(a.equals("M")){
return 1000;
}else{
return 0;
}
}
public int romanToInt(String a1) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct indentation.

int total = 0;
for(int i=0;i<a1.length();i++){
String s1 = a1.charAt(i)+"";
if(i!=a1.length()-1){
String s2 = a1.charAt(i+1)+"";
if(value(s1)>=value(s2)){
total+=value(s1);
}else{
total = total-value(s1)+value(s2);
i++;
}
}else{
total+=value(s1);
}
}
return total;
}

public static void main(String[] s){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing as above.


String romanVal = "XIV";
System.out.println(new RomanToInt().romanToInt(romanVal));

}

}