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

The purpose of this program is to learn to use stacks. You will be given 2 Roman

ID: 3759948 • Letter: T

Question

The purpose of this program is to learn to use stacks. You will be given 2 Roman Numbers with an operator separating them The operator will be either a minus plus or multiply sign You n eed to he 2 Roman Numbers to Arabic Numbers (the kind we use perform the operation and then Convert t display t he result a s a Roman Number If either Roman numeral is invalid print to n error message the user Roman Numbers have these rul 1) The values of Roman Numerals are as follows: 50 00 500 000 2) The value of the Roman Number will be the total of the individual numerals according to these rul e3 If the current numera ess than or equal to the numeral then the value of the previous numeral is added to the total If he current numera is greater than the pre numeral then the previous numeral is subtracted from the current numera l and that result is added to the total When you write your Roman Number as output, keep these rules in mind: 1) Only Roman Numerals that are powers of 10 may be repeated Example: II is valid, XX is valid, not valid DD is not valid 2) No more than 3 of the same Roman Numerals may be repeated in sequence Example II is valid s not valid 3) A smaller Roman Numeral may appear immediately to the left of a larger Roman Numeral only if the value of the smaller is less than or equal to 10 times the value of the larger. Example: IV is valid, IX is valid IL is not valid, CD is valid ND is not valid 4) Only Roman Numbers that are powers of 10 may appear immediately in front of a larger Numeral (for subtraction purposes) Example IX is valid, IV is valid, VK is not valid LC is not valid XC is valid. When you read the Roman Number as input, you will need to push each item on a stack as you read it since you don't know what to do with it until you read the next item. You should apply the above rules to create an algorithm to convert a Roman Number to an Arabic number After you have converted the 2 Roman Numbers to Arabic numbers and performed the operation. you will need to convert the Arabic number into a Roman Number You should convert each Arabic numera he to t appropriate Roman Number e quivalent and print the resulting Roman Number Here are some examples for some numbers II to represent 1 MCMLIII for 1953 and MMMCC CII I for 3303 In this assignment, we will input Roman Numbers in such a way that the result of the expression is 3999 which i MMMCMXCIX, or less For testing cases see the following You can u In Roman Numbers In Arabic numbe MMMDC 3600 CCX CX CX 220 111 109 DC CCXVIII MMDCCCXVII There is a convert on the web at http://www.calculateme.com/cRomanNumerals/ArabicNumeralsToRoman.htm. Please create more test cases User-friend lines s is one of he software quality please make sure your program friendly 13 ul3e You are required to code your own stack and queue If you use Java Generics, there will be a deduction of 10% each.

Explanation / Answer

Answer :

import java.util.*;
public class RomanNumber {
static int value(char a) {
if (a=='m') return 1000;
else if (a=='d') return 500;
else if (a=='c') return 100;
else if (a=='l') return 50;
else if (a=='x') return 10;
else if (a=='v') return 5;
else if (a=='i') return 1;
else return 0;
}

static int value2(char a) {
if (a=='m') return 1000;
else if (a=='d') return 500;
else if (a=='c') return 100;
else if (a=='l') return 50;
else if (a=='x') return 10;
else if (a=='v') return 5;
else if (a=='i') return 1;
else return 0;
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter First Roman Number: ");
String roman=in.nextLine();
System.out.print("Enter Second Roman Number: ");
String roman2=in.nextLine();
roman=roman.toLowerCase();
roman2=roman2.toLowerCase();
int val=0;
int val_next=0;
int temp=0;
int result=0;

int val2=0;
int val_next2=0;
int temp2=0;
int result2=0;

for (int i=0;i<roman.length();i++) {
val=value(roman.charAt(i));
if (i<roman.length()-1) {
val_next=value(roman.charAt(i+1));
} else val_next=0;

if (val_next==0) {
temp=val;
} else {
if (val>val_next) temp=val;
else if (val<val_next) {
temp=val_next-val;
i++;
} else if (val==val_next) temp=val;
}
result+=temp;
}
  
   for (int i=0;i<roman2.length();i++) {
val2=value2(roman2.charAt(i));
if (i<roman2.length()-1) {
val_next2=value2(roman2.charAt(i+1));
} else val_next2=0;

if (val_next2==0) {
temp2=val2;
} else {
if (val2>val_next2) temp2=val2;
else if (val2<val_next2) {
temp2=val_next2-val2;
i++;
} else if (val2==val_next2) temp2=val2;
}
result2+=temp2;
}
   System.out.println("Result is: " + result);
System.out.println("Result is: " + result2);
   while(true)
{
   System.out.println("Choose the operation you want to perform ");
System.out.println("Choose 1 for ADDITION");
System.out.println("Choose 2 for SUBTRACTION");
System.out.println("Choose 3 for MULTIPLICATION");
System.out.println("Choose 4 for DIVISION");
System.out.println("Choose 5 for MODULUS");
System.out.println("Choose 6 for EXIT");
   int n = in.nextInt();
   switch(n)
{
case 1:
int add;
add = result+ result2;
System.out.println("Result : "+add);
break;

case 2:
int sub;
sub = result - result2;
System.out.println("Result : "+sub);
break;

case 3:
int mul;
mul = result * result2;
System.out.println("Result : "+mul);
break;

case 4:
float div;
div = (float) result/ result2;
System.out.print("Result : "+div);
break;

case 5:
int mod;   
mod = result % result2;
System.out.println("Result : "+mod);
break;

case 6:
System.exit(0);
}
   }

}
}

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