Programming Languages‎ > ‎Java‎ > ‎

Java Programming Assignment 4


Requirement

Problem: Write a Java class called RomanNumeral)


Solution


public class HW4_RomanNumeral 
{
public static final String DEFAULT_ROMAN_NUMERAL = "I";
public static final String[] ROMAN_ARRAY_STRING = {"M", "CM", "D","CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
public static HW4_RomanNumeral [] romanArray = new HW4_RomanNumeral[ROMAN_ARRAY_STRING.length] ;
private String romNumStr = "";
private int value = 0;
// Static block (Easy to use for initialization of a static array of objects in loop instead of new, new, new, 13 times)
static
{
for (int i = 0; i < romanArray.length; i++)
{
romanArray[i] = new HW4_RomanNumeral(ROMAN_ARRAY_STRING[i]);
}
}
// Constructor 1: Default Constructor
public HW4_RomanNumeral()
{
// Not needed but in case
romNumStr = "";
value = 0;
}
// Constructor 2: using romNumStr
public HW4_RomanNumeral(String s_romNumStr)
{
setRomNumStr(s_romNumStr);
}

// Constructor 3: using value
public HW4_RomanNumeral(int i_Value)
{
setValue(i_Value);
}
// A. mutator
public void setRomNumStr(String s_romNumStr)
{
if(s_romNumStr != null)
{
s_romNumStr = s_romNumStr.trim().toUpperCase(); // removed leading and trailing white spaces and convert to upper case
romNumStr = s_romNumStr;
// Method C
calculateIntFromRomanStr();
}
}
// B. mutator
public void setValue(int i_value)
{
if(i_value > 0)
{
value = i_value;
// To Do: Method F
calculateRomanStrFromInt();
}
}
// C. - calculates the integer value of the Roman numeral string
private void calculateIntFromRomanStr()
{
int [] digitValues = new int[romNumStr.length()];
for (int i = 0; i < romNumStr.length(); i++)
{
// 'M' = 1000, 'D' = 500, 'C' = 100, 'L' = 50, 'X' = 10, 'V' = 5, 'I' = 1, none of these=0
switch(romNumStr.charAt(i))
{
case 'M':
digitValues[i] = 1000;
break;
case 'D':
digitValues[i] = 500;
break;
case 'C':
digitValues[i] = 100;
break;
case 'L':
digitValues[i] = 50;
break;
case 'X':
digitValues[i] = 10;
break;
case 'V':
digitValues[i] = 5;
break;
case 'I':
digitValues[i] = 1;
break;
default:
// If any of the digitValue are 0, change romNumStr to "", assign 0 to the value instance variable and return
digitValues[i] = 0;
romNumStr = "";
return;
//break;
}
}
// Add or subtract the current digitValues element based on the following rules: 1. If the current digitValues element >= next digitValues element, add the current digitValues element to the value instance variable. 2. If the current digitValues element < next digitValues element, subtract the current digitValues element from the value instance variable. 3. Add the last digitValues element to the value instance variable 
for (int i = 0; i < digitValues.length; i++ )
{
if(i == digitValues.length - 1)
value = value + digitValues[i]; // first if condition requires for last index or next condition will generate exception
else if(digitValues[i] >= digitValues[i+1])
value = value + digitValues[i];
else if(digitValues[i] < digitValues[i+1])
value = value - digitValues[i];
}
}
// D. accessor
public String getRomNumStr()
{
return romNumStr;
}
// E. accessor
public int getValue()
{
return value;
}
// F. generates and assigns the String Roman numeral equivalent of the value of this RomanNumeral
private void calculateRomanStrFromInt()
{
StringBuilder sb = new StringBuilder();
int val = value;
// For each element in the romanArray, *While (val – (value in the romanArray element)) >= 0, append the (romNumStr in the romanArray element) to the StringBuilder and subtract (value in the romanArray element) from val, then repeat to *
for (int i = 0; i < romanArray.length; i++)
{
while(val - romanArray[i].value >= 0)
{
sb.append(romanArray[i].romNumStr);
val = val - romanArray[i].value;
}
}
romNumStr = sb.toString();
}
// G. add another RomanNumeral to this RomanNumeral
public HW4_RomanNumeral sumRomanNumeral(HW4_RomanNumeral oHW4_RomanNumeral)
{
if (oHW4_RomanNumeral != null)
return new HW4_RomanNumeral(this.value + oHW4_RomanNumeral.value);
else
return new HW4_RomanNumeral(); // Need to check before display of sum
}
// H. returns the name of the class, the romNumStr and the value, concatenated in a String
public String toString()
{
// RomanNumeral: MMII = 2002
return this.getClass().getName() + ": " + romNumStr + " = " + value;
/*
// Based on test run looks like not needed
if(romNumStr != "" && value != 0)
return this.getClass().getName() + ": " + romNumStr + " = " + value;
else
return "Something is Wrong. Check your input.";
*/
}
}



HW4_Main class

/*
Name of program: Calculation of Roman Numeral
Programmer's name: ABC
Current Date: dd//mm/yyyy
Computer system and compiler you are using: Win 8, JDK 1.7
Brief description of the program (1-5 sentences): Write a Java program to get sum of two roman numeral
Variable names (if any) and descriptions of what they represent: 
*/
import java.util.Scanner;
public class HW4_Main 
{
static Scanner oScanner = new Scanner(System.in);
public static void main(String[] args) 
{
HW4_RomanNumeral roman1, roman2, roman3;
roman1 = getRomanNumeralFromUser();
roman2 = getRomanNumeralFromUser();
// roman3 = roman1 + roman2
roman3 = roman1.sumRomanNumeral(roman2);
DisplayRomanNumerals(roman1, roman2, roman3);
// Call method Y. (see Y. below), passing roman1 and 2 (this method will change roman1
MultiplyRomanNumeral(roman1, 2);
// Call method Y. again, passing roman2 and 10
MultiplyRomanNumeral(roman2, 10);
// Call the RomanNumeral instance method to add, calling it on roman1 passing roman2 and assigning the return value to roman3.
roman3 = roman1.sumRomanNumeral(roman2);
DisplayRomanNumerals(roman1, roman2, roman3);
}
// X: Get Roman numeral from User
public static HW4_RomanNumeral getRomanNumeralFromUser()
{
String s_UserInputRomanNumeral = "";
System.out.print("Enter Roman Numeral: ");
s_UserInputRomanNumeral = oScanner.nextLine();
// If the String is empty ("") return a new DEFAULT RomanNumeral
s_UserInputRomanNumeral = (s_UserInputRomanNumeral.trim() == "") ? HW4_RomanNumeral.DEFAULT_ROMAN_NUMERAL : s_UserInputRomanNumeral;
return new HW4_RomanNumeral(s_UserInputRomanNumeral);
}
// Y: change the value in the RomanNumeral parameter so the value is muliplied by the int parameter. You MUST use the accessor (method E.) and the mutator (method B.) to change it
public static void MultiplyRomanNumeral(HW4_RomanNumeral oHW4_RomanNumeral, int i_Multiplier)
{
oHW4_RomanNumeral.setValue(oHW4_RomanNumeral.getValue() * i_Multiplier);
}
// DisplayRomanNumerals
public static void DisplayRomanNumerals(HW4_RomanNumeral oRoman1, HW4_RomanNumeral oRoman2, HW4_RomanNumeral oSumRoman)
{
System.out.println("The sum of:");
System.out.println(oRoman1.toString() + " and");
System.out.println(oRoman2.toString() + " equals");
System.out.println(oSumRoman.toString());
}
}

/*
Output:

Enter Roman Numeral:   MCMXLIX  
Enter Roman Numeral: vi  
The sum of:
HW4_RomanNumeral: MCMXLIX = 1949 and
HW4_RomanNumeral: VI = 6 equals
HW4_RomanNumeral: MCMLV = 1955
The sum of:
HW4_RomanNumeral: MMMDCCCXCVIII = 3898 and
HW4_RomanNumeral: LX = 60 equals
HW4_RomanNumeral: MMMCMLVIII = 3958


Enter Roman Numeral:      cdxxxiv 
Enter Roman Numeral: I  
The sum of:
HW4_RomanNumeral: CDXXXIV = 434 and
HW4_RomanNumeral: I = 1 equals
HW4_RomanNumeral: CDXXXV = 435
The sum of:
HW4_RomanNumeral: DCCCLXVIII = 868 and
HW4_RomanNumeral: X = 10 equals
HW4_RomanNumeral: DCCCLXXVIII = 878


Enter Roman Numeral:   LI8ID
Enter Roman Numeral: $C
The sum of:
HW4_RomanNumeral:  = 0 and
HW4_RomanNumeral:  = 0 equals
HW4_RomanNumeral:  = 0
The sum of:
HW4_RomanNumeral:  = 0 and
HW4_RomanNumeral:  = 0 equals
HW4_RomanNumeral:  = 0


Enter Roman Numeral:     XLII
Enter Roman Numeral: 
The sum of:
HW4_RomanNumeral: XLII = 42 and
HW4_RomanNumeral:  = 0 equals
HW4_RomanNumeral: XLII = 42
The sum of:
HW4_RomanNumeral: LXXXIV = 84 and
HW4_RomanNumeral:  = 0 equals
HW4_RomanNumeral: LXXXIV = 84

*/



Comments