CS 1112 Exercise 10

You have until Wednesday, April 28, 11:00pm EDT to submit the two class definitions on Matlab Grader.

Download the files Interval.m and Fraction.m from the Exercises page. Implement the classes using the full Matlab environment. After completing each problem using Matlab, then copy and paste the code into Matlab Grader for further testing and for submission.

Class Interval

1.1 Download the file Interval.m from the Exercises page. Let’s play with some Interval objects in the Command Window:

format compact          % Set Command Window to single spacing
a= Interval(3,7)        % See in Workspace pane that the class of a is Interval. Read
                        % Interval.m to see how properties were declared.
disp(a.left)            % Access the left property using dot notation; should be 3
disp(a.right - a.left)  % Should be 4, the interval’s width
a.shift(10)             % Call a’s shift method to shift interval a to the right by 10
                        % units. Method shift doesn’t return a value (see method
                        % definition in Interval.m), so you do not see anything displayed
                        % in Command Window
disp(a)                 % Display interval a now: ________________________
b= Interval(9,15);
g= a.isIn(b)            % Is interval a in interval b? ____________
                        % Read method isIn. Ask if you have any questions.
h= b.isIn(a)            % Is interval b in interval a? ____________

Observations: To access an instance variable (property), the syntax is ReferenceName.VariableName. To access an instance method (method defined inside a classdef for each object), the syntax is ReferenceName.MethodName(args for 2nd through last parameters).

1.2 Implement method getWidth() in class Interval as specified. Then try the following in the Command Window:

a= Interval(3,7);
w= a.getWidth()    % w should be 4

1.3 Read method scale() in class Interval. Revise scale() to make effective use of method getWidth(). Next try the following in the Command Window:

a.scale(2)         % Note that method scale does not return anything
disp(a)            % a should be (3,11)

1.4 Implement method add() in class Interval as specified. In interval arithmetic, the sum’s left end is the sum of the two original left ends; the sum’s right end is the sum of the two original right ends. Next try it out in the Command Window:

b= Interval(0,2);
c= a.add(b)        % c should be (3,13)

Do you understand everything so far? If not, ask for help!

Copy the contents of your completed file Interval.m into the code box for Problem 1 in Matlab Grader. Test (and correct if necessary) your class definition.

Class Fraction

Download the file Fraction.m from the Exercises page; it is an incomplete class definition. Read it, experiment with it, and implement the incomplete methods. Here’re the specific things to note and do:

2.1 Read the class comment carefully. In our simple Fraction class we simply assume that the numerator and denominator are integers—we do not check for this. A Fraction does not need to be in the reduced form, i.e., 16/6 is fine and does not need to be reduced to 8/3. A negative fraction should have the negative sign associated with the numerator, not denominator. This and other requirements of our Fraction class are taken care of already in the constructor. Read it carefully.

2.2 Read the given method isLessThan() in the classdef. Do you understand it? Now experiment in the Command Window!

a= Fraction(3,4)
b= Fraction(3,6)
a.isLessThan(b)   % True or false? _______
b.isLessThan(a)   % True or false? _______

2.3 Complete method isEqualTo(). Save the file; then create some Fractions and call the isEqualTo() method in the Command Window for testing! For example,

a= Fraction(3,4)
b= Fraction(3,6)
c= Fraction(1,2)
a.isEqualTo(b)    % True or false? _______
b.isEqualTo(c)    % True or false? _______

2.4 Complete method add(). Again there is no need to reduce the fraction. Next try these statements in the Command Window:

a= Fraction(3,4)
b= Fraction(3,6)
c= a.add(b)       % What is fraction c? Is it correct?

2.5 Complete method toDouble() and then try these statements in the Command Window:

a= Fraction(3,4)
x= a.toDouble()   % Call a's toDouble method. Should be 0.75

2.6 Complete method reduce(). You can use any algorithm you like for calculating the GCD, Greatest Common Divisor, but here’s Euclid’s algorithm for finding the GCD between two positive values a and b where ab:

  1. Calculate the remainder r from b divided by a.
  2. If r is zero than a is the GCD.
  3. Otherwise, let b get a and a get r. Repeat from Step 1.

Note that if the numerator is zero, Inf, or NaN (Not a Number, e.g., resulting from 0/0) then “reducing” the fraction simply requires setting the denominator to 1—do not try to calculate the GCD. To check whether a variable x has the value Inf, use the function isinf(): isinf(x) returns true if x is Inf and false otherwise. Similarly, isnan(x) returns true if x is NaN and false otherwise.

After completing method reduce, try these statements (and more) in the Command Window:

a= Fraction(8,6)
a.reduce()           % Call a's reduce method. Since this method doesn't return
                     % anything, nothing is displayed to the command window.
disp(a)              % Is it correct?
a= Fraction(8,2)
a.reduce(); disp(a)  % Is it correct?
a= Fraction(1,3)
a.reduce(); disp(a)  % Numerator and denominator should remain the same
a= Fraction(0,9)
a.reduce(); disp(a)  % Numerator and denominator should remain the same
a= Fraction(9,0)
a.reduce(); disp(a)  % Numerator and denominator should remain the same (Inf and
                     % 1 as originally set by the constructor)
Copy the contents of your completed file Fraction.m into the code box for Problem 2 in Matlab Grader. Test (and correct if necessary) your class definition. Observe from both Problems 1 and 2 that every method that is newly implemented gets tested. Furthermore, multiple test cases are selected to test each method comprehensively—each test targets a different scenario of input in order to determine a method’s correctness.

2.7 Now that you have a correct reduce() method, it would be nice to call reduce() whenever we create a Fraction! Back in the full Matlab environment, read the constructor again and now uncomment the last statement so that method reduce() is called whenever a Fraction is created. Next test the updated constructor using the following code in the Command Window:

a= Fraction(8,6)  % Fraction has the numerator 4 and denominator 3

Do not uncomment the call to method reduce() in the constructor in your submission on Matlab Grader! For the purpose of testing the other methods in Matlab Grader, method reduce() should not be called in the constructor.