1 / 10

(Quickly) More on Null references:

(Quickly) More on Null references:. A null reference is a pointer to nothing. Pointers = space for address in memory Think of RAM, with each space in memory having its own unique address. (different spaces in memory do different things) Circle x; x is now a “null pointer”

Télécharger la présentation

(Quickly) More on Null references:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. (Quickly) More on Null references: • A null reference is a pointer to nothing. • Pointers = space for address in memory • Think of RAM, with each space in memory having its own unique address. (different spaces in memory do different things) Circle x; • x is now a “null pointer” • Meaning x can hold an address for a space in memory that is where a Circle will be, but currently it’s just empty x = new Circle(); • Makes a space for a circle object in memory • memory space holds radius, circumference, area fields • Holds getRadius() method, SetValues() method,etc. • x now holds the address of this new circle in memory. • It “points to” the circle in memory

  2. Matrices: Making Arrays of Arrays 1. double[][] mat = new double[5][]; What have I just made? - an array of 5 addresses (that will eventually point to arrays of doubles). If I can’t do this: 2. double[][] mat = new double [][]? Why can I do #1?

  3. To create an array of arrays: You can do: double[][] mat = {{3.2,4.1,2.5},{7.1,8.2,9.3}}; Or double[][] mat = new double[3][]; mat[1] = new double[] {3.1,2.4}; Or double[][] mat = new double[3][]; double[] arr = {7.2,3.1,2.4}; mat[2] = arr; Or double[][] mat; mat = new double[][] {{3.2,4.1,2.5},{7.1,8.2,9.3}};

  4. Generate random numbers: At the top: import java.util.Random; Then inside the method create a new Random object: Random randvar = new Random(); Then access the randvar object’s nextInt() method, which generates a random number up to (but not including) 100 (or the number you put in as a parameter: int x = randvar.nextInt(100); Also: nextBoolean() nextDouble() nextFloat() nextFloat() Only nextInt takes a value int[] arrfield = {3,7,2,4,1,5}; int[][] matfield= {{3,7,2,4},{1,5,8,5},{3,2,4,1}}; • Create a class with two fields: arrfield and matfield. Create a constructor that initializes them as above: • Write a method in the class that prints out each value in the arrfield (using a loop); • Write a method that generates a random number between 0 and 20, then creates an array that long filled with random numbers between 0 and 100. It sets the arrfield to this new array: • Write a method that prints out every value inside each array in the matfield field: • Write a method that generates 2 random numbers, x and y. It then creates a matrix of x arrays, each y elements long. It then fills each array in the matrix with random numbers. It sets the matfield to this new matrix. • Write a method that generates a random number x. It creates a matrix of x arrays of ints. Then, for each array, it generates a new random number and creates a new array of ints of that length. It fills that array with random numbers, and places it in the original matrix. (In other words, we’re now creating a randomly sized array of randomly sized arrays of ints, with each array filled with random numbers) It then sets the matfield to this new matrix Extra: Sudoku class? Solve the board? What methods in class?

  5. Problem 1: Create a class with two fields: arrfield and matfield. Create a constructor that initializes them: // one likely solution public class ArrayClass { private int[] arrfield; private int[][] matfield; public ArrayClass() { //could also pass array and matrix in as parameters arrfield = new int[] {3,7,2,4,1,5}; matfield = new int[][] {{3,7,2,4},{1,5,8,5},{3,2,4,1}}; } }

  6. Problem 2: Write a method that prints out each value in the arrfield (using a loop) … public void printarr(){ for (int i = 0; i < arrfield.length; i++) { System.out.println(arrfield[i]+ " "); } // Or for (intx:arrfield) { System.out.println(x + " "); } }

  7. Problem 3: Write a method that generates a random number between 0 and 20, then creates an array that long filled with random numbers between 0 and 100. It sets the arrfield to this new array: import java.util.Random; public class ArrayClass { private int[] arrfield; private int[][] matfield; public ArrayClass() { arrfield = new int[] {3,7,2,4,1,5}; matfield = new int[][] {{3,7,2,4},{1,5,8,5},{3,2,4,1}}; } public void makearr(){ Random randvar = new Random(); intlen = randvar.nextInt(20); int[] arr = new int[len]; for (int i = 0; i < arr.len; i++) { arr[i] = randvar.nextInt(100); } arrfield = arr; // what happens to {3,7,2,4,1,5} (the old array?) } }

  8. Problem 4: Write a method that prints out every value inside each array in the matfield field: … public void printmat(){ for (int i = 0; i < matfield.length; i++) { //what does matfield.length give you the length of (specifically)? //what does this loop (above) take you through (specifically)? for (int j = 0; j < matfield[i].length; j++ ) { System.out.print(matfield[i][j]+ " "); } System.out.println(); } // Or for (int[] arr:matfield) { for (intele: arr) { System.out.print (ele+ " "); } System.out.println(); } }

  9. Problem 5: Write a method that generates 2 random numbers, x and y. It then creates a matrix of x arrays, each y elements long. Now fill the array with random numbers. import java.util.Random; public class ArrayClass { private int[] arrfield; private int[][] matfield; public ArrayClass() { arrfield = new int[] {3,7,2,4,1,5}; matfield = new int[][] {{3,7,2,4},{1,5,8,5},{3,2,4,1}}; } public void makematrix(){ Random randvar = new Random(); int rows = randvar.nextInt(20); int cols = randvar.nextInt(20); int[][] mat = new int[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++ ) { mat[i][j] = randvar.nextInt(100); } } matfield = mat; // so exactly what does this do? }

  10. Problem 6: Write a method that generates a random number x. It then creates a matrix of x arrays. Then, for each array, it generates a new random number and creates a new array that length. It fills that array with random numbers, then places that in the matrix import java.util.Random; … public void makematrix(){ // how is this method different from the last one? // Why is it different in this way? Random randvar = new Random(); int rows = randvar.nextInt(20); int[][] mat = new int[rows][]; for (int i = 0; i < rows; i++) { int cols = randvar.nextInt(20); int[] arr = new int[cols]; for (int j = 0; j < cols; j++ ) { arr[j] = randvar.nextInt(100); } mat[i] = arr; } matfield= mat; }

More Related