CS 161 Lab C - Rational Number Calculator

Due Thurs Feb 07 at 11:59pm

Overview

Lots of young math students struggle when doing math with fractions. For this assignment, you'll help them out by making the beginnings of a simple rational number calculator (a rational number is a number with a numerator and a denominator: either a proper or improper fraction). Your calculator will let the user enter two rational numbers, then add them together and show the result in a mixed number format (e.g., a proper fraction with a whole number part). When you're finished, the program output should look something like:

First numerator: 3
First denominator: 4
Second numerator: 9
Second denominator: 6
3/4 + 9/6 = 54/24 (2 6/24)

Note that I typed in the 3, 4, 9, and 6. Also see that you DO NOT need to reduce or simplify the fractions in any way (we'll talk about how to do that later)!

This lab will be completed in pairs. Be sure to review the pair programming guidelines. You also must work with a different partner than last time!

Objectives

Necessary Files

You will want a copy of the LabC.zip file. The BlueJ project in this file includes a provided RationalTester class that you can run in order to help you test your code as you are working. The class you produce should be able to wok flawlessly with this tester.

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

Details

For this lab you will be creating three (3) new Java classes:

You can use the provided tester class to help check that each piece of your program works. Simply uncomment the section you wish to test after you add a feature. You can also use the BlueJ interface: for example, instantiate a Rational object and then call it's methods by right-clicking on the red box (you can also use the "inspect" option to look at the current values of the instance variables).

Once you are finished, check and double-check that your program works flawlessly before submitting it to the submission folder. Also be sure that your names are on the top of all of your class files!

Submitting

Upload the your Rational.java, Mixed.java, and RationalAdder.java classes to the LabC folder on the submission folder on hedwig. Make sure you upload your work to the correct folder! The lab is due at midnight on the day of the lab.

Extensions

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

  1. Add functionality so that the user can type in a Rational on a single line (e.g., as "3/4"). Hint: You can change the Scanner's delimiter so you can call nextInt() twice! You'll then need to change the delimiter back to a new line in order to read that out of the buffer. Alternate Hint: You can provide a String as an argument when creating a Scanner, causing the Scanner to scan just that String (rather than the user input). So you can use one Scanner to get the entire rational from the user, and then another Scanner to split that rational into a numerator and denominator
    • Bonus: Add functionality so that the user can type in the entire equation on one line (e.g., "3/4 + 1/2"). Note that you will need to be careful about whether to use spaces before or after the + sign. Be sure to prompt the user with the proper format!
  2. Add other mathematical operations to the Rational class (subtraction, multiplication, division). Subtraction is very similar to addition, while multiplication involves "cross-multiplying" (e.g., multiply the first numerator by the second denominator). For each operation you'll 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.
  3. Add addition (or other mathematical functions) to the Mixed class. Hint: You can make a Rational object out of a Mixed using the toRational() method, add the two Rational numbers together (using the add() method you already created), and then simply convert the result back to a Mixed using the toMixed() method!

Grading

This assignment will be graded on approximately the following criteria (broken down carefully):

Words of Encouragement

This is one of the trickest labs we've done so far (and may be one of the trickiest of the Semester!) Just go slowly and think your way through it. Remember that we're just doing the same three things: declaring variables, instantiating objects, and calling methods on those objects. It's simply a matter of remembering which variable is which. Talk things over with your partner, and if you get stuck, do not be afraid to ask for help!