Created
October 7, 2015 02:37
-
-
Save connlark/7f6bc4a4e4841ddb7f83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* Created by Connor on 10/5/15. | |
*/ | |
public class RomanNumbers { | |
public int input; | |
//parametrized constructor | |
public RomanNumbers(int number) { | |
input = number; | |
} | |
public String getRoman() { | |
//formatting the input to always have four numbers ex. 0013 | |
String formattedInput = ""; | |
String numSTR = Integer.toString(input); | |
int length = numSTR.length(); | |
if (length < 4) { | |
if (length == 1) { | |
formattedInput = "000" + numSTR; | |
} | |
if (length == 2) { | |
formattedInput = "00" + numSTR; | |
} | |
if (length == 3) { | |
formattedInput = "0" + numSTR; | |
} | |
} else { | |
formattedInput = numSTR; | |
} | |
//getting digits off the number | |
int ones = Integer.parseInt(formattedInput.substring(3)); | |
int tens = Integer.parseInt(formattedInput.substring(2, 3)); | |
int hundreds = Integer.parseInt(formattedInput.substring(1, 2)); | |
int thousands = Integer.parseInt(formattedInput.substring(0, 1)); | |
//finding romanThousands | |
String romanThousands; | |
if (thousands > 0 ) { | |
romanThousands = "MMMMM".substring(0, thousands); | |
} | |
else { | |
romanThousands = ""; | |
} | |
//finding romanHundreds | |
String romanHundreds; | |
if (hundreds <= 3 && hundreds != 0) | |
romanHundreds = "CCCCC".substring(0, hundreds); | |
else if (hundreds == 4) | |
romanHundreds = "CD"; | |
else if (hundreds <= 8 && hundreds != 0) | |
romanHundreds = "DCCCC".substring(0, hundreds -4); | |
else if (hundreds == 9) | |
romanHundreds = "CM"; | |
else | |
romanHundreds = ""; | |
//finding romanTens | |
String romanTens; | |
if (tens <= 3 && tens != 0) | |
romanTens = "XXXXX".substring(0, tens); | |
else if (tens == 4) | |
romanTens = "XL"; | |
else if (tens <= 8 && tens != 0) | |
romanTens = "LXXXX".substring(0, tens -4); | |
else if (tens == 9) | |
romanTens = "XC"; | |
else | |
romanTens = ""; | |
////finding romanOnes | |
String romanOnes; | |
if (ones <= 3) | |
romanOnes = "IIIII".substring(0, ones); | |
else if (ones == 4) | |
romanOnes = "IV"; | |
else if (ones <= 8) | |
romanOnes = "VIIII".substring(0, ones -4); | |
else if (ones == 9) | |
romanOnes = "IX"; | |
else | |
romanOnes = "0"; | |
//printing all of the roman numbers in order | |
return romanThousands + romanHundreds + romanTens + romanOnes; | |
} | |
} | |
import java.util.Scanner; | |
/** | |
* Created by Connor on 10/5/15. | |
*/ | |
public class RomanTester | |
{ | |
public static void main(String [] args) { | |
Scanner key = new Scanner(System.in); | |
System.out.println("Enter an integer: "); | |
int input = key.nextInt(); | |
RomanNumbers num1 = new RomanNumbers(input); | |
System.out.println(input + " in Roman Numerals is: " + num1.getRoman()); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment