Fundamentals of Computer Science II (CSC-152 97F)

[Front Door] [News] [Basics] [Syllabus] [Outlines] [Assignments] [Bailey Docs]

# Assignment 1: Your first classes

• Assigned: Wednesday, 3 September 1997
• Due: 5pm, Wednesday, 10 September 1997

Summary: For this assignment, you will write a few Java classes 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.

Turning it in: Leave your code in a publically readable directory, and email me the location of that directory. Note that your directory should include documentation generated by `javadoc`. If this doesn't make sense to you, let me know.

## A Fraction Class

Develop a simple `Fraction` class that implements simple fractions, with a numerator and denominator.

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 constructors

• `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 getValue()` - 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 faction.
• `Fraction multiply(Fraction f)` - multiply the current fraction by another fraction. This should not modify the value of the current faction.
• `Fraction subtract(Fraction f)` - subtract a fraction from the current fraction. This should not modify the value of the current faction.
• `Fraction divide(Fraction f)` - divide the value of the current fraction by f. This should not modify the value of the current faction.
• `Fraction add(int i)` - an an integer to the current fraction This should not modify the value of the current faction.
• `boolean equal(Fraction f)` - returns true if f is the same as the current fraction, and false otherwise.
• `boolean smaller(Fraction f)` - returns true if the current fraction is smaller than f, and false otherwise.

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)`?
• If `f` is `2/4` and `g` is `1/2`, what is the result of `f.equal(g)`?

## Extra Credit

Make sure that your fractions are always irreducible. For example, the following code should print "`1/2`".

```Fraction f = new Fraction(2,4);
System.out.println(f.toString());
```

Add any other routines that you consider appropriate for a `Fraction` class.

[Front Door] [News] [Basics] [Syllabus] [Outlines] [Assignments] [Bailey Docs]

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.

Source text written by Samuel A. Rebelsky.

Source text last modified Mon Sep 8 10:58:30 1997.

This page generated on Wed Sep 10 08:48:23 1997 by SamR's Site Suite.

Contact our webmaster at rebelsky@math.grin.edu