Homework 2

CSC 390

First Summer Session, 1997

Assigned June 11, due June 18, postponed to June 19

Write a desk calculator program (either an Applet or a Frame-based application) which performs exact fraction arithmetic. The user should be able to enter a fraction, either in a TextField as two integers with a slash in between them or in two separate TextFields, one for the numerator and one for the denominator (your choice). There should be clearly labelled buttons for adding, subtracting, multiplying, and dividing, and either an "=" button or an "enter" button (see below). You may choose to provide other buttons like "clear", "swap" (if using RPN), "1/x", "x2", etc.

You may choose whether to provide an RPN user interface (like an HP calculator: you type in a number, press the "enter" button, type another number, and then press a button for one of the operations to see the answer) or an infix user interface (you type in a number, press a button for one of the operations, type another number, and press the "=" button to see the answer). In either case, the answer should appear in the same TextField(s) that the user's input did.

You are expected to write a class named "Fraction" (perhaps a subclass of "Number") that stores the numerator and denominator of a fraction, and implements methods to add, subtract, multiply, and divide itself by another fraction, returning a new Fraction object. It will also need a method which reduces the Fraction to lowest terms, either returning a new Fraction object or modifying itself. Also implement whatever constructors, toString methods, etc. you think appropriate.

Note: Your "Fraction" class should not be the program itself. A fraction is not a program. A fraction is not a kind of Applet, or Frame, or anything like that; a fraction is a mathematical object with a numerator, a denominator, and certain operations. Your fraction class should concern itself only with mathematical operations, not user-interface issues.

Your program will be graded on correctness, elegance and maintainability of object-oriented design, user-friendliness, and efficiency, in that order. A crude calculator program is provided in the Core Java book on pages 268-269, and you may get some ideas from this, but your program will be quite different in both appearance and operation.

For extra credit, add a square-root key. Note that the square root of an integer fraction may not be an integer fraction: for example, the square root of 5/12 could only be written as sqrt(5)/(2 sqrt(3)), or perhaps (rationalizing the denominator) as sqrt(15) / 6. You'll need to decide how to represent roots, both on the screen and inside the computer; this may require inventing another class named Root or something like that, to be used inside Fraction.