(quickly) more on null references:

10
(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.

Upload: dustin-allen

Post on 03-Jan-2016

31 views

Category:

Documents


2 download

DESCRIPTION

(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” - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: (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”

• 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

Page 2: (Quickly) More on Null references:

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?

Page 3: (Quickly) More on Null references:

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};

Ordouble[][] mat = new double[3][];double[] arr = {7.2,3.1,2.4};mat[2] = arr;

Ordouble[][] mat;mat = new double[][] {{3.2,4.1,2.5},{7.1,8.2,9.3}};

Page 4: (Quickly) More on Null references:

int[] arrfield = {3,7,2,4,1,5};

int[][] matfield= {{3,7,2,4},{1,5,8,5},{3,2,4,1}};

1. Create a class with two fields: arrfield and matfield. Create a constructor that initializes them as above:

2. Write a method in the class that prints out each value in the arrfield (using a loop);

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:

4. Write a method that prints out every value inside each array in the matfield field:

5. 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.

6. 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?

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

Page 5: (Quickly) More on Null references:

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}};

}

}

Page 6: (Quickly) More on Null references:

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]+ " ");}

// Orfor (int x:arrfield) {

System.out.println(x + " ");}

}

Page 7: (Quickly) More on Null references:

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();int len = 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?)}

}

Page 8: (Quickly) More on Null references:

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();

}

// Orfor (int[] arr:matfield) {

for (int ele: arr) {System.out.print (ele + " ");

}System.out.println();

}

}

Page 9: (Quickly) More on Null references:

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?

}

Page 10: (Quickly) More on Null references:

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;

}