0
\$\begingroup\$

Part I-IV recommend code changes as mentioned in link 1, for the initial code given at link 2.

Can you please review the code changes for Part I-IV?

Part I: Constructors (1 point)

Modify the no-parameter constructor to call the two-parameter constructor. Then, fill in the fourth constructor so that it uses the good style and correctly duplicates the input Fraction (it does neither now). Your TA or lab assistant will ask to see your constructors when you get checked off.

Part II: Using Objects (1 point)

Further on in the main method, there are four lines commented out. Remove the comment markers and fill in the two missing expressions so that sumOfTwo is the sum of f1 and f2, and sumOfThree is the sum of f0, f1, and f2.

Part III: Defining Classes (1 point)

The changeNumerator and fracs methods don’t work. Fix them. You may NOT change their signatures. Each fix should require the addition of just one word. These changes may or may not be in the methods themselves.

Part IV: Conditionals and Recursive Functions (1 point)

Rewrite the body of gcd so that it is a recursive function that correctly computes the GCD. Recompile and run your program.

Here are the code changes that are made as shown below:

/* Fraction.java */ import java.io.*; /** The Fraction class implements nonnegative fractions--rational numbers. */ class Fraction { /* private fields within a Fraction. */ private static int numberOfFractions = 0; private int numerator; private int denominator; /** Constructs a Fraction n/d. * @param n is the numerator. Must be nonnegative. * @param d is the denominator. Must be positive. */ public Fraction(int n, int d) { if (n < 0) { System.out.println("Fatal error: Negative numerator."); System.exit(0); } if (d < 1) { System.out.println("Fatal error: Non-positive denominator."); System.exit(0); } numberOfFractions++; this.numerator = n; this.denominator = d; } /** Constructs a Fraction n/1. * @param n is the numerator. Must be nonnegative. */ public Fraction(int n) { this(n, 1); } /** Constructs a Fraction 0/1. */ public Fraction() { this(0,1); } /** Copies the Fraction "original". */ public Fraction(Fraction original) { this(original.numerator,original.denominator); } /** Converts this Fraction to a string format: "numerator/denominator." * Fractions should be printed in reduced form (part of your assignment is * to make this true). * @return a String representation of this Fraction. */ public String toString() { int thisGcd = gcd(numerator, denominator); return (numerator / thisGcd + "/" + denominator / thisGcd); } /** Return the sum of two fractions. * @param f2 is the Fraction to be added. * @return the result of adding f2 to this Fraction. */ public Fraction add(Fraction f2) { Fraction r = new Fraction((numerator * f2.denominator) + (f2.numerator * denominator), denominator * f2.denominator); return r; } /** Replaces this Fraction's numerator with a new value. * @param numerator is the new numerator. Must be nonnegative. */ public void changeNumerator(int numerator) { // DO NOT CHANGE THIS SIGNATURE! // Fix the bug that prevents this method from working correctly. if (numerator < 0) { System.out.println("Fatal error: Negative numerator."); System.exit(0); } this.numerator = numerator; } /** Returns the number of Fraction objects in existence. * @return the number of Fraction objects in existence. */ public static int fracs() { // DO NOT CHANGE THIS SIGNATURE! // Fix the bug that prevents this method from working correctly. return numberOfFractions; } /** Computes the greatest common divisor (gcd) of the two inputs. * @param x must be nonnegative * @param y must be nonnegative * @return the gcd of x and y */ static private int gcd (int x, int y) { /* Replace the following line with your solution. */ if (y == 0) return x; else return gcd(y,x%y); } /** Put the Fraction class through some tests. * @param argv is not used. */ public static void main(String[] argv) { /* Test all four contructors and toString. */ Fraction f0 = new Fraction(); Fraction f1 = new Fraction(3); Fraction f2 = new Fraction(12, 20); Fraction f3 = new Fraction(f2); System.out.println("\nTesting constructors and toString():"); System.out.println("The fraction f0 is " + f0.toString()); System.out.println("The fraction f1 is " + f1); // toString is implicit. System.out.println("The fraction f2 is " + f2); System.out.println("The fraction f3 is " + f3 + ", which should equal f2"); /* Test the add method. */ System.out.println("\nTesting add:"); Fraction sumOfTwo = f1.add(f2); // Sum of f1 and f2. Fraction sumOfThree = f0.add(sumOfTwo); // Sum of f0, f1, and f2. System.out.println("The sum of " + f1 + " and " + f2 + " is " + sumOfTwo); System.out.println("The sum of " + f0 + ", " + f1 + " and " + f2 + " is " + sumOfThree); /* Test the methods used in Part III. */ System.out.println("\nTesting changeNumerator and fracs:"); f3.changeNumerator(7); System.out.println("Now f3 is " + f3 + ", which should be 7/20"); System.out.println("The total number of Fraction objects is " + fracs()); /* Test gcd function (static method). */ System.out.println("\nTesting gcd:"); System.out.println("The gcd of 2 and 10 is: " + gcd(2, 10)); System.out.println("The gcd of 15 and 5 is: " + gcd(15, 5)); System.out.println("The gcd of 24 and 18 is: " + gcd(24, 18)); System.out.println("The gcd of 10 and 10 is: " + gcd(10, 10)); System.out.println("The gcd of 21 and 400 is: " + gcd(21, 400)); } } 
\$\endgroup\$
5
  • \$\begingroup\$I'd argue that you don't get the wrong value for numberOfFractions at all. Nothing is broken.\$\endgroup\$
    – Pimgd
    CommentedSep 19, 2014 at 7:54
  • 4
    \$\begingroup\$You probably haven't learned about exceptions yet. A better way to handle illegal input would be to throw an exception. However, if you wanted to print an error message and exit, then you should 1) Print to System.err instead, and 2) Exit with a non-zero status.\$\endgroup\$CommentedSep 19, 2014 at 17:59
  • 5
    \$\begingroup\$This question appears to be off-topic because it is about solving a puzzle. It cannot be reviewed against best practices properly because that would violate the imposed constraints.\$\endgroup\$CommentedOct 3, 2014 at 11:03
  • 3
    \$\begingroup\$I don't see the artificial constraints as being any different from someone saying "I know the naming is terrible, but it's company standard and I have to do it that way."\$\endgroup\$CommentedOct 3, 2014 at 11:27
  • \$\begingroup\$Has someone started a Meta Question about this?\$\endgroup\$
    – Malachi
    CommentedOct 3, 2014 at 14:07

2 Answers 2

3
\$\begingroup\$

Violation of the rules

The changeNumerator and fracs methods don’t work. Fix them. You may NOT change their signatures. Each fix should require the addition of just one word.

You did change the signatures.

public static int fracs() { // DO NOT CHANGE THIS SIGNATURE! 

Now, in spirit of the course, I'm not going to give you the answer. However, the lines you changed are relevant:

Old Lines:

private int numberOfFractions = 0; public int fracs() { // DO NOT CHANGE THIS SIGNATURE! System.out.println("The total number of Fraction objects is " + f3.fracs()); 

New Lines:

private static int numberOfFractions = 0; public static int fracs() { // DO NOT CHANGE THIS SIGNATURE! System.out.println("The total number of Fraction objects is " + fracs()); 

Since you're only allowed to change one word, only one of these three changes is correct. The rest are not.

\$\endgroup\$
4
  • \$\begingroup\$if i donot change the signature. the solution would be to say, System.out.println("The total number of Fraction objects is " + f0.fracs()+f1.fracs()+f2.fracs()+f3.fracs()+sumOfTwo.fracs()+sumOfThree.fracs()); \$\endgroup\$CommentedSep 20, 2014 at 13:20
  • \$\begingroup\$sorry only one word change would be saying private static int numberOfFractions but making fracs() method as static is better code?\$\endgroup\$CommentedSep 20, 2014 at 13:24
  • 2
    \$\begingroup\$@overexchange I don't think your question is a very good one at all... There's really only one way to do the things they ask (especially when they limit you to one word... that's not programming, that's puzzling). I can't review the changes you've made because the better ways to do it are not allowed by the rules.\$\endgroup\$
    – Pimgd
    CommentedSep 20, 2014 at 23:25
  • \$\begingroup\$Better ways are not allowed because it is yet learn those better ways.\$\endgroup\$CommentedSep 22, 2014 at 8:02
-1
\$\begingroup\$

We just need to add this parameter. since the method is calling f3.fracs() so the total number of fraction will be 1.

return this.numberOfFractions; 
\$\endgroup\$
7
  • \$\begingroup\$numberOfFractions is a static field, and thus cannot be qualified with this., it will fail to compile.\$\endgroup\$
    – rolfl
    CommentedOct 3, 2014 at 11:18
  • \$\begingroup\$@rolfl ---> in the original question the field 'numberOfFractions' is not static.... look at the the link1 and link2 as provided by overexchange, the anwsr ws based on that..... do u have a better solution????\$\endgroup\$
    – Shobhit
    CommentedOct 3, 2014 at 23:24
  • \$\begingroup\$Um, in the original question you have private static int numberOfFractions = 0; which is static.\$\endgroup\$
    – rolfl
    CommentedOct 3, 2014 at 23:26
  • \$\begingroup\$You may be right about link 1 and link 2 (I don't follow links typically), but, in the context of the code being reviewed, your answer will not compile.\$\endgroup\$
    – rolfl
    CommentedOct 3, 2014 at 23:27
  • \$\begingroup\$@rolfl if you read the question properly, in the original question the field 'numberoffFractions' is not static.... the code posted above is the one that has been edited by 'overexchange' and he is not solving what the professor has asked to answer.... Please refer to the first answer by 'Pimgd' u will understand the question. P.S :-- its always better to understand the question before providing a solution.\$\endgroup\$
    – Shobhit
    CommentedOct 5, 2014 at 2:01

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.