java conversion

Page 1 of 1

2 Replies - 478 Views - Last Post: 21 November 2009 - 02:51 PM Rate Topic: -----

#1 relGodLove  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-November 09

java conversion

Post icon  Posted 19 November 2009 - 09:58 PM

Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class, say, Roman, An object of type Roman should do the following:

a. store the number as a Roman numeral
b. convert and store the number into decimal
c. print the number as Roman numeral or decimal number as requested by the user
d. The decimal values of the Roman numerals are:

M -1000
D 500
C 100
L 50
X 10
V 5
I - 1
e.Test your program using the following Roman numerals: MCXIV, CCCLIX, and MDCLXVI.


is there any one out there that can help me resolve this problem...

Is This A Good Question/Topic? 0
  • +

Replies To: java conversion

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java conversion

Posted 19 November 2009 - 10:28 PM

If you want code: Google
or search the forum, the question has been asked many times

If you have problems with the code YOU wrote we will be glad to help

[rules][/rules]

This post has been edited by pbl: 19 November 2009 - 10:29 PM

Was This Post Helpful? 0
  • +
  • -

#3 relGodLove  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-November 09

Re: java conversion

Posted 21 November 2009 - 02:51 PM

View Postpbl, on 19 Nov, 2009 - 09:28 PM, said:

If you want code: Google
or search the forum, the question has been asked many times

If you have problems with the code YOU wrote we will be glad to help

[rules][/rules]


ok here is where I am having trouble.

public class Roman
{

String romanNum;
int decimalnum;

public Roman();
{

romanNum="I";
decimalNum= 1;

}

public Roman(String rString)
{

romanNum = rString;
romanTodecimal();

public void printDecimal();
{
system.out.println ("Decimal Number stored is: " + decimalNum);

}

public void printRoman();
{
System.out.println ("Roman Numeral stored is: " + romanNumeral);

}

public void setRoman(String rString)
{
romanNum = rString;
romanToDecimal();
}

public void romanToDecimal()
{
int sum = 0;
int len = romanNum.length();
int i;

int previous = 1000;

for (i = 0; i < len; i++)
{

switch(romanNum.charArt(i))
{
case 'M':sum + 1000;
if (privious < 1000)
sum = sum - 2 * previous;
previous = 1000;
break;

case 'D':sum + 500;
if (privious < 500)
sum = sum - 2 * previous;
previous = 500;
break;

case 'C':sum + 100;
if (privious < 100)
sum = sum - 2 * previous;
previous = 100;
break;

case 'L':sum + 50;
if (privious < 50)
sum = sum - 2 * previous;
previous = 50;
break;

case 'X':sum + 10;
if (privious < 10)
sum = sum - 2 * previous;
previous = 10;
break;

case 'V':sum + 5;
if (privious < 5)
sum = sum - 2 * previous;
previous = 5;
break;

case 'I':sum + 1;
if (privious < 1)
sum = sum - 2 * previous;
previous = 1;
break;
}

}

decimalNum = sum;


}


}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1