Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 1: Class Matrix (7 pts). We ask to: a) Trace the UML b) Write the code of a Class called Rational designed to perform

Question 1: Class Matrix (7 pts). We ask to: a) Trace the UML b) Write the code of a Class called Rational designed to perform arithmetic operations with fractions. This class uses two private integer variables to represent the "numerator" and the denominator. She owns : Private data:  numerator  denominator Two Builders: o The 1 er initializes the Rational object to zero by setting:  0 in the numerator  1 in the denominator o The 2 nd is used to initialize the Rational object when it is created. It must record the fraction created in its reduced form, i.e.: The fraction 2/4 is equivalent to 12 and will be saved in the object created by:  1 in the numerator  2 in the denominator Public methods:  Accessors for private data.  A method Add(Right Rational) which returns a Rational which is the sum of the two fractions in reduced form.  A method Sub(Right Rational) which returns a Rational which is the subtraction of the two fractions in reduced form. 2  A Mul(Right Rational) method which returns a Rational which is the product of the two fractions in reduced form.  A Div(Right Rational) method which returns a Rational which is the division of the two fractions in reduced form.  A toRationalString() method that returns a String that represents the fraction under the "numerator/denominator" form  A toFloatString() method that returns a String that represents the fraction in the form a floating point decimal number with two decimal places. Private method:  A reduce() method whose task is to modify the numerator and the denominator of the fraction in order to transform it into a reduced form. An example of a test program of this class is given below with the results expected (figure 1):

public class TestRational { public static void main(String[] args) { Rational r1, r2, result; r1 = new Rational(4, 12 ); r2 = new Rational(8 , 12 ); result=r1.add(r2); System.out.println(r1.toRationalString()+" + "+r2.toRationalString()+" = "+

result.toRationalString());

System.out.println(r1.toRationalString()+" + "+r2.toRationalString()+" = "+

result.toFloatString());

result=r1.sub(r2); System.out.println(r1.toRationalString()+" - "+r2.toRationalString()+" = "+

result.toRationalString());

System.out.println(r1.toRationalString()+" - "+r2.toRationalString()+" = "+

result.toFloatString());

result=r1.mult(r2); System.out.println(r1.toRationalString()+" x "+r2.toRationalString()+" = "+

result.toRationalString());

System.out.println(r1.toRationalString()+" x "+r2.toRationalString()+" = "+

result.toFloatString());

result=r1.div(r2); System.out.println(r1.toRationalString()+" / "+r2.toRationalString()+" = "+

result.toRationalString());

System.out.println(r1.toRationalString()+" / "+r2.toRationalString()+" = "+

result.toFloatString());

} }

image text in transcribed

Figure 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions