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

I have been working on this for the better part of a week and have posted questi

ID: 3675873 • Letter: I

Question

I have been working on this for the better part of a week and have posted questions related to this problem and just cannot get this program to work. It is a roman numeral calculator. For the problem we were to create a roman numeral calculator that took roman numeral letters as input, converted them to numbers, performed the desired mathematical computation, and then converted the answer back to roman numerals and displayed the answer using roman numerals. Note that it uses the additive system of roman numerals. Meaning that the number 4 would be displayed IIII, not IV (IV would add up to 6). If someone could get this program running for me I'd be forever grateful. I will post the code I have so far directly below this paragraph. Along with the instructions for what the program is supposed to do for my assignment below that. Sorry about the wall of text. Thank you.

Copy and Paste of the Code:

package roman_calculator;

import java.util.Scanner;

public class RomanCalculator {

  

   Scanner scan = new Scanner(System.in);

  

   boolean doCalculation()

   {

       char operand, operator = getOperator();

       if (operator == 'q')

           return false;

       int operand1 = getOperand(1),operand2 = getOperand(2);

       int answer = doArithmetic(operand1, operand2, operator);

       String sAnswer = convert_to_Roman(answer);

       System.out.println("Answer = " + answer);

       return true;

   }

   char getOperator()

   {

       System.out.println("Enter operation: + - * / q (q ==> quit): ");

       String input = scan.next();

       System.out.println("input="+input);

       char c = input.charAt(0);

       return c;

   }

   int getOperand(int which)

   {

       System.out.println("Enter operand " + which + ":");

       String op = scan.next();

       return convert_from_Roman(op);

   }

   String convert_to_Roman(int value)

   {

       {

           String roman = " ";

           while (value > 0 || value < 4000)

               switch (value)

               {

               case 1000:

                   roman += 'M';

                   break;

               case 500:

                   roman += 'D';

                   break;

               case 100:

                   roman += 'C';

                   break;

               case 50:

                   roman += 'L';

                   break;

               case 10:

                   roman += 'X';

                   break;

               case 5:

                   roman += 'V';

                   break;

               case 1:

                   roman += 'I';

                   break;

               }

           return roman;

       }

   }

   int convert_from_Roman(String value)

   {

       String s = value.toUpperCase();

       s = s.trim();

       int len = s.length();

       int total = 0;

      

       for (int i = len-1; i >= 0; i--)

       {

           char c = value.charAt(i);

           c=Character.toUpperCase(c);

          

           switch (c)

           {

           case 'M':

               total += 1000;

               break;

           case 'D':

               total += 500;

               break;

           case 'C':

               total += 100;

               break;

           case 'L':

               total += 50;

               break;

           case 'X':

               total += 10;

               break;

           case 'V':

               total += 5;

               break;

           case 'I':

               total += 1;

               break;

           }

       }

       return total;

   }

   int doArithmetic(int operand1, int operand2, char operator)

   {

       switch (operator)

       {

       case '+':

           return operand1 + operand2;

       case '-':

           return operand1 - operand2;

       case '*':

           return operand1 * operand2;

       case '/':

           return operand1 / operand2;

       }

       return operator;

   }

   public static void main(String[] args)

   {

       RomanCalculator rc = new RomanCalculator();

       while (rc.doCalculation())

       {

           System.out.println();

       }

       System.out.println("Finished Roman Computations");

   }

}

----------------------------------------------Below this is the original instructions for the question--------------------------------------------------------------------

Roman Calculator Problem

Create a package named roman_calculator for this program.

Your Roman Calculator should be tested with the following inputs (note your program will fill in the answers):

Explanation / Answer

package nyc.c4q.ac21.roman_calculator;

/**

* Code to convert to and from Roman numerals.

*/

public class RomanNumerals {

    /**

* Formats a number in Roman numerals.

* @param value

* The value to format. Must be between 1 and 3999.

* @return

* The value in Roman numerals.

*/

    public static String format(int value) {

        if (value < 1 || value >= 4000) {

            System.err.println("value out of range");

            return "error";

        }

        String result = "";

        while (value >= 1000) {

            result += "M";

            value -= 1000;

        }

        if (value >= 900) {

            result += "CM";

            value -= 900;

        }

        if (value >= 500) {

            result += "D";

            value -= 500;

        }

        if (value >= 400) {

            result += "CD";

            value -= 400;

        }

        while (value >= 100) {

            result += "C";

            value -= 100;

        }

        if (value >= 90) {

            result += "XC";

            value -= 90;

        }

        if (value >= 50) {

            result += "L";

            value -= 50;

        }

        if (value >= 40) {

            result += "XL";

            value -= 40;

        }

        while (value >= 10) {

            result += "X";

            value -= 10;

        }

        if (value >= 9) {

            result += "IX";

            value -= 9;

        }

        if (value >= 5) {

            result += "V";

            value -= 5;

        }

        if (value >= 4) {

            result += "IV";

            value -= 4;

        }

        while (value > 0) {

            result += "I";

            value -= 1;

        }

        return result;

    }

    /**

* Parses a number in Roman numerals.

* @param number

* The number to parse.

* @return

* The value, or -1 if the number isn't in Roman numerals.

*/

    public static int parse(String number) {

        int value = 0;

        int length = number.length();

        // Loop through characters in the string.

        for (int i = 0; i < length; ) {

            // Look at the next character.

            char c = number.charAt(i);

            // Also look at the following character, but be careful that we

            // might be at the end of the string.

            char n;

            if (i < length - 1)

                n = number.charAt(i + 1);

            else

                // Use a character that won't match anything below.

                n = '?';

            // Accumulate value based on the next character or two characters.

            // If we use one character, advance i by one; if we use two

            // characters, advance i by two.

            if (c == 'M') {

                value += 1000;

                i += 1;

            }

            else if (c == 'C' && n == 'M') {

                value += 900;

                i += 2;

            }

            else if (c == 'D') {

                value += 500;

                i += 1;

            }

            else if (c == 'C' && n == 'D') {

                value += 400;

                i += 2;

            }

            else if (c == 'C') {

                value += 100;

                i += 1;

            }

            else if (c == 'X' && n == 'C') {

                value += 90;

                i += 2;

            }

            else if (c == 'L') {

                value += 50;

                i += 1;

            }

            else if (c == 'X' && n == 'L') {

                value += 40;

                i += 2;

            }

            else if (c == 'X') {

                value += 10;

                i += 1;

            }

            else if (c == 'I' && n == 'X') {

                value += 9;

                i += 2;

            }

            else if (c == 'V') {

                value += 5;

                i += 1;

            }

            else if (c == 'I' && n == 'V') {

                value += 4;

                i += 2;

            }

            else if (c == 'I') {

                value += 1;

                i += 1;

            }

            else {

                System.err.println("not Roman numeral:" + number);

                return -1;

            }

        }

        return value;

    }

    public static void main(String[] args) {

        // For each value in the range we support, convert to Roman numerals and back.

        for (int i = 1; i < 4000; ++i) {

            final String rn = format(i);

            final int j = parse(rn);

            System.out.format("%4d = %-15s = %4d ", i, rn, j);

            // The value should be the same!

            if (i != j)

                System.err.println("ERROR!");

        }

    }

}

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