0% found this document useful (0 votes)
74 views

Object Oriented Design: Building A Fraction Class: Fundamentals of Computer Science

The document discusses building a Fraction class in Java using object-oriented techniques. It describes creating a Fraction class to represent fractions with numerator and denominator fields. It also covers creating constructors, including a default constructor and overloaded constructors, as well as methods like multiply() that take another Fraction object as a parameter. The document emphasizes reducing repeated code by extracting common logic like reducing fractions to lowest terms into a private helper method.

Uploaded by

chupa ku
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Object Oriented Design: Building A Fraction Class: Fundamentals of Computer Science

The document discusses building a Fraction class in Java using object-oriented techniques. It describes creating a Fraction class to represent fractions with numerator and denominator fields. It also covers creating constructors, including a default constructor and overloaded constructors, as well as methods like multiply() that take another Fraction object as a parameter. The document emphasizes reducing repeated code by extracting common logic like reducing fractions to lowest terms into a private helper method.

Uploaded by

chupa ku
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Object Oriented Design:

Building a Fraction Class

http://www.zazzle.com/fraction+tshirts

Fundamentals of Computer Science


Outline

 Object oriented techniques


 Constructors

 Methods that take another object of same type

 Private helper methods

 Fraction class
 Create a class to represent fractions
Hey objects, where did you come from?
"Dude, don't you know where objects
come from?!? The object stork totally
dropped us off."

0.1, 0.9,
0.5 0.6
r=0.1 r=0.15
0.5, 0.3
r=0.2

public Ball(double x, double y, double r)


{
posX = x;
posY = y;
radius = r;
}
Constructor = the object stork 3
Automatic Default Constructors

 Rule 1: If you do not create a constructor one will


be automatically created for you
 Default no-arg constructor
 Doesn't do anything, no code runs
 All instance variable are whatever you initialized them to (or
their default value)

public class Fraction


{
private int num; // numerator (upstairs)
private int denom; // denominator (downstairs)
}

4
Creating with Default Constructor

 To create object using no-arg constructor


 Use empty ()'s after the new

 Parameter list always sent when new'ing object


 Java needs to know which constructor to run

public class FractionClient


{
public static void main(String [] args)
{
Fraction a = new Fraction();

Fraction [] fracs = new Fraction[2];


fracs[0] = new Fraction();
fracs[1] = new Fraction();
}
}

5
Declaring Your Own Constructor

 Rule 2: If you declare any constructor, a default


one will not be automatically created

public class Fraction


{
private int num; // numerator (upstairs)
private int denom; // denominator (downstairs)

public Fraction(int n, int d)


{
num = n;
denom = d;
}
}

6
Creating with Default Constructor
public class FractionClient
{
public static void main(String [] args)
{
Fraction a = new Fraction();

Fraction [] fracs = new Fraction[2];


fracs[0] = new Fraction();
fracs[1] = new Fraction();
}
}

% javac FractionClient.java
We broke all the calls to create FractionClient.java:5: error: constructor
a Fraction object since there Fraction in class Fraction cannot be
applied to given types;
no longer exists a no-arg Fraction a = new Fraction();
version of the constructor. ^
required: int,int
found: no arguments
reason: actual and formal argument lists
differ in length

7
Constructor Overloading

 Rule 3: You can declare as many constructor


versions as you need/want

public class Fraction


{
private int num; // numerator (upstairs)
private int denom; // denominator (downstairs)
Hooray!
public Fraction()
Now our
{
code using a }
no-arg
constructor public Fraction(int n, int d)
{
will work
num = n;
again. denom = d;
}
}
8
Parameters of Your Own Type
 Create a new object based on another instance of the
same type
 A copy constructor
public class Fraction
{
private int num; // numerator (upstairs)
private int denom; // denominator (downstairs)

// Create a new Fraction object that has the same


// values as some other fraction object.
public Fraction(Fraction other)
{
num = other.num;
denom = other.denom;
}
}

You can access private instance variables


of another object of the same type
inside a method of that type.
9
Multiplying Fractions

 Goal: Given two fraction objects, return a new


fraction that is the multiplication of the two

public class FractionClient


{
public static void main(String [] args)
{
Fraction a = new Fraction(1, 2);
Fraction b = new Fraction(2, 3);

Fraction c = a.multiply(b);

System.out.println(a + " * " + b + " = " + c);


}
} % java FractionClient
1/2 * 2/3 = 1/3
10
Multiply Method
public class Fraction
{
private int num; // numerator (upstairs)
private int denom; // denominator (downstairs)

...

public Fraction multiply(Fraction other)


{
Fraction result = new Fraction(num * other.num,
denom * other.denom);
return result;
}
}

Denominator of the object that Denominator of the object


called multiply (before the dot). passed as a parameter to
the multiply() method
Fraction c = a.multiply(b);
11
Multiplying Fractions

 Attempt 1: Hmmmm, we forgot something…

public class FractionClient


{
public static void main(String [] args)
{
Fraction a = new Fraction(1, 2);
Fraction b = new Fraction(2, 3);

Fraction c = a.multiply(b);

System.out.println(a + " * " + b + " = " + c);


}
}
% java FractionClient
Fraction@164f1d0d * Fraction@23fc4bec = Fraction@8dc8569

12
Multiplying Fractions

 Attempt 2: Close, but not in lowest terms…

public class Fraction


{
private int num; // numerator (upstairs)
private int denom; // denominator (downstairs)

... An object's toString() method is


called automatically whenever you
public String toString() attempt to print an object
{
return "" + num + "/" + denom;
}
}
% java FractionClient
1/2 * 2/3 = 2/6

13
Lowest Terms
 Attempt 3: Add code to reduce to lowest terms
public class Fraction
{
...
public Fraction multiply(Fraction other)
{
Fraction result = new Fraction(num * other.num,
denom * other.denom);
int i = Math.min(Math.abs(result.num),
Math.abs(result.denom));
if (i == 0)
return result;
while ((result.num % i != 0) || (result.denom % i != 0))
i--;
Fraction result2 = new Fraction(result.num / i,
result.denom / i);
return result2;
}
% java FractionClient
}
1/2 * 2/3 = 1/3

14
Divide Method

 Very similar method for division:


public class Fraction
{
...
public Fraction divide(Fraction other)
{
Fraction result = new Fraction(num * other.denom,
denom * other.num);
int i = Math.min(Math.abs(result.num),
Math.abs(result.denom));
if (i == 0)
return result;
while ((result.num % i != 0) || (result.denom % i != 0))
i--;
Fraction result2 = new Fraction(result.num / i,
result.denom / i);
return result2;
}
}
15
Repeated Code is Evil™
public Fraction multiply(Fraction other)
{
Fraction result = new Fraction(num * other.num,
denom * other.denom);
int i = Math.min(Math.abs(result.num),
Math.abs(result.denom));
if (i == 0)
return result;
while ((result.num % i != 0) || (result.denom % i != 0))
i--;
Fraction result2 = new Fraction(result.num / i,
result.denom / i);
return result2;
}

Where should this code really live? There are a


number of choices, but not here for sure.
We'd have to repeat it in the divide(), add(),
and subtract() methods as well.
16
Helper Methods
 Add a private helper method, reduce()
public class Fraction
{
Because it is a private method, can only be called
private void reduce() inside other methods in the Fraction class
{
int i = Math.min(Math.abs(num), Math.abs(denom));
if (i == 0)
return;
while ((num % i != 0) || (denom % i != 0))
i--;
num = num / i;
denom = denom / i;
}
public Fraction multiply(Fraction other)
{
Fraction result = new Fraction(num * other.num,
denom * other.denom);
result.reduce();
return result;
}
} 17
Fill in the Missing Code
public class Fraction
{
public Fraction multiply(Fraction other)
{
Fraction result = new Fraction(num * other.num,
denom * other.denom);
result.reduce();
return result;
}

public boolean equals(Fraction other)


{
}

public Fraction reciprocal()


{
}

public Fraction add(Fraction other)


{
}

public Fraction subtract(Fraction other)


{
}
} 18
Summary

 Objects
 No-arg default constructors

 Passing objects of same type to method

 Private helper methods

 Fraction object
 Built an object to represent a fraction

You might also like