Please need help to write a program in java.. It has two parts and I am really s
ID: 3704877 • Letter: P
Question
Please need help to write a program in java.. It has two parts and I am really stuck on both. I will really appreciate the help....
Part A)
Write a program to accept a roman numeral and determine if it is valid. You do not need to convert the numeral, just determine if it is valid. This is based on the problem from https://projecteuler.net/problem=89. You can go there for more information. and a set of roman numeral samples.
Part B)
Modify the last assignment so that the user enters a file name containing roman numerals. For each roman numeral in the file print out that numeral and a determination of validity (VALID/NOT VALID).
Thank you...
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.io.FileWriter;
public class RomanNumerals {
public static int toDecimal(String roman_in) {
int decimalValue = 0;
int previousNum = 0;
//Parsing the input roman string from last character to convert it into decimal value
for (int x = roman_in.length() - 1; x >= 0 ; x--) {
char roman_char = roman_in.charAt(x);
switch (roman_char) {
case 'M':
decimalValue = findDecimal(1000, previousNum, decimalValue);
previousNum = 1000;
break;
case 'D':
decimalValue = findDecimal(500, previousNum, decimalValue);
previousNum = 500;
break;
case 'C':
decimalValue = findDecimal(100, previousNum, decimalValue);
previousNum = 100;
break;
case 'L':
decimalValue = findDecimal(50, previousNum, decimalValue);
previousNum = 50;
break;
case 'X':
decimalValue = findDecimal(10, previousNum, decimalValue);
previousNum = 10;
break;
case 'V':
decimalValue = findDecimal(5, previousNum, decimalValue);
previousNum = 5;
break;
case 'I':
decimalValue = findDecimal(1, previousNum, decimalValue);
previousNum = 1;
break;
}
}
//System.out.println(decimal);
return decimalValue;
//returning the total value of integer conversion for given roman string
}
// checking the current value with last value and finding the total of decimal value
public static int findDecimal(int currentDecimal, int previousNum, int lastDecimal) {
if (previousNum > currentDecimal) {
return lastDecimal - currentDecimal;
//if if previous is greater than current then subtract that
} else {
return lastDecimal + currentDecimal;
//if previous value is smaller than current then add current to total
}
}
//method to convert input decimal to roman string
public static String toRoman(int inputNum) {
String roman_str = "";
//string declared to store the roman conversion
while (inputNum >= 1000) {
roman_str += "M";
inputNum -= 1000; //value for M is 1000
}
while (inputNum >= 900) {
roman_str += "CM";
inputNum -= 900; //value for CM is 900
}
while (inputNum >= 500) {
roman_str += "D";
inputNum -= 500; //value for D is 500
}
while (inputNum >= 400) {
roman_str += "CD";
inputNum -= 400; //value for CD is 400
}
while (inputNum >= 100) {
roman_str += "C";
inputNum -= 100; //value for C is 100
}
while (inputNum >= 90) {
roman_str += "XC";
inputNum -= 90; //value for XC is 90
}
while (inputNum >= 50) {
roman_str += "L";
inputNum -= 50; //value for L is 50
}
while (inputNum >= 40) {
roman_str += "XL";
inputNum -= 40; //value for XL is 40
}
while (inputNum >= 10) {
roman_str += "X";
inputNum -= 10; //value for X is 10
}
while (inputNum >= 9) {
roman_str += "IX";
inputNum -= 9; //value for IX is 9
}
while (inputNum >= 5) {
roman_str += "V";
inputNum -= 5; //value for V is 5
}
while (inputNum >= 4) {
roman_str += "IV";
inputNum -= 4; //value for IV is 4
}
while (inputNum >= 1) {
roman_str += "I";
inputNum -= 1; //value for I is 1
}
return roman_str;
//returning the string of minimal roman converted for the input decimal number
}
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("roman.txt"));
// creating a object for given text file of roman strings
try {
String line = br.readLine();
// declared variable line to get line by line input from text file
FileWriter wr = null;
wr = new FileWriter(new File("output.txt"));
// change the output file path as required
int saved_char=0;
int count=0;
//Parsing the file till the last to get all line as input
String formatStr = "%-4s %-1s %-25s %-20s %-20s %-20s%n";
wr.write(String.format(formatStr, "S.No","|", "Input Roman String", "Decimal number", "Minimal Form", "No. of char saved"));
wr.write("-----|--------------------------------------------------------------------------------------" +
System.getProperty("line.separator"));
while (line != null) {
int num = toDecimal(line);
//getting the decimal number for the given string of roman
String roman_out=null;
roman_out = toRoman(num);
//converting the decimal from out from above method into the minimal form
count ++;
int len = line.length() - roman_out.length();
saved_char = saved_char + len;
// checking the length of given roman string with the minimal form we obtained
// getting the number of characters saved
/*wr.write(System.getProperty("line.separator") + count + ") Input: " + line +
" Minimal form: " + roman_out + " Decimal num: " + num +
System.getProperty("line.separator") + " Number of characters saved: " +
len + System.getProperty("line.separator"));*/
wr.write(String.format(formatStr, count, "|", line, num, roman_out, len));
line = br.readLine();
// reading the next input of roman input from the file
}
wr.write(String.format(formatStr, "", "", "", "", "", "---------"));
wr.write(String.format(formatStr, "", "", "", "", "Total", saved_char));
wr.write("Total number of Roman strings checked : " + count);
//writing the count of the roman numbers checked
wr.close();
//closing the file writer
System.out.println(" Total number of characters saved for given " + count
+ " roman strings =" + saved_char + " Thanks..:)");
// output on console
} finally {
br.close();
}
}
}
--------------------------------------------------------------------------------------------------------
import java.io.*;
public class Roman {
public static void main(String[] args){
int sum = 0;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File("Roman.txt")));
String text = null;
while ((text = reader.readLine()) != null) {
sum += text.length() - romanUnitLength(countRomanValue(text));
}
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
if(reader != null) {
reader.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
System.out.println(sum);
}
public static int countRomanValue(String s) {
Object[][] number = {
{"M" , 1000},
{"CM", 900},
{"D" , 500},
{"CD", 400},
{"C" , 100},
{"XC", 90},
{"L" , 50},
{"XL", 40},
{"X" , 10},
{"IX", 9},
{"V" , 5},
{"IV", 4},
{"I" , 1},
};
int result = 0;
while(s.length() > 0) {
for(Object[] n : number) {
String numeral = (String) n[0];
if(s.startsWith(numeral)) {
result += (Integer) n[1];
s = s.substring(numeral.length());
break;
}
}
}
return result;
}
public static int romanUnitLength(int n) {
int[] unitLengths = {0, 1, 2, 3, 2, 1, 2, 3, 4, 2};
int count = 0;
if(n >= 4000) { // for MMMM
count += 2;
}
while(n > 0) {
count += unitLengths[n % 10];
n /= 10;
}
return count;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.