User Tools

Site Tools


cs_176:fraction_add_instance_method

Fraction class instance add method

  • Use the original Fraction class example as a starting point.
    • In the original Fraction class example, the addFraction and subtractFraction methods were static methods in TestFraction.
    • In the Fraction2 BlueJ project, make the addFraction and subtractFraction methods instance methods of the Fraction class.
  • This is the new way we want to be able to work with Fraction Class objects:
            //
            // Code that goes in TestFraction.java:
            //
            Fraction f1 = new Fraction( 5, 8 );
            Fraction f2 = new Fraction( 1, 9 );
            Fraction f3 = new Fraction();
            
            // This time, addFraction is an instance method of the Fraction
            // class, so we use the object f3's addFraction() method, i.e.,
            // f3.addFraction().
            f3.addFraction( f1, f2 ); // f3 holds the result of 5/8 + 1/9
            
            System.out.println( f1 + " + " + f2 + " = " + f3);
cs_176/fraction_add_instance_method.txt · Last modified: 2015/03/06 14:58 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki