0% found this document useful (0 votes)
54 views3 pages

Dictation Scope - 2nd

The document provides code examples for methods to check if a number is prime, find the next prime number, print arrays, sum elements of arrays, sum rows of a 2D array, and test the methods. It includes a method isPrime to check if a number is prime, getNextPrime to find the next prime number, printArray to print arrays, sum to sum array elements, sumRow to sum rows of a 2D array. Main is used to test the methods on sample data.

Uploaded by

chungjerry94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views3 pages

Dictation Scope - 2nd

The document provides code examples for methods to check if a number is prime, find the next prime number, print arrays, sum elements of arrays, sum rows of a 2D array, and test the methods. It includes a method isPrime to check if a number is prime, getNextPrime to find the next prime number, printArray to print arrays, sum to sum array elements, sumRow to sum rows of a 2D array. Main is used to test the methods on sample data.

Uploaded by

chungjerry94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Last Modified: 9/26/2023

COMP2045/COMP2026 Dictation study sheet #2


Write a method isPrime that takes an integer as input and returns true if the integer is a prime number,
otherwise returns false.

boolean isPrime(int n) {
if (n < 2)
return false;
for (int i = 2; i < n; i++) {
if (n % i == 0)
return false;
}
return true;
}

Write a method getNextPrime that output the nearest prime number that is n or larger than n where n is input
by user

int getNextPrime(int n) {
int i = n;
while (!isPrime(i)) {
i++;
}
return i;
}

Write a method printArray that takes an array of integers as input and prints the array in the following
format:

[1, 2, 3, 4, 5]

void printArray(int[] arr) {


System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i < arr.length - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}

1/3
Last Modified: 9/26/2023

Call the method printArray in the main method to print the following array:

[1, 2, 3, 4, 5]

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


printArray(arr);

Write a method that takes a 2D integer array and sum all the elements in the array, return the sum.

int sum(int[][] arr) {


int sum = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
}
}
return sum;
}

Call the method sum in the main method to sum the following array:

{{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15}}

int[][] arr = {{1,2,3,4,5},


{6,7,8,9,10},
{11,12,13,14,15}};
int result = sum(arr);
System.out.println(result);

Write a method sumRow that takes a 2D integer array and find the sum for each row of arrays, return the sum
of each row.

int[] sumRow(int[][] arr) {


int[] result = new int[arr.length];

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


for (int j = 0; j < arr[i].length; j++)
result[i] += arr[i][j];

2/3
Last Modified: 9/26/2023

return result;
}

Call the method sumRow in the main method to sum the following array:

{{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15}}

int[][] arr = {{1,2,3,4,5},


{6,7,8,9,10},
{11,12,13,14,15}};
int[] result = sumRow(arr);
for (int i : result)
System.out.println(i);

3/3

You might also like