Docsity

Print Array. Java - Lab 12 - Introduction to Computing and Programming | CSCI 1301, Lab Reports of Computer Science

Toggle sidebar
bg1
CSCI 1301
Lab 12
PrintArray.java
This week we’re moving on to arrays. Today, we’ll be working on a fairly simple
lab that constructs some simple int arrays and prints them to the screen. Remember that
when you need to access every member of an array, a for loop is an easy way to do it.
(An enhanced for loop is even easier!)
1.Write a method with the signature :
public static void printArray( int[] arg ) .
This method should output the values of the argument array, separated by commas and
wrapped in braces. Remember, the client is passing in the argument array, you don’t
know what values are held inside it or even how big it is yet! An example:
> [ 0, 1, 2, 3, 4 ]
The array in this case has length 5 and arg[i] == i for every index i.
2. Write a method with the signature :
public static int[] countArray(int size) .
This method should return a new int array of the argument length such that the 0th
element is 0, the 1st element is 1, and so on. For example, countArray(5) should
return the array above. Your method shouldn’t print the array, though. Just return it!
3. Write a method with the signature :
public static int[] fibArray(int size) .
This is a fun one. The Fibonacci sequence is 1, 1, 2, 3, 5, 8, 13, … The initial elements
are 1, 1. After that, every element is the sum of the previous two elements. 1+1=2,
1+2=3, 2+3=5, and so on. You may assume the size of the array you are creating is at
least 2. For example, printArray( fibArray(8) ) should output:
> [ 1, 1, 2, 3, 5, 8, 13, 21]
I recommend creating a new int array (I usually call mine result), load in the value 1 for
the 0th and 1st elements, then- inside a for loop- make sure the ith element is the sum of
the
previous two.
4. Finally, play around with these in the main method. Create some arrays and
print them using these three methods. Easy as pie.

Partial preview of the text

Download Print Array. Java - Lab 12 - Introduction to Computing and Programming | CSCI 1301 and more Lab Reports Computer Science in PDF only on Docsity!

CSCI 1301

Lab 12 PrintArray.java This week we’re moving on to arrays. Today, we’ll be working on a fairly simple lab that constructs some simple int arrays and prints them to the screen. Remember that when you need to access every member of an array, a for loop is an easy way to do it. (An enhanced for loop is even easier!) 1.Write a method with the signature : public static void printArray( int[] arg ). This method should output the values of the argument array, separated by commas and wrapped in braces. Remember, the client is passing in the argument array, you don’t know what values are held inside it or even how big it is yet! An example:

[ 0, 1, 2, 3, 4 ] The array in this case has length 5 and arg[i] == i for every index i.

  1. Write a method with the signature : public static int[] countArray(int size). This method should return a new int array of the argument length such that the 0th element is 0, the 1st element is 1, and so on. For example, countArray(5) should return the array above. Your method shouldn’t print the array, though. Just return it!
  2. Write a method with the signature : public static int[] fibArray(int size). This is a fun one. The Fibonacci sequence is 1, 1, 2, 3, 5, 8, 13, … The initial elements are 1, 1. After that, every element is the sum of the previous two elements. 1+1=2, 1+2=3, 2+3=5, and so on. You may assume the size of the array you are creating is at least 2. For example, printArray( fibArray(8) ) should output:

[ 1, 1, 2, 3, 5, 8, 13, 21] I recommend creating a new int array (I usually call mine result), load in the value 1 for the 0th and 1st elements, then- inside a for loop- make sure the ith element is the sum of the previous two.

  1. Finally, play around with these in the main method. Create some arrays and print them using these three methods. Easy as pie.