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

: Laura Croft is getting ready to make a trip to Egypt to check out a new tomb.

ID: 3568279 • Letter: #

Question

: Laura Croft is getting ready to make a trip to Egypt to check out a new tomb. She has been
notified that the each door into the tomb is locked by an ancient combination lock. To enter the tomb, a
person must solve a simple mathematical calculation. Unfortunately all of the numbers are written in
Roman numerals. As part of her team, your job is to write a program that will quickly perform the
calculation and present the result as a Roman numeral.
As with previous programs, this program must use modular programming. Remember that functions
should be designed to do a single task.
The program will read the calculations from a file, perform the operation on the Roman numerals and
send the answer to a file.
Input: All input will come from a file named input.txt. The file will contain simple mathematical
equations, each one on a separate line. The format for each line will be:
<Roman numeral><space><operator><space><Roman numeral>
Each Roman numeral will be valid and will represent a number from 1 100. The operators available
are + and -. There will be no multiplication or division in the file, only addition and subtraction. There is
no limit to the number of calculations in the file.
Output: All output will be written to a file named output.txt. Once the calculation is performed, the
answer will be written to the file in Roman numeral form. Each answer will be written on a separate
line.
Roman Numerals: This project will involve the following Roman numerals:
? C = 100
? L = 50
? X = 10
? V = 5
? I = 1

The letters that comprise Roman numerals will generally be in the order listed above. For example
LXXVI represents 50 + 10 + 10 + 5 + 1 or 76. Occasionally the letters may appear in an improper order.
This happens or values of 4 and 9. For example, XL = 40 (-10 + 50). When the letter is not in the right
order, it signifies the number out of order is a negative number rather than a positive number.
Reading From the File: It will probably be easier for you to complete this assignment by reading from
the file a character at a time and determining what to do with the character you read. And since the
positivity of the letter in the Roman numeral is dependent upon the next letter in the Roman numeral,
you will want to write the program to read in the next character before deciding if the value is positive
or negative.

Explanation / Answer

Program code:

import java.util.*;

import java.io.*;

//Class ComputeRomans will get the roman values from a file

//computes the calculations and

//writes the output into a file

public class ComputeRomans

{

     Scanner scan;

     //this method will gets the values from the file input.txt

//and returns output to the output.txt file

     boolean doCalculation() throws Exception

     {

          scan= new Scanner(new File("input.txt"));

        

          String a;

          String b;

          String c,convertResult;

          char operator;

          int x, y;   

          int result[]=new int[10];

          int i=0;

          while(scan.hasNextLine())

          {           

              a=scan.next();

              x=getOperand(a);

              b=scan.next();

              operator=getOperator(b);

              c=scan.next();

              y=getOperand(c);           

              result[i]=doArithmetic(x,y,operator);         

              i++;

          }

          scan.close();

          PrintStream print=new PrintStream(new File("output.txt"));

          for(int j=0;j<i;j++)

          {

              System.out.println(result[j]);

              convertResult=toRoman(result[j]);

              System.out.println(convertResult);

              print.println(convertResult);

          }

          print.close();

          return true;

     }

   

     //to convert the string to character to get the

     //character

     char getOperator(String op)

     {

          char a;      

          a=op.charAt(0);

          return a;

     }

     //this will convert the string into integer

     //and returns the integer value

     int getOperand(String s)

     {

          String str=s;     

          int a=convert_from_Roman(str);

          if(a==-1)

          {

              System.out.println("Invalid operand!!");          

          }

          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)

     {

          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)

     {

          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;

          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

              result = 0;

          return result;

     }

     //   Returns a string containing the numeral in Roman numeral form.

     public String toRoman(int num)

     {

          // to store the resultant string.

          String Roman = "";

          //   Declare and Initiate our Arrays

          String onesArray[] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

          String tensArray[] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};

          String hundredsArray[] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};

          //   Get the ones in the number

          int % 10;

          //   Get the tens

          num = (num - ones) / 10;

          int tens = num % 10;

          //   Get the hundreds

          num = (num - tens) / 10;

          int hundreds = num % 10;

          //Get and write the thousands in the number to our string

          num = (num - hundreds) / 10;

          for (int i = 0; i < num; i++) {

              Roman += "M";

          }

          //Write the hundreds

          if (hundreds >= 1) {

              Roman += hundredsArray[hundreds - 1];

          }

          //Write the tens

          if (tens >= 1) {

              Roman += tensArray[tens - 1];

          }

          //And finally, write the ones

          if (ones >= 1) {

              Roman += onesArray[ones - 1];

          }

          //   Return the resultant string.

          return String.valueOf(Roman);

     }

     //main function

     public static void main(String[] args)throws Exception

     {

          ComputeRomans rc = new ComputeRomans();

          if(rc.doCalculation())

          {

              System.out.println("Good Job");

          }

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

     }

}

--------------------------------------------------------------------------------------------------------------------------

Sample Input:

input.txt file:

XXXV + XCIII

LXXXIX - LXXVII

XXXI + XXXIII

C + C

LXVI - XVI

LXXIV + LII

-----------------------------------------------------------------------------------------------------------

Sample Output:

output.txt file:

CXLVIII

XIV

LXIV

CC

L

CXXVIII

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