CSC-140 Assignment 2

advertisement
CSC-140
Assignment 2
1
Introduction
This second assignment deals with converting integer values to Roman numerals. You will get to play with the String data type and with while loops.
Here is a sample output from my program:
Ubuntu:~> java Assignment2
Enter a number: 0
0 does not exists as a Roman numeral
Ubuntu:~> java Assignment2
Enter a number: -89
The number cannot be negative
Ubuntu:~> java Assignment2
Enter a number: 1984
1984 = MCMLXXXIV
The following table lists the Roman numerals:
Integer Roman Numeral
1000
M
500
D
100
C
50
L
10
X
5
V
1
I
So, e.g., 1001 = MI. However, 900 is not DCCC but CM. The same argument
goes for the numbers 400, 90, 40, 9, and 4. These numbers must be handled
separately.
Also remember, you can use the + operator on Strings: "M" + "I" = "MI".
0 has no equivalent in Roman numerals, and you cannot convert negative
numbers.
1
2
Handout Code
Again, I have written some code. This time to input integer numbers.
import j a v a . i o . ∗ ;
public c l a s s Assignment1 {
s t a t i c public int r e a d I n t ( ) {
B u f f e r e d R e a d e r i n = new B u f f e r e d R e a d e r (new InputStreamReader (
;
try {
return I n t e g e r . p a r s e I n t ( i n . r e a d L i n e ( ) ) ;
} catch ( IOException e ) {}
return 0 ;
}
public s t a t i c void main ( S t r i n g a r g s [ ] ) {
// your code g o e s h e r e
}
}
3
Topics Covered
To finish this assignment you need to understand the following topics in Java:
• Working with String variables. Remember it is String and not string
for the type name for strings.
• Working with while loops.
4
Work List
1. Make sure you read the ’Topics Covered’ section and familiarize yourself
with all the topics.
2. Create a file Assignment2.java and put the handout code into it. You
should now be able to compile and run it as it is. Remember, javac is
2
the name of the compiler and java the name of the virtual machine.
If you are not at UNLV you can log into java.cs.unlv.edu. (You can
do this assignment in BlueJ as well if you like!)
3. Implement the code for the conversion.
4. Test your program with a number of different input values. What
happens if you enter 0 or a negative number?
5. Once you are happy with your solution, go to csgfms.cs.unlv.edu
and hand it in. Just the .java file!
6. In the writeup try to sketch an algorithm that turns Roman numerals
into integer numbers. Do not implement it, as we do not have the
’tools’ to do so yet.
7. Bring to class (along with the printout of your solution) a write up in
which you explain how you solved the problem, and show output from
a couple of tests; enough to convince me that it works.
5
Due Date
The Assignment1.java file must be uploaded to csgfms.cs.unlv.edu no
later than Monday February 14th before midnight (midnight between Monday and Tuesday), and the write up should be brought to class the day after.
3
Download