CSci 2101 Lab 2. Java loops and methods.

Due Monday, September 12th, at 11:59pm (by e-mail)

28 points

The lab will be done in pairs, announced in class.

Reminder of the setup:

Lab tasks

For every method that you write you must have at least three tests. Some methods require more tests because they need to handle more different situations. Make sure that you test for different conditions.
Write your tests before you write your method. Add more tests as needed.

Problem 1 (6 points)

Write and test a method countChar that takes a string and a character (for instance, a letter) and returns the number of times the character appears in the string. For instance, countChar("Morris", 'r') returns 2. The method performs a case-sensitive comparison ('A' is not the same as 'a').

Problem 2 (6 points)

Write and test a method hasEvenOccurrences that takes a string and a character and returns true if the string has an even number of occurrences of the character and false otherwise. For instance, hasEvenOccurrences("Morris", 'r') returns true and hasEvenOccurrences("Morris", 'M') returns false.
Recall that 0 is an even number.
You may call the method from Problem 1 in your method.

Problem 3 (10 points)

Write and test a method that takes a positive integer number and prints out all of its pairs of factors. For instance, a call printFactors(12) should print:

12 can be factored as:
1 and 12
2 and 6
3 and 4
The format of the output can be different.

Problem 3 (6 points)

Write and test a method that takes a positive integer n and returns a product of two random integers between 1 and n (inclusive). Make sure that zero is not among the numbers that you multiply.

How to submit

Send the file with all your comments by email to me: elenam at The subject of the message must be 2101 Lab 2. Make sure to CC your group partner if you worked with another person.

CSci 2101 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.