The Fraction class found in the package type.lib represents simple fractions. It is not part of the standard Java library, so you won't find its API documented with the standard Java classes. Instead, you need to rely on the author of the classs to provide you with the documentation.
Create a program named AddFractions. Using a Scanner object, prompt the user for two fractions, create two Fraction objects with the correct numerators and denominators, and output the sum of the two fractions. The only facilities your program needs from the Fraction class is a constructor and the method add. Try to use the PrintStream method printf to print your output.
Here is an example run of the program (red text indicates
something you have to type in):
java AddFractions
Numerator and denominator of first fraction: 1 2
Numerator and denominator of second fraction: 3 5
1/2 + 3/5 = 11/10
Submit your program using the command:
submit 1020 L04 AddFractions.java
Create a program named FractionArithmetic.
Using a Scanner object, prompt
the user for two fractions, create two Fraction
objects with the correct numerators and denominators,
and output the sum, difference, product, and quotient of
the two fractions.
Try to use the PrintStream
method printf to print your output.
Hint: This program is similar to the previous one, so you can
just modify the previous program and save it under the new name.
Here is an example run of the program (red text indicates
something you have to type in):
java FractionArithmetic
Numerator and denominator of first fraction: 1 2
Numerator and denominator of second fraction: 8 9
1/2 + 8/9 = 25/18
1/2 - 8/9 = -7/18
1/2 * 8/9 = 4/9
(1/2) / (8/9) = 9/16
Submit your program using the command:
submit 1020 L04 FractionArithmetic.java
Create a program named QuotedFraction. Using a Scanner object, prompt the user for one fraction, create the Fraction object with the correct numerator and denominator, and output the fraction as a proper fraction surrounded by quotes. Your program should set a static field to produce the quotes (i.e., you should not print quotes yourself). Your program should use a Fraction method to produce the proper fraction.
Here is an example run of the program (red text indicates
something you have to type in):
java QuotedFraction
Numerator and denominator of the fraction: 17 3
"5 2/3"
Submit your program using the command:
submit 1020 L04 QuotedFraction.java
Euler's number e is widely used throughout mathematics, the physical sciences, and engineering. Its value can be written as an infinite sum; the first 8 terms of the sum are:
The exclamation mark ! indicates the factorial of the preceding integer, which can be computed using a method from type.lib.ToolBox
Create a program named E that approximates e using the first 8 terms of its infinite series (as shown above). Make sure to use named constants instead of magic numbers. Use type.lib.ToolBox to compute the necessary factorials, and Fraction to represent the fractions. You will require the round method from java.lang.Math.
Also, use java.lang.Math to compute the value of e and compare it to the series approximation. Use the PrintStream method printf to print your output.
Here is an example run of the program (red text indicates
something you have to type in):
java E
Taylor series: 2.718254
Math.exp(1) : 2.718282
Notice that our approximation is only good to 4 decimal places; according to Wikipedia, Leonhard Euler was able to calculate e to 23 decimal places in 1748.
Submit your program using the command:
submit 1020 L04 E.java