• Advertisement
Sign in to follow this  

very weird problem in my program with roman numerals.

This topic is 1865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having the weirdest problem ever. See my assignment was to convert roman numerals to dec and vise versa. I also had to do arithmetic with them and convert them back to roman. when I do subtract it seems to work with all numbers except: X - II = XIII = 13 which is wrong!!!
anyways, here is the code for your viewing displeasure :-D

Roman.java

public class Roman
{
protected String romanNum;
protected int decimalNum;
public Roman()
{
romanNum = "I";
decimalNum = 1;
}
public Roman(String rString)
{
romanNum = rString;
romanToDecimal();
}
public void printDecimal()
{
System.out.println(decimalNum);
}
public void printRoman()
{
System.out.println(romanNum);
}
public void setRoman(String rString)
{
romanNum = rString;
romanToDecimal();
}
public void setDecimal(int dec)
{
this.decimalNum = dec;
this.DecimalToRoman();
}
public String getRoman()
{
return romanNum;
}

public int getDecimal()
{
return decimalNum;
}

public void DecimalToRoman()
{
String sum = "";
int temp = 0;

int answer = this.decimalNum / 1000;

for(int i = 0; i < answer; ++i)
{
sum = sum + "M";
}

answer = decimalNum % 1000;

temp = answer;
answer = temp / 500;

for(int i = 0; i < answer; ++i)
{
sum = sum + "D";
}
answer = temp % 500;

temp = answer;
answer = temp / 100;

for(int i = 0; i < answer; ++i)
{
sum = sum + "C";
}

answer = temp % 100;

temp = answer;
answer = temp / 50;

for(int i = 0; i < answer; ++i)
{
sum = sum + "L";
}

answer = temp % 50;

temp = answer;
answer = temp / 10;

for(int i = 0; i < answer; ++i)
{
sum = sum + "X";
}

answer = temp % 10;
temp = answer;
answer = temp / 5;

for(int i = 0; i < answer; ++i)
{
sum = sum + "X";
}

answer = temp % 5;

temp = answer;
answer = temp / 1;
for(int i = 0; i < answer; ++i)
{
sum = sum + "I";
}
answer = temp % 1;
romanNum = sum;

}
public void romanToDecimal()
{
int sum = 0;
int len = romanNum.length();
int index;
int previous = 1000;
for (index = 0; index < len; index++)
{
switch (romanNum.charAt(index))
{
case 'M': sum = sum + 1000;
if (previous < 1000)
sum = sum - 2 * previous;
previous = 1000;
break;
case 'D': sum = sum + 500;
if (previous < 500)
sum = sum - 2 * previous;
previous = 500;
break;
case 'C': sum = sum + 100;
if (previous < 100)
sum = sum - 2 * previous;
previous = 100;
break;
case 'L': sum = sum + 50;
if (previous < 50)
sum = sum - 2 * previous;
previous = 50;
break;
case 'X': sum = sum + 10;
if (previous < 10)
sum = sum - 2 * previous;
previous = 10;
break;
case 'V': sum = sum + 5;
if (previous < 5)
sum = sum - 2 * previous;
previous = 5;
break;
case 'I': sum = sum + 1;
previous = 1;
}
}
decimalNum = sum;
}

// to string
public String toString()
{
return "Roman num: " + romanNum;
}
}


ExtRomans


public class ExtRomans extends Roman
{
public ExtRomans sub(ExtRomans r1, ExtRomans r2)
throws romanException
{
ExtRomans ret = new ExtRomans();
int val1 = r1.decimalNum;
int val2 = r2.decimalNum;

int Sub = (val1 - val2);
if(Sub < 0)
{
throw new romanException("Roman numerals cannot be negative");
}

System.out.println("Subtract = " + Sub);




ret.setDecimal(Sub);

ret.printRoman();
return ret;
}
public ExtRomans multiply(ExtRomans r1, ExtRomans r2)
{
int val1 = r1.decimalNum;
int val2 = r2.decimalNum;

ExtRomans ret = new ExtRomans();

int answer = (val1 * val2);
ret.setDecimal(answer);
return ret;
}
public ExtRomans divide(ExtRomans r1, ExtRomans r2)
throws romanException
{
int val1 = r1.decimalNum;
int val2 = r2.decimalNum;
int div = 0;
if(val1 < val2)
{
throw new romanException("the divisor cannot be less then the dividend");
}
else
{
div = (val1/val2);
}
ExtRomans ret = new ExtRomans();
ret.setDecimal(div);
return ret;
}
public ExtRomans add(ExtRomans r1, ExtRomans r2)
{
int val1 = r1.decimalNum;
int val2 = r2.decimalNum;

int sum = val1 + val2;
ExtRomans ret = new ExtRomans();
ret.setDecimal(sum);
return ret;
}
}


romanException.java


public class romanException extends Exception
{
public romanException(String message)
{
super(message);
}
}



and last Application.java



import java.util.*;
public class TestRoman
{
static Scanner console = new Scanner(System.in);
static String romanStr1;
static String romanStr2;

public static void divRoman()
{
try
{
System.out.println("Enter the first roman numeral: ");

String roman1 = console.next();

System.out.println("Enter the second roman numeral: ");

String roman2 = console.next();

ExtRomans r1 = new ExtRomans();
ExtRomans r2 = new ExtRomans();
ExtRomans r3 = new ExtRomans();
r1.setRoman(roman1);
r2.setRoman(roman2);
r3 = r3.divide(r1, r2);

System.out.println(r3);
}
catch(romanException e)
{
System.out.println(e.getMessage());
}


}

public static void mulRoman()
{
System.out.println("Enter first roman to multiply: ");

String roman1 = console.next();

System.out.println("Enter the second roman to multiply: ");

String roman2 = console.next();

ExtRomans r1 = new ExtRomans();
ExtRomans r2 = new ExtRomans();
ExtRomans r3 = new ExtRomans();

r1.setRoman(roman1);
r2.setRoman(roman2);
r3 = r3.multiply(r1, r2);

System.out.println(r3);
}
public static void subRoman() throws romanException
{
try
{
System.out.println("Enter first number to subtract: ");

romanStr1 = console.next();

ExtRomans r1 = new ExtRomans();
r1.setRoman(romanStr1);

System.out.println("Enter the second num to subtract");

romanStr2 = console.next();

ExtRomans r2 = new ExtRomans();
r2.setRoman(romanStr2);

ExtRomans r3 = new ExtRomans();

r3 = r3.sub(r1, r2);

System.out.println("The decimal val = " + r3.getDecimal());
System.out.println(r3);

}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static void addRoman()
{
romanStr1 = " ";
romanStr2 = " ";
System.out.println("Type the first roman number: ");
romanStr1 = console.next();

ExtRomans r1 = new ExtRomans();
r1.setRoman(romanStr1);
System.out.println("Type the second roman number: ");
romanStr2 = console.next();
ExtRomans r2 = new ExtRomans();
r2.setRoman(romanStr2);

ExtRomans r3 = new ExtRomans();

r3 = r3.add(r1, r2);
System.out.println(r3);
}
public static void main(String[] args) throws romanException
{
boolean quit = false;

String menu = " ";

menu += ""
+ "1) add two roman numbers \n"
+ "2) divide two roman numbers \n"
+ "3) Multiply two roman numbers \n"
+ "4) Subtract two roman numbers \n"
+ "5) Exit";
double val1;
while(!quit)
{

System.out.println(menu);
int choice = console.nextInt();

switch(choice)
{
case 1:
{
TestRoman.addRoman();
}
break;
case 2:
{
TestRoman.divRoman();
}
break;
case 3:
{
TestRoman.mulRoman();
}
break;
case 4:
{
TestRoman.subRoman();
}
break;
case 5:
{
quit = true;
}
}
}
// exRo.divideRomans(romanString, romanString2);

}
}



i can't figure out why X - II = XIII = 13

Share this post


Link to post
Share on other sites
Advertisement
You're doing a lot of extraneous operations. Start with this:


public void DecimalToRoman()
{
String sum = "";
int temp = 0;

int answer = this.decimalNum / 1000;

for(int i = 0; i < answer; ++i)
{
sum = sum + "M";
}

temp = decimalNum % 1000;

answer = temp / 500;

for(int i = 0; i < answer; ++i)
{
sum = sum + "D";
}

temp %= 500;

answer = temp / 100;

for(int i = 0; i < answer; ++i)
{
sum = sum + "C";
}

temp %= 100;

answer = temp / 50;

for(int i = 0; i < answer; ++i)
{
sum = sum + "L";
}

temp %= 50;

answer = temp / 10;

for(int i = 0; i < answer; ++i)
{
sum = sum + "X";
}

temp %= 10;

answer = temp / 5;

for(int i = 0; i < answer; ++i)
{
sum = sum + "X";
}

answer = temp % 5;

for(int i = 0; i < answer; ++i)
{
sum = sum + "I";
}

romanNum = sum;

}


Past that, check your code for fives. It will never output a V; it was carelessly copy-pasted from the tens code. Edited by Ectara

Share this post


Link to post
Share on other sites
For homework it is generally best to let the person struggle with it, so they learn for themselves. Good answer, though!

Share this post


Link to post
Share on other sites
To clarify, that doesn't mean you cannot help at all. Directly posting working answers is strongly discouraged. Hinting people toward the solution or pointing out bugs in the OP's current attempt is welcome however.

Share this post


Link to post
Share on other sites
Well, to be fair i did do most of the work i just needed to know why i was getting an X in the place of a V that would of been easy had i checked it but was kind of in a time crunch.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement