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

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

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!