Explore BrainMass

Java Programming with Commented Code

A marketing research company has hired you to develop a program. Analytics Nation, Inc. has gathered information from about five different products. They would like to follow up with 1% of these households. Analytics Nation requires a third party to select 1% of the households for one of the products. Choices must be random.

Analytics Nation needs you to develop a program that uses a random number generator to identify 1% of the households for one product. Each household is mapped to a unique number ranging from 1 to the number of households as listed below. Choose one product from the following:

Product 1 (mounth wash)
3,765 households

Product 2 (dental floss)
3,800 households

Product 3 (grass seed)
2,400 households

Product 4 (grass fertilizer)
4,012 households

Product 5 (Blue-Ray DVD unit)
3,580 households


Develop a program to select the households for follow up. The user will be prompted for their selection. The code will randomly select 1% of the households for one of the five products using the random method. Your code would generate numbers for 1% of the available household population. For example, there are 2,400 households on record for product 3. The households are individually numbered from 1 to 2,400. You need to generate 24 random numbers between 1-2,400. A table containing these numbers should be produced and printed to the screen. The table must not contain duplicates. Print 7 numbers per row.

You need to use at least one repetition statement, at least one array and the random method in this code. For generating random numbers you should use the random() static method of class Math. It returns a double so you will need to cast it as an integer. If you chose product three you would set the generator range to 2,400 and generate random numbers for 1% (24) of the households. These numbers should range between 1 and 2,400:

number = (int) ( range * Math.random() ) + 1;

Solution Preview

import java.util.Scanner;

public class AnalyticsNation {

public static void main(String[] args) {

//product info
int products[][] = {
{1, 3765},
{2, 3800},
{3, 2400},
{4, 4012},
{5, 3580}

Scanner scan = new Scanner(;
boolean stop = false;
String selection;

do {
//print menu
System.out.println("Select a product.");
System.out.println("1. product 1 (3,765)");
System.out.println("2. product 2 (3,800)");
System.out.println("3. product 3 (2,400)");
System.out.println("4. product 4 (4,012)");

Solution Summary

The expert examines Java programming with commented codes.