CS 161 Homework 4 - Rational and Mixed Numbers

Due Thu Oct 03 at 9:00am

Overview

Lots of young math students struggle when doing math with fractions. For this assignment, you'll help them out by making two simple calculator programs:

  1. A Rational Adder program that adds 3 different rational numbers (a rational number is a number with a numerator and a denominator: either a proper or improper fraction):
    Enter first rational: 1/2
    Enter second rational: 17/9
    Enter third rational: -3/4
    1/2 + 17/9 + -3/4 = 118/72 (1 46/72)

    Note that I typed in the rational numbers.

  2. A Mixed Multiplier program that multiplies mixed numbers (a mixed number is a proper fraction with a whole number part):
    Enter first mixed: 1 1/2
    Enter second mixed: 3 2/5
    1 1/2 * 3 2/5 = 5 1/10

Notice in the above examples that you DO NOT need to reduce or simplify the fractions in any way (we'll talk about how to do that later)!

This assignment should be completed individually.

Objectives

Necessary Files

You will need a copy of the Hwk4.zip file. The BlueJ project in this file includes a provided RationalTester class that can use to help test some of your code as you are working (but you'll need to make any testers you want for the Mixed class!). The project also contains the README.txt that you will need to fill out and turn in along with your assignment.

Remember to extract the files from the zip folder before proceeding!

Assignment Details

For this assignment you will be making 4 classes: Rational, Mixed, RationalAdder, and MixedMultiplier. The first two of these are "object classes"--classes that model objects we will instantiate (with instance variables, constructors, etc). The second two are "main classes"--classes that have a main method and will process user input. Each class is described in more detail below.

As you write your classes, think about how you can use the various templates and recipes we've talked about in class.

Rational

Rational represents a rational number, which is a number that has two integer parts: a numerator and a denominator. A rational number expresses the ratio of the numerator to the denominator. For example, the rational number 1/2 expresses the ratio of 1 to 2, while the rational number 3/2 expresses the ration of 3 to 2. You can think of these as improper fractions, but you will need to represent them as two numbers (the numerator and the denominator), rather than as a decimal number.

  1. Think about what attributes a Rational has. What instance variables will you need to declare? What data type will these instance variables have?
  2. You will need to create one constructor for the Rational class that takes in parameters and assigns them to the attributes.
  3. You will also need to create a second constuctor for the Rational class. This constructor should take a single String as a parameter, representing the String version of a Rational (for example "1/2").
    • For this constructor, you will need to extract the numerator and denominator from the String parameter and store them in the instance variables. For example, if the value in the parameter is "7/10", you will need to break this apart into "7" and "10" Strings, convert those Strings into numbers, and then store the numbers as attributes.
    • Remember that you can use the .substring() method to fetch a piece of a String, the .indexOf() method to figure out the index of a particular character (such as the '/' in the fraction), and the Integer.parseInt() method to convert a String into an integer. If you have trouble with any of these methods, please check in with me!
      • Optional: it is also possible to use a Scanner built on a String (using the String as the parameter to the constructor) and nextInt() to get a number from a String!
  4. Your class should have a getter and setter for each attribute (this will let you practice writing these methods!)
  5. A Rational will need a toString() method so that you can easily print it out. Remember, a toString() method does not actually print anything, it just returns a String that can be printed elsewhere!
  6. A Rational will also an add() method. This method should take in another Rational as an argument, and return a new Rational as the result. For example, if I make a Rational object that represents 3/4 and another object that represents 1/2, I should be able to add them together to produce a third object that represents 5/4. This way I can get the result, but still have my 3/4 and 1/2 objects to do more math with!
    • Remember how parameters are just variables, and variables can be Object types? The same thing is happening here: the "variable" of our parameter will be a Rational-shaped box, and we will put inside it a Rational object. Similarly, we can make a new Rational object (using the constructor appropriate setters) and then return that object, the exact same way we would return an int or a String.
    • Think about how you add rational numbers without a computer: find a common denominator, multiply the numerators by the appropriate value, and then adding those numbers. You do NOT need to find the lowest common denominator--it is fine and expected to have non-reduced fractions!
  7. Similarly, your Rational will need to have a multiply() method. Like add(), this method should take in another Rational as an argument, and return a new Rational as the result.
    • Again, think about how you calculate the product of two fractions (hint: cross-multiply), and write that into code!
  8. Finally, a Rational will need a toMixed() method that returns a new Mixed object that represents the same value. This will let you easily switch between Rationals and Mixeds. IMPORTANT HINT: Implement most of the Mixed class before you do this!!
    • You'll need to calculate the whole number, new numerator, and new denominator for your new Mixed object. In order to figure out the whole number part, you can use integer division (or the Math.floor() function)--this will literally give you the whole number part of the resulting quotient.
    • In order to figure out the remainder of the whole number division (that will become the new numerator), you can subtract from the numerator in a manner similar to how we calculated change. Alternatively, you can use the modulo operator % to DIRECTLY get the remainder of the division! If you have questions on this, please ask! This is slicker ;)

Mixed

Mixed represents a mixed number: the combination of a whole number and a proper fraction--a rational number where the numerator is smaller than the denominator. For example, the rational number 3/2 can be expressed as the mixed number 1 1/2, while the rational number 1/2 can be expressed as the mixed number 0 1/2.

  1. Think about what attributes a Mixed has. What instance variables will you need to declare?
  2. You will need to create one constructor for the Mixed class that takes in parameters and assigns them to the attributes.
  3. Like with the Rational class, you will also need to create a second constuctor for the Mixed class. This constructor should take a single String as a parameter, representing the String version of a Mixed (for example "3 1/4").
    • This constructor will work similar to the one in the Rational class--you'll need to split the String parameter apart, convert the Strings into numbers, and then store them in instance variables. Look at your code for the Rational class as a place to start.
    • Hint: You can find the location of a space (a ' ' character) just as easily as a '/' character!
  4. A Mixed will need a toString() method so that you can easily print it out. Remember, a toString() method does not actually print anything, it just returns a String that can be printed elsewhere!
  5. A Mixed will need a toRational() method that returns a new Rational object that represents the same value. This will let you easily switch between Mixeds and Rationals. IMPORTANT HINT: Implement most of the Rational class before you do this!!
    • This method will be like Rational's .toMixed() method, except you need to calculate the new numerator and denominator.
  6. A Mixed will need an add() method. This method should take in another Mixed as an argument, and return a new Mixed as the result (the same way that the Rational's add() method works).
    • If you think about it, you'll quickly discover that adding Mixed numbers together is not as straight forward as working with fractions (example: how do you easily add 1 2/3 + 2 3/4?). How do we normally add these numbers? Well we convert them into fractions (e.g., 5/3 and 11/4) and then add those. This is exactly how we will solve the problem here!
    • This method should call upon the toRational() method (described below) to get a Rational number with the same value as itself, as well as a Rational number version of the parameter. You can then add those Rational numbers (using their add() method), get the sum, and change that sum BACK into a Mixed number to return!
      • This technique demonstrates the usefulness of being able to switch between data types. It's why we created the Rational class!
  7. A Mixed will also need an multiply() method. This method should take in another Mixed as an argument, and return a new Mixed as the result. Again, you can use the same technique as your add() method to easily do this math.

RationalAdder

This main class (containing a main() method) runs a program that allows the user to add Rational numbers. It should prompt the user for three (3) Rationals, add them together, and then print out both the Rational and Mixed versions of the sum. See above for example output.

MixedMultiplier

This main class (containing a main() method) runs a program that allows the user to multiply Mixed numbers. It should prompt the user for two (2) Rationals, multiply them together, and then print out both the Mixed version of the product. See above for example output.

Helpful Advice: On Testers

I have provided a (basic) tester in the form of RationalTester. This can help you make sure your Rational class is working correctly, without needing to click around in BlueJ a lot. You are welcome to use this class or not as you see fit--you are not required to use it!

However, I hope this illustrates how handy testers are, and if you have time I encourage you to extend the tester and/or create one for Mixed numbers. As programmers, we often need to produce "extra" code that doesn't get used in order to make sure that things works. This is like how you need to build extra scaffolding when you're constructing a building, even if that buttressing ends up being removed from the final product.

If you make any testers, include them in your final program submission! I'd love to see what you did :)

Extensions

If you finish early (hah!), these extensions will help you to achieve enlightment (and better prepare you for the exam!) You can earn up to 5 points of extra credit for extensions on this assignment. Be sure to finish the rest of your program first!

  1. Add functionality so that the user can type in the entire equation on one line (e.g., "3/4 + 1/2"). You can use the Scanner and/or String methods to pull the numbers out of this String. Note that you will need to be careful about whether to use spaces before or after the + sign (the operation). Be sure to prompt the user with the proper format!
  2. Add other mathematical operations to the Rational and Mixed classes (subtraction, division). Subtraction is very similar to addition, while division is like multiplication but you invert the numerator and denominator (can you re-use your multiplication method?). You may want to create an appropriate calculating class (e.g., RationalSubtracter), which will work very similarly to your RationalAdder--you may be able to copy code between them.

Submitting Your Assignment

Grading

This assignment will be graded on approximately the following criteria: