Java Reference
In-Depth Information
added, subtracted, multiplied, or divided); and if the user clicks the button E ,
the program terminates.
7. The Calculator programming example of this chapter is designed to perform
operations on integers. Write a similar program that can be used to perform
operations on decimal numbers. (Note: If division by zero occurs with values
of the int data type, the program throws a division by zero exception.
However, if you divide a decimal number by zero, Java does not throw the
division by zero exception; it returns the answer as infinity . However, if
division by zero occurs, your calculator program must output the message
ERROR: / by zero .)
In Programming Exercise 2 in Chapter 8, we defined a class Roman to
implement Roman numerals in a program. In that exercise, we also imple-
mented the method romanToDecimal to convert a Roman numeral into its
equivalent decimal number.
a. Modify the definition of the class Roman so that the data members are
declared as protected . Also include the method decimalToRoman ,
which converts the decimal number (the decimal number must be a
positive integer) to an equivalent Roman numeral format. Write the
definition of the method decimalToRoman .Yourdefinitionofthe class
Roman must contain the method toString , which returns the string
containing the number in Roman format. For simplicity, we assume
that only the letter I can appear in front of another letter and that it
appears only in front of the letters V and X . For example, 4 is represented
as IV , 9 is represented as IX , 39 is represented as XXXIX ,and 49 is
represented as XXXXIX .Also, 40 is represented as XXXX , 190 is represented
as CLXXXX , and so on.
b. Derive the class ExtendedRoman from the class Roman to do the
following. In the class ExtendedRoman , include the methods add ,
subtract , multiply , and divide so that arithmetic operations can be
performed on Roman numerals.
To add (subtract, multiply, or divide) Roman numerals, add (subtract,
multiply, or divide, respectively) their decimal representations and then
convert the result to the Roman numeral format. For subtraction, if the
first number is smaller than the second number, throw the exception,
'' Because the first number is smaller than the second, the
numbers cannot be subtracted ''. Similarly, for division, the numerator
must be larger than the denominator.
c. Write the definitions of the methods add , subtract , multiply , and
divide as described in Part b. Also, your definition of the class
ExtendedRoman must contain the method toString that returns the
string containing the number in Roman format.
d. Write a program to test various operations on your class ExtendedRoman .
8.
Search WWH ::




Custom Search