0% found this document useful (0 votes)
68 views75 pages

Advanced Topics in Java Core Concepts in Data Structures 1st Edition Noel Kalicharan (Auth.) Download

The document provides information on various ebooks related to advanced topics in Java and data structures, including links for instant downloads. It also includes a detailed overview of sorting algorithms such as selection sort and insertion sort, explaining their processes and implementations in Java. Additionally, the document contains a table of contents outlining the chapters and topics covered in the book.

Uploaded by

kikumiasciak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views75 pages

Advanced Topics in Java Core Concepts in Data Structures 1st Edition Noel Kalicharan (Auth.) Download

The document provides information on various ebooks related to advanced topics in Java and data structures, including links for instant downloads. It also includes a detailed overview of sorting algorithms such as selection sort and insertion sort, explaining their processes and implementations in Java. Additionally, the document contains a table of contents outlining the chapters and topics covered in the book.

Uploaded by

kikumiasciak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

Advanced Topics in Java Core Concepts in Data

Structures 1st Edition Noel Kalicharan (Auth.)


pdf download

https://ebookgate.com/product/advanced-topics-in-java-core-
concepts-in-data-structures-1st-edition-noel-kalicharan-auth/

Get Instant Ebook Downloads – Browse at https://ebookgate.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Java Structures Data Structures in Java for the Principled


Programmer 2nd edition Duane Bailey

https://ebookgate.com/product/java-structures-data-structures-in-java-
for-the-principled-programmer-2nd-edition-duane-bailey/

ebookgate.com

Data Structures Outside In with Java 1st Edition Sesh


Venugopal

https://ebookgate.com/product/data-structures-outside-in-with-
java-1st-edition-sesh-venugopal/

ebookgate.com

CAIA Level II Advanced Core Topics in Alternative


Investments 2nd Edition Caia Association

https://ebookgate.com/product/caia-level-ii-advanced-core-topics-in-
alternative-investments-2nd-edition-caia-association/

ebookgate.com

Core Topics in Pain 1st Edition Anita Holdcroft

https://ebookgate.com/product/core-topics-in-pain-1st-edition-anita-
holdcroft/

ebookgate.com
Java Software Structures Designing and Using Data
Structures 3rd Edition John Lewis

https://ebookgate.com/product/java-software-structures-designing-and-
using-data-structures-3rd-edition-john-lewis/

ebookgate.com

Data structures and algorithms made easy in Java data


structure and algorithmic puzzles 2nd Edition Narasimha
Karumanchi
https://ebookgate.com/product/data-structures-and-algorithms-made-
easy-in-java-data-structure-and-algorithmic-puzzles-2nd-edition-
narasimha-karumanchi/
ebookgate.com

Core topics in mechanical ventilation 1st Edition Iain


Mackenzie

https://ebookgate.com/product/core-topics-in-mechanical-
ventilation-1st-edition-iain-mackenzie/

ebookgate.com

AAGBI Core Topics in Anaesthesia 2015 1st Edition


Griffiths

https://ebookgate.com/product/aagbi-core-topics-in-
anaesthesia-2015-1st-edition-griffiths/

ebookgate.com

Data Structures Other Objects using Java 4th Edition


Michael Mann

https://ebookgate.com/product/data-structures-other-objects-using-
java-4th-edition-michael-mann/

ebookgate.com
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a Glance

About the Author���������������������������������������������������������������������������������������������������������������xiii


About the Technical Reviewers������������������������������������������������������������������������������������������ xv
Preface����������������������������������������������������������������������������������������������������������������������������� xvii

■■Chapter 1: Sorting, Searching, and Merging���������������������������������������������������������������������1


■■Chapter 2: Introduction to Objects����������������������������������������������������������������������������������29
■■Chapter 3: Linked Lists����������������������������������������������������������������������������������������������������71
■■Chapter 4: Stacks and Queues���������������������������������������������������������������������������������������111
■■Chapter 5: Recursion�����������������������������������������������������������������������������������������������������143
■■Chapter 6: Random Numbers, Games, and Simulation��������������������������������������������������167
■■Chapter 7: Working with Files���������������������������������������������������������������������������������������189
■■Chapter 8: Introduction to Binary Trees������������������������������������������������������������������������219
■■Chapter 9: Advanced Sorting�����������������������������������������������������������������������������������������259
■■Chapter 10: Hashing������������������������������������������������������������������������������������������������������287

Index���������������������������������������������������������������������������������������������������������������������������������309

v
Chapter 1

Sorting, Searching, and Merging

In this chapter, we will explain the following:


• How to sort a list of items using selection sort
• How to sort a list of items using insertion sort
• How to add a new item to a sorted list so that the list remains sorted
• How to sort an array of strings
• How to sort related (parallel) arrays
• How to search a sorted list using binary search
• How to search an array of strings
• How to write a program to do a frequency count of words in a passage
• How to merge two sorted lists to create one sorted list

1.1 Sorting an Array: Selection Sort


Sorting is the process by which a set of values are arranged in ascending or descending order. There are many reasons
to sort. Sometimes we sort in order to produce more readable output (for example, to produce an alphabetical listing).
A teacher may need to sort her students in order by name or by average score. If we have a large set of values and we
want to identify duplicates, we can do so by sorting; the repeated values will come together in the sorted list.
Another advantage of sorting is that some operations can be performed faster and more efficiently with sorted
data. For example, if data is sorted, it is possible to search it using binary search—this is much faster than using a
sequential search. Also, merging two separate lists of items can be done much faster than if the lists were unsorted.
There are many ways to sort. In this chapter, we will discuss two of the “simple” methods: selection and insertion
sort. In Chapter 9, we will look at more sophisticated ways to sort. We start with selection sort.
Consider the following list of numbers stored in a Java array, num:

1
Chapter 1 ■ Sorting, Searching, and Merging

Sorting num in ascending order using selection sort proceeds as follows:


1st pass
• Find the smallest number in the entire list, from positions 0 to 6; the smallest is 15,
found in position 4.
• Interchange the numbers in positions 0 and 4. This gives us the following:

2nd pass
• Find the smallest number in positions 1 to 6; the smallest is 33, found in position 5.
• Interchange the numbers in positions 1 and 5. This gives us the following:

3rd pass
• Find the smallest number in positions 2 to 6; the smallest is 48, found in position 5.
• Interchange the numbers in positions 2 and 5. This gives us the following:

4th pass
• Find the smallest number in positions 3 to 6; the smallest is 52, found in position 6.
• Interchange the numbers in positions 3 and 6. This gives us the following:

5th pass
• Find the smallest number in positions 4 to 6; the smallest is 57, found in position 4.
• Interchange the numbers in positions 4 and 4. This gives us the following:

2
Chapter 1 ■ Sorting, Searching, and Merging

6th pass
• Find the smallest number in positions 5 to 6; the smallest is 65, found in position 6.
• Interchange the numbers in positions 5 and 6. This gives us the following:

The array is now completely sorted. Note that once the 6th largest (65) has been placed in its final position (5),
the largest (79) would automatically be in the last position (6).
In this example, we made six passes. We will count these passes by letting the variable h go from 0 to 5. On each
pass, we find the smallest number from positions h to 6. If the smallest number is in position s, we interchange the
numbers in positions h and s.
In general, for an array of size n, we make n-1 passes. In our example, we sorted 7 numbers in 6 passes.
The following is a pseudocode outline of the algorithm for sorting num[0..n-1]:

for h = 0 to n - 2
s = position of smallest number from num[h] to num[n-1]
swap num[h] and num[s]
endfor

We can implement this algorithm as follows, using the generic parameter list:

public static void selectionSort(int[] list, int lo, int hi) {


//sort list[lo] to list[hi] in ascending order
for (int h = lo; h < hi; h++) {
int s = getSmallest(list, h, hi);
swap(list, h, s);
}
}

The two statements in the for loop could be replaced by the following:

swap(list, h, getSmallest(list, h, hi));

We can write getSmallest and swap as follows:

public static int getSmallest(int list[], int lo, int hi) {


//return location of smallest from list[lo..hi]
int small = lo;
for (int h = lo + 1; h <= hi; h++)
if (list[h] < list[small]) small = h;
return small;
}

public static void swap(int list[], int i, int j) {


//swap elements list[i] and list[j]
int hold = list[i];
list[i] = list[j];
list[j] = hold;
}

3
Chapter 1 ■ Sorting, Searching, and Merging

To test whether selectionSort works properly, we write Program P1.1. Only main is shown. To complete the
program, just add selectionSort, getSmallest, and swap.

Program P1.1
import java.util.*;
public class SelectSortTest {
final static int MaxNumbers = 10;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] num = new int[MaxNumbers];
System.out.printf("Type up to %d numbers followed by 0\n", MaxNumbers);
int n = 0;
int v = in.nextInt();
while (v != 0 && n < MaxNumbers) {
num[n++] = v;
v = in.nextInt();
}
if (v != 0) {
System.out.printf("\nMore than %d numbers entered\n", MaxNumbers);
System.out.printf("First %d used\n", MaxNumbers);
}
if (n == 0) {
System.out.printf("\nNo numbers supplied\n");
System.exit(1);
}
//n numbers are stored from num[0] to num[n-1]
selectionSort(num, 0, n-1);
System.out.printf("\nThe sorted numbers are\n");
for (v = 0; v < n; v++) System.out.printf("%d ", num[v]);
System.out.printf("\n");
} //end main

// selectionSort, getSmallest and swap go here

} //end class SelectSortTest

The program requests up to 10 numbers (as defined by MaxNumbers), stores them in the array num, calls
selectionSort, and then prints the sorted list.
The following is a sample run of the program:

Type up to 10 numbers followed by 0


57 48 79 65 15 33 52 0

The sorted numbers are


15 33 48 52 57 65 79

Note that if the user enters more than ten numbers, the program will recognize this and sort only the first ten.

4
Chapter 1 ■ Sorting, Searching, and Merging

1.1.1 Analysis of Selection Sort


To find the smallest of k items, we make k-1 comparisons. On the first pass, we make n-1 comparisons to find the
smallest of n items. On the second pass, we make n-2 comparisons to find the smallest of n-1 items. And so on, until
the last pass where we make one comparison to find the smaller of two items. In general, on the jth pass, we make n-j
comparisons to find the smallest of n-j+1 items. Hence, we have this:

total number of comparisons = 1 + 2 + …+ n-1 = ½ n(n-1) » ½ n2

We say selection sort is of order O(n2) (“big O n squared”). The constant ½ is not important in “big O” notation
since, as n gets very big, the constant becomes insignificant.
On each pass, we swap two items using three assignments. Since we make n-1 passes, we make 3(n-1)
assignments in all. Using “big O” notation, we say that the number of assignments is O(n). The constants 3 and 1 are
not important as n gets large.
Does selection sort perform any better if there is order in the data? No. One way to find out is to give it a sorted list
and see what it does. If you work through the algorithm, you will see that the method is oblivious to order in the data.
It will make the same number of comparisons every time, regardless of the data.
As we will see, some sorting methods (mergesort and quicksort; see Chapters 5 and 9) require extra array storage to
implement them. Note that selection sort is performed “in place” in the given array and does not require additional storage.
As an exercise, modify the programming code so that it counts the number of comparisons and assignments
made in sorting a list using selection sort.

1.2 Sorting an Array: Insertion Sort


Consider the same array as before:

Now, think of the numbers as cards on a table that are picked up one at a time in the order they appear in the
array. Thus, we first pick up 57, then 48, then 79, and so on, until we pick up 52. However, as we pick up each new
number, we add it to our hand in such a way that the numbers in our hand are all sorted.
When we pick up 57, we have just one number in our hand. We consider one number to be sorted.
When we pick up 48, we add it in front of 57 so our hand contains the following:
48 57
When we pick up 79, we place it after 57 so our hand contains this:
48 57 79
When we pick up 65, we place it after 57 so our hand contains this:
48 57 65 79
At this stage, four numbers have been picked up, and our hand contains them in sorted order.
When we pick up 15, we place it before 48 so our hand contains this:
15 48 57 65 79
When we pick up 33, we place it after 15 so our hand contains this:
15 33 48 57 65 79
Finally, when we pick up 52, we place it after 48 so our hand contains this:
15 33 48 52 57 65 79

5
Chapter 1 ■ Sorting, Searching, and Merging

The numbers have been sorted in ascending order.


The method described illustrates the idea behind insertion sort. The numbers in the array will be processed one
at a time, from left to right. This is equivalent to picking up the numbers from the table one at a time. Since the first
number, by itself, is sorted, we will process the numbers in the array starting from the second.
When we come to process num[h], we can assume that num[0] to num[h-1] are sorted. We insert num[h] among
num[0] to num[h-1] so that num[0] to num[h] are sorted. We then go on to process num[h+1]. When we do so, our
assumption that num[0] to num[h] are sorted will be true.
Sorting num in ascending order using insertion sort proceeds as follows:

1st pass
• Process num[1], that is, 48. This involves placing 48 so that the first two numbers are sorted;
num[0] and num[1] now contain the following:

The rest of the array remains unchanged.

2nd pass
• Process num[2], that is, 79. This involves placing 79 so that the first three numbers are sorted;
num[0] to num[2] now contain the following:

The rest of the array remains unchanged.

3rd pass
• Process num[3], that is, 65. This involves placing 65 so that the first four numbers are sorted;
num[0] to num[3] now contain the following:

The rest of the array remains unchanged.

4th pass
• Process num[4], that is, 15. This involves placing 15 so that the first five numbers are sorted.
To simplify the explanation, think of 15 as being taken out and stored in a simple variable
(key, say) leaving a “hole” in num[4]. We can picture this as follows:

6
Chapter 1 ■ Sorting, Searching, and Merging

The insertion of 15 in its correct position proceeds as follows:


• Compare 15 with 79; it is smaller, so move 79 to location 4, leaving location 3 free.
This gives the following:

• Compare 15 with 65; it is smaller, so move 65 to location 3, leaving location 2 free.


This gives the following:

• Compare 15 with 57; it is smaller, so move 57 to location 2, leaving location 1 free.


This gives the following:

• Compare 15 with 48; it is smaller, so move 48 to location 1, leaving location 0 free.


This gives the following:

• There are no more numbers to compare with 15, so it is inserted in location 0,


giving the following:

• We can express the logic of placing 15 (key) by comparing it with the numbers to its left,
starting with the nearest one. As long as key is less than num[k], for some k, we move num[k] to
position num[k + 1] and move on to consider num[k-1], providing it exists. It won’t exist when
k is actually 0. In this case, the process stops, and key is inserted in position 0.
5th pass
• Process num[5], that is, 33. This involves placing 33 so that the first six numbers are sorted.
This is done as follows:
• Store 33 in key, leaving location 5 free.
• Compare 33 with 79; it is smaller, so move 79 to location 5, leaving location 4 free.
• Compare 33 with 65; it is smaller, so move 65 to location 4, leaving location 3 free.
• Compare 33 with 57; it is smaller, so move 57 to location 3, leaving location 2 free.
• Compare 33 with 48; it is smaller, so move 48 to location 2, leaving location 1 free.

7
Chapter 1 ■ Sorting, Searching, and Merging

• Compare 33 with 15; it is bigger, so insert 33 in location 1. This gives the following:

• We can express the logic of placing 33 by comparing it with the numbers to its left, starting
with the nearest one. As long as key is less than num[k], for some k, we move num[k] to
position num[k + 1] and move on to consider num[k-1], providing it exists. If key is greater
than or equal to num[k] for some k, then key is inserted in position k+1. Here, 33 is greater
than num[0] and so is inserted into num[1].
6th pass
• Process num[6], that is, 52. This involves placing 52 so that the first seven (all) numbers are
sorted. This is done as follows:
• Store 52 in key, leaving location 6 free.
• Compare 52 with 79; it is smaller, so move 79 to location 6, leaving location 5 free.
• Compare 52 with 65; it is smaller, so move 65 to location 5, leaving location 4 free.
• Compare 52 with 57; it is smaller, so move 57 to location 4, leaving location 3 free.
• Compare 52 with 48; it is bigger; so insert 52 in location 3. This gives the following:

The array is now completely sorted.


The following is an outline of how to sort the first n elements of an array, num, using insertion sort:

for h = 1 to n - 1 do
insert num[h] among num[0] to num[h-1] so that num[0] to num[h] are sorted
endfor

Using this outline, we write the function insertionSort using the parameter list.

public static void insertionSort(int list[], int n) {


//sort list[0] to list[n-1] in ascending order
for (int h = 1; h < n; h++) {
int key = list[h];
int k = h - 1; //start comparing with previous item
while (k >= 0 && key < list[k]) {
list[k + 1] = list[k];
--k;
}
list[k + 1] = key;
} //end for
} //end insertionSort

The while statement is at the heart of the sort. It states that as long as we are within the array (k >= 0) and
the current number (key) is less than the one in the array (key < list[k]), we move list[k] to the right
(list[k+1] = list[k]) and move on to the next number on the left (--k).

8
Chapter 1 ■ Sorting, Searching, and Merging

We exit the while loop if k is equal to -1 or if key is greater than or equal to list[k], for some k. In either case,
key is inserted into list[k+1].
If k is -1, it means that the current number is smaller than all the previous numbers in the list and must be
inserted in list[0]. But list[k + 1] is list[0] when k is -1, so key is inserted correctly in this case.
The function sorts in ascending order. To sort in descending order, all we have to do is change < to > in the while
condition, like this:

while (k >= 0 && key > list[k])

Now, a key moves to the left if it is bigger.


We write Program P1.2 to test whether insertionSort works correctly. Only main is shown. Adding the function
insertionSort completes the program.

Program P1.2
import java.util.*;
public class InsertSortTest {
final static int MaxNumbers = 10;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] num = new int[MaxNumbers];
System.out.printf("Type up to %d numbers followed by 0\n", MaxNumbers);
int n = 0;
int v = in.nextInt();
while (v != 0 && n < MaxNumbers) {
num[n++] = v;
v = in.nextInt();
}
if (v != 0) {
System.out.printf("\nMore than %d numbers entered\n", MaxNumbers);
System.out.printf("First %d used\n", MaxNumbers);
}
if (n == 0) {
System.out.printf("\nNo numbers supplied\n");
System.exit(1);
}
//n numbers are stored from num[0] to num[n-1]
insertionSort(num, n);
System.out.printf("\nThe sorted numbers are\n");
for (v = 0; v < n; v++) System.out.printf("%d ", num[v]);
System.out.printf("\n");
} //end main

public static void insertionSort(int list[], int n) {


//sort list[0] to list[n-1] in ascending order
for (int h = 1; h < n; h++) {
int key = list[h];
int k = h - 1; //start comparing with previous item
while (k >= 0 && key < list[k]) {
list[k + 1] = list[k];
--k;
}

9
Chapter 1 ■ Sorting, Searching, and Merging

list[k + 1] = key;
} //end for
} //end insertionSort
  
} //end class InsertSortTest

The program requests up to ten numbers (as defined by MaxNumbers), stores them in the array num, calls
insertionSort, and then prints the sorted list.
The following is a sample run of the program:

Type up to 10 numbers followed by 0


57 48 79 65 15 33 52 0
The sorted numbers are
15 33 48 52 57 65 79

Note that if the user enters more than ten numbers, the program will recognize this and sort only the first ten.
We could easily generalize insertionSort to sort a portion of a list. To illustrate, we rewrite insertionSort
(calling it insertionSort1) to sort list[lo] to list[hi] where lo and hi are passed as arguments to the function.
Since element lo is the first one, we start processing elements from lo+1 until element hi. This is reflected in the
for statement. Also now, the lowest subscript is lo, rather than 0. This is reflected in the while condition k >= lo.
Everything else remains the same as before.

public static void insertionSort1(int list[], int lo, int hi) {


//sort list[lo] to list[hi] in ascending order
for (int h = lo + 1; h <= hi; h++) {
int key = list[h];
int k = h - 1; //start comparing with previous item
while (k >= lo && key < list[k]) {
list[k + 1] = list[k];
--k;
}
list[k + 1] = key;
} //end for
} //end insertionSort1

We can test insertionSort1 with Program P1.2a.

Program P1.2a
import java.util.*;
public class InsertSort1Test {
final static int MaxNumbers = 10;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] num = new int[MaxNumbers];
System.out.printf("Type up to %d numbers followed by 0\n", MaxNumbers);
int n = 0;
int v = in.nextInt();
while (v != 0 && n < MaxNumbers) {
num[n++] = v;
v = in.nextInt();
}

10
Chapter 1 ■ Sorting, Searching, and Merging

if (v != 0) {
System.out.printf("\nMore than %d numbers entered\n", MaxNumbers);
System.out.printf("First %d used\n", MaxNumbers);
}
if (n == 0) {
System.out.printf("\nNo numbers supplied\n");
System.exit(1);
}
//n numbers are stored from num[0] to num[n-1]
insertionSort1(num, 0, n-1);
System.out.printf("\nThe sorted numbers are\n");
for (v = 0; v < n; v++) System.out.printf("%d ", num[v]);
System.out.printf("\n");
} //end main

// insertionSort1 goes here

} //end class InsertSort1Test

1.2.1 Analysis of Insertion Sort


In processing item j, we can make as few as 1 comparison (if num[j] is bigger than num[j-1]) or as many as j-1
comparisons (if num[j] is smaller than all the previous items). For random data, we would expect to make ½(j-1)
comparisons, on average. Hence, the average total number of comparisons to sort n items is:

∑ 2 ( j − 1) = ½ {1 + 2 + ... + n − 1} = ¼ n (n − 1) ≈ ¼ n
1 2

j =2

We say insertion sort is of order O(n2) (“big O n squared”). The constant ¼ is not important as n gets large.
Each time we make a comparison, we also make an assignment. Hence, the total number of assignments is also
¼ n(n-1) » ¼ n2.
We emphasize that this is an average for random data. Unlike selection sort, the actual performance of insertion
sort depends on the data supplied. If the given array is already sorted, insertion sort will quickly determine this by
making n-1 comparisons. In this case, it runs in O(n) time. One would expect that insertion sort will perform better
the more order there is in the data.
If the given data is in descending order, insertion sort performs at its worst since each new number has to travel
all the way to the beginning of the list. In this case, the number of comparisons is ½ n(n-1) » ½ n2. The number of
assignments is also ½ n(n-1) » ½ n2.
Thus, the number of comparisons made by insertion sort ranges from n-1 (best) to ¼ n2 (average) to ½ n2 (worst).
The number of assignments is always the same as the number of comparisons.
As with selection sort, insertion sort does not require extra array storage for its implementation.
As an exercise, modify the programming code so that it counts the number of comparisons and assignments
made in sorting a list using insertion sort.

11
Chapter 1 ■ Sorting, Searching, and Merging

1.3 Inserting an Element in Place


Insertion sort uses the idea of adding a new element to an already sorted list so that the list remains sorted. We can
treat this as a problem in its own right (nothing to do with insertion sort). Specifically, given a sorted list of items from
list[m] to list[n], we want to add a new item (newItem, say) to the list so that list[m] to list[n+1] are sorted.
Adding a new item increases the size of the list by 1. We assume that the array has room to hold the new item.
We write the function insertInPlace to solve this problem.

public static void insertInPlace(int newItem, int list[], int m, int n) {


//list[m] to list[n] are sorted
//insert newItem so that list[m] to list[n+1] are sorted
int k = n;
while (k >= m && newItem < list[k]) {
list[k + 1] = list[k];
--k;
}
list[k + 1] = newItem;
} //end insertInPlace

Using insertInPlace, we can rewrite insertionSort (calling it insertionSort2) as follows:

public static void insertionSort2(int list[], int lo, int hi) {


//sort list[lo] to list[hi] in ascending order
for (int h = lo + 1; h <= hi; h++)
insertInPlace(list[h], list, lo, h - 1);
} //end insertionSort2

We can test insertionSort2 with Program P1.2b.

Program P1.2b
import java.util.*;
public class InsertSort2Test {
final static int MaxNumbers = 10;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] num = new int[MaxNumbers];
System.out.printf("Type up to %d numbers followed by 0\n", MaxNumbers);
int n = 0;
int v = in.nextInt();
while (v != 0 && n < MaxNumbers) {
num[n++] = v;
v = in.nextInt();
}
if (v != 0) {
System.out.printf("\nMore than %d numbers entered\n", MaxNumbers);
System.out.printf("First %d used\n", MaxNumbers);
}
if (n == 0) {
System.out.printf("\nNo numbers supplied\n");
System.exit(1);
}

12
Chapter 1 ■ Sorting, Searching, and Merging

//n numbers are stored from num[0] to num[n-1]


insertionSort2(num, 0, n-1);
System.out.printf("\nThe sorted numbers are\n");
for (v = 0; v < n; v++) System.out.printf("%d ", num[v]);
System.out.printf("\n");
} //end main

public static void insertionSort2(int list[], int lo, int hi) {


//sort list[lo] to list[hi] in ascending order
for (int h = lo + 1; h <= hi; h++)
insertInPlace(list[h], list, lo, h - 1);
} //end insertionSort2

public static void insertInPlace(int newItem, int list[], int m, int n) {


//list[m] to list[n] are sorted
//insert newItem so that list[m] to list[n+1] are sorted
int k = n;
while (k >= m && newItem < list[k]) {
list[k + 1] = list[k];
--k;
}
list[k + 1] = newItem;
} //end insertInPlace

} //end class InsertSort2Test

1.4 Sorting a String Array


Consider the problem of sorting a list of names in alphabetical order. In Java, a name is stored in a String variable,
and we’ll need a String array to store the list. For the most part, we can work with String as if it were a primitive
type, but it is sometimes useful to remember that, strictly speaking, it is a class. Where necessary, we will point out
the distinction.
One difference that concerns us here is that we cannot use the relational operators (==, <, >, and so on) to
compare strings. We must use functions from the String class (or write our own). Common functions include equals,
equalsIgnoreCase, compareTo, and compareToIgnoreCase. We write a function to sort an array of strings using
insertion sort. We call it insertionSort3.

public static void insertionSort3(String[] list, int lo, int hi) {


//sort list[lo] to list[hi] in ascending order
for (int h = lo + 1; h <= hi; h++) {
String key = list[h];
int k = h - 1; //start comparing with previous item
while (k >= lo && key.compareToIgnoreCase(list[k]) < 0) {
list[k + 1] = list[k];
--k;
}
list[k + 1] = key;
} //end for
} //end insertionSort3

13
Chapter 1 ■ Sorting, Searching, and Merging

The function is pretty much the same as the previous ones except for the declaration of list and the use of
compareToIgnoreCase to compare two strings. If case matters, you can use compareTo.
We test insertionSort3 with Program P1.3.

Program P1.3
import java.util.*;
public class SortStrings {
final static int MaxNames = 8;
public static void main(String[] args) {
String name[] = {"Graham, Ariel", "Perrott, Chloe",
"Charles, Kandice", "Seecharan, Anella", "Reyes, Aaliyah",
"Graham, Ashleigh", "Reyes, Ayanna", "Greaves, Sherrelle" };

insertionSort3(name, 0, MaxNames - 1);

System.out.printf("\nThe sorted names are\n\n");


for (int h = 0; h < MaxNames; h++)
System.out.printf("%s\n", name[h]);
} //end main

// insertionSort3 goes here

} //end class SortStrings

When run, Program P1.3 produced the following output:

The sorted names are

Charles, Kandice
Graham, Ariel
Graham, Ashleigh
Greaves, Sherrelle
Perrott, Chloe
Reyes, Aaliyah
Reyes, Ayanna
Seecharan, Anella

1.5 Sorting Parallel Arrays


It is quite common to have related information in different arrays. For example, suppose, in addition to name, we have
an integer array id such that id[h] is an identification number associated with name[h], as shown in Figure 1-1.

14
Chapter 1 ■ Sorting, Searching, and Merging

name id

0 Graham, Ariel 3050


1 Perrott, Chloe 2795
2 Charles, Kandice 4455
3 Seecharan, Anella 7824
4 Reyes, Aaliyah 6669
5 Graham, Ashleigh 5000
6 Reyes, Ayanna 5464
7 Greaves, Sherrelle 6050

Figure 1-1. Two arrays with related information

Consider the problem of sorting the names in alphabetical order. At the end, we would want each name to have
its correct ID number. So, for example, after the sorting is done, name[0] should contain “Charles, Kandice” and id[0]
should contain 4455.
To achieve this, each time a name is moved during the sorting process, the corresponding ID number must also
be moved. Since the name and ID number must be moved “in parallel,” we say we are doing a “parallel sort” or we are
sorting “parallel arrays.”
We rewrite insertionSort3 to illustrate how to sort parallel arrays. We simply add the code to move an ID
whenever a name is moved. We call it parallelSort.

public static void parallelSort(String[] list, int id[], int lo, int hi) {
//Sort the names in list[lo] to list[hi] in alphabetical order,
//ensuring that each name remains with its original id number.
for (int h = lo + 1; h <= hi; h++) {
String key = list[h];
int m = id[h]; // extract the id number
int k = h - 1; //start comparing with previous item
while (k >= lo && key.compareToIgnoreCase(list[k]) < 0) {
list[k + 1] = list[k];
id[k+ 1] = id[k]; //move up id number when we move a name
--k;
}
list[k + 1] = key;
id[k + 1] = m; //store the id number in the same position as the name
} //end for
} //end parallelSort

We test parallelSort by writing Program P1.4.

Program P1.4
import java.util.*;
public class ParallelSort {
final static int MaxNames = 8;
public static void main(String[] args) {
String name[] = {"Graham, Ariel", "Perrott, Chloe",
"Charles, Kandice", "Seecharan, Anella", "Reyes, Aaliyah",
"Graham, Ashleigh", "Reyes, Ayanna", "Greaves, Sherrelle" };
int id[] = {3050,2795,4455,7824,6669,5000,5464,6050};

parallelSort(name, id, 0, MaxNames - 1);

15
Chapter 1 ■ Sorting, Searching, and Merging

System.out.printf("\nThe sorted names and IDs are\n\n");


for (int h = 0; h < MaxNames; h++)
System.out.printf("%-20s %d\n", name[h], id[h]);
} //end main

// parallelSort goes here

} //end class ParallelSort

When Program P1.4 was run, it produced the following output:

The sorted names and IDs are

Charles, Kandice 4455


Graham, Ariel 3050
Graham, Ashleigh 5000
Greaves, Sherrelle 6050
Perrott, Chloe 2795
Reyes, Aaliyah 6669
Reyes, Ayanna 5464
Seecharan, Anella 7824

We note, in passing, that if we have several sets of related items to process, storing each set in a separate array
would not be the best way to proceed. It would be better to group the items in a class and work with the group as we
would a single item. We’ll show you how to do this in Section 2.14.

1.6 Binary Search


Binary search is a fast method for searching a list of items for a given one, providing the list is sorted (either ascending
or descending). To illustrate the method, consider a list of 13 numbers, sorted in ascending order and stored in an
array num[0..12].

Suppose we want to search for 66. The search proceeds as follows:


1. We find the middle item in the list. This is 56 in position 6. We compare 66 with 56. Since 66
is bigger, we know that if 66 is in the list at all, it must be after position 6, since the numbers
are in ascending order. In our next step, we confine our search to locations 7 to 12.
2. We find the middle item from locations 7 to 12. In this case, we can choose either item 9 or
item 10. The algorithm we will write will choose item 9, that is, 78.
3. We compare 66 with 78. Since 66 is smaller, we know that if 66 is in the list at all, it must be
before position 9, since the numbers are in ascending order. In our next step, we confine
our search to locations 7 to 8.
4. We find the middle item from locations 7 to 8. In this case, we can choose either item 7 or
item 8. The algorithm we will write will choose item 7, that is, 66.
5. We compare 66 with 66. Since they are the same, our search ends successfully, finding the
required item in position 7.
16
Chapter 1 ■ Sorting, Searching, and Merging

Suppose we were searching for 70. The search will proceed as described above until we compare 70 with 66
(in location 7).
• Since 70 is bigger, we know that if 70 is in the list at all, it must be after position 7, since the
numbers are in ascending order. In our next step, we confine our search to locations 8 to 8.
This is just one location.
• We compare 70 with item 8, that is, 72. Since 70 is smaller, we know that if 70 is in the list at
all, it must be before position 8. Since it can’t be after position 7 and before position 8, we
conclude that it is not in the list.
At each stage of the search, we confine our search to some portion of the list. Let us use the variables lo and hi
as the subscripts that define this portion. In other words, our search will be confined to num[lo] to num[hi].
Initially, we want to search the entire list so that we will set lo to 0 and hi to 12, in this example.
How do we find the subscript of the middle item? We will use the following calculation:
mid = (lo + hi) / 2;

Since integer division will be performed, the fraction, if any, is discarded. For example, when lo is 0 and hi is 12,
mid becomes 6; when lo is 7 and hi is 12, mid becomes 9; and when lo is 7 and hi is 8, mid becomes 7.
As long as lo is less than or equal to hi, they define a nonempty portion of the list to be searched. When lo is
equal to hi, they define a single item to be searched. If lo ever gets bigger than hi, it means we have searched the
entire list and the item was not found.
Based on these ideas, we can now write a function binarySearch. To be more general, we will write it so that the
calling routine can specify which portion of the array it wants the search to look for the item.
Thus, the function must be given the item to be searched for (key), the array (list), the start position of the
search (lo), and the end position of the search (hi). For example, to search for the number 66 in the array num, above,
we can issue the call binarySearch(66, num, 0, 12).
The function must tell us the result of the search. If the item is found, the function will return its location. If not
found, it will return -1.

public static int binarySearch(int key, int[] list, int lo, int hi) {
//search for key from list[lo] to list[hi]
//if found, return its location; otherwise, return -1
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (key == list[mid]) return mid; // found
if (key < list[mid]) hi = mid - 1;
else lo = mid + 1;
}
return -1; //lo and hi have crossed; key not found
}

If item contains a number to be searched for, we can write code as follows:

int ans = binarySearch(item, num, 0, 12);


if (ans == -1) System.out.printf("%d not found\n", item);
else System.out.printf("%d found in location %d\n", item, ans);

If we want to search for item from locations i to j, we can write the following:

int ans = binarySearch(item, num, i, j);

17
Chapter 1 ■ Sorting, Searching, and Merging

We can test binarySearch with Program P1.5.

Program P1.5
public class BinarySearchTest {
public static void main(String[] args) {
int[] num = {17, 24, 31, 39, 44, 49, 56, 66, 72, 78, 83, 89, 96};
int n = binarySearch(66, num, 0, 12);
System.out.printf("%d\n", n); //will print 7; 66 in pos. 7
n = binarySearch(66, num, 0, 6);
System.out.printf("%d\n", n); //will print -1; 66 not in 0 to 6
n = binarySearch(70, num, 0, 12);
System.out.printf("%d\n", n); //will print -1; 70 not in list
n = binarySearch(89, num, 5, 12);
System.out.printf("%d\n", n); //will print 11; 89 in pos. 11
} //end main

// binarySearch goes here


} //end class BinarySearchTest

When run, the program will print the following:

7
-1
-1
11

1.7 Searching an Array of Strings


We can search a sorted array of strings (names in alphabetical order, say) using the same technique we used for
searching an integer array. The major differences are in the declaration of the array and the use of the String function
compareTo, rather than == or <, to compare two strings. The following is the string version of binarySearch:

public static int binarySearch(String key, String[] list, int lo, int hi) {
//search for key from list[lo] to list[hi]
//if found, return its location; otherwise, return -1
while (lo <= hi) {
int mid = (lo + hi) / 2;
int cmp = key.compareTo(list[mid]);
if (cmp == 0) return mid; // search succeeds
if (cmp < 0) hi = mid -1; // key is ‘less than’ list[mid]
else lo = mid + 1; // key is ‘greater than’ list[mid]
}
return -1; //lo and hi have crossed; key not found
} //end binarySearch

Since we need to know whether one string is equal to, or less than, another, it is best to use the compareTo
method.

18
Chapter 1 ■ Sorting, Searching, and Merging

Note that we call compareTo only once. The value returned (cmp) tells us all we need to know. If we are
comparing words or names and we want the case of the letters to be ignored in the comparison, we can use
compareToIgnoreCase.
The function can be tested with Program P1.6.

Program P1.6
import java.util.*;
public class BinarySearchString {
final static int MaxNames = 8;
public static void main(String[] args) {
String name[] = {"Charles, Kandice", "Graham, Ariel",
"Graham, Ashleigh", "Greaves, Sherrelle", "Perrott, Chloe",
"Reyes, Aaliyah", "Reyes, Ayanna", "Seecharan, Anella"};

int n = binarySearch("Charles, Kandice", name, 0, MaxNames - 1);


System.out.printf("%d\n", n);
//will print 0, location of Charles, Kandice

n = binarySearch("Reyes, Ayanna", name, 0, MaxNames - 1);


System.out.printf("%d\n", n);
//will print 6, location of Reyes, Ayanna

n = binarySearch("Perrott, Chloe", name, 0, MaxNames - 1);


System.out.printf("%d\n", n);
//will print 4, location of Perrott, Chloe

n = binarySearch("Graham, Ariel", name, 4, MaxNames - 1);


System.out.printf("%d\n", n);
//will print -1, since Graham, Ariel is not in locations 4 to 7

n = binarySearch("Cato, Brittney", name, 0, MaxNames - 1);


System.out.printf("%d\n", n);
//will print -1 since Cato, Brittney is not in the list

} //end main

// binarySearch goes here

} //end class BinarySearchString

This sets up the array name with the names in alphabetical order. It then calls binarySearch with various names
and prints the result of each search.
One may wonder what might happen with a call like this:

n = binarySearch("Perrott, Chloe", name, 5, 10);

Here, we are telling binarySearch to look for "Perrott, Chloe" in locations 5 to 10 of the given array. However,
locations 8 to 10 do not exist in the array. The result of the search will be unpredictable. The program may crash or
return an incorrect result. The onus is on the calling program to ensure that binarySearch (or any other function) is
called with valid arguments.

19
Chapter 1 ■ Sorting, Searching, and Merging

1.8 Example: Word Frequency Count


Let’s write a program to read an English passage and count the number of times each word appears. Output consists
of an alphabetical listing of the words and their frequencies.
We can use the following outline to develop our program:

while there is input


get a word
search for word
if word is in the table
add 1 to its count
else
add word to the table
set its count to 1
endif
endwhile
print table

This is a typical “search and insert” situation. We search for the next word among the words stored so far. If the search
succeeds, the only thing to do is increment its count. If the search fails, the word is put in the table and its count set to 1.
A major design decision here is how to search the table, which, in turn, will depend on where and how a new
word is inserted in the table. The following are two possibilities:
1. A new word is inserted in the next free position in the table. This implies that a sequential
search must be used to look for an incoming word since the words would not be in any
particular order. This method has the advantages of simplicity and easy insertion, but
searching takes longer as more words are put in the table.
2. A new word is inserted in the table in such a way that the words are always in alphabetical
order. This may entail moving words that have already been stored so that the new word
may be slotted in the right place. However, since the table is in order, a binary search can
be used to search for an incoming word.
For (2), searching is faster, but insertion is slower than in (1). Since, in general, searching is done more frequently
than inserting, (2) might be preferable.
Another advantage of (2) is that, at the end, the words will already be in alphabetical order and no sorting will be
required. If (1) is used, the words will need to be sorted to obtain the alphabetical order.
We will write our program using the approach in (2). The complete program is shown as Program P1.7.

Program P1.7
import java.io.*;
import java.util.*;
public class WordFrequency {
final static int MaxWords = 50;
public static void main(String[] args) throws IOException {
String[] wordList = new String[MaxWords];
int[] frequency = new int[MaxWords];
FileReader in = new FileReader("passage.txt");
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));

for (int h = 0; h < MaxWords; h++) {


frequency[h] = 0;
wordList[h] = "";
}

20
Chapter 1 ■ Sorting, Searching, and Merging

int numWords = 0;
String word = getWord(in).toLowerCase();
while (!word.equals("")) {
int loc = binarySearch(word, wordList, 0, numWords-1);
if (word.compareTo(wordList[loc]) == 0) ++frequency[loc]; //word found
else //this is a new word
if (numWords < MaxWords) { //if table is not full
addToList(word, wordList, frequency, loc, numWords-1);
++numWords;
}
else out.printf("'%s' not added to table\n", word);
word = getWord(in).toLowerCase();
}
printResults(out, wordList, frequency, numWords);
in.close();
out.close();
} // end main

public static int binarySearch(String key, String[] list, int lo, int hi){
//search for key from list[lo] to list[hi]
//if found, return its location;
//if not found, return the location in which it should be inserted
//the calling program will check the location to determine if found
while (lo <= hi) {
int mid = (lo + hi) / 2;
int cmp = key.compareTo(list[mid]);
if (cmp == 0) return mid; // search succeeds
if (cmp < 0) hi = mid -1; // key is 'less than' list[mid]
else lo = mid + 1; // key is 'greater than' list[mid]
}
return lo; //key must be inserted in location lo
} //end binarySearch

public static void addToList(String item, String[] list, int[] freq, int p, int n) {
//adds item in position list[p]; sets freq[p] to 1
//shifts list[n] down to list[p] to the right
for (int h = n; h >= p; h--) {
list[h + 1] = list[h];
freq[h + 1] = freq[h];
}
list[p] = item;
freq[p] = 1;
} //end addToList

public static void printResults(PrintWriter out, String[] list, int freq[], int n) {
out.printf("\nWords Frequency\n\n");
for (int h = 0; h < n; h++)
out.printf("%-20s %2d\n", list[h], freq[h]);
} //end printResults

21
Chapter 1 ■ Sorting, Searching, and Merging

public static String getWord(FileReader in) throws IOException {


//returns the next word found
final int MaxLen = 255;
int c, n = 0;
char[] word = new char[MaxLen];
// read over non-letters
while (!Character.isLetter((char) (c = in.read())) && (c != -1)) ;
//empty while body
if (c == -1) return ""; //no letter found
word[n++] = (char) c;
while (Character.isLetter(c = in.read()))
if (n < MaxLen) word[n++] = (char) c;
return new String(word, 0, n);
} // end getWord

} //end class WordFrequency

Suppose the following data is stored in passage.txt:

Be more concerned with your character than your reputation,


because your character is what you really are,
while your reputation is merely what others think you are.
Our character is what we do when we think no one is looking.

When Program P1.7 is run, it stores its output in output.txt. Here is the output:

Words Frequency

are 2
be 1
because 1
character 3
concerned 1
do 1
is 4
looking 1
merely 1
more 1
no 1
one 1
others 1
our 1
really 1
reputation 2
than 1
think 2
we 2
what 3
when 1
while 1
with 1
you 2
your 4

22
Chapter 1 ■ Sorting, Searching, and Merging

Here are some comments on Program P1.7:


• For our purposes, we assume that a word begins with a letter and consists of letters only.
If you want to include other characters (such as a hyphen or apostrophe), you need only
change the getWord function.
• MaxWords denotes the maximum number of distinct words catered for. For testing the
program, we have used 50 for this value. If the number of distinct words in the passage
exceeds MaxWords (50, say), any words after the 50th will be read but not stored, and a message
to that effect will be printed. However, the count for a word already stored will be incremented
if it is encountered again.
• main initializes the frequency counts to 0 and the items in the String array to the empty
string. It then processes the words in the passage based on the outline shown at the start of
Section 1.8.
• getWord reads the input file and returns the next word found.
• All words are converted to lowercase so that, for instance, The and the are counted as the same
word.
• binarySearch is written so that if the word is found, its location is returned. If the word is not
found, then the location in which it should be inserted is returned. The function addToList
is given the location in which to insert a new word. Words to the right of, and including, this
location, are shifted one position to make room for the new word.

1.9 Merging Ordered Lists


Merging is the process by which two or more ordered lists are combined into one ordered list. For example, given two
lists of numbers, A and B, as follows:
A: 21 28 35 40 61 75
B: 16 25 47 54
they can be combined into one ordered list, C:
C: 16 21 25 28 35 40 47 54 61 75
The list C contains all the numbers from lists A and B. How can the merge be performed?
One way to think about it is to imagine that the numbers in the given lists are stored on cards, one per card, and
the cards are placed face up on a table, with the smallest at the top. We can imagine the lists A and B as follows:
21 16
28 25
35 47
40 54
61
75
We look at the top two cards, 21 and 16. The smaller, 16, is removed and placed in C. This exposes the number 25.
The top two cards are now 21 and 25. The smaller, 21, is removed and added to C, which now contains 16 21. This
exposes the number 28.
The top two cards are now 28 and 25. The smaller, 25, is removed and added to C, which now contains 16 21 25.
This exposes the number 47.
The top two cards are now 28 and 47. The smaller, 28, is removed and added to C, which now contains 16 21 25 28.
This exposes the number 35.

23
Chapter 1 ■ Sorting, Searching, and Merging

The top two cards are now 35 and 47. The smaller, 35, is removed and added to C, which now contains 16 21 25
28 35. This exposes the number 40.
The top two cards are now 40 and 47. The smaller, 40, is removed and added to C, which now contains 16 21 25
28 35 40. This exposes the number 61.
The top two cards are now 61 and 47. The smaller, 47, is removed and added to C, which now contains 16 21 25
28 35 40 47. This exposes the number 54.
The top two cards are now 61 and 54. The smaller, 54, is removed and added to C, which now contains 16 21 25
28 35 40 47 54. The list B has no more numbers.
We copy the remaining elements (61 75) of A to C, which now contains the following:
16 21 25 28 35 40 47 54 61 75
The merge is completed.

At each step of the merge, we compare the smallest remaining number of A with the smallest remaining number
of B. The smaller of these is added to C. If the smaller comes from A, we move on to the next number in A; if the smaller
comes from B, we move on to the next number in B.
This is repeated until all the numbers in either A or B have been used. If all the numbers in A have been used, we add
the remaining numbers from B to C. If all the numbers in B have been used, we add the remaining numbers from A to C.
We can express the logic of the merge as follows:

while (at least one number remains in both A and B) {


if (smallest in A < smallest in B)
add smallest in A to C
move on to next number in A
else
add smallest in B to C
move on to next number in B
endif
}
if (A has ended) add remaining numbers in B to C
else add remaining numbers in A to C

1.9.1 Implementing the Merge


Assume that an array A contains m numbers stored in A[0] to A[m-1] and an array B contains n numbers stored in
B[0] to B[n-1]. Assume that the numbers are stored in ascending order. We want to merge the numbers in A and B
into another array C such that C[0] to C[m+n-1] contains all the numbers in A and B sorted in ascending order.
We will use integer variables i, j, and k to subscript the arrays A, B, and C, respectively. “Moving on to the next
position” in an array can be done by adding 1 to the subscript variable. We can implement the merge with this
function:

public static int merge(int[] A, int m, int[] B, int n, int[] C) {


int i = 0; //i points to the first (smallest) number in A
int j = 0; //j points to the first (smallest) number in B
int k = -1; //k will be incremented before storing a number in C[k]
while (i < m && j < n) {
if (A[i] < B[j]) C[++k] = A[i++];
else C[++k] = B[j++];
}
if (i == m) ///copy B[j] to B[n-1] to C
for ( ; j < n; j++) C[++k] = B[j];

24
Chapter 1 ■ Sorting, Searching, and Merging

else // j == n, copy A[i] to A[m-1] to C


for ( ; i < m; i++) C[++k] = A[i];
return m + n;
} //end merge

The function takes the arguments A, m, B, n, and C, performs the merge, and returns the number of elements,
m + n, in C.
Program P1.8 shows a simple main function that tests the logic of merge. It sets up arrays A and B, calls merge,
and prints C. When run, the program prints the following:

16 21 25 28 35 40 47 54 61 75

Program P1.8
public class MergeTest {
public static void main(String[] args) {
int[] A = {21, 28, 35, 40, 61, 75}; //size 6
int[] B = {16, 25, 47, 54}; //size 4
int[] C = new int[20]; //enough to hold all the elements
int n = merge(A, 6, B, 4, C);
for (int j = 0; j < n; j++) System.out.printf("%d ", C[j]);
System.out.printf("\n");
} //end main

// merge goes here

} //end class MergeTest

As a matter of interest, we can also implement merge as follows:

public static int merge(int[] A, int m, int[] B, int n, int[] C) {


int i = 0; //i points to the first (smallest) number in A
int j = 0; //j points to the first (smallest) number in B
int k = -1; //k will be incremented before storing a number in C[k]
while (i < m || j < n) {
if (i == m) C[++k] = B[j++];
else if (j == n) C[++k] = A[i++];
else if (A[i] < B[j]) C[++k] = A[i++];
else C[++k] = B[j++];
}
return m + n;
}

The while loop expresses the following logic: as long as there is at least one element to process in either A or B,
we enter the loop. If we are finished with A (i == m), copy an element from B to C. If we are finished with B (j == n),
copy an element from A to C. Otherwise, copy the smaller of A[i] and B[j] to C. Each time we copy an element from
an array, we add 1 to the subscript for that array.
While the previous version implements the merge in a straightforward way, it seems reasonable to say that this
version is a bit neater.

25
Chapter 1 ■ Sorting, Searching, and Merging

EXERCISES 1

1. A survey of 10 pop artists is made. Each person votes for an artist by specifying the number
of the artist (a value from 1 to 10). Each voter is allowed one vote for the artist of their choice.
The vote is recorded as a number from 1 to 10. The number of voters is unknown beforehand,
but the votes are terminated by a vote of 0. Any vote that is not a number from 1 to 10 is
a spoiled vote. A file, votes.txt, contains the names of the candidates. The first name is
considered as candidate 1, the second as candidate 2, and so on. The names are followed by
the votes. Write a program to read the data and evaluate the results of the survey.
Print the results in alphabetical order by artist name and in order by votes received (most votes first).
Print all output to the file, results.txt.
2. Write a program to read names and phone numbers into two arrays. Request a name and
print the person’s phone number. Use binary search to look up the name.
3. Write a program to read English words and their equivalent Spanish words into two arrays.
Request the user to type several English words. For each, print the equivalent Spanish word.
Choose a suitable end-of-data marker. Search for the typed words using binary search.
Modify the program so that the user types Spanish words instead.
4. The median of a set of n numbers (not necessarily distinct) is obtained by arranging the
numbers in order and taking the number in the middle. If n is odd, there is a unique middle
number. If n is even, then the average of the two middle values is the median. Write a
program to read a set of n positive integers (assume n < 100) and print their median; n is not
given, but 0 indicates the end of the data.
5. The mode of a set of n numbers is the number that appears most frequently. For example, the
mode of 7 3 8 5 7 3 1 3 4 8 9 is 3. Write a program to read a set of n positive integers
(assume n < 100) and print their mode; n is not given, but 0 indicates the end of the data.
6. An array chosen contains n distinct integers arranged in no particular order. Another array
called winners contains m distinct integers arranged in ascending order. Write code to
determine how many of the numbers in chosen appear in winners.
7. A multiple-choice examination consists of 20 questions. Each question has five choices,
labeled A, B, C, D, and E. The first line of data contains the correct answers to the 20
questions in the first 20 consecutive character positions, for example:
BECDCBAADEBACBAEDDBE
Each subsequent line contains the answers for a candidate. Data on a line consists of a
candidate number (an integer), followed by one or more spaces, followed by the 20 answers
given by the candidate in the next 20 consecutive character positions. An X is used if a
candidate did not answer a particular question. You may assume all data is valid and stored
in a file exam.dat. A sample line is as follows:
4325 BECDCBAXDEBACCAEDXBE
There are at most 100 candidates. A line containing a “candidate number” 0 only indicates
the end of the data.

26
Chapter 1 ■ Sorting, Searching, and Merging

Points for a question are awarded as follows—correct answer: 4 points; wrong answer:
-1 point; no answer: 0 points.
Write a program to process the data and print a report consisting of candidate number and
the total points obtained by the candidate, in ascending order by candidate number. At the
end, print the average number of points gained by the candidates.
8. A is an array sorted in descending order. B is an array sorted in descending order. Merge A and
B into C so that C is in descending order.

9. A is an array sorted in descending order. B is an array sorted in descending order. Merge A and
B into C so that C is in ascending order.

10. A is an array sorted in ascending order. B is an array sorted in descending order. Merge A and
B into C so that C is in ascending order.

11. An array A contains integers that first increase in value and then decrease in value. Here’s an
example:

It is unknown at which point the numbers start to decrease. Write efficient code to code to
copy the numbers in A to another array B so that B is sorted in ascending order. Your code
must take advantage of the way the numbers are arranged in A.
12. Two words are anagrams if one word can be formed by rearranging all the letters of the
other word, for example: section, notices. Write a program to read two words and determine
whether they are anagrams.
Write another program to read a list of words and find all sets of words such that words
within a set are anagrams of each other.

27
Chapter 2

Introduction to Objects

In this chapter, we will explain the following:


• What is a class, an object, a field, and a method
• That an object variable does not hold an object but, rather, a pointer (or reference) to where
the object is actually located
• The distinction between a class variable (also called a static variable) and an instance variable
(also called a non-static variable)
• The distinction between a class method (also called a static method) and an instance method
(also called a non-static method)
• What the access modifiers public, private, and protected mean
• What is meant by information hiding
• How to refer to class and instance variables
• How to initialize class and instance variables
• What is a constructor and how to write one
• What is meant by overloading
• What is meant by data encapsulation
• How to write accessor and mutator methods
• How to print an object’s data in various ways
• Why the tostring() method is special in Java
• What happens when we assign an object variable to another
• What it means to compare one object variable with another
• How to compare the contents of two objects
• How a function can return more than one value using an object

29
Chapter 2 ■ Introduction to Objects

2.1 Objects
Java is considered an object-oriented programming language. The designers created it such that objects become the
center of attention. Java programs create and manipulate objects in an attempt to model how the real world operates.
For our purposes, an object is an entity that has a state and methods to manipulate that state. The state of an object is
determined by its attributes.
For example, we can think of a person as an object. A person has attributes such as name, age, gender, height,
color of hair, color of eyes, and so on. Within a program, each attribute is represented by an appropriate variable;
for instance, a String variable can represent name, an int variable can represent age, a char variable can represent
gender, a double variable can represent height, and so on.
We normally use the term field names (or, simply, fields) to refer to these variables. Thus, the state of an object is
defined by the values in its fields. In addition, we will need methods to set and/or change the values of the fields as
well as to retrieve their values. For example, if we are interested in a person’s height, we would need a method to “look
into” the object and return the value of the height field.
A car is another common example of an object. It has attributes such as manufacturer, model, seating capacity,
fuel capacity, actual fuel in the tank, mileage, type of music equipment, and speed. A book object has attributes such
as author, title, price, number of pages, type of binding (hardcover, paperback, spiral), and if it is in stock. A person,
a car, and a book are examples of concrete objects. Note, however, that an object could also represent an abstract
concept such as a department in a company or a faculty in a university.
In the previous example, we did not speak of a specific person. Rather, we spoke of a general category “person”
such that everyone in the category has the attributes mentioned. (Similar remarks apply to car and book.) In Java
terminology, “person” is a class. We think of a class as a general category (a template) from which we can create
specific objects.
An object, then, is an instance of a class; in this example, a Person object would refer to a specific person. To work
with two Person objects, we would need to create two objects from the class definition of Person. Each object would
have its own copy of the field variables (also called instance variables); the values of the variables in one object could
be different from the values of the variables in the other object.

2.2 Defining Classes and Creating Objects


The simplest Java programs consist of a single class. Within the class, we write one or more methods/functions to
perform some task. Program P2.1 shows an example.

Program P2.1
//prompt for two numbers and find their sum
import java.util.*;
public class Sum {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter first number: ");
int a = in.nextInt();
System.out.printf("Enter second number: ");
int b = in.nextInt();
System.out.printf("%d + %d = %d\n", a, b, a + b);
}
} //end class Sum

The program consists of one class (ProgramP1_1) and one method (main) within the class. The class is used
simply as the framework within which to write the logic of the program. We will now show how to define and use a
class to create (we say instantiate) objects.

30
Chapter 2 ■ Introduction to Objects

In Java, every object belongs to some class and can be created from the class definition only. Consider the
following (partial) definition of the class Book:

public class Book {


private static double Discount = 0.25; //class variable
private static int MinBooks = 5; //class variable

private String author; // instance variable


private String title; // instance variable
private double price; // instance variable
private int pages; // instance variable
private char binding; // instance variable
private boolean inStock; // instance variable

// methods to manipulate book data go here


} //end class Book

The class header (the first line) consists of the following:


• An optional access modifier; public is used in the example and will be used for most of our
classes. Essentially it means that the class is available for use by any other class; it can also be
extended to create subclasses. Other access modifiers are abstract and final; we won’t deal
with those in this book.
• The keyword class.
• A user identifier for the name of the class; Book is used in the example.
The braces enclose the body of the class. In general, the body will include the declaration of the following:
• Static variables (class variables); there will be one copy for the entire class—all objects will
share that one copy. A class variable is declared using the word static. If we omit the word
static, the variable is instance.
• Non-static variables (instance variables); each object created will have its own copy. It’s the
instance variables that comprise the data for an object.
• Static methods (class methods); these are loaded once when the class is loaded and can be
used without creating any objects. It makes no sense for a static method to access non-static
variables (which belong to objects), so Java forbids it.
• Non-static methods (instance methods); these can be used only via an object created from the
class. It’s the non-static methods that manipulate the data (the non-static fields) in objects.
• The String class is predefined in Java. If word is String (a String object, to be precise) and
we write word.toLowerCase(), we are asking that the instance method toLowerCase of the
String class be applied to the String object, word. This method converts uppercase letters to
lowercase in the (String) object used to invoke it.
• Similarly, if in is a Scanner object (created when we say new Scanner...), the expression
in.nextInt() applies the instance method nextInt to the object in; here, it reads the next
integer from the input stream associated with in.
In the Book class, we declare two class variables (Discount and MinBooks, declared with static) and six instance
variables; they are instance by default (the word static is omitted).

31
Chapter 2 ■ Introduction to Objects

2.2.1 Access to Class and Instance Variables


In addition to static, a field can be declared using the optional access modifiers private, public, or protected. In
the Book class, we declared all our instance variables using private. The keyword private indicates that the variable
is “known” only inside the class and can be manipulated directly only by methods within the class. In other words, no
method from outside the class has direct access to a private variable. However, as we will see shortly, we can provide
public methods that other classes can use to set and access the values of private variables. This way, we ensure that
class data can be changed only by methods within the class.
Declaring a variable public means that it can be accessed directly from outside the class. Hence, other classes
can “do as they please” with a public variable. For example, if Discount is declared as public, then any other class
can access it using Book.Discount and change it in any way it pleases. This is not normally encouraged since a class
then loses control over its data.
For the most part, we will declare a class’s fields using private. Doing so is the first step in implementing the
concept of information hiding, which is part of the philosophy of object-oriented programming. The idea is that users
of an object must not be able to deal directly with the object’s data; they should do so via the object’s methods.
Declaring a variable protected means that it can be accessed directly from the class and any of its subclasses, as
well as other classes in the same package. We will not use protected variables in this introduction.
If no access modifier is specified, then the variable can be accessed directly by other classes in the same
package only.
A method within a class can refer to any variable (static or non-static, public or private) in the class simply
by using its name. (An exception is that a static method cannot access non-static variables.) If a static variable is
known outside the class (that is, not private), it is referenced by qualifying the variable with the class name, as in
Book.Discount and Book.MinBooks.
From outside the class, a nonprivate instance variable can be referenced only via the object to which it belongs;
this is illustrated in the next Section. However, as indicated, good programming practice dictates that, most of the
time, our variables will be declared private, so the notion of direct access from outside the class does not arise.

2.2.2 Initializing Class and Instance Variables


When the Book class is loaded, storage is immediately allocated to the class variables Discount and MinBooks; they
are then assigned initial values of 0.25 and 5, respectively. The meaning behind these variables is that if five or more
copies of a book are sold, then a 25 percent discount is given. Since these values apply to all books, it would be a waste
of storage to store them with each book’s data, hence their declaration as static variables. All book objects will have
access to the single copy of these variables. (Note, however, that if we wanted to vary these values from book to book,
then they become attributes of a specific book and would have to be declared non-static.)
When the class is first loaded, no storage is allocated to the instance (non-static) variables. At this time, we have
only a specification of the instance variables, but none actually exists as yet. They will come into existence when an
object is created from the class. The data for an object is determined by the instance variables. When an object is
“created,” storage is allocated for all the instance variables defined in the class; each object created has its own copy of
the instance variables. To create an object, we use the keyword new as in the following:

Book b;
b = new Book();

The first statement declares b as a variable of type Book. From this, we see that a class name is considered to be a
type (similar to int or char) and can be used to declare variables. We say that b is an object variable of type Book.
The declaration of b does not create an object; it simply creates a variable whose value will eventually be a pointer
to an object. When declared as shown, its value is undefined.
The second statement finds some available memory where a Book object can be stored, creates the object, and
stores the address of the object in b. (Think of the address as the first memory location occupied by the object. If the
object occupies locations 2575 to 2599, its address is 2575.) We say that b contains a reference or pointer to the object.
Thus, the value of an object variable is a memory address, not an object. This is illustrated as shown in Figure 2-1.
32
Chapter 2 ■ Introduction to Objects

2575
author
title
price
pages
binding

2575 inStock
2599
b

Figure 2-1. An instance of a Book object

As a shortcut, we can declare b and create a book object in one statement, like this:

Book b = new Book();

It is a common error to think that the Book variable b can hold a Book object. It cannot; it can hold only a reference
to a Book object. (In a similar manner, we should be familiar with the idea that a String variable does not hold a
string but, rather, the address of where the string is stored.) However, where the distinction (between an object and a
reference to the object) does not matter, we will speak as if b holds a Book object.
Once an object b is created, we can refer to its instance fields like this:

b.author b.title b.price


b.pages b.binding b.inStock

However, we can do so from outside the class only if the fields are declared public. We will see later how to access
the fields indirectly when they are declared private.
When an object is created, unless we say otherwise, its instance fields are initialized as follows:
• Numeric fields are set to 0.
• Character fields are set to '\0' (Unicode '\u0000', to be precise).
• Boolean fields are set to false.
• Object fields are set to null. (A variable with the value null means that it does not reference or
point to anything.)
In our example, the following happens:
• b.author (of type String) is set to null; remember that String is an object type.
• b.title (of type String) is set to null.
• b.price (of type double) is set to 0.0.
• b.pages (of type int) is set to 0.
• b.binding (of type char) is set to '\0'.
• b.inStock (of type boolean) is set to false.
We could specify an initial value when we declare an instance variable. Consider this code:

public class Book {


private static double Discount = 0.25;
private static int MinBooks = 5;

33
Chapter 2 ■ Introduction to Objects

private String author = "No Author";


private String title;
private double price;
private int pages;
private char binding = 'P'; // for paperback
private boolean inStock = true;
}

Now, when an object is created, author, binding, and inStock will be set to the specified values while title,
price, and pages will assume the default values. A variable is given a default value only if no explicit value is assigned
to it. Suppose we create an object b with this:

Book b = new Book();

The fields will be initialized as follows:


• author is set to "No Author". // specified in the declaration
• title is set to null. // default for (String) object type
• price is set to 0.0. // default for numeric type
• pages is set to 0. // default for numeric type
• binding is set to 'P'. // specified in the declaration
• inStock is set to true. // specified in the declaration

2.3 Constructors
Constructors provide more flexible ways of initializing the state of an object when it is created. In the following
statement, Book() is termed a constructor:

Book b = new Book();

It is similar to a method call. But, you might say, we did not write any such method in our class definition. True,
but in such cases, Java provides a default constructor—one with no arguments (also called a no-arg constructor). The
default constructor is quite simplistic; it just sets the values of the instance variables to their default initial values.
Later, we could assign more meaningful values to the object’s fields, as in the following:

b.author = "Noel Kalicharan";


b.title = "DigitalMath";
b.price = 29.95;
b.pages = 200;
b.binding = 'P'; //for paperback
b.inStock = true; //stock is available

Now suppose that when we create a book object, we want Java to assign the author and title automatically.
We want to be able to use statements such as the following for creating new book objects:

Book b = new Book("Noel Kalicharan", "DigitalMath");

34
Chapter 2 ■ Introduction to Objects

We can do this, but we must first write an appropriate constructor, one defined with two String parameters.
The following shows how it can be done:

public Book(String a, String t) {


author = a;
title = t;
}

Here are some important points to note:


• A constructor for a class has the same name as the class. Our class is called Book; therefore, the
constructor must be called Book. Since a constructor is meant to be used by other classes, it is
declared public.
• A constructor can have zero or more parameters. When called, the constructor must be given
the appropriate number and type of arguments. In our example, the constructor is declared
with two String parameters, a and t. When calling the constructor, two String arguments
must be supplied.
• The body of the constructor contains the code that would be executed when the constructor
is called. Our example sets the instance variable author to the first argument and title to the
second argument. In general, we can have statements other than those that set the values of
instance variables. We can, for instance, validate a supplied value before assigning it to a field.
We will see an example of this in the next section.
• A constructor does not have a return type, not even void.
• If initial values are provided for instance variables in their declaration, those values are stored
before the constructor is called.
For example, suppose the class Book is now declared as follows:

public class Book {


private static double Discount = 0.25;
private static int MinBooks = 5;

private String author = "No Author";


private String title;
private double price;
private int pages;
private char binding = 'P'; // for paperback
private boolean inStock = true;

public Book(String a, String t) {


author = a;
title = t;
}
} //end class Book

The statement

Book b = new Book("Noel Kalicharan", "DigitalMath");

35
Chapter 2 ■ Introduction to Objects

will be executed as follows:


1. Storage is found for a Book object, and the address of the storage is stored in b.
2. The fields are set as follows:

author is set to "No Author"; // specified in the declaration


title is set to null; // default for (String) object type
price is set to 0.0; // default for numeric type
pages is set to 0; // default for numeric type
binding is set to 'P'; // specified in the declaration
inStock is set to true. // specified in the declaration
  
3. The constructor is called with arguments "Noel Kalicharan" and "DigitalMath"; this
sets author to "Noel Kalicharan" and title to "DigitalMath", leaving the other fields
untouched. When the constructor is finished, the fields will have the following values:

author "Noel Kalicharan"


title "DigitalMath"
price 0.0
pages 0
binding 'P'
inStock true

2.3.1 Overloading a Constructor


Java allows us to have more than one constructor, provided each has a different signature. When several
constructors can have the same name, this is referred to as overloading the constructor. Suppose we want to be able
to use the no-arg constructor as well as the one with author and title arguments. We can include both in the class
declaration like this:

public class Book {


private static double Discount = 0.25;
private static int MinBooks = 5;

private String author = "No Author";


private String title;
private double price;
private int pages;
private char binding = 'P'; // for paperback
private boolean inStock = true;

public Book() { }

public Book(String a, String t) {


author = a;
title = t;
}
} //end class Book
  

36
Chapter 2 ■ Introduction to Objects

Observe that the body of the no-arg constructor consists of an empty block. When the following statement is executed,
the instance variables are set to their initial values (specified or default), and the constructor is executed. In this case,
nothing further happens.

Book b = new Book();

Be warned that when we provide a constructor, the default no-arg constructor is no longer available. If we want to
use a no-arg constructor as well, we must write it explicitly, as in the previous example. We are free, of course, to write
whatever we want in the body, including nothing.
As a final example, we provide a constructor that lets us set all the fields explicitly when an object is created. Here
it is:

public Book(String a, String t, double p, int g, char b, boolean s) {


author = a;
title = t;
price = p;
pages = g;
binding = b;
inStock = s;
}
  
If b is a variable of type Book, a sample call is as follows:

b = new Book("Noel Kalicharan", "DigitalMath", 29.95, 200, 'P', true);

The fields will be given the following values:

author "Noel Kalicharan"


title "DigitalMath"
price 29.95
pages 200
binding 'P'
inStock true

2.4 Data Encapsulation, Accessor, and Mutator Methods


We will use the term user class to denote a class whose methods need to access the fields and methods of another class.
When a class’s field is declared public, any other class can access the field directly, by name. Consider the
following class:

public class Part {


public static int NumParts = 0; // class variable
public String name; // instance variable
public double price; // instance variable
}
  
Here, we define one static (or class) variable and two instance variables as public. Any user class can access the
static variable using Part.NumParts and can include statements such as this:

Part.NumParts = 25;

37
Chapter 2 ■ Introduction to Objects

This may not be desirable. Suppose NumParts is meant to count the number of objects created from Part. Any
outside class can set it to any value it pleases, so the writer of the class Part cannot guarantee that it will always reflect
the number of objects created.
An instance variable, as always, can be accessed via an object only. When a user class creates an object p of
type Part, it can use p.price (or p.name) to refer directly to the instance variable and can change it, if desired, with
a simple assignment statement. There is nothing to stop the user class from setting the variable to an unreasonable
value. For instance, suppose that all prices are in the range 0.00 to 99.99. A user class can contain the following
statement, compromising the integrity of the price data:

p.price = 199.99;

To solve these problems, we must make the data fields private; we say we must hide the data. We then provide
public methods for others to set and retrieve the values in the fields. Private data and public methods are the essence
of data encapsulation. Methods that set or change a field’s value are called mutator methods. Methods that retrieve
the value in a field are called accessor methods.
Let’s show how the two problems mentioned can be solved. First, we redefine the fields as private:

public class Part {


private static int NumParts = 0; // class variable
private String name; // instance variable
private double price; // instance variable
}

Now that they are private, no other class has access to them. If we want NumParts to reflect the number of
objects created from the class, we would need to increment it each time a constructor is called. We could, for example,
write a no-arg constructor as follows:

public Part() {
name = "NO PART";
price = -1.0; // we use –1 since 0 might be a valid price
NumParts++;
}

Whenever a user class executes a statement such as the following, a new Part object is created and 1 is added
to NumParts:

Part p = new Part();

Hence, the value of NumParts will always be the number of Part objects created. Further, this is the only way to
change its value; the writer of the class Part can guarantee that the value of NumParts will always be the number of
objects created.
Of course, a user class may need to know the value of NumParts at any given time. Since it has no access to
NumParts, we must provide a public accessor method (GetNumParts, say; we use uppercase G for a static accessor, since
it provides a quick way to distinguish between static and non-static), which returns the value. Here is the method:

public static int GetNumParts() {


return NumParts;
}

The method is declared static since it operates only on a static variable and does not need an object
to be invoked. It can be called with Part.GetNumParts(). If p is a Part object, Java allows you to call it with
p.GetNumParts(). However, this tends to imply that GetNumParts is an instance method (one that is called via an

38
Chapter 2 ■ Introduction to Objects

object and operates on instance variables), so it could be misleading. We recommend that class (static) methods be
called via the class name rather than via an object from the class.
As an exercise, add a field to the Book class to count the number of book objects created and update the
constructors to increment this field.

2.4.1 An Improved Constructor


Instead of a no-arg constructor, we could take a more realistic approach and write a constructor that lets the user
assign a name and price when an object is created, as in the following:

Part af = new Part("Air Filter", 8.75);

We could write the constructor as:

public Part(String n, double p) {


name = n;
price = p;
NumParts++;
}
  
This will work except that a user can still set an invalid price for a part. There is nothing to stop the user from
writing this statement:

Part af = new Part("Air Filter", 199.99);

The constructor will dutifully set price to the invalid value 199.99. However, we can do more in a constructor
than merely assign values to variables. We can test a value and reject it, if necessary. We will take the view that if an
invalid price is supplied, the object will still be created but a message will be printed and the price will be set to –1.0.
Here is the new version of the constructor:

public Part(String n, double p) {


name = n;
if (p < 0.0 || p > 99.99) {
System.out.printf("Part: %s\n", name);
System.out.printf("Invalid price: %3.2f. Set to -1.0.\n", p);
price = -1.0;
}
else price = p;
NumParts++;
} //end constructor Part
  
As a matter of good programming style, we should declare the price limits (0.00 and 99.99) and the “null” price
(-1.0) as class constants. We could use the following:

private static final double MinPrice = 0.0;


private static final double MaxPrice = 99.99;
private static final double NullPrice = -1.0;

These identifiers can now be used in the constructor.

39
Chapter 2 ■ Introduction to Objects

2.4.2 Accessor Methods


Since a user class may need to know the name or price of an item, we must provide public accessor methods for name
and price. An accessor method simply returns the value in a particular field. By convention, we preface the name of
these methods with the word get. The methods are as follows:

public String getName() { // accessor


return name;
}

public double getPrice() { // accessor


return price;
}
  
Note that the return type of an accessor is the same as the type of the field. For example, the return type of
getName is String since name is of type String.
Since an accessor method returns the value in an instance field, it makes sense to call it only in relation to a
specific object (since each object has its own instance fields). If p is an object of type Part, then p.getName() returns
the value in the name field of p and p.getPrice() returns the value in the price field of p.
As an exercise, write accessor methods for all the fields of the Book class.
These accessors are examples of non-static or instance methods (the word static is not used in their
declaration). We can think of each object as having its own copy of the instance methods in a class. In practice,
though, the methods are merely available to an object. There will be one copy of a method, and the method will be
bound to a specific object when the method is invoked on the object.
Assuming that a Part object p is stored at location 725, we can picture the object as shown in Figure 2-2.

725

name getName()

725 price getPrice()


p

Figure 2-2. A Part object with its fields and accessors

Think of the fields name and price as locked inside a box, and the only way the outside world can see them is via
the methods getName and getPrice.

2.4.3 Mutator Methods


As the writer of the class, we have to decide whether we will let a user change the name or price of an object after it
has been created. It is reasonable to assume that the user may not want to change the name. However, prices change,
so we should provide a method (or methods) for changing the price. As an example, we write a public mutator method
(setPrice, say) that user classes can call, as in the following:

p.setPrice(24.95);

40
Chapter 2 ■ Introduction to Objects

This sets the price of Part object p to 24.95. As before, the method will not allow an invalid price to be set. It will
validate the supplied price and print an appropriate message, if necessary. Using the constants declared in Section
2.4.1, here is setPrice:

public void setPrice(double p) {


if (p < MinPrice || p > MaxPrice) {
System.out.printf("Part: %s\n", name);
System.out.printf("Invalid price: %3.2f; Set to %3.2f\n", p, NullPrice);
price = NullPrice;
}
else price = p;
} //end setPrice

With this addition, we can think of Part p as shown in Figure 2-3.

725
name getName()
price getPrice()
725
p
setPrice()

Figure 2-3. Part object with setPrice() added

Observe the direction of the arrow for setPrice; a value is being sent from the outside world to the private field
of the object.
Again, we emphasize the superiority of declaring a field private and providing mutator/accessor methods for it
as opposed to declaring the field public and letting a user class access it directly.
We could also provide methods to increase or decrease the price by a given amount or by a given percentage.
These are left as exercises.
As another exercise, write mutator methods for the price and inStock fields of the Book class.

2.5 Printing an Object’s Data


To verify that our parts are being given the correct values, we would need some way of printing the values in an
object’s fields.

2.5.1 Using an Instance Method (the Preferred Way)


One way of doing this is to write an instance method (printPart, say), which, when invoked on an object, will print
that object’s data. To print the data for Part p, we will write this:

p.printPart();

Here is the method:

public void printPart() {


System.out.printf("\nName of part: %s\n", name);
System.out.printf("Price: $%3.2f\n", price);
} //end printPart
  
41
Chapter 2 ■ Introduction to Objects

Suppose we create a part with this:

Part af = new Part("Air Filter", 8.75);

The expression af.printPart() would display the following:

Name of part: Air Filter


Price: $8.75

When printPart is called via af, the references in printPart to the fields name and price become references to
the fields of af. This is illustrated in Figure 2-4.

name: Air Filter


725
price: 9.50
af

printPart()

System.out.printf("\nName of part: %s\n", name);


System.out.printf("Price: $%3.2f\n", price);

Figure 2-4. name and price refer to the fields of af

2.5.2 Using a Static Method


We could, if we want, write printPart as a static method, which will be called with p as an argument in order to print
its fields. In this case, we will write the following:

public static void printPart(Part p) {


System.out.printf("\nName of part: %s\n", p.name);
System.out.printf("Price: $%3.2f\n", p.price);
}

The field names have to be qualified with the object variable p. Without p, we would have the case of a static
method referring to a non-static field, which is forbidden by Java.
If c is a Part object created in a user class, we will have to use the following to print its fields:

Part.printPart(c);

This is slightly more cumbersome than using the instance method, shown previously. By comparison, you can
use, for instance, Character.isDigit(ch) to access the static method isDigit in the standard Java class Character.

2.5.3 Using the toString() Method


The toString method returns a String and is special in Java. If we use an object variable in a context where a string is
needed, then Java will attempt to invoke toString from the class to which the object belongs. For example, suppose
we write the following, where p is a Part variable:

System.out.printf("%s", p);

42
Chapter 2 ■ Introduction to Objects

Since it is not clear what it means to print an arbitrary object, Java will look for guidance in the class itself.
Presumably, the class will know how to print its objects. If it provides a toString method, Java will use it. (If it
doesn’t, Java will print something generic like the name of the class and the address, in hexadecimal, of the object, for
instance: Part@72e15c32.) In our example, we could add the following to the class Part:

public String toString() {


return "\nName of part: " + name + "\nPrice: $" + price + "\n";
}

If af is the Air Filter part, then the following statement would invoke the call af.toString():

System.out.printf("%s", af);

In effect, the printf becomes this:

System.out.printf("%s", af.toString());

af.toString() will return this:

"\nName of part: Air Filter \nPrice: $8.75\n"

The result is that printf will print this:

Name of part: Air Filter


Price: $8.75

2.6 The Class Part


Putting all the changes together, the class Part now looks like this:

public class Part {


// class constants
private static final double MinPrice = 0.0;
private static final double MaxPrice = 99.99;
private static final double NullPrice = -1.0;
private static int NumParts = 0; // class variable

private String name; // instance variable


private double price; // instance variable

public Part(String n, double p) { // constructor


name = n;
if (p < MinPrice || p > MaxPrice) {
System.out.printf("Part: %s\n", name);
System.out.printf("Invalid price: %3.2f; Set to %3.2f\n", p, NullPrice);
price = NullPrice;
}
else price = p;
NumParts++;
} //end constructor Part

43
Another Random Scribd Document
with Unrelated Content
the Emperor Alexander to his late Majesty, which cost
upwards of 1000 guineas, was knocked down at £125."

These prices do not show that the people cared


much to possess relics of their late sovereign;
indeed, he was speedily forgotten, and all eyes were
turned to the rising sun. The newspapers teemed
with anecdotes of him, from his childhood upwards
(mostly very sorry stuff), and, oblivious of his errors,
inanity, and frivolity, the people hailed William (why
or wherefore?) as "The Patriot King." Until the death
of the Duke of York, he had excited no more public
interest than any of the other royal princes; but
when that event took place, he was looked upon as
heir to the throne, had an increased grant from
Parliament, and lived a somewhat retired life at
Bushey Park, with his wife, Amelia Adelaide, eldest
child of George, Duke of Saxe-Coburg-Meiningen,
whom he married on July 18, 1818.

His life, previous to his accession to the throne, is


not within the province of this book—it is sufficient to
say that at no time was he remarkable for his
intellect, tractability, or social manners. Hear what
Greville,[3] an acute observer, even if he were
somewhat of a cynic, says about him at his accession

"London, July 16.—I returned here on the 6th of this
month, and have waited these ten days to look about me,
and see and hear what is passing. The present King and his
proceedings occupy all attention, and nobody thinks any more
of the late King, than if he had been dead fifty years, unless it
be to abuse him and rake up all his vices and misdeeds.
Never was elevation like that of William IV. His life has,
hitherto, passed in obscurity and neglect, in miserable
poverty, surrounded by a numerous progeny of bastards,
without consideration or friends, and he was ridiculous from
his grotesque ways and little meddling curiosity. Nobody ever
invited them into their house, or thought it necessary to
honour him with any mark of attention or respect; and so he
went on for about forty years, till Canning brought him into
notice by making him Lord High Admiral at the time of his
grand ministerial schism. In that post he distinguished himself
by making absurd speeches, by a morbid official activity, and
by a general wildness which was thought to indicate incipient
insanity, till shortly after Canning's death and the Duke's[4]
accession, it is well known, the latter dismissed him. He then
dropped back into obscurity, but had become, by this time,
somewhat more of a personage than he was before. His brief
administration of the Navy, the death of the Duke of York,
which made him heir to the throne, his increased wealth and
regular habits, had procured him more consideration, though
not a great deal. Such was his position when George IV. broke
all at once, and after three months of expectation, William
finds himself King."
CHAPTER II.
1830.

Proclamation of William IV. — The Beer Act — The Queen and gas — Burial of
George IV. — The King and the Duke of Cumberland — The King as a soldier
— He meddles with the uniforms of the army.

On Monday, June 28, 1830, the king came at an


early hour to St. James's Palace to witness the
ceremony of his proclamation, which was duly done
at 10 a.m., with the usual pomp, the heralds giving
forth that, with the acquiescence of everybody—
"We do now hereby, with one voice and consent of tongue
and heart, proclaim that the High and Mighty Prince William,
Duke of Clarence, is now, by the death of the late Sovereign,
of happy memory, become our only lawful and rightful Liege
Lord William the Fourth, by the Grace of God, King of Great
Britain and Ireland;" and so forth.

It was a gay sight, for people had not had time to


get into mourning costume, and the bright summer
dresses of the ladies made it a brilliant show.
He commenced his reign with a gracious act,
which considerably added to his popularity. Before
the ceremony of proclamation he showed himself at
a window in St. James's Palace, before which some
thousands of people had assembled. According to
the Globe—
"By some Jack-in-Office, the spectators were ordered to be
dispersed, which was speedily done by the Life Guards. On
the arrival of the heralds to proclaim the accession, the King
reappeared at his window, and, finding a vacant space below,
which, previously, was crowded, with some degree of
surprise, said, 'What has become of the people?' On being
told they had been removed, 'By whose order?' next inquired
the King. He was so dissatisfied with the answer as to
command the gate of the courtyard immediately to be re-
opened, and the public to be re-admitted, who soon re-
assembled in great numbers, and cheered their Sovereign
most vociferously."

The change of rulers did not affect Parliament. The


Lords adjourned for a day, and the Commons did
very little business until all the members had taken
the oath of allegiance to the new sovereign, who
kept on the old Ministry, with the Duke of Wellington
at its head. Very shortly afterwards, the question of a
Regency (the Princess Victoria being only twelve
years old) cropped up; and after that, on July 12th,
was read a third time and passed in the House of
Lords "An Act to permit the general Sale of Beer and
Cyder by Retail in England" (1 Gul. IV. c. lxiv.), which
the Times describes as "a great victory obtained for
the poor over the unpitying avarice of the rich."

Beer always had been the standard drink of


England, and, at this time, no cheap substitute had
been found for it. Tea was far too dear for common
folk, as was coffee, and cocoa or chocolate were only
for the well-to-do. This Act is virtually that under
which beer-houses are now licensed, which made a
licence to sell beer only easy to obtain. It suited the
times, and was very popular. A song, which is still
sung, but which dates from early Victorian times,
makes a slight error as to the intention of the Act,
but it shows a grateful remembrance of the same. It
is called—

"I likes a Drop of Good Beer."

"Come one and all, both great and small


With voices loud and clear,
And let us sing, bless Billy the King,
Who bated the tax upon beer.

Chorus:

"For I likes a drop of good beer, I does,


I'se pertickler fond of my beer, I is;
And —— his eyes whoever he tries
To rob a poor man of his beer."
The accompanying illustration, by an anonymous
artist, shows the Duke of Wellington providing the
people with beer, in a popular manner. It is entitled
"Opening the Beer Trade; or, Going into a New Line
of Business."

The background is formed of two houses; one the


sign of the King's Head; the other, the Druggist's
Arms. Outside the closed door of the latter, which is
"To let, enquire of the Brewers," stands Timothy
Mix'em, dealer in compounds, who, looking at the
group, mournfully remarks, "They'll soon shut up all
the houses by opening the Trade." The King's Head is
kept by Arthur and Co., dealer in swipes, who
proclaims on his windows, "Genuine Beer, from Malt
and Hops only," and has a placard that the New Beer
Act commences October 10, 1830. The old Duke of
Wellington says to the dustman and his wife, "Come,
my Britons, here's your real malt and hops;" whilst
Peel, as pot-boy, remarks, "No poisonous drugs here,
my boys, it's all real stuff."

On July 23rd, Parliament was dissolved.

Ever since the accession of William IV. his slightest


movements were chronicled, even down to the
smallest of small beer, such as[5]—
"The Duke of Wellington, when at Windsor, a few days ago,
directed that the gas might be cut off from the interior of the
castle, by the desire of the Queen, who, we understand,
entertained apprehensions lest an accident might be caused
by explosion. Her Majesty's wishes will, of course, be
immediately complied with, and directions have already been
given to the Gas Company for the purpose."

The movements of the Princess Victoria, who had


now become a personage, were also duly chronicled,
and we are told how "The presence of the Duchess
(of Kent) and her interesting daughter will, no doubt,
attract numerous visitors to Malvern."

George IV., after lying in state, was buried on July


15th, with all the pomp usually accompanying the
burial of a King of England. Greville tells us how his
successor behaved on this occasion—
"At the late King's funeral he behaved with great indecency.
That ceremony was very well managed, and a fine sight, the
military part particularly, and the Guards were magnificent.
The attendance was not very numerous, and, when they had
all got together in St. George's Hall, a gayer company I never
beheld; with the exception of Mount Charles, who was deeply
affected, they were all as merry as grigs. The King was chief
mourner, and, to my astonishment, as he entered the chapel,
directly behind the body, in a situation in which he should
have been apparently, if not really, absorbed in the
melancholy duty he was performing, he darted up to
Strathaven, who was ranged on one side below the Dean's
stall, shook him heartily by the hand, and then went on
nodding to the right and left. He had, previously, gone as
chief mourner to sit for an hour at the head of the body as it
lay in state, and he walked in procession, with his household,
to the apartment. I saw him pass from behind the screen.
Lord Jersey had been in the morning to Bushey to kiss hands
on being made Chamberlain, when he had received him very
graciously, told him it was the Duke, and not himself, who had
made him, but that he was delighted to have him. At Windsor,
when he arrived, he gave Jersey the white wand; or, rather,
took one from him he had provided for himself, and gave it
him again with a little speech. When he went to sit in state,
Jersey preceded him, and he said, when all was ready, 'Go on
to the body, Jersey; you will get your dress coat as soon as
you can.'"

Personal gossip about the King, is not the scheme


of this book; but, as it formed the main topic of
general conversation at the time, it cannot be passed
over. His brother, the greatly disliked Duke of
Cumberland, afterwards King of Hanover, had
usurped the functions of the other colonels of the
guards, and had elected himself a permanent Gold
Stick, but the new monarch said his rank was too
high for him to perform such service, and relegated
the office to its former footing, that each colonel
should share the office in turns.

Nor was this the only friction between the


brothers. The Duke of Cumberland's horses had
hitherto occupied the stables allotted to the Queen,
and when Lord Errol, her Master of the Horse, asked
her where she would have her horses stabled, she
replied, she "did not know, but he was to put them in
their proper place." Accordingly, the King was asked
for an order to remove the duke's horses, which was
given through the Duke of Leeds, who went to the
Duke of Cumberland, and received for answer that
"he would be d—d if they should go;" but on its
being represented to him that if he did not remove
them, they would be turned out, he sulkily gave way.

The King, who, as every one knows, had been


brought up as a sailor, now turned his attention to
things military, and his first review is thus described
by Greville—
"July 20.—Yesterday was a very busy day with his Majesty,
who is going much too fast, and begins to alarm his Ministers
and astonish the world. In the morning he inspected the
Coldstream Guards, dressed (for the first time in his life) in a
military uniform, and with a great pair of gold spurs half way
up his legs like a game-cock, although he was not to ride, for,
having chalk stones in his hands, he can't hold the reins."

He next began to meddle with the uniforms, etc. in


the army, doubtless with a view to save the pockets
of the officers, for army dress, under George the
Magnificent, had become very much gold belaced
and expensive; but of all the orders issued on August
2nd from the Horse Guards, we will only take two.
"The moustachios of the Cavalry (excepting in the Life
Guards, the Horse Guards, and the Hussars) to be abolished,
and the hair of the non-commissioned officer and soldier
throughout the regular force to be cut close at the sides and
back of the head, instead of being worn in that bushy and
unbecoming fashion adopted by some regiments."

The illustration on the opposite page is taken from


a contemporary song called "Adieu, my
Moustachios!" Words by T. Haynes Bayly; music by J.
Blewitt, and the first verse runs thus—
"Adieu, my moustachios! farewell to my tip!
Lost, lost is the pride of my chin and my lip!
When Laura last saw me she said that the world
Contain'd no moustachois so charmingly curl'd!
But razors are ruthless, my honours they nip,
Adieu, my moustachois! farewell to my tip!"

Order No. 2 was as follows:—


"The four regiments of Hussars to be dressed exactly alike.
Their officers to have one dress only, and that of a less costly
pattern, which will forthwith be prepared."

Of course, this, like the former ukase, could not


escape the satirist, and we have the accompanying
illustration by R. S. entitled, "Raising the Wind by Royal
Authority. His Majesty intends diminishing the
extravagant expense of the Military Officer's dress.
See the papers."
Here we see the Jew old clothesmen chaffering
against each other and bargaining with Hussar
Officers for their compulsorily left-off finery.
CHAPTER III.
1830.

The King as "bon bourgeois" — Mobbed — Street song about him — A sailor in
Guildhall — Behaviour of the public at Windsor — Charles X. in England —
The "New Police" — A modest advertisement.

The King affected the bon bourgeois, which, after


the regal etiquette of the late King, rather astonished
the lieges. The Magazine of Fashions for August, says

"He comes unexpectedly and unattended, as they are
trooping the guard at St. James's, attired like a private
gentleman, and nods graciously to the people, passes jokes
with the officers, and tells the privates 'they shall rise by their
own merits.'

"He comes to town on the dickey of his own chariot.

"He goes to Somerset House in a pair-horse carriage


without a lancer, dragoon, or policeman to attend him,
because he says, 'his guards are his people;' and he stops
purposely in the streets that the people may say 'they have
seen a King!'

"He employs a hairdresser in Water Lane, Fleet Street, to


make his coachman's white and curled wigs; because the
poor fellow, when he knew better days, lived at the West End,
and was employed by the then Duke of Clarence. We have
seen these wigs being made.

"He has all the members of his family, as a family, about


him, and 'harmony and affection' is his favourite toast.

"He neither likes moustaches nor foreign servants; because


the one disguises an Englishman's face, and the other dupes
an Englishman's pocket.

"He observes an old sailor upon the lamp-post, near


Somerset House, who gets aloft 'to look out for his captain'
(old blue trouser's own words), and he sends him enough to
rum it for a week.

"He overhauls the documents of the Navy Pay Office, to


ascertain if any arrears of pay or prize-money are due to the
seamen; and he orders refreshments to the poor recruits, to
encourage them to become soldiers.

"He meets two ladies (by character as well as title) in St.


James's, one of whom solicits the honour to kiss his hand.
'Madam,' says the gallant monarch, 'my glove for courtiers,
but my cheek for ladies; may I be permitted to touch yours?'
Lady M—— 'wore her blushing honours thick about her.'

"He asks people to dinner in the style of a friend, rather


than a command, and does not require their presence if they
have 'a better engagement.'

"Above all things, he impresses upon those who pay their


respects to him officially, or visit him familiarly, that his friends
are the Queen's.

"He proceeds in person, and in a style becoming the


splendour of the Crown, to dissolve Parliament, appearing
himself in the costume of a thorough-paced sailor; thus
practising in his own person the precepts he command—thus
giving countenance to his fellow-tars appearing in his
presence in the dress which they can afford to procure, and in
which they have conquered.

"His Majesty, we hear, paid great attention to Sir Robert


Wilson at the levée, and, after conversing with him familiarly
for some time, said, in conclusion, 'Meet me to-night at
Sussex's, and bring your daughters with you.'

"A female servant of Mr. Brown, of Northampton, being in


town with her mistress, was permitted to go to the review on
Monday last, and, having obtained liberty from one of the
soldiers to pass in front of the ranks, she approached the
Royal carriage without knowing it, and asked one of the
Ladies of Honour, 'Which is the Queen?' The Queen, hearing
the inquiry, immediately answered, 'I am the Queen!' 'Oh, do
show me the King, then!' The King, hearing the request,
instantly turned round, and said with a smile, 'I am the King!'
evidently enjoying her amazement and delight. The Queen
permitted the woman to hold her hand, which she had seized
in the hurry of the moment, for several minutes."

Greville gives us a sketch of his bourgeoisie and its


consequences—
"All this was very well; no great harm in it; more affable,
less dignified than the late King; but, when this (a Privy
Council) was over, and he might very well have sat himself
quietly down and rested, he must needs put on his plainer
clothes, and start on a ramble about the streets, alone, too.
In Pall Mall he met Watson Taylor, and took his arm, and went
up St. James's Street. There he was followed by a mob,
making an uproar, and when he got near White's, a woman
came up and kissed him. Belfast (who had been sworn in
Privy Councillor in the morning), who saw this from White's,
and Clinton, thought it time to interfere, and came out to
attend him. The mob increased, and, always holding Mr.
Taylor's arm, and flanked by Clinton and Belfast, who got
shoved and kicked about, to their inexpressible wrath, he got
back to the Palace, amid shouting and bawling and applause.
When he got home, he asked them to go in and take a quiet
walk in the garden, and said, 'Oh, never mind all this; when I
have walked about a few times they will get used to it, and
take no notice.'

"They even sang songs about him in the streets, of which


the following is one:—
"The King and the Sailor.

"In Portsmouth town, at the sign of the Ship,


A jolly Jack Tar sat drinking flip;
A messmate was there, who spun him a yarn
That we'd a new King, he'd soon give him to larn.

"Says sailor Ben to sailor Jim,


'He's a King and a sailor trim,
And 'bout him there's no palaver or fuss,
A cause, don't you know, he is one of us.'

"Says sailor Ben to his messmate Jim,


'He knows that I've sailed under him;
And when our ship's paid off at Chatham,
I'll go and have a good stare at 'em.'

"Now Ben Block he arriv'd at the park,


And soon the King and Queen did mark;
Says Ben, says he, 'I'll bet you a tanner,
He hails you in a King-like manner.'

"'Ye ho!' says Ben, and he soon brought-to,


And his boatswain's whistle out he drew;
When the King turn'd round with pride and joy,
'Halloo!' says he 'what ship ahoy?'

"Now Ben, he answered with a grin,


'The Royal Charlotte I've sailed in;
She was nam'd arter your royal mother,
Whose great and glorious son you are!'

The King the hand of Ben he shook


The King the hand of Ben he shook,
And said, 'At that time I was a Mid;'
Then Ben lugged out his 'bacca box,
And said to the King, 'Come, take a quid.'

"'If you won't, the Queen may like a bit,


Mayhap, like one of the Indian squaws;'
So he scrap'd up to her, and offered his box,
'No, thank ye,' says she, 'I never chaws.'

"The King, he gave promotion to Ben,


So he thought that he'd steer back again;
But the Queen, he thought he first would tell her,
'That her husband, the King, was a d—d good fellow'!"

Par parenthèse, here is a story of a sailor (Times,


August 9th)—
"Guildhall. Before Alderman Ansley.—An old tar, the very
beau ideal of a 'true British sailor,' who gave his name as Will
Robinson, his dark visage surmounted with a quantity of black
hair, twisted and matted like so many ropes' ends, was
charged with being drunk and assaulting the patrol of Aldgate
Ward.

"Bunce, the complainant, stated that between three and


four o'clock the preceding evening, he found the tar stretched
keel uppermost upon the footway in Aldersgate Street,
exposed, not altogether decently, to the gaze of a crowd of
idle boys. Bunce roused him, and advised him to move on;
but, instead of obeying, Will ordered him to sheer off, or he'd
pour a broadside into him; and, suiting the action to the
word, commenced pummelling complainant most furiously.
Bunce would have had no chance against the heavy metal of
Will Robinson, but Hawkins, the marshal-man, came up, and
with his aid the tar was secured in the Compter. While they
were on the way, the tar contrived to get his pocket-knife
open in his hand, but Hawkins perceived it and took it from
him.

"'You hear what the officer says?' observed the alderman,


addressing the prisoner.

"'Yes; but it is a d—d lie,' roared out Will Robinson,


enforcing his assertion by a loud thump of his clenched fist
upon the bar.

"'He says you drew a knife upon him,' said the alderman.

"'Your honour knows I can't spin a long yarn like this here
chap,' replied the old tar, 'but I never hurted man, woman, or
child in my life, barring 'twas a frog-eater; but I was a lad
then, and it was in the cause of old England; and d—e, I
don't think I'd hurt him neither, after a glass of grog or two.'

"Alderman. 'How long have you been in England?'

"'Only two hours ashore, your honour,' replied Will. 'I'd just
come from China, and got taking a glass with one messmate
and a glass with another.'

"Alderman. 'The sure way to get drunk. You should have


taken a glass with but one messmate.'

"'Your honour is an excellent preacher, and it's all very true;


but if an old sailor, after a long voyage, when all hands are
piped ashore, refused to drink with every mate who asked
him, he'd be called a scaly fellow, and you know I should not
like that.'

"Bunce. 'I did not mind the assault, but I thought it was
better to put him in a place of safety for his own sake.'
"'D—e, you're an honest fellow, after all,' exclaimed the tar,
seizing the officer's hand and squeezing it till the tears started
into Bunce's eyes. 'Come, and we'll make it right over a glass
of grog, old boy.'

"Alderman. 'I doubt whether you have any money left.'

"Will felt in his pockets, and could not find a copper. 'All
gone! all gone!' exclaimed the tar, mournfully.

"'It's all right—I've got his money safe,' said Bunce, drawing
forth an ample handful of silver and gold.

"'Huzza! huzza! Old England for ever!' vociferated the


delighted tar, when he saw the money; and, seizing Bunce by
the collar, 'Come along, come along, old boy; I'm as dry as a
dolphin.'

"Bunce refused till he counted the money, shilling by


shilling, in the presence of the alderman; but, when he began
to do so, Will found the operation too slow for the current of
his feelings; and, catching up the officer by the waist, he
carried him off in triumph, exclaiming, 'Keep it, my boy, keep
it; we'll drink every penny of it; and maybe his honour there'
(turning to the alderman), 'would take a drop of summut.'

"The alderman could not contain his gravity, but he


declined the offer; and Will set off with the officer still firmly
held in his grip."

As a specimen of the manners of the age (and I


cannot see that they have greatly improved now), we
may take the following extract from a private letter,
dated Windsor, August 15th:—
"You would perceive, from the newspapers, that the Grand
Terrace was thrown open to the public yesterday week. From
the walk immediately under the castle you may see portions
of the magnificent rooms—the splendid ceilings, window
drapery, and chandeliers. I was delighted with the sight, and
again visited the terrace on Sunday. The terrace was then
crowded, and I am sorry to add, English-like, some of the
people, (of the lower class, certainly) had behaved so ill, that
the public were excluded from that part adjoining the
building. Some of the creatures who abused the privilege thus
extended to the public, not only ascended the steps leading
to the state apartments, but actually climbed up into the
windows to look into the rooms, thus intruding their rudeness
on the King. It is said that his Majesty himself, from a
window, saw a person writing his name on one of the statues,
and observed on the occasion, 'I shall be compelled to do as
my brother did, exclude the public from this part, if such
conduct is continued.' The grass was all trampled and injured,
the people would not confine themselves to the gravel walks."

By the way, about this time, the King gave the


Zoological Society the whole of the collection of
beasts and birds belonging to the late King,
amounting to 150.

England has frequently afforded shelter to


unfortunate princes—notably, in late times, to Louis
XVIII., who resided at Hartwell, in Buckinghamshire
—and now another French King, Charles X., sought
her protection, arriving at Portsmouth on August
17th, and proceeding to Lulworth in Dorsetshire,
where he was welcomed at the castle, which was
placed at his disposal by Joseph Weld, Esq., a
relative of the cardinal of that name. Here he
remained some time, afterwards residing at Holyrood
Palace, and finally retired to Austria, where he died.

On June 19, 1829, the King said "Le Roi le veult"


to an Act of Parliament (10 Geo. IV. c. 44) entitled
"An Act for improving the Police in and near the
Metropolis"—the present Police Act—introduced by
Sir Robert Peel, from which fact the policemen were
called "Bobbies" and "Peelers." They commenced
duty on September 29, 1829, and were, at first,
extremely unpopular, because of their strictness,
compared to the Bow Street runners, patrols, and
night watchmen. The parishes complained bitterly of
the increased expense, but they forgot how much
better they were guarded. It was also alleged that
there were too few policemen distributed over
certain districts, and too many in others; but that
was a defect in administration almost certain to occur
at first start, which experience afterwards rectified.
Perhaps, also, the best men were not chosen, as the
force was not so popular as now, when none but
men of unblemished character are admitted, whilst
as to the present physique of the over fifteen
thousand Metropolitan Police, any general would be
proud of such a division, which is utterly unattainable
in any army.
Here is a sketch of the uniform of the "New Police"
as they were called, copied from a satirical print of
Sir Robert Peel, by the celebrated H. B. (John Doyle,
father of Richard Doyle, to whom Punch owed so
much). The hats were worn until a comparatively
recent period, and in summer-time they wore white
trousers.

The following extract from the Times of September


16th gives an account of the police as they were at
the expiration of twelve months from their
inauguration:—
"There are 16 divisions of the police, and each division
contains, on an average, 200 men, except the K division,
which contains 32; there are also, in each division, six
Welcome to Our Bookstore - The Ultimate Destination for Book Lovers
Are you passionate about books and eager to explore new worlds of
knowledge? At our website, we offer a vast collection of books that
cater to every interest and age group. From classic literature to
specialized publications, self-help books, and children’s stories, we
have it all! Each book is a gateway to new adventures, helping you
expand your knowledge and nourish your soul
Experience Convenient and Enjoyable Book Shopping Our website is more
than just an online bookstore—it’s a bridge connecting readers to the
timeless values of culture and wisdom. With a sleek and user-friendly
interface and a smart search system, you can find your favorite books
quickly and easily. Enjoy special promotions, fast home delivery, and
a seamless shopping experience that saves you time and enhances your
love for reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!

ebookgate.com

You might also like