Question: JAVA help Add your Roman Numeral Number program to your Menu Program by adding the class's Java file to your Menu Project and then renaming
JAVA help
Add your Roman Numeral Number program to your Menu Program by adding the class's Java file to your Menu Project and then renaming the Roman Numeral class's main() method to another name. Then call the new method by using the class name as follows. If your class's name is romanNumber and your method name is myMethod(), then the call is made by romanNumber.myMethod(). Use the second menu option to identify and acces the program.
Roman Numeral Number program :
import java.util.Scanner;
public class RomanConversionMS
{
int value(char r)
{
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
int romanToDecimal(String str)
{
// Initialize result
int res = 0;
for (int i=0; i { // Getting value of symbol s[i] int s1 = value(str.charAt(i)); // Getting value of symbol s[i+1] if (i+1 { int s2 = value(str.charAt(i+1)); // Comparing both values if (s1 >= s2) { // Value of current symbol is greater // or equalto the next symbol res = res + s1; } else { res = res + s2 - s1; i++; // Value of current symbol is // less than the next symbol } } else { res = res + s1; i++; } } return res; } String decimalToRoman(Integer number) { number = Math.min(3999, Math.max(1, number)); // wraps number between 1-3999 String asRomanNumerals = ""; // Array including numerals in ascending order String[] RN = {"I", "V", "X", "L", "C", "D", "M" }; int i = 0; // Index used to keep track which digit we are translating while (number > 0) { switch(number % 10) { case 1: asRomanNumerals = RN[i] + asRomanNumerals; break; case 2: asRomanNumerals = RN[i] + RN[i] + asRomanNumerals; break; case 3: asRomanNumerals = RN[i] + RN[i] + RN[i] + asRomanNumerals; break; case 4: asRomanNumerals = RN[i] + RN[i + 1] + asRomanNumerals; break; case 5: asRomanNumerals = RN[i + 1] + asRomanNumerals; break; case 6: asRomanNumerals = RN[i + 1] + RN[i] + asRomanNumerals; break; case 7: asRomanNumerals = RN[i + 1] + RN[i] + RN[i] + asRomanNumerals; break; case 8: asRomanNumerals = RN[i + 1] + RN[i] + RN[i] + RN[i] +asRomanNumerals; break; case 9: asRomanNumerals = RN[i] + RN[i + 2] + asRomanNumerals; break; } number = (int) number / 10; i += 2; } return asRomanNumerals; } private static Scanner in = new Scanner(System.in); public static void main(String[] args) { RomanConversionMS ob = new RomanConversionMS(); String s; do { System.out.println("1.Integer to Roman Coversion : 2. Roman to Integer Conversion "); System.out.println("Enter your choice: "); int choice=in.nextInt(); if(choice==1) { System.out.println("Input an integer < 4000 : "); System.out.println("The roman numeral version is: " + ob.decimalToRoman(in.nextInt())); } else if (choice==2) { System.out.println("Input a Roman Numeral : "); System.out.println("The integer number is : " + ob.romanToDecimal(in.next())); } else { System.out.println("Invalid Choice"); } System.out.println("Do you want to continue...(Press Y or N)"); s=in.next(); if (!s.equalsIgnoreCase("Y") || !s.equalsIgnoreCase("N")) { System.out.println("Goodbye!"); } }while(s.equals("Y")); } } MENU CODE : package menuHolder; import java.util.Scanner; public class MenuChoices { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { String menuChoice = ""; do { System.out.println("A) choice 1"); System.out.println("B) choice 2"); System.out.println("Q) Quit"); System.out.println("Please select a menu item."); do { System.out.println("Enter \"A\", \"B\", or \"Q\" "); menuChoice = keyboard.nextLine().toLowerCase().trim(); } while (!menuChoice.equals("a") && !menuChoice.equals("b") && !menuChoice.equals("q")); switch (menuChoice) { case "a": moduleOne(); break; case "b": moduleTwo(); break; default: System.out.println("Good-bye!!!"); break; } System.out.println("An excilent choice!"); } while (!menuChoice.equals("q")); } private static void moduleTwo() { int start, interval, times; System.out.print("Enter the starting number: "); start = keyboard.nextInt(); System.out.print("Enter the interval (e.g. 2 or 7): "); interval = keyboard.nextInt(); System.out.print("How many times do you want to count? "); times = keyboard.nextInt(); if(times < 2) System.out.println("The ending number needs to be at least one counting interval lager than the starting number!"); else { int num = start; for(int i = 1; i <= times; i++) { System.out.print(num + " "); num = num + interval; } System.out.println(); } } private static void moduleOne() { int start, interval, maximum; System.out.print("Enter the starting number: "); start = keyboard.nextInt(); System.out.print("Enter the interval (e.g. 2 or 7): "); interval = keyboard.nextInt(); System.out.print("What is the maximum value you want to go to? "); maximum = keyboard.nextInt(); if(start+interval > maximum) System.out.println("The ending number needs to be at least one counting interval lager than the starting number!"); else { for(int i = start; i <= maximum; i = i + interval) { System.out.print(i + " "); } System.out.println(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
