Learning Objectives: Use loops to implement repetitive actions Use if-else state
ID: 3766574 • Letter: L
Question
Learning Objectives:
Use loops to implement repetitive actions
Use if-else statements to provide different paths of execution
Develop algorithms to produce the desired result
Generate random numbers within a specified range
Description:
In this assignment you are going to display numbers as roman numerals, as described in this article .
Create a jGrasp project called A4 that includes two java files: Roman.java and RomanApp.java
Implement class Roman as specified in the UML class diagram above. Pay special attention to the class name, method names, parameter lists, and return types. They all need to match the specification in the UML class diagram. No fields, public methods, or constructors should be added.
Notice the underlined operations. This indicates that the methods are static.
Roman.java:
Method summary:
Print the roman numerals that correspond to 1, 5, 10, 50, 100, and 1000 and the permitted subtractive forms (see expected output)
The summary should be printed in tabular form and the columns should be left aligned.
Use format specifiers to implement the columns and include a larger space to separate the numerals on the left from the subtracted forms on the right. This makes the summary easier to read.
Notice that there is no subtractive form in the first line.
Method printNumeral:
If the number passed to the method is between 1 and 39 (inclusive) print the roman numeral corresponding to the parameter value. Otherwise print: Valid number range: 1 – 39
In order to print numbers as roman numerals, you need to understand how the roman numerals work. Depending on the resource you might find slight variations. For this assignment I want you to use the description provided in this article: http://www.purplemath.com/modules/romannum.htm ( FYI: The article consists of two pages ).
Resist the temptation to hard-code the numerals for all 39 numbers. Instead look for patterns and use control statements (e.g. if, if-else, loops) to implement this method. Look for patterns that avoid code repetition.
Even though it is forbidden to add public methods to the class (it needs to be implemented exactly as specified in the UML class diagram) you are free to add private methods to structure your code.
RomanApp.java
RomanApp includes the main method and it is a test client for class Roman.
Hint: static methods should be called on the type NOT on an instance. In order to test the class Roman do the following:
a) Display the summary
b) Use a loop to call the method printNumeral repeatedly. Pass the numbers 0 to 40 (inclusive) as argument. After each method call the cursor should be advanced to the next line.
c) Use a loop to print roman numerals corresponding to three random numbers between 10 and 20 (inclusive).
Your random numbers may differ from the output on the right. All 3 parts should be separated by a single empty line for clarity.(see output)
Expected Output:
Expected Output:
I = 1
V = 5 IV = 4
X = 10 IX = 9
L = 50 XL = 40
C = 100 XC = 90
D = 500 CD = 400
M = 1000 CM = 900
Valid number range: 1 - 39
I
II
III
IV
V
VI
VII
VIII
IX
X
XI
XII
XIII
XIV
XV
XVI
XVII
XVIII
XIX
XX
XXI
XXII
XXIII
XXIV
XXV
XXVI
XXVII
XXVIII
XXIX
XXX
XXXI
XXXII
XXXIII
XXXIV
XXXV
XXXVI
XXXVII
XXXVIII
XXXIX
Valid number range: 1 - 39
3 random numbers between 10 and 20:
XIX
XV
XX
Explanation / Answer
import java.util.*;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
class Roman
{
static String printNumeral(int intnum)
{
String result="";
int m, d, c, l, x, v, i, n;
if (intnum >= 1000)
{
m = intnum / 1000;
n = 0;
for (; n < m; n++){
// System.out.print("M");
result=result+"M";}
intnum = intnum%1000;
}
if (intnum >= 900)
{
// System.out.print("CM");
result=result+"CM";
intnum = intnum%900;
}
else if (intnum >= 500)
{
d = intnum / 500;
n = 0;
for (; n < d; n++){
// System.out.print("D");
result=result+"D";
}
intnum = intnum%500;
}
if (intnum >= 400)
{
//System.out.print("CD");
intnum = intnum%400;
}
else if (intnum >= 100)
{
c = intnum / 100;
n = 0;
for (; n < c; n++){
// System.out.print("C");
result=result+"C";
}
intnum = intnum%100;
}
if (intnum >= 90)
{
// System.out.print("XC");
result=result+"XC";
intnum = intnum%90;
}
else if (intnum >= 50)
{
l = intnum / 50;
n = 0;
for (; n < l; n++){
// System.out.print("L");
result=result+"L";
}
intnum = intnum%50;
}
if (intnum >= 40)
{
// System.out.print("XL");
intnum = intnum%40;
}
else if (intnum >= 10)
{
x = intnum / 10;
n = 0;
for (; n < x; n++){
// System.out.print("X");
result=result+"X";
}
intnum = intnum%10;
}
if (intnum >= 9)
{
//System.out.print("IX");
result=result+"IX";
intnum = intnum%9;
}
else if (intnum >= 5)
{
v = intnum / 5;
n = 0;
for (; n < v; n++){
// System.out.print("V");
result=result+"V";
}
intnum = intnum%5;
}
if (intnum >= 4)
{
// System.out.print("IV");
result=result+"IV";
intnum = intnum%4;
}
else if (intnum >= 1)
{
i = intnum;
n = 0;
for (; n < i; n++){
// System.out.print("I");
result=result+"I";
}
}
//System.out.println();
return result;
}
static void RandomG()
{
Random r=new Random();
System.out.println("3 random numbers between 10 and 20");
for(int i=0;i<3;i++)
{
int n=r.nextInt(20-10+1)+10;
System.out.println(printNumeral(n));
}
}
}
public class RomanApp {
public static void main(String[] args)
{
for(int i=1;i<40;i++)
{ if(i<1 || i>39)
{
System.out.println("Valid number range: 1 - 39");
System.exit(0);
}
System.out.println(Roman.printNumeral(i));
}
System.out.println();
Roman.RandomG();
System.out.println();
System.out.println(Roman.printNumeral(1)+"="+1);
System.out.println(Roman.printNumeral(5)+"="+5+" "+Roman.printNumeral(4)+"="+4);
System.out.println(Roman.printNumeral(10)+"="+10+" "+Roman.printNumeral(9)+"="+9);
System.out.println(Roman.printNumeral(50)+"="+50+" "+Roman.printNumeral(49)+"="+49);
System.out.println(Roman.printNumeral(100)+"="+100+" "+Roman.printNumeral(90)+"="+90);
System.out.println(Roman.printNumeral(500)+"="+500+" "+Roman.printNumeral(400)+"="+400);
System.out.println(Roman.printNumeral(1000)+"="+1000+" "+Roman.printNumeral(900)+"="+900);
}}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.