2. Rational Numbers (15 points) In mathematics, a rational number is any number that can be...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
2. Rational Numbers (15 points) In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, a numerator p and a non-zero denominator q. Since q may be equal to 1, every integer is a rational number. 1 -- Wikipedia Write a Java class for rational numbers. The class should have 1. two fields of type int, one for the numerator and the other for the denominator. 2. a constructor with two parameters, for the numerator and denominator, respectively. 3. four methods called add, subtract, multiply, and divide, respectively; Each method takes another rational number as the parameter, does the calculation using this and the parameter rational number, and returns the result rational. 4. a simplify method that simplifies this rational number. Each simplified rational number should satisfy the following three requirements: a. Common factors between the numerator and the denominator should be cancelled out; For example, 12/30 should become 2/5 after simplification. b. The denominator should always be positive, while the numerator could be positive, negative, or zero; c. The denominator should always be 1 when the rational number is an integer. 5. a toString method which returns the string representation of this in the form Notes: numerator/denominator. You may assume the following when completing the class: 1) The constructor will never be used to instantiate a rational number with denominator equal to 0; 2) The parameters of add, subtract, multiply, and divide will never be null; 3) The parameter of divide will never be equal to 0. You may define additional methods when you see fit. Tests in RationalTest.java should all pass after you've completed the class. What to do: In Rational.java [Task 2] Add the missing fields to class Rational. [Task 3] Complete the constructor and the methods add, subtract, multiply, divide, simplify, and toString. package hk.edu.polyu.comp.comp2021.assignment1.complex; import org.junit.Test; import org.junit.Before; import static org.junit.Assert.*; public class RationalTest { @Test public void testConstructor_01() { Rational r1 = new Rational(1, 2); assertEquals("1/2", r1.toString()); } @Test public void testSimplify_01() { Rational r1 = new Rational(4, 10); r1.simplify(); assertEquals("2/5", r1.toString()); } @Test public void testSimplify_02() { Rational r2 = new Rational(12, 3); r2.simplify(); assertEquals("4/1", r2.toString()); @Test public void testAddition() { Rational r1 = new Rational(1, 2); Rational r2 = new Rational(1, 3); Rational rSum = r1.add(r2); rSum.simplify(); assertEquals("5/6", rSUm.toString()); @Test public void testSubstraction() { Rational r1 = new Rational(2, 3); Rational r2 = new Rational(1, 4); } Rational rSub = r1.subtract (r2); rSub.simplify(); assertEquals("5/12", rSub.toString()); @Test public void test Muliplication() { Rational r1 = new Rational(7, 8); } Rational r2 = new Rational(5, 6); Rational rMul = r1.multiply(r2); rMul.simplify(); assertEquals("35/48", rMul.toString()); @Test public void testDevision() { Rational r1 = new Rational(2, 3); Rational r2 = new Rational (3, 4); Rational rDiv = r1.divide (r2); rDiv.simplify(); assertEquals("8/9", rDiv.toString()); } package hk.edu.polyu.comp.comp2021.assignment1.complex; public class Rational { // Todo: add the missing fields } public Rational(int numerator, int denominator) { } // Todo: complete the constructor public Rational add(Rational other) { // Todo: complete the method return null; } public Rational subtract (Rational other) { // Todo: complete the method return null; } public Rational multiply (Rational other) { // Todo: complete the method return null; } public Rational divide (Rational other) { // Todo: complete the method return null; } public String toString() { // Todo: complete the method return ""; } public void simplify() { } // Todo: complete the method 2. Rational Numbers (15 points) In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, a numerator p and a non-zero denominator q. Since q may be equal to 1, every integer is a rational number. 1 -- Wikipedia Write a Java class for rational numbers. The class should have 1. two fields of type int, one for the numerator and the other for the denominator. 2. a constructor with two parameters, for the numerator and denominator, respectively. 3. four methods called add, subtract, multiply, and divide, respectively; Each method takes another rational number as the parameter, does the calculation using this and the parameter rational number, and returns the result rational. 4. a simplify method that simplifies this rational number. Each simplified rational number should satisfy the following three requirements: a. Common factors between the numerator and the denominator should be cancelled out; For example, 12/30 should become 2/5 after simplification. b. The denominator should always be positive, while the numerator could be positive, negative, or zero; c. The denominator should always be 1 when the rational number is an integer. 5. a toString method which returns the string representation of this in the form Notes: numerator/denominator. You may assume the following when completing the class: 1) The constructor will never be used to instantiate a rational number with denominator equal to 0; 2) The parameters of add, subtract, multiply, and divide will never be null; 3) The parameter of divide will never be equal to 0. You may define additional methods when you see fit. Tests in RationalTest.java should all pass after you've completed the class. What to do: In Rational.java [Task 2] Add the missing fields to class Rational. [Task 3] Complete the constructor and the methods add, subtract, multiply, divide, simplify, and toString. package hk.edu.polyu.comp.comp2021.assignment1.complex; import org.junit.Test; import org.junit.Before; import static org.junit.Assert.*; public class RationalTest { @Test public void testConstructor_01() { Rational r1 = new Rational(1, 2); assertEquals("1/2", r1.toString()); } @Test public void testSimplify_01() { Rational r1 = new Rational(4, 10); r1.simplify(); assertEquals("2/5", r1.toString()); } @Test public void testSimplify_02() { Rational r2 = new Rational(12, 3); r2.simplify(); assertEquals("4/1", r2.toString()); @Test public void testAddition() { Rational r1 = new Rational(1, 2); Rational r2 = new Rational(1, 3); Rational rSum = r1.add(r2); rSum.simplify(); assertEquals("5/6", rSUm.toString()); @Test public void testSubstraction() { Rational r1 = new Rational(2, 3); Rational r2 = new Rational(1, 4); } Rational rSub = r1.subtract (r2); rSub.simplify(); assertEquals("5/12", rSub.toString()); @Test public void test Muliplication() { Rational r1 = new Rational(7, 8); } Rational r2 = new Rational(5, 6); Rational rMul = r1.multiply(r2); rMul.simplify(); assertEquals("35/48", rMul.toString()); @Test public void testDevision() { Rational r1 = new Rational(2, 3); Rational r2 = new Rational (3, 4); Rational rDiv = r1.divide (r2); rDiv.simplify(); assertEquals("8/9", rDiv.toString()); } package hk.edu.polyu.comp.comp2021.assignment1.complex; public class Rational { // Todo: add the missing fields } public Rational(int numerator, int denominator) { } // Todo: complete the constructor public Rational add(Rational other) { // Todo: complete the method return null; } public Rational subtract (Rational other) { // Todo: complete the method return null; } public Rational multiply (Rational other) { // Todo: complete the method return null; } public Rational divide (Rational other) { // Todo: complete the method return null; } public String toString() { // Todo: complete the method return ""; } public void simplify() { } // Todo: complete the method
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
QUIZ... Let D be a poset and let f : D D be a monotone function. (i) Give the definition of the least pre-fixed point, fix (f), of f. Show that fix (f) is a fixed point of f. [5 marks] (ii) Show that...
-
Sugar (C12H22O11) is a molecular compound that stays together inwater, while NaCl and MgSO4?7H2O are ionic compounds that dissociate into cations andanions as illustrated in the NaCl example below:...
-
Using the facts from the previous problem, how would your answer change if Isabel's after-tax rate of return were 8 percent? In previous problem Isabel, a calendar-year taxpayer, uses the cash method...
-
1. Would you be enthusiastic, nice, and fun if you worked for Barcelona? Why or why not? 2. Focusing on the issue that's central to this video-HR strategies and processes-explain why conflict is...
-
The column with constant \(E I\) has the end constraints shown. Determine the critical load for the column. L
-
The following unadjusted trial balance is prepared at fiscal year-end for Nelson Company. Rent expense and salaries expense are equally divided between selling activities and the general and...
-
Consider the following scenario: As the PR intern at a large public relations agency, team members often ask you to review client presentations as a fresh set of eyes. After reviewing a handful of...
-
To the nearest dollar, how much money would I have to invest at 6/% to earn interest of $4,500 per month?
-
Based on your experiences as a coffee consumer, does Starbucks strategy as described in Concepts & Connections 1. 2 seem to set it apart from rivals? Does the strategy seem to be keyed to a...
-
Describe briefly the format of the standard audit report currently in use in the United Kingdom (as required by ISA (UK and Ireland) 700 (FRC, 2013b) and how it differs from the standard audit report...
-
Let \(\mathcal{W}\) be the region between the sphere of radius 4 and the cube of side 1, both centered at the origin. What is the flux through the boundary \(\mathcal{S}=\partial \mathcal{W}\) of a...
-
How does the Divergence Theorem imply that the flux of the vector field \(\mathbf{F}=\left\langle x^{2}, y-e^{z}, y-2 z xightangle\) through a closed surface is equal to the enclosed volume?
-
Let \(\mathbf{F}=\left\langle y^{2}, 2 z+x, 2 y^{2}ightangle\). Use Stokes' Theorem to find a plane with equation \(a x+b y+c z=0\) (where \(a, b, c\) are not all zero) such that \(\oint_{C}...
-
ECG (electrogardiogram) signals are signals taken from the human body in order to measure the activity and health of the human heart. An ECG signal shows whether a patient has heart disease (called...
-
ABC company leased new advanced computer equipment to STU Ltd on 1 January 2019.STULtd has to pay annual rental of $290,000 starting at 1 January 2019. It is a four years lease with ultimate rental...
-
Based on seven years of data collected by Transport Canada, 45.8% of all bird strikes on moving aircraft involve gulls. Suppose that the next year, planes are asked to emit gull-repelling sound...
-
On a multiple-choice test with 50 questions, each question has possible answers of a, b, c, and d, one of which is correct. For students who guess at all answers, find the mean, variance, and...
-
Use the given data to find the minimum sample size required to estimate a population proportion or percentage. Margin of error: four percentage points; confidence level: 99%; is estimated to be 0.20...
-
As a check on the quality of eye glasses purchased over the internet, glasses were individually ordered from several different online vendors. Among the 92 lenses with antireflection coating, 61...
-
With reference to Exercise 10.55, find a large sample 95% confidence interval for the true difference of probabilities. Data From Exercise 10.55 10.55 As a check on the quality of eye glasses...
-
Two bonding agents, \(A\) and \(B\), are available for making a laminated beam. Of 50 beams made with Agent \(A, 11\) failed a stress test, whereas 19 of the 50 beams made with Agent \(B\) failed. At...
Study smarter with the SolutionInn App