Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Roman Calculator, this routine either returns false if the user wants to quit or it does on Roman calculation.
- // Renee Waggoner
- // July 8, 2019
- // Special Requirements: None
- package roman_calculator;
- import java.util.*;
- public class RomanCalculator {
- Scanner scan = new Scanner(System.in);
- boolean doCalculation() {
- char operator;
- // prompt user for operator till user enters one of +, *, -, / or q
- while (true) {
- System.out.print("Operator: + - * / q for quit: ");
- operator = getOperator();
- if (operator == 'q')
- {
- return false;
- }
- // if operator is either + - / * then break
- // Note: I tried to not use break in this code but it kept coming up, so I relented.
- if (operator == '+' || operator == '*' || operator == '/' || operator == '-')
- break;
- // else prompt user to reenter operator
- System.out.println("Your operand is bad ... try again:");
- }
- int operand1, operand2;
- operand1 = getOperand(1);
- operand2 = getOperand(2);
- int answer = doArithmetic(operand1, operand2, operator, operator);
- System.out.println("Answer = " + convert_to_Roman(answer));
- 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() {
- String operator1;
- operator1 = scan.next();
- return operator1.charAt(0);
- }
- // 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) {
- int upperCase = 0;
- String operand;
- // prompt user for operand which till user enters all in romans
- while (true) {
- System.out.print("Enter operand" + which + ": ");
- operand = scan.next();
- operand = operand.toUpperCase();
- // this variable is the index of string where it has bad character
- int bad = -1;
- // check every character of operand if it is roman
- for (int i = 0; i < operand.length(); i++) {
- // if current character is not roman then set bad to i and break
- // Note: broke it up here because it was giving me a headache, but booleans are my favorite
- if (operand.charAt(i) != 'M' && operand.charAt(i) != 'D' && operand.charAt(i) !=
- 'C' && operand.charAt(i) != 'L' && operand.charAt(i) != 'X' && operand.charAt(i) != 'V'&& operand.charAt(i) != 'I')
- {
- bad = i;
- break;
- }
- }
- // if bad character is unchanged that means bad character is not encountered
- if (bad == -1)
- break;
- System.out.println(operand + " has a bad character at position: " + bad);
- }
- upperCase = convert_from_Roman(operand);
- return upperCase;
- }
- // 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) {
- String num = "";
- while (value > 0)
- {
- if (value >= 1000)
- {
- num = num + "M";
- value = value - 1000;
- } else if (value >= 500) {
- num = num + "D";
- value = value - 500;
- } else if (value >= 100) {
- num = num + "C";
- value = value - 100;
- } else if (value >= 50) {
- num = num + "L";
- value = value - 50;
- } else if (value >= 10) {
- num = num + "X";
- value = value - 10;
- } else if (value >= 5) {
- num = num + "V";
- value = value - 5;
- } else if (value >= 1) {
- num = num + "I";
- value = value - 1;
- }
- }
- num = num + "";
- return num;
- }
- // Convert Roman Numeral String to an integer. If the
- // Roman Numeral String is invalid, return -1.
- int convert_from_Roman(String value) {
- int total = 0;
- for (int i = 0; i < value.length(); i++) {
- if (value.charAt(i) == 'M') {
- total += 1000;
- } else if (value.charAt(i) == 'D') {
- total += 500;
- } else if (value.charAt(i) == 'C') {
- total += 100;
- } else if (value.charAt(i) == 'L') {
- total += 50;
- } else if (value.charAt(i) == 'X') {
- total += 10;
- } else if (value.charAt(i) == 'V') {
- total += 5;
- } else if (value.charAt(i) == 'I') {
- total += 1;
- } else {
- total = -1;
- }
- }
- return total;
- }
- int invalid_operand_position(String value, int result, int num1, int num2) {
- {
- for (int i = 0; i < value.length(); i++) {
- if (num1 < 0) {
- System.out.println("Invalid operand at position: " + i);
- return Integer.MIN_VALUE;
- }
- if (num2 < 0) {
- System.out.println("Invalid operand at position: " + i);
- return Integer.MIN_VALUE;
- }
- if (num1 >= num2) {
- result = result + num1;
- } else {
- result = result + num2 - num1;
- i++;
- }
- }
- return result;
- }
- }
- // Perform the arithmetic indicated by the operator (+ - * /)
- // and return answer
- int doArithmetic(int operand1, int operand2, int choiceFromUser, char operator) {
- if (operator == '+') {
- choiceFromUser = operand1 + operand2;
- } else if (operator == '-') {
- choiceFromUser = operand1 - operand2;
- } else if (operator == '*') {
- choiceFromUser = operand1 * operand2;
- } else if (operator == '/') {
- choiceFromUser = operand1 / operand2;
- }
- return choiceFromUser;
- }
- public static void main(String[] args) {
- RomanCalculator rc = new RomanCalculator();
- while (rc.doCalculation()) {
- System.out.println();
- }
- System.out.println("Finished Roman Computations");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment