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

<pre>You are to design and implement a Roman numeral calculator Class. The subtr

ID: 3640200 • Letter: #

Question

<pre>You are to design and implement a Roman numeral calculator Class.
The subtractive Roman numeral notation commonly in use today was used
only rarely during the time of the Roman Republic and Empire.
For ease of calculation, the Romans most frequently used a purely additive
notation in which a number was simply the sum of its
digits (4 equals IIII in this notation, not IV).
Each number starts with the digit of highest value and ends with the digit
of smallest value. This is the notation you will use in this program.

Your class inputs two Roman numbers and an arithmetic operator and
prints out the result of the operation, also as a Roman number.
The values of the Roman digits are as follows:
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

Thus, the number MDCCCCLXXXXVI represents 1996, because 1996 really
consists of:

1000 + 500 + 100 + 100 + 100 + 100 + 50 + 10 + 10 + 10 + 10 + 5 + 1.

M + D + C + C + C + C + L + X + X + X + X + V + I

The arithmetic operators that your program should recognize in the
input are +, -, *, and /. These should perform the C++ operations of
integer addition, subtraction, multiplication, and division.

One way of approaching this problem is to convert the Roman numbers
into integers, perform the required operation, and then convert the
result back into a Roman number for printing.

The following might be a sample run of the class (user inputs are in bold italics):

Operator: + - * / q for quit
<h3><em>+</em></h3>
Enter operand1
<h3><em>MCCXXVI</em></h3>
Enter operand2
<h3><em>LXVIIII</em></h3>
Answer =MCCLXXXXV
Good Job
Operator: + - * / q for quit
etc.

The program should check for errors in the input, such as illegal digits
or arithmetic operators, and display an error message when these
errors are found. Assume that the input numbers are in purely additive
form - this is, digits are followed only by digits of the same or lower value.


You might find that it is easier to start out by writing a Decimal calculator.
Then add on the Roman Numeral - Decimal conversion routines to solve this problem


// ****************************************************************
// ******* STARTING CODE **************************************
// ****************************************************************
import java.util.*;

public class RomanCalculator {
// scan can now be used anywhere within this class
Scanner scan = new Scanner(System.in);

// This routine either returns false if the use wants to quit,
// or it does one Roman Calculator calculation
boolean doCalculation()
{
char operand ;

// Call getOperator to get either - + * / or q.
// If q is returned, we return a false.

// ************** FILL IN CODE

// If the operator is + - * or / then call
// getOperand(1) for the first operand and
// call getOperand(2) for the second operand

// ************** FILL IN CODE

// call doArithmetic and print out the result using
// convert_to_Roman to generate Roman Numeral output.

// ************** FILL IN CODE
return true;
}

// This routine prompts the user with
// Operator: + - * / q for quit
// If none of these are entered, this routine complains and
// prompts the user again. Otherwise the operator is returned.
char getOperator()
{

// ************** FILL IN CODE
}

// This routine prompts the user for either operand1 or operand2
// depending on the value of which. This routine uppercases the
// input and calls convert_from_Roman to create an integer.
// If the input is invalid ( negative return from convert_from_Roman)
// then complain and prompt the user again.
int getOperand(int which)
{

// ************** FILL IN CODE
}


// Routine to convert an integer to a Roman Numeral String.
// When you do this routine, you might find it handy to
// create a utility routine that looks like:
// String addRomanDigit(String starting, int num, char digit)
String convert_to_Roman(int value)
{

// ************** FILL IN CODE
}


// Convert Roman Numeral String to an integer. If the
// Roman Numeral String is invalid, return -1.
int convert_from_Roman(String value)
{

// ************** FILL IN CODE
}

// Perform the arithmetic indicated by the operator (+ - * /)
// and return answer
int doArithmetic(int operand1, int operand2, char operator)
{

// ************** FILL IN CODE
}

public static void main(String[] args) {
RomanCalculator rc = new RomanCalculator();
while (rc.doCalculation())
{
System.out.println("Good Job");
}
System.out.println("Finished Roman Computations");
}

}</pre>

Explanation / Answer

The comment lines explain what the following functions do...:)..

import java.util.*;
public class RomanCalculator
{
    Scanner scan = new Scanner(System.in);

    // This routine either returns false if the use wants to quit,
    // or it does one Roman Calculator calculation
    boolean doCalculation()
    {
        char operand ;
        // Call getOperator to get either - + * / or q.
        // If q is returned, we return a false.
        // ************** FILL IN CODE
        operand=getOperator();
        if(operand=='q')return false ;
        // If the operator is + - * or / then call
        // getOperand(1) for the first operand and
        // call getOperand(2) for the second operand
        // ************** FILL IN CODE
        int a=0,b=0,c=0;
        if(operand=='+'||operand=='-'||operand=='*'||operand=='/')
        {
            a=getOperand(1);
            b=getOperand(2);
        }
        // call doArithmetic and print out the result using
        // convert_to_Roman to generate Roman Numeral output.
        // ************** FILL IN CODE
        c=doArithmetic(a,b,operand);
        String d=convert_to_Roman(c);
        System.out.println("Answer= "+d);
        return true;
    }

    // This routine prompts the user with
    // Operator: + - * / q for quit
    // If none of these are entered, this routine complains and
    // prompts the user again. Otherwise the operator is returned.
    char getOperator()
    {
    // ************** FILL IN CODE
        String s;
        char a;
        System.out.println("Operator: + - * / q for quit");
        s = scan.nextLine();
        a=s.charAt(0);
        return a;
    }

    // This routine prompts the user for either operand1 or operand2
    // depending on the value of which. This routine uppercases the
    // input and calls convert_from_Roman to create an integer.
    // If the input is invalid ( negative return from convert_from_Roman)
    // then complain and prompt the user again.
    int getOperand(int which)
    {
    // ************** FILL IN CODE
        System.out.println("Enter operand "+which);
        String s=scan.nextLine();
        s=s.toUpperCase();
        int a=convert_from_Roman(s);
        if(a==-1)
        {
            System.out.println("Invalid operand!!");
            getOperand(which);
        }
        return a;
    }

    // Routine to convert an integer to a Roman Numeral String.
    // When you do this routine, you might find it handy to
    // create a utility routine that looks like:
    // String addRomanDigit(String starting, int num, char digit)
    String convert_to_Roman(int value)
    {
    // ************** FILL IN CODE
        int num=value;
        int c=0;
        String s="";
        if (num==0)
            return "zero";
        else if (num < 0)
        {
            s=s+"-";
            num *= -1;
        }
        while (num >= 1000)
        {
            s=s+"M";
            num -= 1000;
        }
        while (num >= 500)
        {
            s=s+"D";
            num -= 500;
        }
        while (num >= 100)
        {
            s=s+"C";
            num -= 100;
        }
        while (num >= 50)
        {
            s=s+"L";
            num -= 50;
        }
        while (num >= 10)
        {
            s=s+"X";
            num -= 10;
        }
        while (num >= 5)
        {
            s=s+"V";
            num -= 5;
        }
        while (num==1)
        {
            s=s+"I";
            num--;
        }
        return s;
    }

    // Convert Roman Numeral String to an integer. If the
    // Roman Numeral String is invalid, return -1.
    int convert_from_Roman(String value)
    {
     // ************** FILL IN CODE
        int i,a=0,b;
        for(i=0;i<value.length();i++)
        {
            b=toDecimal(value.charAt(i));
            if(b==0)return -1;
            a=a+b;
        }
        return a;
    }
    int toDecimal(char symbol)
    {
        if (symbol == 'I')
            return 1;
        else if (symbol == 'V')
            return 5;
        else if (symbol == 'X')
            return 10;
        else if (symbol == 'L')
            return 50;
        else if (symbol == 'C')
            return 100;
        else if (symbol == 'D')
            return 500;
        else if (symbol == 'M')
            return 1000;
        return 0;
    }

    // Perform the arithmetic indicated by the operator (+ - * /)
    // and return answer
    int doArithmetic(int operand1, int operand2, char operator)
    {
    // ************** FILL IN CODE
        int result;
        if (operator == '+')
            result = operand1 + operand2;
        else if (operator == '-')
            result = operand1 - operand2;
        else if (operator == '*')
            result = operand1 * operand2;
        else if (operator == '/')
            result = operand1 / operand2;
        else
            result = 0;
        return result;
    }

    public static void main(String[] args) {
        RomanCalculator rc = new RomanCalculator();
        while (rc.doCalculation())
        {
            System.out.println("Good Job");
        }
        System.out.println("Finished Roman Computations");
    }

}

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