Assignment Rational Numbers Solution



ArrayLists, Objects, Classes and Methods


For this assignment you will need to write two classes. The first class will be

named A rational number is a fraction, it has a numerator and a denominator. The details of the Rational class are below. The

second class will be named This class will contain the main method. The details of the RationalDriver class are also below.

  • Default value constructor that sets the numerator to 1 and the denominator to 1

  • Explicit value constructor that sets the numerator and denominator to values specified by the user

  • Get Methods

  • Set Methods

  • A toString Method

  • An equals Method

  • An add Method

  • A subtract Method

  • A reduce method (used internally)

  • Data variables appropriate to a rational class

The RationalDriver class will create and manipulate objects of type ‘Rational’. The class will contain the main method and will accommodate the following:

  • An ArrayList of rational objects.

  • Code to load the ArrayList with Rationals. Values are: 2/3, 2/18, 3/12, 9/3, 2/5, and 22/7

(note you must load the ArrayList with enlarged fractions like 2/18 and your program will reduce the fraction during object construction.)

  • After loading the ArrayList of Rational number objects, a menu is continually displayed.

The menu will have the following options:

    1. Display the value of a Rational object. Prompt for the index.

Eastern Washington University

CSCD211 – Programming Principles II

  1. Change the value of a rational. Prompt for the index of the Rational to be changed, and then the new denominator and new numerator. (You may wish to create a new Rational object to accomplish this.)

  1. Add two Rational objects together and display the sum as a new Rational object. (Prompt for two index values that indicate the two Rationals to be added.)

  1. Subtract two Rational objects and display the difference as a new Rational object. (Prompt for two index values that indicate the two Rationals. The second will be subtracted from the first.)

  1. Sort the array of Rational number objects. Use a sort method from the Java API

  1. Determine if a particular Rational object is in the ArrayList

  2. Print the array of Rational number objects to the screen.

  3. Quit

Note: all rational numbers will be in their reduced form. If the user enters 2/4 the rational number that will be displayed is 1/2.

You are guaranteed that an integer will be entered for the numerator and the denominator. A rational number can be negative.


  • Solve the problems systematically testing as you go

  • You should be able to write the get, set and default value constructor.

  • As we cover topics in class you should make the appropriate changes to your code.

TO TURN IN (via the Canvas system)

  • A zip file that contains both .java files necessary to run your program.

Eastern Washington University

error: Content is protected !!