CIS163AA - Java Prog I - Sect 5811 - Quiz #2 - by Pat Moss
Due Monday, November 21, 2005
Name: Pat Moss
I. Definitions: Please explain the following terms:
1. documentation -- Supplemental information about a program, including
(p. 646) comments in a program's source code and printed
reports such as a user's guide.
2. default constructor --
a. constructor -- A special method in a class that is invoked when
(p. 644) an object is instantiated from the class. Used to
initialize the object.
b. default constructor -- If the programmer does not provide a
constructor, then Java provides a "default
constructor" that initializes the object.
3. instantiation -- The act of creating an object from a class.
(p. 650)
4. array -- A programming language construct used to store an
(p. 640) ordered list of primitive values or objects. Each
element in the array is referenced using a numerical
index from 0 to N-1, where N is the size of the array.
5. subscript -- (a.k.a. index)
(p. 660,649) The integer value used to specify a particular
element in an array.
6. length of an array -- The number of elements in an array. If there
a N elements, then the index range is 0 to N-1.
We can use the length constant, which is held in
the array object, to determine the array length.
(p. 374)
7. ArrayList class -- The ArrayList class is part of the java.util package
(p. 403-407,738-740) of the Java standard class library. It provides a
service similar to an array in that it can store a
list of values and reference them by an index.
However, whereas an array remains a fixed size
throughout its existence, an ArrayList object
dynamically grows and shrinks as needed. A data
element can be inserted or removed frm any location
(index) with a single method invocation.
8. initializer list -- A comma-separated list of values, delimited by
(p. 650) braces ({}), used to initialize and specify the
size of an array.
9. multidimensional array -- An array that uses more than one index to
(p. 654) specify a value stored in the array.
II. Question or short essay
1. What is the syntax of the command that declares and creates
an array object?
a. Primitive types
int[] grades; (p. 379)
int grades[]; (p. 379)
int[] height = new int[11]; (p. 371)
int[] scores = {87, 98, 97}; (p. 380: initializer list)
b. Array of objects
String[] words = new String[5]; (p. 382)
2. What does it mean for an array to do bounds checking? What happens
when a Java array is indexed with an invalid value? (p. 373-379)
The index operator performs automatic bounds checking, which ensures
that the index is in range for the array being referenced.
If the index is not valid, an exception called
ArrayIndexOutOfBoundsException is thrown.
3. How is an object-oriented program different from a procedure-oriented
program?
a. A procedure-oriented program is usually "self contained": All
code needed is generated "from scratch".
b. An object-oriented program extends classes from existing classes,
thus "reusing" and "extending" existing classes.
4. Compare and contrast check boxes and radio buttons.
a. check box: (p. 264-267)
(1) rectangular in appearance
(2) the user can select zero or more choices
(3) Listener: Uses ItemListener (p. 267)
b. radio button: (p. 267-272)
(1) round in appearance
(2) the user can select zero or one choices
(3) the choices within a group are mutually exclusive
(4) Listener: Uses ActionListener (p. 271)
5. How do you print a money value so that it is properly formatted
as currency? (p. 130-135)
import java.text.NumberFormat;
NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
System.out.println ("Total: " + fmt1.format(totalCost);
III. Programming Exercises
Design and implement an application that creates a histogram that
allows you to visually inspect the frequency distribution of a set
of values. The program should read in an arbitrary number of integers
that are in the range 1 to 100 inclusive, then produce a chart similar
to the one below that indicates how many input values fell in the
range 1-10, 11-20, and so on. Print one asterisk for each value entered.
Hand in a printed copy of your source code.
1 - 10 | *****
11 - 20 | **
21 - 30 | ************
31 - 40 |
41 - 50 | ***
51 - 60 | *******
61 - 70 | **
71 - 80 | *****
81 - 90 | ********
91 - 100 | *********