Exercise 8.1
Rational Calculations (20 points)
The paragraph below is reiterating Exercise 6.2
Rational Reduction which you have completed previously:
Many fractions (rationals) can be reduced. As an
example, a rational with a numerator of 12 and a denominator of 16
reduces to 3/4. Write a method named Reduce in which
you pass two parameters by reference, a numerator and a
denominator. The reduced numerator and denominator is to be returned
back in the parameters that were passed by reference. If the
original rational cannot be reduced, just return the original numerator
and denominator. Incorporate your method Reduce into a
Windows program.
In this exercise your are going to build upon
Exercise 6.2.
Create a class named Rational for performing
arithmetic with fractions.
Use integer variables to represent the private
data of the class - the numerator and the denominator.
Provide a constructor that enables an object of this class to be
initialized when it is declared. The constructor should contain default
values of 1 for a numerator and 1 for a denominator in the rare case of no
initializers being provided. The rationals need to be stored in
reduced form. For example, the fraction
2/4 would be stored in the
object as 1 in the
numerator and 2 in
the denominator.
Provide public member functions (methods) that
perform each of the following tasks:
- Adding two Rational numbers. The result
should be stored in reduced form.
- Subtracting two Rational numbers. The
result should be stored in reduced form.
- Multiplying two Rational numbers. The
result should be stored in reduced form.
- Dividing two Rational numbers. The result
should be stored in reduced form.
- Returning a Rational number in floating-point (double)
format.
- Returning a string representing a Rational
number in the form
a/b ,
where a is the
numerator and b
is the denominator.
Provide a private member function named Reduce.
This code is reused from your exercise 6.2.
Provide get accessors to be able to retrieve the
numerator and denominator variables.
Write a main Windows GUI program (Ex8_1) to
test your class.
Result (Save to
your local machine and run) Note:
Extra credit if you make an icon that represents the program topic. (2
points).
Extra credit if you clear all the resulting data on the form when changing
any of the input values. (4 points).
Extra credit if you make the Rational objects able to be seen in other
event methods. In other words, if you add a second button to the
form, you should be able to get access to the previous objects. (10
points) |