Computer Science 15-110, Spring 2010
Recitation for Week #9
===================================================================
In class this week we are starting to write our own classes.
The first class we are writing is a simple Fraction class. We would like the following code to work:
// We want this, but can't have it (darn!) public static void main(String[] args) { Fraction f1 = 2/3; Fraction f2 = 3/9; Fraction f3 = f1*f2; System.out.println(f1); System.out.println(f2); System.out.println(f3); }
Unfortunately, we cannot overload the / and * operators highlighted above to work this way (you can in some other languages, but not in Java).
So instead we'll settle on this version, which is similar but written in a way that we actually can support:
// Compare this to the version above public static void main(String[] args) { Fraction f1 = new Fraction(2,3); Fraction f2 = new Fraction(3,9); Fraction f3 = f1.times(f2); System.out.println(f1); System.out.println(f2); System.out.println(f3); }
And here is the code we wrote in class (warts and all) to do it:
===================================================================
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem