2 Replies - 2197 Views - Last Post: 02 August 2014 - 09:48 AM Rate Topic: -----

#1 kishanp09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-July 14

Roman Numeral Program Help

Posted 29 July 2014 - 01:53 PM

Hi,
I have made a program that is supposed to convert roman numerals to decimals. It is NOT supposed to convert decimals to roman numerals. The only problem it seems there is with the program is that it only adds and does NOT subtract. This causes some roman numeral conversions to be flawed. What can I try to fix this. Keep in mind that the reason some methods exist is because the directions for the code say that I have to make it this way.


Instructions:
Write a program that converts a number entered in Roman numerals to decimal.
Your program should consist of a class called Roman. An object of type Roman
should be able to do the following:
1) Store the number as a Roman numeral.
2) Convert and store the number into decimal.
3) Print the number as a Roman numeral or decimal number as requested by
the user. (Be sure to over-ride the toString function).
4) Your class must contain the method romanToDecimal to convert a Roman
numeral into its equivalent decimal number.
5) Test your program using the following Roman numerals: MCXIV, CCCLIX,
and MDCLXVI.



This is what I have so far. It compiles and there are no errors, but it does not subtract I from X, or I from V and so on. For example,CMXLVII should be 947, but the program outputs 1067.

import javax.swing.*;
import java.util.*;

public class Roman{
    private String roman;
    private int decimal;
    public int romanToDecimal(String s)
    {
        int sum =0;
                    
        for(int i = s.length() - 1; i>=1; i--)
        {
           
            
           if(i!=0){
                if(getValue(s.charAt(i-1))>=getValue(s.charAt(i))){
              
                    if(s.charAt(i)=='I'){
                        sum+= 1;
                    }
                    else if( s.charAt(i)=='V'){
                        sum+= 5;
                    }
                    else if (s.charAt(i) == 'X'){
                        sum+= 10;
                    }
                    else if (s.charAt(i) == 'L'){
                        sum+= 50;
                    }
                    else if (s.charAt(i) == 'C'){
                        sum+= 100;
                    }
                    else if (s.charAt(i) == 'D'){
                        sum+= 500;
                    }
                    else if (s.charAt(i) == 'M'){
                        sum+= 1000;
                    }
                }
              else if(getValue(s.charAt(i-1))<=getValue(s.charAt(i))){
                  if(s.charAt(i)=='I'){
                        sum+= 1;
                    }
                    else if( s.charAt(i)=='V'){
                        sum+= 5;
                    }
                    else if (s.charAt(i) == 'X'){
                        sum+= 10;
                    }
                    else if (s.charAt(i) == 'L'){
                        sum+= 50;
                    }
                    else if (s.charAt(i) == 'C'){
                        sum+= 100;
                    }
                    else if (s.charAt(i) == 'D'){
                        sum+= 500;
                    }
                    else if (s.charAt(i) == 'M'){
                        sum+= 1000;
                    }
                }
                }
              else{
                  if(s.charAt(i)=='I'){
                        sum-= 1;
                    }
                    else if( s.charAt(i)=='V'){
                        sum-= 5;
                    }
                    else if (s.charAt(i) == 'X'){
                        sum-= 10;
                    }
                    else if (s.charAt(i) == 'L'){
                        sum-= 50;
                    }
                    else if (s.charAt(i) == 'C'){
                        sum-= 100;
                    }
                    else if (s.charAt(i) == 'D'){
                        sum-= 500;
                    }
                    else if (s.charAt(i) == 'M'){
                        sum-= 1000;
                    }
            }
            
        }
        return sum;
    }
    
    
                
    int getValue(char c){
        switch(c)
        {
            case 'I':
            
            return 1;
            
            case 'V':
            return 5;
            
            case 'X':
            return 10;
            
            case 'L':
            return 50;
            
            case 'C':
            return 100;
            
            case 'D':
            return 500;
            
            case 'M':
            return 1000;
            
            default:
            return 0;
        }
        
    }
        
    
    public Roman(){
        roman = " ";
        decimal = 0;
    }
    
     
    public Roman(String r,  int d){
        roman = r;
        decimal = d;
    }
    
    public String getRoman(){
        return roman;
    }
    
    public void setRoman(String s){
        roman = s;
    }
    
    public void setDecimal(int d){
        decimal = d;
    }
   
    //EXTRA CREDIT CAN GO HERE I THINK
    
    public static void main(String [] args){
        Roman myRoman = new Roman();
        Roman r1 = new Roman("MCM", 1900);
        myRoman.setRoman("XLIV");
        myRoman.setDecimal(44);
        r1.setRoman("CMXLVII");
        int newDecimal = r1.romanToDecimal(r1.getRoman());
        r1.setDecimal(newDecimal);
        System.out.println(r1);
        
    }
   
    public String toString()
   {
    return "Roman Numeral: " + roman
    +"\n Decimal: " + decimal;
   }
 }

This post has been edited by CasiOo: 29 July 2014 - 01:54 PM
Reason for edit:: Fixed code tags [code]Your code[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: Roman Numeral Program Help

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: Roman Numeral Program Help

Posted 30 July 2014 - 02:02 AM

It's easier to think of IV as 4 rather than (5-1). There are only a small number of combinations like this in modern usage.

http://en.wikipedia...._Roman_numerals
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3487
  • View blog
  • Posts: 15,766
  • Joined: 20-September 08

Re: Roman Numeral Program Help

Posted 02 August 2014 - 09:48 AM

I'd tend to use a stack-based approach for this then you can inspect whether to add or subtract
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1