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

2018-09-26 More Arrays

posted Sep 26, 2018, 5:44 AM by Konstantinovich Samuel   [ updated Sep 27, 2018, 7:03 AM ]
Homework - Complete the lab (due Friday)


Exam Modification (Due to Fire Drill)
Split into 2 parts:
  Coding Questions Friday
  Multiple Choice Question Monday.

-Go over problems where there was difficulty



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


Lab + HW (due Friday in case you can't finish)
For all problems:
Write test cases. 

When given an assignment:
Never change the classname, parameters, return type, or
the names of the methods assigned.

Make a new repos MKS21X-arrays
include the file ArrayDemo.java which has several static methods:


Do not use built in array methods in Arrays (do it yourself!)
0a. Make a function to print a 1D array of ints.
public static void printArray(int[]ary){}


0b. Make a function to print a 2d array of ints.
public static void printArray(int[][]ary){}

Notice both 0a and 0b share the same name.
This is called method overloading.
Overloading is allowed when the parameters differ in
type and/or quantity.



1. Calculate and return how many zeros are in the parameter

(a rectangular 2D array of integers)


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



2a. Modify a given 2D array of integer as follows:
Replace all the of values with 1's 
EXCEPT when the row number is the same as the column number:
you must fill those with 3's instead.

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


2b. Make a new 2d array the same dimensions as the given parameter.
Fill that array with with 1's,
except where the value of the original array is negative:
you must fill those with 3's instead.

//You may NOT modify the parameter vals.

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

Comments