Download Lab 10

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSC 204 Lab 10: More on Methods and Arrays
Goals
After doing this lab, you should be able to:

overload methods

understand how arrays are passed to and returned from methods

access two dimensional arrays in a number of specific ways

begin using the Java BigInteger class
Materials Needed
Be sure you have the following on hand during the lab.

This sheet and your course notebook.
Method
Change into your Labs folder (cd Labs), and make a Lab10 folder (mkdir Lab10). Change
into this directory to all work associated with this lab. Copy over the files you will need with this
lab by typing :
cp /pub/digh/CSC204/Lab10/* .
Lab Steps
I. Passing One Dimensional Arrays as Parameters into Methods
1. The program arOne.java in your account demonstrates how we can send a one-dimensional
array to a method and have its contents displayed. As this method simply prints out the array, its
return type is void. Take a look at this program. A copy is included. We print the array of global
constant size ASIZE out in main first. Notice in the statement :
ar1[ASIZE-1] = 2001;
does this set the last element of the array to 2001 or the next to last element? __________________
2. Now, let’s do a complete trace through this program as it is written now to determine the output.
Draw the contents of array ar1 below.
3. Compile and test your program when you’re done and check your results. Notice the differences
between Triple1 and Triple2. One is value-returning and the other is void. Notice how the
Triple2 method which is void has the right to actually modify the array object sent in as a
1
parameter. The Triple1 method makes a copy of the array, modifies the copy, and returns the
copy.
4. Let’s now add a new method to this program named ArrayAvg(). This method computes the
average values in the array. Just like the DisplayArray() method, this only needs one
parameter. What should be the return type of ArrayAvg()? _____________________________
Should ArrayAvg() be void or value-returning? _______________________________________
5. Once you’ve coded up your ArrayAvg() method and included it below the Triple2()
method, include a call to it from the main method after that first DisplayArray() method call.
6. Now, let’s compile and test your program. You should get an array average of 213.6.
II. Passing Two Dimensional Arrays as Parameters into Methods
1. The program arTwo.java in your account shows you how to declare a two dimensional array
of integers and then use a method to print the contents of the array, DisplayArray(). Take a
look at this program.
If you change the size of your array do you have to change any of your methods? ____________
2. Notice the second method called DisplayArray(). The compiler will not mind at all that
you have another method with the same name. Why not? What is it called if you have two
methods with the same name in the same class scope? (Hint : Look back at the Account class)
________________________________________________________________________________
3. Now, what purpose exactly does this second version of DisplayArray() serve?
________________________________________________________________________________
4. Now, let’s do a complete trace through this program as it is written now to determine the output.
Draw the contents of array ar2 below.
5. Compile and test your program when you’re done and check your results.
6. Let’s now add a new method to this program named DisplayOddRows(). This method
displays only the rows of an array whose index is an odd number (i.e., rows indexed 1, 3, 5, etc.).
2
It will be really similar to DisplayArray except that you’ll need an if statement within your
nested loop body.
7. When you’re done, include a call to this method from main. Compile and run your program. It’s
output should be :
16 17 18 19 20 21
28 29 30 31 32 33
40 41 42 43 44 45
8. Compile and run program arThree.java. The program accepts a series of numbers into an
array and then calls a method to compute the sum.
The program prompts you initially for the maximum number of items you want to enter. If you
decided to enter fewer numbers, you just enter a negative value and the input is terminated.
Note the use of a boolean and the compound boolean expression in our for-loop to control the
termination of the loop.
9. Now, you need to write the method to compute and return the sum. Uncomment the method call
in main, compile, and test.
10. You should have copied the files inData and inData2 from the directory. What do they
contain? Now notice what happens if you do the following at the command prompt:
$ java arThree < inData
Can you explain what happened? What if you use inData2 instead?
Now give this a try:
$ java arThree < inData2 > outData
What do you find in file outData?
The above are just a few examples of what is know as I/O redirection.
3
III: Simple factorials
1. The program factorial.java will compute the factorial of an integer value. Take a look
at it. Now, let’s compile and run this program using a single integer as a command line
argument.
2. Factorials grow very quickly. Compute the values below:
13!
14!
15!
16!
Something very peculiar has happened…16! is less than 15! . This is because 16! is too large to
be represented as an integer, so the leading digit has been removed.
What exception was raised when the number became too large to be
stored?
What 's peculiar about 20!?
3. This program can only compute factorials up to 15!. That's not very useful. Let's try changing
the type of the result to get larger values in the final part of lab.
IV: The BigInteger Class
1. To go beyond the primitive types, we need to use a new class. Fortunately, the BigInteger
class is one of the standard Java classes, so to get extremely large integers, we can use this
class. Copy the program factorial.java to bifactorial.java. Now, go in and
change the name to bifactorial and everywhere the result is declared from int to
BigInteger.
2. Try compiling this program. You should notice a lot of errors. Changing to a non-primitive
value takes a bit of doing. First of all, you need to include the library at the top of the program
with
import java.math.BigInteger;
4
3. Second, there is no direct conversion between integers and BigIntegers, so you'll need to replace
product = 1; with
product = BigInteger.ONE;
If you are using an integer value other than 0 or 1, you can create a new BigInteger with its
value using BigInteger.valueOf (the integer) . So, you could have initialized the
product using
product = BigInteger.valueOf(1);
4. Finally, you can not use the standard mathematical symbols +, -, *, etc on new types. (Some
programming languages do allow you to redefine these, but Java is not one of those languages.)
Instead of product *= i; you'll need
product = product.multiply (BigInteger.valueOf(i));
5. Get your program to compile and run and create a script of a listing, compilation, and run that
computes the factorial of a large number (somewhere between 200 and 1000). Print out this
script and include in your notebook.
5