Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The purpose of this lab is to gain experience in the design and implementation o

ID: 668054 • Letter: T

Question

The purpose of this lab is to gain experience in the design and implementation of a simple Java class. You will create a class definition and write a simple main method that will test the methods of your class to ensure they work as expected.

The required task is to create a class definition for the concept of a Roman numeral. When a Roman numeral object is declared, it should hold a representation of the Roman numeral (e.g., MMXV) and provide the following functionality:

A constructor that initializes the Roman numeral to the value supplied as the parameter to the constructor in Roman numeral format

A constructor that initializes the Roman numeral to the value supplied as the parameter to the constructor in decimal format

A method that returns the value of the Roman numeral

A method that returns the decimal equivalent of the value of the Roman numeral

Note: a Roman numeral object is only allowed to permanently store the Roman numeral representation of the number (i.e., adding an int member for the decimal equivalent is not allowed).

Your test program must create Roman numeral objects and enable the user to sufficiently test the class methods for correctness. I/O can be console based – e.g., you may want to prompt the user for a Roman numeral, create a Roman numeral object and then output the numeral in both Roman numeral and decimal format.

The extended task is to add functionality to the class to add Roman numerals. Note to get any credit for this method this addition must be done using the Roman numeral format of the numbers, not by converting them to decimal values and adding those. Your test code should be extended to allow the user to exercise the add function as many times as desired.

As a reminder, these are the decimal values of the Roman numeral “digits”:

                M    1000

     D    500

     C    100

     L    50

     X    10

     V    5

     I    1

Explanation / Answer

CONVERTING ROMAN TO DECIMAL:

public class RomanToDecimal
{
public static void romanToDecimal(java.lang.String romanNumber)
{
int decimal = 0;
int lastNumber = 0;
String romanNumeral = romanNumber.toUpperCase();
for (int x = romanNumeral.length() - 1; x >= 0 ; x--)
{
char convertToDecimal = romanNumeral.charAt(x);
switch (convertToDecimal)
{
case 'M':
decimal = processDecimal(1000, lastNumber, decimal);
lastNumber = 1000;
break;
case 'D':
decimal = processDecimal(500, lastNumber, decimal);
lastNumber = 500;
break;
case 'C':
decimal = processDecimal(100, lastNumber, decimal);
lastNumber = 100;
break;
case 'L':
decimal = processDecimal(50, lastNumber, decimal);
lastNumber = 50;
break;
case 'X':
decimal = processDecimal(10, lastNumber, decimal);
lastNumber = 10;
break;
case 'V':
decimal = processDecimal(5, lastNumber, decimal);
lastNumber = 5;
break;
case 'I':
decimal = processDecimal(1, lastNumber, decimal);
lastNumber = 1;
break;
}
}
System.out.println(decimal);
}
public static int processDecimal(int decimal, int lastNumber, int lastDecimal)
{
if (lastNumber > decimal)
{
return lastDecimal - decimal;
}
else
{
return lastDecimal + decimal;
}
}
public static void main(java.lang.String args[])
{
romanToDecimal("XIV");
}
}

CONVERTING INTEGERS TO ROMAN NUMERALS:

import java.util.LinkedHashMap;
import java.util.Map;
public static String RomanNumerals(int Int)
{
LinkedHashMap<String, Integer> roman_numerals = new LinkedHashMap<String, Integer>();
roman_numerals.put("M", 1000);
roman_numerals.put("CM", 900);
roman_numerals.put("D", 500);
roman_numerals.put("CD", 400);
roman_numerals.put("C", 100);
roman_numerals.put("XC", 90);
roman_numerals.put("L", 50);
roman_numerals.put("XL", 40);
roman_numerals.put("X", 10);
roman_numerals.put("IX", 9);
roman_numerals.put("V", 5);
roman_numerals.put("IV", 4);
roman_numerals.put("I", 1);
String res = "";
for(Map.Entry<String, Integer> entry : roman_numerals.entrySet())
{
int matches = Int/entry.getValue();
res += repeat(entry.getKey(), matches);
Int = Int % entry.getValue();
}
return res;
}
public static String repeat(String s, int n)
{
if(s == null)
{
return null;
}
final StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++)
{
sb.append(s);
}
return sb.toString();
}
for (int i = 1;i<256;i++)
{
System.out.println("i="+i+" -> "+RomanNumerals(i));
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote