-
Notifications
You must be signed in to change notification settings - Fork 20k
Conversions implemented in Java #433
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
Changes from all 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,101 @@ | ||
public class IntToRoman { | ||
|
||
int value(int toPret){ | ||
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. This methods you can write as |
||
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(); | ||
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. You don't need |
||
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) | ||
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. Between |
||
intToRoman(a%toRepeat); | ||
return str.toString(); | ||
|
||
} | ||
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. Correct indentation. |
||
|
||
|
||
public static void main(String[] s){ | ||
|
||
int intVal = 11128; | ||
System.out.println(new IntToRoman().intToRoman(intVal)); | ||
|
||
} | ||
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. Please correct the indentation. |
||
|
||
} |
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) { | ||
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. 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){ | ||
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. Same thing as above. |
||
|
||
String romanVal = "XIV"; | ||
System.out.println(new RomanToInt().romanToInt(romanVal)); | ||
|
||
} | ||
|
||
} |
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.
Please have a look on the code style conventions Between methods and
{
a space.