You are on page 1of 2

BSEH231/BITH331 OBJECT ORIENTED PROGRAMMING

Assignment 2

INSTRUCTIONS

Answer all questions.

Submit a PDF file

Question 1

a) In a list of numbers, the number that occurs most often is called the mode of that list. For
example, consider the following list of numbers:

The number which occurs most often in the above list is 15(it occurs 3 times). Therefore, 15 is
the mode of this list.

Write a full java program that contains a method called findMode() that takes a LinkedList of
Integers as a parameter and returns the mode of the LinkedList as an integer. The method
should not assume that the values are sorted in increasing order, therefore when looking for
the mode, the method should not take benefit of the fact that the numbers are ordered and same
numbers are stored on consecutive locations. You may use any other lists, or other data
structures from the java Collections framework to help you solve this problem. You may
assume that the LinkedList passed is not null. [20]

b) Instead of integers as above this question works with first names of people as Strings. Write
a full java program that contains a method called findMostCommonName() that takes a
LinkedList of names as a parameter and returns the most common name in the LinkedList as
String.

e.g Tawanda, Tatenda, Susan, Grace, Emily, John, Tatenda, Peter, James, Susan, Tatenda

Tatenda occurs 3 times, therefore it is the most common name. [20]

Question 2
A method named printTabs (with no parameters) produces a table as the numbers 1 to 10, their
squares and their cubes as shown below.
a) Write the method printTabs. Include the method in a working program named Test.java
and invoke the method printTabs from main().
b) Modify program Test.java by adding a function selTabs to accept the starting value of
the table, the number of values to be displayed, and the increment between the values.
If the increment is not explicitly sent, the function should use a default value of 1. A
call to selTabs(6,5,2) should produce a table of five lines, the first line starting with
number 6 and each succeeding number increasing by 2.

12 144 1728

14 196 2744 [20marks]

You might also like