Espresso: A Concentrated Introduction to Java

Laboratory: Designing Your Own Classes

Summary: In this laboratory, you will extend your knowledge of numeric values in Java.

Primary Classes Used:

Source Files:



Exercise 0: Preparation

In this lab, you will continue to use the Code project.

a. Create a new package for this lab, called username.fractions. Note that you should use your own username in place of username.

b. Make copies of and

c. Update those files to use your package.

d. Compile and run them to verify that they work.

Exercise 1: Multiplication

a. Extend the Fraction class so that it permits multiplication of two fractions.

b. Test your code.

Exercise 2: Fractional Portions

As you may know, we can represent every non-negative rational number as a whole number plus a fractional value no smaller than 0 and strictly less than 1.

a. Write a method of the Fraction class, fractional, that identifies and returns this fractional value as a Fraction. Your procedure need only work for positive numbers.

For example,

Fraction f = new Fraction(11,3);
   // Prints 2/3
f = new Fraction(1,2);
   // Prints 1/2
f = new Fraction(4,2);
   // Prints 0/2 or something similar

b. Test your procedure.

Exercise 3: From String to Fraction

Write and test a third constructor for the Fraction class. This constructor should accept a string as a parameter, parse that string, and generate the appropriate fraction. For example,

Fraction f = new Fraction("1/4");
   // Prints 0.25
f = new Fraction("120/3");
   // Prints 40.0

You can expect that the string will have two positive integers separated by a slash. You may find it useful to reflect on the indexOf method of the java.lang.String class and on various methods of the java.lang.Integer class.

Exercise 4: A Simple Calculator

Write a main class that reads in two fractions and prints out their sum and product in both fractional and decimal form.

Exercise 5: A Counter Class

Write and test a class, Counter, that generates objects that can count. Objects in class Counter should provide two methods: increment, which adds 1 to the counter, and get, which gets the current value of the counter.

Make sure to verify that if you create two separate objects in class Counter, you can change the two objects separately.

Exercise 6: Extending Counters

a. Update your Counter class to include a second constructor that Allows the user to specify a starting value.

b. Update your Counter class to include a reset method that reset the counter to the starting value.

c. Test both updates.

For Those With Extra Time

Extra 1: Further Extending Counters

Identify other methods that would be useful to include in the Counter class and add them.

Extra 2: Further Extending Fractions

Identify other methods that would be useful to include in the Fraction class and add them.


Friday, 11 February 2005 [Samuel A. Rebelsky]

Wednesday, 22 February 2006 [Samuel A. Rebelsky]

This page was generated by Siteweaver on Thu Mar 30 15:24:35 2006.
The source to the page was last modified on Wed Feb 22 08:35:58 2006.
This page may be found at

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky