## Solution for Programming Exercise 7.1

This page contains a sample solution to one of the exercises from Introduction to Programming Using Java.

### Exercise 7.1:

Write a subroutine that creates an ArrayList containing several *different*
random integers in the range from 1 up to some specified maximum. The number of integers and the
maximum allowed value for the integers should be parameters to the subroutine. Write a main()
routine to test your subroutine.

**Discussion**

The exercise asks for an ArrayList containing integers. That has to mean
using ArrayList<Integer>, since an ArrayList can't contain values of
type int. The problem is pretty easy: Start with an empty list,
then generate random integers and add them to the list until you have as many integers as you
want. Since all the integers in the list must be different, before adding an integer to the
list, we need to test whether that integer is already in the list. One way to do that is
with the indexOf() method from the ArrayList class. If numbers
is an ArrayList<Integer> and num is an int,
then numbers.indexOf(num) returns -1 if num is **not** in the list.
(Note that this relies on autoboxing to convert num into an object of type
Integer, since indexOf() requires an object as its parameter.)
To create the list of numbers, we just add integers to the list until it reaches the desired size.
Assuming that count is the desired number of integers and max is the
maximum allowed value for the integers, this can be done with

ArrayList<Integer> numbers = new ArrayList<Integer>(); while (numbers.size() < count) { int num = (int)(Math.random()*max) + 1; if ( numbers.indexOf(num) == -1 ) numbers.add( num ); }

This code is the heart of the solution. There is one problem, however. If count > max, then it's impossible to get count different integers in the range from 1 to max, because there aren't that many different numbers in the range! In fact, the while loop will be an infinite loop in that case since the size of the ArrayList can never become larger than max. My solution avoids the infinite loop by throwing an IllegalArgumentException when count > max. You can see the complete subroutine, and a main() routine that tests it, below.

Note that an ArrayList in this program is simply printed using System.out.println(). The toString() method of the ArrayList class will be called to get the String representation of the list. That method outputs the items from the list, enclosed between [ and ] and separated by commas.

**The Solution**

import java.util.ArrayList; public class SeveralRandomIntegers { /** * Creates an ArrayList that contains several integers chosen at * random from a specified range of values. All the integers * in the list are different. * @param count the number of random integers to be created * @param max the integers are chosen in the range 1 to max, inclusive * @return an ArrayList containing the integers. The integers are * random and are in a random order. * @throws IllegalArgumentException if max is greater than count. In * that case there are fewer than count different integers in the * range 1 to max. */ public static ArrayList<Integer> makeRandomInts( int count, int max ) { if (count > max) { throw new IllegalArgumentException("Can't have " + count + " different integers in the range 1 to " + max); } ArrayList<Integer> numbers = new ArrayList<Integer>(); while (numbers.size() < count) { int num = (int)(Math.random()*max) + 1; if ( numbers.indexOf(num) == -1 ) numbers.add( num ); } return numbers; } public static void main(String[] args) { System.out.println("10 integers chosen from the range 1 to 100:"); for (int i = 0; i < 8; i++) System.out.println( makeRandomInts(10,100) ); System.out.println(); System.out.println("10 integers chosen from the range 1 to 10:"); for (int i = 0; i < 3; i++) System.out.println( makeRandomInts(10,10) ); System.out.println(); System.out.println("25 integers chosen from the range 1 to 10000:"); System.out.println( makeRandomInts(25,10000) ); System.out.println(); System.out.println("End with an exception!"); makeRandomInts(10,9); } }