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

For a problem in my Java course we must make a program that calculates roman num

ID: 3675623 • Letter: F

Question

For a problem in my Java course we must make a program that calculates roman numerals. The program asks the user what operation to perform and for two operands. Most of the code is already done, but I am having a problem getting the char getOperator() and boolean doCalculation() methods to work. When I run the program it asks for the operator and will continue regardless of whether the input is valid or not (if I enter q to quit it does not quit). Furthermore, if I enter roman numerals for calculation it always returns the answer 0. I'm a beginner and a little stumped as to how to get it working. Any help in fixing the code would be greatly appreciated. I have copy and pasted the code below. The two methods I'm getting errors with are toward the top of it.

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();

       return 0;

   }

   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);

           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");

   }

}

Explanation / Answer

Answers:)

1. in the below lines:

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

you are returning string but you are returning it to char so fix it as below:

System.out.println("Enter operation: + - * / q (q ==> quit): ");
String input = scan.next();
System.out.println("input="+input);
char c = input.charAt(0);
return c;

2. char c = value.charAt(i); in convert_from_Roman() is not giving the uppercase so do it as below:

  char c = value.charAt(i);
c=Character.toUpperCase(c);

3. check your logic for convert_from_Roman because it is giving 6 even for IV

4. convert_to_Roman() is in infinite loop as while (value > 0 && value < 4000) is nver false

check your logic for this

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