Espresso: A Concentrated Introduction to Java


Laboratory: Designing Your Own Classes

Summary: In this laboratory, you will explore the concepts involved in writing your own classes in Java.

Primary Classes Used:

Source Files:

Contents

Exercises

Exercise (i): Designing a Fraction Class

Consider a class that we might write to represent fractions.

What fields might we use to store a fraction?

What operations might we like a fraction object to be able to perform? In other words, what might we want to do with a fraction in a program?

What parameters might we like to be able to pass to a constructor of fraction objects? In other words, if we want to construct a new fraction in a program, what sort of values might we like to construct it from?

Exercise 0: Preparation

a. Start Eclipse, and create a new package within the Code project named username.fractions.

b. Copy the following files into the directory /home/username/CSC152/Eclipse/Code/username/fractions:

c. Refresh the package, so that Eclipse will recognize the files.

d. Update the source files so they will run in your package.

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

f. Read through them to understand how they work.

Exercise 1: Multiplication

a. Add an object method multiply to the Fraction class that 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.

Try to do this without converting the values stored in Fraction.numerator and Fraction.denominator to int data types. Note that the purpose of using BigIntegers to store the fields in this class is to allow them to be very very large. If a method in the class converts them to ints during a computation, we essentially lose this property.

For example,

Fraction f = new Fraction(11,3);
pen.println(f.fractional());
   // Prints 2/3
f = new Fraction(1,2);
pen.println(f.fractional());
   // Prints 1/2
f = new Fraction(4,2);
pen.println(f.fractional());
   // 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");
pen.println(f.doubleValue());
   // Prints 0.25
f = new Fraction("120/3");
pen.println(f.doubleValue());
   // 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 and test a main class, Calculator, that reads in two fractions and prints out their sum and product in both fractional and decimal form.

Exercise 5: A Counter Class

a. 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.

b. Test your class. 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 resets the counter to the starting value.

c. If you haven't done so already, test both of these 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.


Written and revised by Samuel A. Rebelsky, 2005-2006.
Revised further by Marge M. Coahran, 2006-2007.
Samuel A. Rebelsky
rebelsky@grinnell.edu
--->