CSCI 161 — Lab 12

Due: Friday, May 11th @ 11:59PM

Objectives

Overview

Required Methods

lengthOfNumber (10 points)

Determines the digit count of a non-negative number

Signature

static int lengthOfNumber (int n)

Parameter

Examples

lengthOfNumber (9);    // returns 1
lengthOfNumber (1234); // returns 4
lengthOfNumber (92);   // returns 2
lengthOfNumber (0);    // returns 1

countCharInString (10 points)

Counts the number of occurrence of a character in a string

HINT: use substring() for recursive calls

Signature

static int countCharInString (char c, String s)

Parameters

Examples

countCharInString ('a', "hello");       // returns 0
countCharInString ('g', "programming"); // returns 2

gcd (15 points)

Computes the greatest common divisor of two numbers, x and y

Signature

static int gcd (int x, int y)

Parameters

Examples

gcd (12, 18); // returns 6
gcd (73, 37); // returns 1

removeMatchingDigits (15 points)

Removes all occurrences of a digit from a number

Signature

static int removeMatchingDigits (int n, int digit)

Parameters

Examples

removeMatchingDigits (12345, 2); // returns 1345
removeMatchingDigits (12345, 5); // returns 1234
removeMatchingDigits (99999, 9); // returns 0
removeMatchingDigits (2111, 1);  // returns 2
removeMatchingDigits (323, 3);   // returns 2

fibonacci (20 points)

Returns the nth fibonacci number

https://en.wikipedia.org/wiki/Fibonacci_number

Signature

static int fibonacci (int n)

Parameter

Examples

fibonacci (0);  // returns 0
fibonacci (1);  // returns 1
fibonacci (2);  // returns 1
fibonacci (3);  // returns 2
fibonacci (10); // returns 55

isSorted (20 points)

Returns true if and only if the array a is sorted from indicies [low, high)

Hints:

Signature

static boolean isSorted (int[] a, int low, int high)

Parameters

Examples

int[] a1 = { 2, 1, 4, 5 };
boolean r1 = isSorted (a1, 0, a1.length)
// r1 is false

int[] a2 = { 10, 15 };
boolean r2 = isSorted (a2, 0, a2.length)
// r2 is true

Submission

Submit the Java source code file on Autolab under the "Lab 12" assignment.

Grading