• Tidak ada hasil yang ditemukan

COMPSCI 105 S1 2014 Tutorial 1: Nested Loops, Arrays

N/A
N/A
Protected

Academic year: 2023

Membagikan "COMPSCI 105 S1 2014 Tutorial 1: Nested Loops, Arrays"

Copied!
4
0
0

Teks penuh

(1)

Please send comments and corrections to your tutor

Computer Science 105, S1C 2014 1/4

COMPSCI 105 S1 2014 Tutorial 1: Nested Loops, Arrays

Please download sample code from

http://www.cs.auckland.ac.nz/courses/compsci105ssc/tutorials/

Q1. What is the output of the following code? Run the program and try to understand its output.

private void ex01() {

int[] nums = { 8, 10, 3, 4, 12, 200 };

for (int i = nums.length - 1 ; i >= 1 ; i--) {

if (i == 1) {

} else {

} } }

System.out.println(nums[i]);

nums[i - 1] = nums[i];

System.out.format("%d,", nums[i]);

Q2. Complete the nestedLoops method below such that it generates the following output:

9 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 7 6 5 4 3 2 1 6 5 4 3 2 1 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1

Hint: Initially only try to get the numbers printed out correctly and then if you have time attempt to format like above so that there is left padding.

(2)

Please send comments and corrections to your tutor

Computer Science 105, S1C 2014 2/4

private void ex02(){

}

Q3. Given the following code:

int count = 0;

for(int i = 0; i < 10; i += 2) {

for(int j = 0; j < 5; ++j) {

for(int k = 5; k > 0; --k) {

++count;

} } }

System.out.println(count);

What number can we expect to be output?

Q4. A Point object contains two values, namely x and y. Complete the increaseDecreasePointValues() method which decreases the x value by 1 and increases the y value by 2 of each Point object in the 2D Point array which is passed to the method as a parameter. The output of the ex04() method should be:

(1, 2) (1, 3) (1, 4) (2, 5) (2, 3) (2, 3) (3, 6) (6, 8) (3, 2) (4, 2) (7, 6) (4, 1)

(0, 4) (0, 5) (0, 6) (1, 7) (1, 5) (1, 5) (2, 8) (5, 10) (2, 4) (3, 4) (6, 8) (3, 3)

(3)

Please send comments and corrections to your tutor

Computer Science 105, S1C 2014 3/4

private void ex04() {

Point[][] points = create2DPointArray();

print2DPointArray(points);

increaseDecreasePointValues(points);

print2DPointArray(points);

}

private void increaseDecreasePointValues(Point[][] pts) {

}

private Point[][] create2DPointArray() {

Point[][] pointArray = {

{new Point(1, 2), new Point(1, 3), new Point(1, 4)}, {new Point(2, 5), new Point(2, 3), new Point(2, 3)}, {new Point(3, 6), new Point(6, 8), new Point(3, 2)}, {new Point(4, 2), new Point(7, 6), new Point(4, 1)}, };

return pointArray;

}

private void print2DPointArray(Point[][] pts) {

//Code is not shown here, look in the supplied code }

(4)

Please send comments and corrections to your tutor

Computer Science 105, S1C 2014 4/4

Q5. Complete the makeAFullCopy() method which returns a full copy of the 2D Point array passed as a parameter.

private void ex05() {

Point[][] points = create2DPointArray();

Point[][] pointsCopy = makeAFullCopy(points);

boolean flag = false;

for (int i = 0; i < points.length; i++) {

for (int j = 0; j < points[i].length; j++) {

if( points[i][j] == pointsCopy[i][j]) {

flag = true;

break;

} } }

System.out.println( (flag) ? "This should not execute."

: "Correctly copied"

);

}

private Point[][] makeAFullCopy(Point[][] pts) { Point[][] ptsCopy;

}

What do you need to watch out for when copying arrays?

Referensi

Dokumen terkait