Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: Single Subscript Array
|
Posted: Nov 9, 2002 1:15 PM
|
|
Solution to the single-subscripted array problem:
You can download the code at: http://www.quantumhyperspace.com/SourceBank/IntegerTest.java
/* IntegerTest.java * @author: Charles Bell * @version: Nov 9, 2002 */
import javax.swing.*;
/** Problem: * Use a single-subscripted array to solve the following problem. * Read in 20 numbers, each of which is between 10 and 100, inclusive. * As each number is read, print it only if it is not a duplicate of a * number already read. Provide for the "worst case" in which all * 20 numbers are different. Use the smallest possible array * to solve this problem */ public class IntegerTest{
/* a single-subscripted array */ private int[] numbers; /* between 10 and 100, inclusive. */ private int min = 10; private int max = 100; /* 20 numbers */ private int problemSize = 20; private boolean debug = true;
public static void main(String[] args){ IntegerTest test = new IntegerTest(); test.runProblem(); System.exit(0); } /* Does all the work to complete the problem. */ public void runProblem(){ String message = "Enter a number between " + String.valueOf(min) + " and " + String.valueOf(max) + " inclusive."; /* Read in 20 numbers */ for (int i = 0; i < problemSize; i++){ int nextNumber = getNumberInRange(message); if (!testNumber(nextNumber, numbers)){ if (numbers == null) { numbers = new int[1]; numbers[0] = nextNumber; }else{ /* The array size will only increase if a new number is input. */ numbers = addArrayElement(nextNumber, numbers); } /* As each number is read, print it only if it is not * a duplicate of a number already read */ if (debug) System.out.println(nextNumber); JOptionPane.showMessageDialog(null, String.valueOf(nextNumber)); } } if (debug) System.out.println("numbers input: " + problemSize); /* The array size will be equal to the problemSize if all different numbers are entered. */ /* The array size will be less than the problemSize if duplicate numbers were entered. */ if (debug) System.out.println("array size: " + numbers.length); if (debug) { for (int i = 0; i < numbers.length; i++){ System.out.println("numbers[" + i + "] = " + numbers[i]); } } } /** Uses a JOptionPane input dialog to request a number string from the * the user, providing the user an input message. Checks to ensure the * number input is in the range required by the problem. Loops until * range requirement is met. */ private int getNumberInRange(String inputMessage){ int n = -1; while ((n < min) || (n > max)){ String input = JOptionPane.showInputDialog(null, inputMessage); try{ n = Integer.parseInt(input); if ((n < min) || (n > max)) JOptionPane.showMessageDialog(null, "That was an invalid integer. Try again.", "Error", JOptionPane.ERROR_MESSAGE); }catch(NumberFormatException nfe){ n = -1; System.err.println("NumberFormatException: " + nfe.getMessage()); JOptionPane.showMessageDialog(null, "That was invalid. Try again.", "Error", JOptionPane.ERROR_MESSAGE); } } return n; }
/* Test a number to see if in input array. */ private boolean testNumber(int n, int[] array){ boolean testStatus = false; if (array != null){ for (int i = 0; i < array.length; i++){ if (n == array[i]) testStatus = true; } } return testStatus; } /** Creates a new array from intarray adding an element with value n. */ public int[] addArrayElement(int n, int[] intarray){ int[] newarray = new int[intarray.length + 1]; for (int i = 0;i < intarray.length;i++){ newarray[i] = intarray[i]; } newarray[intarray.length] = n; return newarray; }
}
|
|