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 Manny's after-tax rate of return were 8 percent? In previous problem Manny, a calendar-year taxpayer, uses the cash method...
-
The RBC Heritage Classic Foundation license plate is available to anyone with a vehicle registered in the state of South Carolina. It costs $75 every two years with $66 to $71 going to the foundation...
-
Determine whether each of the following is true or false. In each case, assume \(P\) is located at \(t=0\) and \(F\) is located at \(t=n\), and the \(A\) s are spread uniformly over the planning...
-
Gundy Company expects to produce 1,200,000 units of Product XX in 2014. Monthly production is expected to range from 80,000 to 120,000 units. Budgeted variable manufacturing costs per unit are direct...
-
Explain why there are the same number of tables in the relational model as compared to the number of entities in the logical model.
-
6. Marcy Fetter, a staff analyst at the Los Angeles plant of Computer Products Corporation, is assigned to the team that is developing the process design for producing an RFID sensor. The corporate...
-
Kensington Corporation, Inc. (an October 31 fiscal year-end corporation) plans to purchase $2,700,000 of used office fixtures (7-year property), its only personalty acquired during the year....
-
Economists Michael Tanner and Stephen Moore of the Cato Institute recently calculated the hourly wage equivalent of welfare for a single mother with two children for each of the 50 United States....
-
According to economists Henry Saffer of Kean University, Frank J. Chaloupka of the University of Illinois at Chicago, and Dhaval Dave of CUNY Graduate Center, using the criminal justice system to...
-
Locate at least one article that comments on the tax provisions for capital gains that were included as part of the 2012 Taxpayer Relief Act. Summarize the comments and provide a citation for your...
-
Soft-drink companies pay universities for the exclusive pouring rights to sell their products on campus. In a recent deal, UCLA signed a contract with Pepsi for $1.5 million per year limiting...
-
Calculate the work done in a reversible expansion of three moles of an ideal gas from pressure of 4 bar to 1 bar at 298K. (A)-10304 J (B) +10304 J (C)- 3435 J (D)-107.7 J
-
How do the principles of (a) Physical controls and (b) Documentation controls apply to cash disbursements?
-
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...
-
A particle moves on a predetermined trajectory with the equation of motion \(s(t)=k t^{2}\), with \(k\) constant and with magnitude of the acceleration equal to \(a=2 k\). Show by using (3.74) that...
-
A projectile is launched from the Earth's surface with velocity \(v_{0}=50.0 \mathrm{~m} / \mathrm{s}\), at an angle \(\theta=60^{\circ}\) to the vertical. Determine the radius of curvature of the...
-
The equation of motion of a particle is given in Cartesian coordinates by the relation \(\mathbf{r}(t)=\alpha t^{2} \hat{\mathbf{i}}+\beta t^{2} \hat{\mathbf{j}}\). Determine (i) the trajectory of...
Study smarter with the SolutionInn App