# Homework 1: A Fraction Class

• Assigned: Wednesday, 30 August 2000
• Due: 5pm, Tuesday, 5 September 2000

Summary: For this assignment, you will write a Java class so as to familiarize yourself with the components and techniques for writing Java programs. You may not have learned all the information for this assignment by the time it was assigned, you will have learned it well in advance of the due date for the assignment.

Group work: You should each write your own classes. However, you may discuss the assignment with each other and help each other correct problems.

Turning it in: Leave your code in a publically readable directory, and email me the location of that directory. Here's some sample Unix code for setting up the directory.

```% mkdir CS152.HW1
% mv Fraction.java CS152.HW1
% mv TestFraction.java CS152.HW1
% share CS152.HW1
% cs CS152.HW1
% share Fraction.java
% share TestFraction.java
```

## Assignment

In class, we began to consider a simple `Faction` class that implements simple fractions, with a numerator and denominator. Finish writing the basic class (we may add more facilities in the next assignment). You need not do anything special with nonpositive denominators.

Your `Fraction` class should provide the following constructors

• `Fraction()` - create a new fraction with a reasonable default value.
• `Fraction(int num,int denom)` - create a new fraction with numerator `num` and denominator `denom`.
• `Fraction(int val)` - create a new fraction with value `val` (that is, numerator `val` and denominator `1`).

Your `Fraction` class should provide the following methods

• `void setValue(int num, int demon)` - set the value of the fraction.
• `void setValue(Fraction f)` - set the value of the fraction.
• `int getNumerator()` - get the numerator.
• `int getDenominator()` - get the denominator.
• `float toFloat()` - convert the fraction to a floating point number.
• `String toString()` - convert the fraction to a string of the form `num/denom`.
• `Fraction add(Fraction f)` - add a fraction to the current fraction. This should not modify the value of the current fraction.
• `Fraction multiply(Fraction f)` - multiply the current fraction by another fraction. This should not modify the value of the current fraction.
• `Fraction subtract(Fraction f)` - subtract a fraction from the current fraction. This should not modify the value of the current fraction.
• `Fraction divide(Fraction f)` - divide the value of the current fraction by f. This should not modify the value of the current fraction.
• `Fraction add(int i)` - an an integer to the current fraction This should not modify the value of the current fraction.

Once you have completed your basic class, create a class, `TestFraction` that provides a `main` routine that appropriate tests your routines. Here are some things that you might test:

• What happens with compound operations, such as
`f.Add(g).multiply(h)`?

## Extra Credit

Be creative in your choice of additional facilities for the Fraction class.

## History

Wednesday, 30 August 2000

• Created, based primarily on an old assignment from CSC152 97F.

Friday, 1 September 2000

• Removed some parts of the assignment.

Monday, 4 September 2000

• Fixed lots of typos (thanks Laurelin!)
• Added instructions for setting up the directory.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2000F/Homeworks/hw.01.html

Source text last modified Mon Sep 4 09:38:09 2000.

This page generated on Mon Sep 4 09:37:32 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu