Class Bubble

java.lang.Object
  |
  +--Bubble

public class Bubble
extends java.lang.Object

Sample program for CISC 121, Fall 2000. This program reads a list of up to 10 integers, sorts them in ascending order, and prints them out. It uses the bubble sort algorithm.

Input: The integers to be sorted. These are read from the standard input. The program repeatedly prompts for an integer, then asks the user if s/he is done. This repeats until the user is done, or until the maximum number of integers is reached. If the user enters characters that are not integer format, the program will ask him/her to try again.

Output: The sorted list of integers, on the standard output.

Libraries Used: SavitchIn class for input.

Known Limitations: When the user answers the "are you done?" question, any character other than 'n' is considered a yes.

Author:
Margaret Lamb, tutorial X

Field Summary
private static int MAX_SIZE
          The maximum number of integers the program will accept
 
Constructor Summary
Bubble()
           
 
Method Summary
static void main(java.lang.String[] args)
          the main method for the program.
private static void printArray(int[] theArray, int size)
          prints an array of integers on the standard output, one per line.
private static int readArray(int[] inputArray)
          prompts for and reads an array of integers from the standard input.
private static void sortArray(int[] theArray, int size)
          sorts an array of integers in ascending order using the "bubble sort" algorithm.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

MAX_SIZE

private static final int MAX_SIZE
The maximum number of integers the program will accept
Constructor Detail

Bubble

public Bubble()
Method Detail

main

public static void main(java.lang.String[] args)
the main method for the program. Creates an array of integers, then calls other methods to read, sort, and print them.
Parameters:
args - The command-line arguments. Not used in this program, but required by Java

printArray

private static void printArray(int[] theArray,
                               int size)
prints an array of integers on the standard output, one per line.

assumptions: size is not negative, and the array has at least size elements

Parameters:
theArray - the array to be printed
size - the number of elements in the array to be printed

readArray

private static int readArray(int[] inputArray)
prompts for and reads an array of integers from the standard input. Repeatedly prompts for an integer, then asks the user if s/he is done. This repeats until the user is done, or until the maximum number of integers is reached. If the user enters characters that are not integer format, the method will ask him/her to try again.

Limitation: If the user answers the "are you done?" question with any character but a lower-case 'n', it is considered a yes.

Parameters:
inputArray - the array into which the integers are read
Returns:
the number of integers read

sortArray

private static void sortArray(int[] theArray,
                              int size)
sorts an array of integers in ascending order using the "bubble sort" algorithm.
 assumptions:
     1. size is not negative
     2. the array has least size elements
 
Parameters:
theArray - the array to be sorted
size - the number of elements in the array to be sorted