Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎notes‎ > ‎

2017-09-25

posted Sep 25, 2017, 9:51 AM by Samuel Konstantinovich   [ updated Sep 25, 2017, 10:08 AM ]
Exam Friday. All of Java so far. Not including objects. (We start them soon)

-Go over all problems there was difficulty solving.

 public static void main (String[]args){
	char[] fakeString = {m,c,g,k,s};
	for (int i = 0; i < fakeString.length; i++){
		System.out.println(fakeString[i] +  );
	} 
	
	// Three different ways to initialize an int[][]. 
	//Applies to 2D arrays of another type too. 

	int[][] multiA = new int[3][4]; 
	// first number is the # of arrays in multiA
	// second number is the # of items of each array in multiA
	// for multiA, all arrays must be the same length. 


	// for multiB, the arrays within can have different lengths. 
	int[][] multiB = new int[3][];
	multiB[0] = new int[4]; // each row, 0,1 and 2
	mutliB[1] = new int[4]; // can be a different length 
	multiB[2] = new int[4];

	
	int[][] multiC = new int[][]{
		{0,0,0,0}, // can make these different lengths as well
		{0,0,0,0},
		{0,0,0,0},
		};
	// for multiC, arrays within can have different lengths. 
}


Lets try these 2 problems:

Write test cases. 
Compare with your neighbors.

1. Calculate and return how many zeros are in the parameter
(a rectangular 2D array of integers)

public static int countZeros2D(int[][] nums){

}

Hint for problem 2: 
First make a function to print a 1D array.
Then make a function to print a 2d array.
THEN you can attempt these problems.

2. Fill the given 2-dimensional array of integers with 1's.
Where the row number is the same as the column number:
   you must fill those with 3's instead.

public static void fill2D(int[][] vals){
  
}

3. Make a new 2d array the same dimensions as the given parameter.
Fill that array with with 1's, 
except where the row number is the same as the column number:
   you must fill those with 3's instead.

You may NOT modify vals.

public static int[][] fill2DCopy(int[][] vals){
  
}



Comments