Background Classes are java constructs, among other properties, that allow the i
ID: 3860366 • Letter: B
Question
Background
Classes are java constructs, among other properties, that allow the implementation of user defined data types.
Assignment
Create two classes one named Rational to represent rational numbers and their operations: sum, subtraction, product, division, equality, and less that; and one class named RUPolynomial to represent polynomial over the real numbers with one indeterminate and their operations: sum, subtraction, product, division, and equality.
As noted in class we can use two integers to represent a rational number, but this representation might no be fully reduced:
10 35 = 5 7
use the Euclidean algorithm to reduce every rational number.
For the polynomial class, use and array-based representation to store the coefficients. Every class should have a toString() method to convert the object into a humanreadable string of characters.
To test each class method, randomly generate two files, rationals.txt and polynomials.txt respectively, with the following structure:
• rationals.txt has 12 lines, every line consists of two decimal integers separated by a blank space (numerator, and denominator respectively). Select each pair of lines consecutively to test for every rational number operator (a total of 6).
• polynomials.txt has 20 lines, to represent 10 polynomials, (a) non-negative degree and (b) real coefficients separated by a blank space. Select each 4 lines to test consecutively for every polynomial operator.
Finally, store in two files, rational results.txt and polynomial results.txt human readable versions of each test. For example, the test of rational division could be produce a string of the form:
“(3/4) ÷ (5/3) = (9/20)”
Explanation / Answer
public class RationalNumber { private int numerator, denominator; //----------------------------------------------------------------- // Constructor: Sets up the rational number by ensuring a nonzero // denominator and making only the numerator signed. //----------------------------------------------------------------- public RationalNumber (int numer, int denom) { if (denom == 0) denom = 1; // Make the numerator "store" the sign if (denom < 0) { numer = numer * -1; denom = denom * -1; } numerator = numer; denominator = denom; reduce(); } //----------------------------------------------------------------- // Returns the numerator of this rational number. //----------------------------------------------------------------- public int getNumerator () { return numerator; } //----------------------------------------------------------------- // Returns the denominator of this rational number. //----------------------------------------------------------------- public int getDenominator () { return denominator; } //----------------------------------------------------------------- // Returns the reciprocal of this rational number. //----------------------------------------------------------------- public RationalNumber reciprocal () { return new RationalNumber (denominator, numerator); } //----------------------------------------------------------------- // Adds this rational number to the one passed as a parameter. // A common denominator is found by multiplying the individual // denominators. //----------------------------------------------------------------- public RationalNumber add (RationalNumber op2) { int commonDenominator = denominator * op2.getDenominator(); int numerator1 = numerator * op2.getDenominator(); int numerator2 = op2.getNumerator() * denominator; int sum = numerator1 + numerator2; return new RationalNumber (sum, commonDenominator); } //----------------------------------------------------------------- // Subtracts the rational number passed as a parameter from this // rational number. //----------------------------------------------------------------- public RationalNumber subtract (RationalNumber op2) { int commonDenominator = denominator * op2.getDenominator(); int numerator1 = numerator * op2.getDenominator(); int numerator2 = op2.getNumerator() * denominator; int difference = numerator1 - numerator2; return new RationalNumber (difference, commonDenominator); } //----------------------------------------------------------------- // Multiplies this rational number by the one passed as a // parameter. //----------------------------------------------------------------- public RationalNumber multiply (RationalNumber op2) { int numer = numerator * op2.getNumerator(); int denom = denominator * op2.getDenominator(); return new RationalNumber (numer, denom); } //----------------------------------------------------------------- // Divides this rational number by the one passed as a parameter // by multiplying by the reciprocal of the second rational. //----------------------------------------------------------------- public RationalNumber divide (RationalNumber op2) { return multiply (op2.reciprocal()); } //----------------------------------------------------------------- // Determines if this rational number is equal to the one passed // as a parameter. Assumes they are both reduced. //----------------------------------------------------------------- public boolean equals (RationalNumber op2) { return ( numerator == op2.getNumerator() && denominator == op2.getDenominator() ); } //----------------------------------------------------------------- // Returns this rational number as a string. //----------------------------------------------------------------- public String toString () { String result; if (numerator == 0) result = "0"; else if (denominator == 1) result = numerator + ""; else result = numerator + "/" + denominator; return result; } //----------------------------------------------------------------- // Reduces this rational number by dividing both the numerator // and the denominator by their greatest common divisor. //----------------------------------------------------------------- private void reduce () { if (numerator != 0) { int common = gcd (Math.abs(numerator), denominator); numerator = numerator / common; denominator = denominator / common; } } //----------------------------------------------------------------- // Computes and returns the greatest common divisor of the two // positive parameters. Uses Euclid's algorithm. //----------------------------------------------------------------- private int gcd (int num1, int num2) { while (num1 != num2) if (num1 > num2) num1 = num1 - num2; else num2 = num2 - num1; return num1; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.