CS170 Lab 10 Using Java Built-in Objects Lab preparation: • Save the file named Lab10.java from the class website into your lab10 directory. Background: Earlier this semester, we spent some time writing (recursive) code to calculate a factorial. In mathematical terms, we express this as x! = 1 * 2 * 3 * … * x. While you're probably sick of factorial, we're going to make one more version of this calculation. But first, let's examine the limitations of computations. Examine the iterative (ie using loops) version of factorial included in Lab10.java. In main, write a loop to print out the numbers 1-30 and their factorial values: for(int i = 1; i < 31; i++) { System.out.println(i + "\t" + factorial(i)); } At some point, notice that this code no longer gives you the correct values. Why not? What goes wrong? Instead of using a primitive type such as int or even long, we'll use the Java object BigInteger. BigIntegers are Java objects that can represent arbitrarily big integer values. There is no upper bound except the limitations of memory size and processing speed. Read the documentation of the BigInteger class at http://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html To use BigIntegers, you have to add import java.math.BigInteger; to the beginning of your program. We can create a BigInteger object out of a String representing an integer value. For example: BigInteger ten = new BigInteger("10"); Try it. What happens if you print this object with: System.out.println(ten); Because BigIntegers are not primitive types, the usual math operators like +, =, %, /, and * don’t work. Instead we have to use methods like add. To add two BigIntegers, invoke add on one and pass the other as an argument. A BigInteger representing the new value is returned. For example: BigInteger small = new BigInteger("17"); BigInteger big = new BigInteger("1700000000"); BigInteger total = small.add(big); Take a look at the BigInteger class documentation for other mathematical methods you can use. Exercise: • Change the factorial method to return a BigInteger. You can leave the parameter value alone (ie, the parameter will remain an integer). You'll need to modify the method to use BigInteger objects and instance methods. Hint: remember that you can easily form Strings from numerical values by concatenating the numerical value with the empty String "". For Example: String s = 5 + ""; • What happens when you rerun your loop in main? Do you get the correct answers? • Turn-in your work or demo your code: ◦ Make sure you comment your method! • If you are physically present in lab and finish your work, demo your code to your lab TA. Make sure both you and the TA sign the sign-in sheet • If you are not present during your assigned time or cannot complete the lab during your allotted time, you will need to submit your work to BB: • Submit your work to the Lab10 assignment on BB. Remember to include your name, userID, and section number as comments on the first line of your file. • Submit only the Lab10.java file (not the .class file!) • If you do not finish during your lab period, the turn-in deadline is 5pm on Saturday, Apr. 9th. • No late submissions for labs!