Project Requirements: You will implement at least three different classes: Roman
ID: 3648666 • Letter: P
Question
Project Requirements:You will implement at least three different classes: RomanToArabic, ArabicToRoman, and ConverterGUI classes and preferably a fourth test class to execute the application.
Part I RomanToArabic class (20 pts):
This class should receive a Roman number as String and convert it to an Arabic number. It should handle all errors(exceptions) that may occur.
Part II ArabicToRomanclass (20 points):
This class should receive an Arabic number and convert it to a String, which represents a Roman number. It should handle all errors(exceptions) that may occur.
Part III ConverterGUI class (40 pts): This class may contain the main method.
This class creates a GUI using the swing components and the RomanToArabic and ArabicToRoman classes to convert user input to roman or arabic numbers. Start by displaying a window to the user, which explains about the application. Then ask the user to press a button to display a second GUI to perform the conversions. The second window may be as follows but feel free to design it in any way that you would like it to be.
Be sure to use at least the following GUI components :
Explanation / Answer
package rs.schedule.web.converter; 002 003 import javax.faces.component.UIComponent; 004 import javax.faces.context.FacesContext; 005 import javax.faces.convert.Converter; 006 007 /** 008 * Calculates numbers between 0 and 3000 009 * */ 010 public class ArabicRomanConverter implements Converter{ 011 012 //two arrays: numbers and letters with corresponding indices 013 private final static int[] numbers = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; 014 private final static String[] letters = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 015 016 public static Integer Roman2Arabic(String roman){ 017 if (roman.length() == 0) 018 return null; 019 020 roman = roman.toUpperCase(); // Convert to upper case letters. 021 int i = 0; 022 int arabic = 0; 023 024 while (i number) { 041 // Combine the two letters to get one value, and move on to next position in string. 042 arabic += (nextNumber - number); 043 i++; 044 } 045 else{ 046 arabic += number; 047 } 048 } 049 } 050 if (arabic > 3000) 051 return null; 052 return arabic; 053 } 054 055 public static String Arabic2Roman(Integer arabic) { 056 String roman = ""; // The roman numeral. 057 //arabic represents the part of num that still has to be converted to Roman numeral representation. 058 for (int i = 0; i = numbers[i]) { 060 roman += letters[i]; 061 arabic -= numbers[i]; 062 } 063 } 064 return roman; 065 } 066 067 private static int letterToNumber(char letter) { 068 // Find the integer value of letter considered as a Roman numeral. Return 069 // -1 if letter is not a legal Roman numeral. The letter must be upper case. 070 switch (letter) { 071 case 'I': return 1; 072 case 'V': return 5; 073 case 'X': return 10; 074 case 'L': return 50; 075 case 'C': return 100; 076 case 'D': return 500; 077 case 'M': return 1000; 078 default: return -1; 079 } 080 } 081 082 public static void main(String[] args){ 083 System.out.println(Roman2Arabic("XXVI")); 084 System.out.println(Arabic2Roman(26)); 085 } 086 087 @Override 088 public Object getAsObject(FacesContext context, UIComponent component, String value) { 089 090 Integer arabic = Roman2Arabic(value); 091 092 return arabic; 093 } 094 095 @Override 096 public String getAsString(FacesContext context, UIComponent component, Object value) { 097 098 Integer arabic = (Integer) value; 099 String roman = Arabic2Roman(arabic); 100 101 return roman; 102 } 103 104 }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.