Hands On Java: Project 6



1. Practice designing, declaring and using classes.
2. Practice defining attributes.
3. Practice defining methods.


Each of these projects deals with objects that are not readily modeled using the predefined Java data types. In each project, you are to build a class to represent the object described, as well as the operations appropriate for such an object. Your instructor will tell you which one of the following projects to do.


6.1. Extend class Fraction by adding methods for the arithmetic operators +, - and /, and the six relational operators (==, !=, <, >, <= and >=). Then write code that tests your new methods.

For extra credit, write a method for inverting a Fraction (i.e., if fract == n/d, its inversion is d/n).

6.2. A quadratic equation has the form

   ax^2 + bx + c = 0

where a, b, and c are all real values. Write a class Quadratic that can be used to model a quadratic equation, with operations to construct, input, output, access the attributes, evaluate (for a given value of x), and find the x value at which the value of the Quadratic is minimized (or maximized).

To test your class, write a program that allows a user to enter a quadratic; computes and displays the maximum and minimum; and then allows the user to evaluate it at some input value.

6.3. A phone number consists of four separate pieces of information: an area code, an exchange, a local number, and a long-distance indicator (true or false). Design and build a PhoneNumber class that models a phone number, providing operations to construct, input, output, access each of the attributes of a PhoneNumber object, and indicate whether or not the number is long-distance. The input operation should read a local or long-distance number and set the long-distance indicator accordingly. The output operation should display a local number differently from the way it displays a long-distance number (e.g., 555-1234 vs. (616) 555-1234).

To test your class, write a program that simulates an intelligent computer modem dialer by reading a PhoneNumber, and displaying the number to be dialed. If the number is a long distance number, it should be preceded by 1-; otherwise it should be displayed as a local number.

6.4. A dual stop watch can time two runners simultaneously. It has one button to start the timer and two buttons to stop the timer (one for each runner.) It will have three attributes for keeping track of the time: a start time and for each runner a stop time. It will also have two attributes that indicate the state (running or not running) of the stop watch for each runner. Design and build a DualStopWatch class that models a stop watch. Your class should provide operations to construct, start timing, stop the timer for either runner, and display the time for either runner. If the stop watch is running, you should display the current elapsed time. If the watch is not running you should display the final elapsed time.

Hint: You can get the current system time in milliseconds using the class java.util.Date via the code:

   long current = (new Date()).getTime();

To test your classes, write a program that starts the watch and prompts the user to stop it. Make sure to check all the operations of the stop watch.

Turn In: A hard copy of this grade sheet, attached to hard copies of

  1. all source files you created for this project; and
  2. the output from an execution of your executable program.

Don't forget to clean up your directory when you are all finished...

Back to the Lab Table of Contents

Back to the Lab Exercise

Back to the Table of Contents

Back to the Introduction

Copyright 2000 by Prentice Hall. All rights reserved.