Difference between revisions of "CSC212 Lab 3 2014"

From dftwiki3
Jump to: navigation, search
(Arrays)
(Arrays)
Line 24: Line 24:
 
}
 
}
 
</source>
 
</source>
# Make your program store numbers in the table using a for-loop.  The numbers should be 0, 2, 4, 6, ...18.  Make your program display the contents of the array '''table''' with a for-loop.
+
<br />
# Make your program store random numbers in the array, and find the largest element, and its index.  Using random numbers in Java requires ''importing a new module into the program'', generating a new  
+
<ol>
 +
<li /> Make your program store numbers in the table using a for-loop.  The numbers should be 0, 2, 4, 6, ...18.  Make your program display the contents of the array '''table''' with a for-loop.
 +
<li /> Make your program store random numbers in the array, display the contents of the array, and find the '''largest element''', and '''its index'''.  Using random numbers in Java requires ''importing a new module into the program'', generating a '''new object of type Random'''.  See below for a way to start:
 
<br />
 
<br />
 
:::<source lang="java" highlight="1,6">
 
:::<source lang="java" highlight="1,6">
Line 41: Line 43:
 
</source>
 
</source>
 
<br />
 
<br />
 +
<li /> Make your program create 2 arrays of 10 elements.  It will initialize the first array with the numbers 1 to 10, and the second with 0s.  It will display both.  Then it will copy all the numbers from the first array to the second one, and overwrite all the 0s in the process.  It will display both arrays at the end.
 +
<li /> Make your program create an array '''Fib''' of size 10 elements, and store the ''Fibonacci'' series in it.  The Fibonacci series can be defined as follows:
 +
 +
                1 1 2 3 5 8 13 21 ...
 +
 +
Every element is the sum of the previous 2.  You need to start with putting 1 and 1 in the first 2 cells, then compute
 +
 +
:::<source lang="java">
 +
 +
  // in some loop
 +
  Fib[i] = Fib[i-1] + Fib[i-2]
 +
 +
</source>

Revision as of 15:10, 10 September 2014

--D. Thiebaut (talk) 16:00, 10 September 2014 (EDT)


Contents




This lab presents several problems that deal with arryas, classes, objects, and inheritance.



Arrays


Takes this simple example program and modify it in the different ways suggested.

public class Lab3_1 {
	public static void main( String[] args ) {
		int table[] = new int[10];
          
		


	}
}


  1. Make your program store numbers in the table using a for-loop. The numbers should be 0, 2, 4, 6, ...18. Make your program display the contents of the array table with a for-loop.
  2. Make your program store random numbers in the array, display the contents of the array, and find the largest element, and its index. Using random numbers in Java requires importing a new module into the program, generating a new object of type Random. See below for a way to start:
    import java.util.Random;
    
    public class Lab3_1 {
    	public static void main( String[] args ) {
    		int table[] = new int[10];
    		Random randomGenerator = new Random();
    
    		for (int i=0; i<table.length; i++ ) 
    			table[i] = randomGenerator.nextInt(100);
    	}
    }
    

  3. Make your program create 2 arrays of 10 elements. It will initialize the first array with the numbers 1 to 10, and the second with 0s. It will display both. Then it will copy all the numbers from the first array to the second one, and overwrite all the 0s in the process. It will display both arrays at the end.
  4. Make your program create an array Fib of size 10 elements, and store the Fibonacci series in it. The Fibonacci series can be defined as follows: 1 1 2 3 5 8 13 21 ... Every element is the sum of the previous 2. You need to start with putting 1 and 1 in the first 2 cells, then compute
       // in some loop
       Fib[i] = Fib[i-1] + Fib[i-2]