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
- To practice designing and implementing classes from scratch
- To continue practicing with user input and output
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:
-
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.- Think about what attributes a Rational has. What instance variables will you need to declare?
- You will make one (1) constructor for the Rational class that takes no parameters and creates a Rational representing 0/1 (0/0 would be illegal!). You should then use getters and setters to actually change the numerator and denominator of that Rational.
-
A Rational will need a
toString()
method so that you can print it out. Remember, atoString()
method does not actually print anything, it just returns a String that can be printed elsewhere! - You'll need to make a getter and setter method for each of the Rational's instance variables.
-
A Rational will also need 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 objcts 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 and appropriate setters) and then return that object, the exact same way we would return an int or a String.
- We can add rational numbers by finding a common denominator, multiplying by the appropriate value, and then adding the numerators. You do NOT need to find the lowest common denominator--it is fine and expected to have non-reduced fractions!
-
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!!- 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 remaings of the whole-number division (that will become the new numerator), you can use the modulo operator (%) to literally get the remainder of the division! If you have questions on this, please ask!
-
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.- Think about what attributes a Mixed has. What instance variables will you need to declare?
- You will make at least one (1) constructor for the Mixed class, which takes integers for the whole part, numerator, and denominator as parameters.
- A Mixed will need a
toString()
method so that you can print it out. - You do not need to make a getter and setter method for each of the Mixed's instance variables.
- You do not need to make an
add()
method for the Mixed (though you might think about how you would--it is not as easy as you think!) - Finally, the Mixed class 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.
-
RationalAdder
A class that asks the user for two Rationals to add, then adds them up and prints out both the Rational and Mixed versions of the sum.- This class will look the programs we've been making so far, with just a
main()
method rather than constructors and instance variables. - This class should prompt the user to enter two rational numbers, using the Scanner to read what they type. You can have the user type in one part at a time (e.g., the first numerator, first denominator, second numerator, second denominator). See Extensions below for other options.
- Using the provided values, instantiate two Rational objects, then add them together to produce a third Rational object that is the sum.
- Print out the sum both as a Rational, and after you have converted it to a Mixed object.
- This class will look the programs we've been making so far, with just a
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!
- Finally, be sure to fill out the lab partner evaluation survey on Moodle after you turn in your work!
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!
-
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!
- 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. - 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 theadd()
method you already created), and then simply convert the result back to a Mixed using thetoMixed()
method!
Grading
This assignment will be graded on approximately the following criteria (broken down carefully):
- You have implemented the Rational class with only a single constructor [10%]
- Your Rational class has a functioning toString() method (that doesn't print directly!) [10%]
- Your Rational class has appropriate attributes, and getters/setters for those attributes [10%]
- Your Rational class has an add() method that returns a new Rational [15%]
- Your Rational objects can be converted to Mixeds [15%]
- You have implemented the Mixed class [5%]
- Your Mixed class has a functioning toString() method [10%]
- Your Mixed objects can be converted to Rationals [10%]
- You have implemented the RationalAdder class, which reads user input and adds Rationals [10%]
- You completed your lab partner evaluation [5%]
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!