100% found this document useful (11 votes)
58 views

Where can buy Growing Algorithms and Data Structures 4th Edition David Scuse ebook with cheap price

David

Uploaded by

tyseeruzmez
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
100% found this document useful (11 votes)
58 views

Where can buy Growing Algorithms and Data Structures 4th Edition David Scuse ebook with cheap price

David

Uploaded by

tyseeruzmez
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/ 72

Download the full version of the ebook now at ebookultra.

com

Growing Algorithms and Data Structures 4th


Edition David Scuse

https://ebookultra.com/download/growing-
algorithms-and-data-structures-4th-edition-david-
scuse/

Explore and download more ebook at https://ebookultra.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Data Structures and Algorithms in Java 4th Edition Michael


T. Goodrich

https://ebookultra.com/download/data-structures-and-algorithms-in-
java-4th-edition-michael-t-goodrich/

ebookultra.com

Java Collections An Introduction to Abstract Data Types


Data Structures and Algorithms 1st Edition David A. Watt

https://ebookultra.com/download/java-collections-an-introduction-to-
abstract-data-types-data-structures-and-algorithms-1st-edition-david-
a-watt/
ebookultra.com

Learning JavaScript Data Structures and Algorithms 2nd


Edition Loiane Groner

https://ebookultra.com/download/learning-javascript-data-structures-
and-algorithms-2nd-edition-loiane-groner/

ebookultra.com

Learning F Functional Data Structures and Algorithms 1st


Edition Masood

https://ebookultra.com/download/learning-f-functional-data-structures-
and-algorithms-1st-edition-masood/

ebookultra.com
Data Structures Algorithms In Go 1st Edition Hemant Jain

https://ebookultra.com/download/data-structures-algorithms-in-go-1st-
edition-hemant-jain/

ebookultra.com

Concise Notes on Data Structures and Algorithms Ruby


Edition Christopher Fox

https://ebookultra.com/download/concise-notes-on-data-structures-and-
algorithms-ruby-edition-christopher-fox/

ebookultra.com

Data Structures and Algorithms in Java 6th Edition Michael


T. Goodrich

https://ebookultra.com/download/data-structures-and-algorithms-in-
java-6th-edition-michael-t-goodrich/

ebookultra.com

Data Structures and Algorithms Using Python 1st Edition


Rance D. Necaise

https://ebookultra.com/download/data-structures-and-algorithms-using-
python-1st-edition-rance-d-necaise/

ebookultra.com

AI algorithms data structures and idioms in Prolog Lisp


and Java 6th Edition George F. Luger

https://ebookultra.com/download/ai-algorithms-data-structures-and-
idioms-in-prolog-lisp-and-java-6th-edition-george-f-luger/

ebookultra.com
Growing Algorithms and Data Structures 4th Edition
David Scuse Digital Instant Download
Author(s): David Scuse
ISBN(s): 9780100007369, 0100007368
Edition: 4th
File Details: PDF, 3.23 MB
Year: 2011
Language: english
Growing

Algorithms

And

Data Structures

(Fourth Edition)

© David Scuse
Department of Computer Science
University of Manitoba
June, 2011
GROWING ALGORITHMS AND DATA STRUCTURES

INTRODUCTION ................................................................................................................ 1
1 PROCEDURAL PROGRAMMING ................................................................................... 3
1.1 INTRODUCTION ........................................................................................................... 3
1.2 PROGRAMMING ............................................................................................................ 3
1.3 HELLO WORLD ............................................................................................................ 4
1.4 COMMENTS ................................................................................................................ 5
1.5 ASSIGNMENT STATEMENT ................................................................................................ 6
1.6 CONDITIONAL EXECUTION ............................................................................................... 8
1.7 LOOPS ................................................................................................................... 12
1.8 FUNCTIONS/METHODS ................................................................................................. 13
1.9 LISTS/ARRAYS .......................................................................................................... 14
1.10 PASSING PARAMETERS ............................................................................................. 19
1.11 SCOPE ............................................................................................................... 22
1.12 DATA TYPES ......................................................................................................... 22
1.13 STRINGS ............................................................................................................. 24
1.14 MODULES/CLASSES ................................................................................................ 26
1.15 DETERMINING PRIME NUMBERS ................................................................................... 27
1.16 DIFFERENCES BETWEEN PYTHON 2 AND PYTHON 3 .............................................................. 29
1.17 DIFFERENCES BETWEEN PYTHON AND JAVA ...................................................................... 29
1.18 SUMMARY ............................................................................................................ 29
2 GROWING ALGORITHMS ......................................................................................... 31
2.1 INTRODUCTION ......................................................................................................... 31
2.2 SEARCHING A LIST ..................................................................................................... 31
2.2.1 Linear Search................................................................................................. 33
2.2.2 Linear Search of an Ordered List ....................................................................... 36
2.2.3 Binary Search ................................................................................................ 38
2.3 SEARCHING A LIST OF STRINGS ...................................................................................... 40
2.4 RECAP ................................................................................................................... 41
2.5 ARRAY INITIALIZATION ................................................................................................. 41
2.6 CONDITIONAL OPERATOR .............................................................................................. 42
2.7 THE FOR STATEMENT................................................................................................... 43
2.8 SYSTEM.ARRAYCOPY .................................................................................................... 45
2.9 ROTATING THE ELEMENTS IN AN ARRAY .............................................................................. 46
2.10 GENERATING PRIME NUMBERS .................................................................................... 47
2.11 GENERATING SEQUENCES OF VALUES ............................................................................ 48
2.12 GENERATING A FREQUENCY COUNT OF VALUES ................................................................. 49
2.13 MERGE ALGORITHM ................................................................................................. 50
2.14 MERGING STRINGS ................................................................................................. 58
2.15 PARALLEL ARRAYS .................................................................................................. 59
2.15.1 Maintaining the Number of Elements in the Arrays .......................................... 60
2.15.2 Using a Sentinel Value ................................................................................. 63
2.15.3 Copying the Arrays ..................................................................................... 65
2.15.4 Global Variables.......................................................................................... 67
2.15.5 Recap ....................................................................................................... 68
2.16 GROWING ALGORITHMS ............................................................................................ 69
2.17 DEBUGGING ......................................................................................................... 70
2.18 TESTING ............................................................................................................. 74
2.19 DEFENSIVE PROGRAMMING ........................................................................................ 74
2.20 ALGORITHM EFFICIENCY ............................................................................................ 74
2.21 SUMMARY ............................................................................................................ 76

i
3 OBJECTS .................................................................................................................. 77
3.1 INTRODUCTION ......................................................................................................... 77
3.2 DATA TYPES ............................................................................................................. 77
3.3 BASIC OBJECT STRUCTURE ............................................................................................ 77
3.4 WHAT IS AN OBJECT? .................................................................................................. 82
3.5 SIMPLE CLASS STRUCTURE ............................................................................................ 83
3.6 VISIBILITY MODIFIERS ................................................................................................. 85
3.7 MANIPULATING AN OBJECT ............................................................................................ 85
3.8 CLASSES AND INSTANCES ............................................................................................. 86
3.9 STATE INFORMATION ................................................................................................... 87
3.10 ARRAYS OF OBJECTS ............................................................................................... 89
3.11 CLASS VARIABLES AND CLASS METHODS ........................................................................ 92
3.12 WRAPPER CLASSES ................................................................................................. 93
3.13 IMMUTABLE OBJECTS ............................................................................................... 95
3.14 COMPARING OBJECTS .............................................................................................. 96
3.15 ENCAPSULATION AND INFORMATION HIDING ..................................................................... 98
3.16 FACTORY METHOD .................................................................................................. 98
3.17 THE MAIN CLASS ................................................................................................... 99
3.18 SUMMARY .......................................................................................................... 100
4 FILE INPUT AND OUTPUT ...................................................................................... 103
4.1 INTRODUCTION ....................................................................................................... 103
4.2 SCANNER CLASS ...................................................................................................... 103
4.3 BUFFERED FILE INPUT ................................................................................................ 103
4.4 FILE OUTPUT .......................................................................................................... 105
4.5 RECAP ................................................................................................................. 107
4.6 MERGING FILES ....................................................................................................... 108
4.7 TWO USEFUL TEXTPAD FEATURES .................................................................................. 113
4.8 EXCEPTIONS........................................................................................................... 113
4.8.1 Thowing Exceptions ...................................................................................... 115
4.8.2 Programmer-Defined Exceptions ..................................................................... 116
4.9 AN INPUT/OUTPUT PROCESSING CLASS ........................................................................... 117
5 STRINGS ............................................................................................................... 121
5.1 INTRODUCTION ....................................................................................................... 121
5.2 CHARACTER MANIPULATION ......................................................................................... 121
5.3 CHARACTER WRAPPER CLASS ....................................................................................... 123
5.4 STRINGS............................................................................................................... 124
5.5 STRING METHODS .................................................................................................... 126
5.6 STRING COMPARISON ................................................................................................ 128
5.7 STRINGS ARE OBJECTS .............................................................................................. 129
5.8 STRINGS ARE IMMUTABLE ............................................................................................ 131
5.9 DATA VALIDATION .................................................................................................... 132
5.10 STRING FORMATTING ............................................................................................. 133
5.11 STRING PROCESSING ............................................................................................. 136
5.12 STRING TOKENIZING ............................................................................................. 138
5.12.1 Extracting Tokens ..................................................................................... 138
5.12.2 String Split Method ................................................................................... 139
5.13 BINARY SEARCH OF AN ARRAY OF STRINGS .................................................................... 139
5.14 PATTERN MATCHING .............................................................................................. 140
5.15 MORE PATTERN MATCHING ...................................................................................... 142
5.16 COUNTING CHARACTERS ......................................................................................... 143
5.17 A MUTABLE STRING CLASS ...................................................................................... 145
5.18 STRINGBUFFER CLASS ........................................................................................... 146
5.18.1 StringBuffer Methods................................................................................. 146
5.18.2 Using a StringBuffer Object ........................................................................ 147

ii
6 OBJECT EXAMPLES ................................................................................................ 149
6.1 INTRODUCTION ....................................................................................................... 149
6.2 PERSON EXAMPLE..................................................................................................... 149
6.3 VIDEO STORE EXAMPLE .............................................................................................. 150
6.4 EMPLOYEE EXAMPLE .................................................................................................. 151
6.5 GST ACCOUNT EXAMPLE ............................................................................................ 153
6.6 COFFEE SHOP EXAMPLE .............................................................................................. 154
6.6.1 Iteration 1 ................................................................................................... 154
6.6.2 Iteration 2 ................................................................................................... 155
6.6.3 Iteration 3 ................................................................................................... 156
6.6.4 Iteration 4 ................................................................................................... 157
6.6.5 Recap ......................................................................................................... 159
6.7 WORDS CLASS........................................................................................................ 159
7 OBJECT REPRESENTATION .................................................................................... 163
7.1 INTRODUCTION ....................................................................................................... 163
7.2 OBJECT REFERENCES ................................................................................................. 163
7.3 ARRAYS ARE OBJECTS ................................................................................................ 165
7.4 STRINGS............................................................................................................... 168
7.4.1 Strings are Immutable .................................................................................. 169
7.5 PASSING PARAMETERS ............................................................................................... 170
7.6 CLONING OBJECTS ................................................................................................... 174
7.7 INFORMATION ABOUT OBJECTS ..................................................................................... 175
7.8 SUMMARY ............................................................................................................. 175
8 OBJECT ORIENTATION PRACTICES ........................................................................ 177
8.1 INTRODUCTION ....................................................................................................... 177
8.2 BASIC CLASS STRUCTURE ........................................................................................... 177
8.3 REMOVE ACCESSORS AND MUTATORS .............................................................................. 182
8.4 MOVE OBJECT PROCESSING INTO ITS OWN CLASS ................................................................ 184
8.5 USING FILE INPUT .................................................................................................... 186
8.6 USING A FACTORY METHOD ......................................................................................... 187
8.7 COMPLETE CREDIT-CARD EXAMPLE ................................................................................. 188
8.8 SUMMARY ............................................................................................................. 192
9 OBJECT COLLECTIONS ........................................................................................... 193
9.1 INTRODUCTION ....................................................................................................... 193
9.2 ARRAYLISTS........................................................................................................... 193
9.3 COMPILING ARRAYLISTS ............................................................................................. 195
9.4 ARRAYLIST METHODS ................................................................................................ 196
9.5 CASTING .............................................................................................................. 197
9.6 REVERSING THE ELEMENTS IN AN ARRAYLIST ..................................................................... 198
9.7 SEARCHING AN ARRAYLIST .......................................................................................... 199
9.8 PRINTING THE CONTENTS OF AN ARRAYLIST ....................................................................... 201
9.9 ROTATING THE CONTENTS OF AN ARRAYLIST ...................................................................... 201
9.10 INSERTING INTO AN ARRAYLIST IN ASCENDING ORDER ...................................................... 202
9.11 SEARCHING AN ARRAYLIST OF INTEGERS WITH A BINARY SEARCH .......................................... 202
9.12 SEARCHING AN ARRAYLIST OF STRINGS WITH A BINARY SEARCH ........................................... 203
9.13 CREDIT-CARDS EXAMPLE ........................................................................................ 204
9.14 GENERICS ......................................................................................................... 206
9.15 BOXING AND UNBOXING ......................................................................................... 207
9.16 OUR VERSION OF ARRAYLIST.................................................................................... 208
9.17 ARRAYLIST ALGORITHMS......................................................................................... 209
9.18 PROCESSING COURSE MARKS ................................................................................... 210
9.19 SUMMARY .......................................................................................................... 214

iii
10 OBJECT HIERARCHIES ........................................................................................... 215
10.1 INTRODUCTION .................................................................................................... 215
10.2 OBJECTS ........................................................................................................... 215
10.3 ANIMALS EXAMPLE ................................................................................................ 216
10.4 CLASS HIERARCHIES ............................................................................................. 219
10.5 VISIBILITY ......................................................................................................... 220
10.6 INHERITING AND OVERRIDING METHODS....................................................................... 221
10.7 CONSTRUCTORS IN HIERARCHIES ............................................................................... 224
10.8 CASTING OBJECTS ................................................................................................ 227
10.9 INSTANCEOF ....................................................................................................... 228
10.10 CLASS VARIABLES AND CLASS METHODS ...................................................................... 229
10.11 CLASS OBJECT .................................................................................................... 230
10.12 ARRAYS ARE OBJECTS ............................................................................................ 231
10.13 COLLECTIONS OF OBJECTS ....................................................................................... 231
10.14 ARRAYLISTS ....................................................................................................... 233
10.15 GENERICS ......................................................................................................... 234
10.16 DRIVER’S LICENSE EXAMPLE ..................................................................................... 235
10.17 RECAP .............................................................................................................. 238
10.18 SUMMARY .......................................................................................................... 238
11 MORE OBJECT HIERARCHY TOPICS ....................................................................... 239
11.1 INTRODUCTION .................................................................................................... 239
11.2 POLYMORPHISM ................................................................................................... 239
11.3 ABSTRACT CLASSES .............................................................................................. 245
11.4 INHERITING AND SHADOWING VARIABLES ..................................................................... 246
12 OBJECT HIERARCHY EXAMPLES ............................................................................. 249
12.1 INTRODUCTION .................................................................................................... 249
12.2 BANK ACCOUNT EXAMPLE ........................................................................................ 249
12.2.1 Recap ..................................................................................................... 252
12.3 CREDIT CARD EXAMPLE .......................................................................................... 252
12.4 STOCK PORTFOLIO EXAMPLE ..................................................................................... 255
12.4.1 Iteration 1 ............................................................................................... 256
12.4.2 Iteration 2 ............................................................................................... 258
12.4.3 Iteration 3 ............................................................................................... 259
12.4.4 Iteration 4 ............................................................................................... 263
12.4.5 Iteration 5 ............................................................................................... 267
12.4.6 Iteration 6 ............................................................................................... 268
12.4.7 Iteration 7 ............................................................................................... 269
12.4.8 Iteration 8 ............................................................................................... 271
12.4.9 Iteration 9 ............................................................................................... 271
12.4.10 Complete Stock Portfolio Example ............................................................... 272
12.4.11 Recap ..................................................................................................... 277
12.5 SUMMARY .......................................................................................................... 277
13 SORTING ............................................................................................................... 279
13.1 INTRODUCTION .................................................................................................... 279
13.2 BUBBLE SORT ..................................................................................................... 279
13.3 SELECTION SORT ................................................................................................. 282
13.4 INSERTION SORT ................................................................................................. 284
13.5 SORTING AN ARRAY OF STRINGS ................................................................................ 286
13.6 SORTING INTO DESCENDING ORDER............................................................................ 287
13.7 SUMMARY .......................................................................................................... 287
14 RECURSION ........................................................................................................... 289
14.1 INTRODUCTION .................................................................................................... 289
14.2 ITERATION ......................................................................................................... 289
14.3 RECURSION ........................................................................................................ 290

iv
14.4 PROCESSING STRINGS ........................................................................................... 293
14.4.1 String Reverse ......................................................................................... 293
14.4.2 Palindromes ............................................................................................. 294
14.4.3 String Padding .......................................................................................... 295
14.4.4 Number Conversions ................................................................................. 295
14.5 PROCESSING ARRAYS............................................................................................. 296
14.6 BINARY SEARCH .................................................................................................. 299
14.7 RECURSIVE MERGE ............................................................................................... 300
14.8 PROCESSING ARRAYLISTS ....................................................................................... 300
14.9 FIBONACCI NUMBERS ............................................................................................. 301
14.10 PROCESSING OBJECTS ........................................................................................... 303
14.11 THE RECURSION STACK .......................................................................................... 304
14.12 ITERATION VERSUS RECURSION ................................................................................. 306
14.13 RECURSION EXAMPLES ........................................................................................... 307
14.13.1 Palindrome .............................................................................................. 307
14.13.2 Reversing Words....................................................................................... 307
14.13.3 Filling an Array ......................................................................................... 308
14.13.4 Precomputing Values ................................................................................. 308
14.13.5 Frequency Count ...................................................................................... 309
15 LINKED LISTS ....................................................................................................... 311
15.1 INTRODUCTION .................................................................................................... 311
15.2 LISTS .............................................................................................................. 311
15.3 ARRAYS ............................................................................................................ 311
15.4 A SIMPLE LINKED LIST ........................................................................................... 311
15.5 A BETTER LINKED LIST ........................................................................................... 314
15.6 A LINKED LIST CLASS ............................................................................................ 316
15.7 INSERTING AT THE BEGINNING OF A LINKED LIST ............................................................. 319
15.8 INSERTING AT THE END OF A LINKED LIST ..................................................................... 320
15.9 DELETING AN ELEMENT ........................................................................................... 321
15.10 LINKED LIST EXAMPLES .......................................................................................... 326
15.10.1 Find Last Node ......................................................................................... 326
15.10.2 Print String Objects ................................................................................... 327
15.10.3 Print Adjacent String Objects ...................................................................... 327
15.10.4 Reverse List Elements ............................................................................... 328
15.10.5 Add Back Links ......................................................................................... 328
15.10.6 Split a List into Two Lists ........................................................................... 328
15.10.7 Rotating the Contents of a Linked List.......................................................... 329
15.10.8 Remove String Objects .............................................................................. 330
15.11 RECAP .............................................................................................................. 330
15.12 SUMMARY .......................................................................................................... 331
16 MULTI-DIMENSIONAL ARRAYS .............................................................................. 333
16.1 INTRODUCTION .................................................................................................... 333
16.2 ONE-DIMENSIONAL ARRAYS ..................................................................................... 333
16.3 MULTI-DIMENSIONAL ARRAYS ................................................................................... 333
16.4 MATRICES ......................................................................................................... 335
16.5 MATRIX OBJECT ................................................................................................... 337
16.6 ARRAY INITIALIZATION ........................................................................................... 339
16.7 ARRAYS OF OBJECTS ............................................................................................. 339
16.8 TIC-TAC-TOE ..................................................................................................... 340
17 GENERIC DATA STRUCTURES ................................................................................ 343
17.1 INTRODUCTION .................................................................................................... 343
17.2 GENERIC LINKED LIST ............................................................................................ 343
17.3 USING COMPARETO ............................................................................................... 345
17.4 JAVA’S GENERICS ................................................................................................. 346
17.5 USING COMPARETO WITH GENERICS ............................................................................ 349

v
17.6 USING A JAVA INTERFACE ........................................................................................ 349
18 GROWING AND REFACTORING .............................................................................. 353
18.1 INTRODUCTION .................................................................................................... 353
18.2 REFACTORING ..................................................................................................... 353
18.2.1 Rename Method ....................................................................................... 353
18.2.2 Extract Method ......................................................................................... 353
18.2.3 Convert Procedural Design to Objects .......................................................... 354
18.2.4 Comments ............................................................................................... 354
18.3 DESIGN ............................................................................................................ 354
18.4 FLIGHTS EXAMPLE ................................................................................................ 355
18.4.1 Iteration 1 ............................................................................................... 355
18.4.2 Iteration 2 ............................................................................................... 356
18.4.3 Iteration 3 ............................................................................................... 358
18.4.4 Iteration 4 ............................................................................................... 359
18.4.5 Iteration 5 ............................................................................................... 361
18.4.6 Iteration 6 ............................................................................................... 363
18.4.7 Iteration 7 ............................................................................................... 364
18.4.8 Iteration 8 ............................................................................................... 365
18.4.9 Iteration 9 ............................................................................................... 367
18.4.10 Iteration 10 ............................................................................................. 367
18.4.11 Iteration 11 ............................................................................................. 369
18.5 COMPLETE FLIGHTS EXAMPLE .................................................................................... 370
18.6 SUMMARY .......................................................................................................... 375
19 MISCELLANEOUS TOPICS ...................................................................................... 377
19.1 INTRODUCTION .................................................................................................... 377
19.2 SYSTEM STREAMS................................................................................................. 377
19.3 FILE PROCESSING WRAPPER CLASSES .......................................................................... 381
19.4 BUFFERED CONSOLE INPUT ...................................................................................... 382
19.5 FILES AND THE END-OF-LINE CHARACTER(S) ................................................................. 384
19.6 NON-RECTANGULAR ARRAYS .................................................................................... 384
19.7 PARALLEL ARRAYS ................................................................................................ 385
19.8 HIGHER DIMENSIONAL ARRAYS ................................................................................. 386
19.9 MERGE SORT ...................................................................................................... 387
19.10 QUICKSORT ....................................................................................................... 388
19.11 SORTING A LINKED LIST ......................................................................................... 389
19.12 BINARY TREES .................................................................................................... 393
19.13 TOWERS OF HANOI ............................................................................................... 395
19.14 SYSTEM PROPERTIES ............................................................................................. 397
19.15 INFORMATION ABOUT OBJECTS .................................................................................. 397
19.16 THE SYSTEM TIMER ............................................................................................... 398
19.17 THE STACK TRACE ................................................................................................ 399
19.18 GARBAGE COLLECTION ........................................................................................... 400
19.19 THE HEAP.......................................................................................................... 400
19.20 THE OBJECT HIERARCHY ......................................................................................... 401
19.21 CHANGING THE HASH CODE ..................................................................................... 402
19.22 FILE DIRECTORY PROCESSING ................................................................................... 402
19.23 RECURSIVE FILE DIRECTORY PROCESSING ..................................................................... 403
19.24 STACK-BASED FILE DIRECTORY PROCESSING ................................................................. 404
19.25 CREATING OBJECTS DYNAMICALLY .............................................................................. 406
19.26 CALLING METHODS DYNAMICALLY .............................................................................. 407
19.27 POLYMORPHISM WITHOUT HIERARCHIES ........................................................................ 408
19.28 COMPILING AND EXECUTING A JAVA PROJECT .................................................................. 411
19.29 SUMMARY .......................................................................................................... 413
INDEX........................................................................................................................... 415

vi
INTRODUCTION

Courses such as COMP 1010 and COMP 1012 focus on developing algorithms using
primitive data types (ints, doubles, etc.), conditions, loops, arrays, and methods. These are the
basic building blocks of computer programming and this style of programming is referred to
as “procedural programming”. In COMP 1020, we examine how these building blocks can
be put together to solve more complex problems. At the same time, we also make the
transition from procedural programming to object-oriented programming. Writing good
object-oriented programs is not easy, it takes some time to stop thinking in a procedural
manner and start thinking in an object-oriented manner.

Our study of algorithms concentrates on the development of algorithms that work correctly
and are easy to understand. We will not be concerned with developing algorithms that are as
efficient as possible. We will note where certain algorithms are inefficient and could be
improved but we will not attempt to make each algorithm optimal in terms of its memory
requirements and/or execution time.

In COMP 1010 and COMP 1012, the organization of each program in an assignment was
normally described in class. In this course, the organization of solution for assignments will
not be given; instead, the student is expected to develop the structure of each program by
him/herself (with some hints from the instructor). Instead of developing the entire program at
once, the program will be developed (“grown”) slowly. We will concentrate on “doing the
simplest thing that works”, in other words, develop a simple, incomplete version of the
algorithm that executes correctly and then gradually improve the algorithm until it provides
the required functionality (start small and grow slowly). We will not add functionality that
is not explicitly required!

“Simple systems are easier to build, easier to maintain, smaller, and faster than
complex ones. A simple system ‘maximizes the work done’, by increasing ‘the
amount of work not done’. A program must do exactly what’s required by the
user. Adding unasked-for-functionality dramatically increases the development
time and decreases stability.”
Allen Holub, Holub on Patterns, p. 6.

David Scuse
Department of Computer Science
University of Manitoba

June, 2011

1
2
Chapter 1 Procedural Programming

1 PROCEDURAL PROGRAMMING

1.1 Introduction

In this chapter, we review the basic constructs used to build procedural (no objects) programs.
This chapter includes both Python and Java examples to enable students who were introduced
to procedural programming using the Python language to make the transition to Java. The
programs in these notes were developed using Python version 3.2 and Java version 6.

In the following sections, we develop programs in both Python and Java – these programs are
stored in source-code files; although Python statements may also be entered at a Python
command prompt; we will not take advantage of this technique.

1.2 Programming

A program is a collection of statements in a programming language. The statements are


executed sequentially, beginning with the first statement and continuing statement by
statement until the last statement in the program is encountered (although control structures
may be used to change the order in which statements are executed). The process of
developing programs is called programming (naturally) and the process of removing errors in
programs is referred to as “debugging”.

In the following sections, we will briefly review the programming constructs used to build
procedural programs and examine how programs can be built from these fairly simple
constructs. The following comment from the book Think Python, How to Think Like a
Computer Scientist provides a good description of the thought processes used while
developing programs:

“The goal of this book is to teach you to think like a computer scientist. This way of
thinking combines some of the best features of mathematics, engineering, and natural
science. Like mathematicians, computer scientists use formal languages to denote
ideas (specifically computations). Like engineers, they design things, assembling
components into systems and evaluating tradeoffs among alternatives. Like scientists,
they observe the behavior of complex systems, form hypotheses, and test predictions.

The single most important skill for a computer scientist is problem solving. Problem
solving means the ability to formulate problems, think creatively about solutions, and
express a solution clearly and accurately. As it turns out, the process of learning to
program is an excellent opportunity to practice problem-solving skills.”
Allen Downey, Think Python, pp. 1.
http://greenteapress.com/thinkpython

3 1.2 Programming
Chapter 1 Procedural Programming

1.3 Hello World

The “Hello World” program is the program that is most frequently used to illustrate a new
programming language. Hello World is an important program not only because it provides a
starting point when learning a new language but also because it ensures that the necessary
infrastructure (compiler, paths to libraries, etc.) is set up correctly. The following Python
program prints Hello World!.

print("Hello World!")

In these notes, the output from most programs or program segments is shown directly after the
program. So the program above generates the output:

Hello World!

If any error messages are generated as the program is executed, the error messages are
displayed for the programmer. The following program generates an error because the
statement printer is not defined in Python.

printer("Hello World!")

Traceback (most recent call last):


File "C:\HelloWorld.py", line 1, in <module> printer("Hello World!")
NameError: name 'printer' is not defined

The following Java method prints Hello World!. Note that Java statements must be
terminated by a semi-colon (;):

public static void main(String[] parms)


{
System.out.println("Hello World!");
}

However, this method is not a complete Java program; Java methods must be defined within a
class before they can be compiled and executed correctly. The following program defines a
class named HelloWorld that is placed around the method. By convention, the first letter of
the name of each Java class is defined in upper case. Note that in Java, the name of the class
must be the same as the name of the file in which the class is defined. So the program below
would be defined in a file named HelloWorld.java.

public class HelloWorld


{
public static void main(String[] parms)
{
System.out.println("Hello World!");
}
}

4 1.3 Hello World


Chapter 1 Procedural Programming

Java uses a two-step process in which a program is first compiled and then, if there are no
compilation errors, the program can be executed. The compilation and the execution are
separate steps so two commands must be issued to compile and execute a Java program.
When a Java source file is compiled, Java creates a corresponding file called a “class” file (in
the same directory as the source file). If the source file is named HelloWorld.java, the class
file will be named HelloWorld.class. The class file is used when the program is executed.

In these notes, all programs are run using the (Windows) TextPad environment; if you are
using a different system, your output may be displayed in a slightly different manner. As can
be seen from the following TextPad Tools menu, a Java program is first compiled using
Ctrl+1 and is then executed using Ctrl+2. The Python tool was manually added to TextPad
so depending on how you configured Python, the Python entry is not necessarily in the
TextPad Tools menu, or, if it is, it is not necessary Tool 3.

1.4 Comments

The Python language uses the # character to mark the beginning of a comment. All characters
on the current line that follow the # character are ignored by Python.

# This is my first Python program

print("Hello World!")

The Java language supports two types of comments – the single-line comment that begins
with // and a multiline comment that is surrounded by the characters /* and */

// This is my first Java program


public class HelloWorld
{
public static void main(String[] parms)
{
System.out.println("Hello World!");
}
}

5 1.4 Comments
Chapter 1 Procedural Programming

/*
This is my first Java program
*/

public class HelloWorld


{
public static void main(String[] parms)
{
System.out.println("Hello World!");
}
}

1.5 Assignment Statement

The assignment statement is one of the fundamental constructs in programming languages. It


is used to assign a value to a variable. A variable in Python is a name that begins with an
alphabetic character and is not one of the Python reserved keywords. A variable in Java
follows the same rules (except that the variable must not be the same as one of the Java
reserved keywords).

The following Python program assigns the integer value 25 to the variable i. The program
then prints the value of i. (Again, remember that the output of the program is shown after the
program – the line that contains 25 is the output of the program, not part of the program.)

i = 25
print(i)

25

The two Python statements do not contain a statement terminator. However, the semi-colon
(;) is used in Python as a statement delimiter (when more than one Python statement is on the
same line); so the following Python program is equivalent to the one above.)

i = 25; print(i)

25

In Java, the equivalent statements are:

int i;
i = 25;
System.out.println(i);

Again, unlike Python, Java statements must be defined within a method which is defined
within a class. So the complete Java program is:

public class Program1


{
public static void main(String[] parms)

6 1.5 Assignment Statement


Chapter 1 Procedural Programming

{
int i;
i = 25;
System.out.println(i);
}
}

25

In the remainder of this chapter, we will not include the Java method declarations or Java
class declarations unless it is helpful to do so.

In addition to the method and class information that must be defined in a Java program, the
Java program above also includes the statement

int i;

This statement declares the variable/identifier i to be of type int. The data type int is
common to both Python and Java; the difference is that in Python, variables are not declared
before they are used; in Java, variables must be declared before they are used. Also, in Java,
variables declared to be of a specific type may only be assigned values of that type; in Python,
since variables are not declared to be of a specific type, they may be assigned a value of any
type (this is referred to as “dynamic typing”).

Variables in both Python and Java are case sensitive which means that the variable apple is
not the same variable as Apple.

The arithmetic operators that manipulate integers in Python are the same as those in Java:

Python Java Meaning


+ + addition
- - subtraction
* * multiplication
// (Python 3) / integer (truncating) division
/ (must cast to a real value) real division
% % modulo division (remainder)
** Math.pow(base,exponent) exponentiation

Arithmetic expressions are formed in the same manner in both languages. The following
arithmetic expressions are the same in both Python and Java. Parentheses may be used to
force the order of the evaluation of expressions.

2 + 3
(4 * 5) + 10
(25 - 3) * (-10 +100)

7 1.5 Assignment Statement


Chapter 1 Procedural Programming

Both Python and Java support shortcuts when performing some of the basic assignment
operations.

Python Java Meaning


NA i++ i = i + 1
NA i-- i = i - 1
i += j i += j i = i + j
i -= j i -= j i = i - j
i *= j i *= j i = i * j
i //= j i /= j i = i / j

We will examine data types in more detail later in this chapter but for now, we will work with
simple integers.

1.6 Conditional Execution

In Python, conditional execution is defined with an if statement. Indentation is used to


indicate that one or more statements (a “suite” in Python terms) are within the true portion of
the if statement or within the false/else portion of the if statement. Note that there is a colon :
at the end of the if header and also at the end of the else header.

i = 2
if i > 0:
print(i, "is greater than zero")
else:
print(i, "is not greater than zero")

2 is greater than zero

In Java, conditional execution is similar but requires different delimiters: curly brackets { and
} are used indicate that statements are within the true portion of the if statement or within the
false portion. The indentation of statements within the curly brackets is a programming
convention but is not strictly necessary.

int i;
i = 2;
if (i > 0)
{
System.out.println(i +" is greater than zero");
}
else
{
System.out.println(i +" is not greater than zero");
}

2 is greater than zero

Note that the Java println statement does not add a blank character between parameters; if a

8 1.6 Conditional Execution


Chapter 1 Procedural Programming

blank is required, it must be included by the programmer.

In Python, multiple statements may be included inside the true portion of an if statement
and/or inside the false/else portion of an if statement. Indentation is used to indicate that
statements are contained within the true portion or the false portion of the if statement.

i = 2
if i > 0:
print(i, end=" ")
print("is greater than zero")
else:
print(i, end=" ")
print("is not greater than zero")

2 is greater than zero

In Python, the print function causes a newline character to be generated after the last
character is printed unless the parameter end=" " is included at the end of the parameter list.

In Java, the statements are very similar. Multiple statements may be included within the curly
brackets that enclose the true portion and/or the false/else portion of the if statement.

int i;
i = 2;
if (i > 0)
{
System.out.print(i);
System.out.println(" is greater than zero");
}
else
{
System.out.print(i);
System.out.println(" is not greater than zero");
}

In Java, the print statement does not generate a newline character at the end of the characters
that are printed, while the println statement does generate a newline character at the end of
the printed characters.

The logical expression (or Boolean expression) in the if statement is an expression that
evaluates to either true or false. In the programs above, a simple logical expression that
compares integer values illustrates the use of the numeric comparison operators.

The numeric comparison operators in Python are the same as those in Java:

Python Java Meaning


== == equal
!= != not equal
< < less than

9 1.6 Conditional Execution


Chapter 1 Procedural Programming

> > greater than


<= <= less than or equal
>= >= greater than or equal

More complex logical expressions can be created by including logical operators that join
numeric (relational) expressions.

The logical operators in Python use the name of the operator while those in Java use symbols:

Python Java Meaning


and && and
or || or
not ! not

The result of evaluating a logical expression is a Boolean value (see the Section: Data Types):

Python Java Meaning


True true true
False false false

Python supports some shortcuts when defining logical expressions. For example, the
expression below is a valid Python expression.

3 <= i <= 12

The expression above is not valid in Java but the statement below is equivalent and is also
valid in Python:

(3 <= i) && (i <= 12)

At times it is necessary to have more than 2 conditions. Python supports this with the elif
statement.

i = 2
if i > 0:
print(i, "is a positive number")
elif i < 0:
print(i, "is a negative number")
else:
print(i, "is zero")

2 is a positive number

There may be any number of elif statements and the else statement is optional.

Java does not support the elif statement; instead, additional if statements are used.

10 1.6 Conditional Execution


Chapter 1 Procedural Programming

int i;
i = 2;
if (i > 0)
{
System.out.println(i +" is a positive number");
}
else if (i < 0)
{
System.out.println(i +" is a negative number");
}
else
{
System.out.println(i +" is equal to zero");
}

2 is a positive number

if statements may be nested within other control structures (such as if statements). Again,
the indentation of the statements in Python is important.

i = -20000000
if i > 0:
if i > 1000000:
print(i, "is a big positive number")
else:
print(i, "is a positive number")
elif i < 0:
if i < -1000000:
print(i, "is a big negative number")
else:
print(i, "is a negative number")
else:
print(i, "is zero")

-20000000 is a big negative number

In Java, if statements may also be nested within other control structures.

i = -20000000;
if (i > 0)
{
if (i > 1000000)
{
System.out.println(i +" is a big positive number");
}
else
{
System.out.println(i +" is a positive number");
}
}
else if (i < 0)
{
if (i < -1000000)
{
System.out.println(i +" is a big negative number");
}
else
{

11 1.6 Conditional Execution


Chapter 1 Procedural Programming

System.out.println(i +" is a negative number");


}
}
else
{
System.out.println(i +" is equal to zero");
}

-20000000 is a big negative number

1.7 Loops

A loop is a control structure that causes the statements inside the loop to be executed a
specified number of times. For example, the following Python loop causes the print
statement inside the loop to be executed 5 times, with the variable i being assigned the values
0, 1, 2, 3,and 4. Again, note that the for statement header is terminated with a colon.

for i in range(0, 5):


print(i)

0
1
2
3
4

The corresponding program in Java is very similar.

int i;
for (i=0; i<5; i++)
{
System.out.println(i);
}

Both languages also support a while loop.

# Python while loop


i = 0
while i < 5:
print(i)
i += 1

// Java while loop


i = 0;
while (i < 5)
{
System.out.println(i);
i += 1; // or i++;
}

In Python, if a loop may terminate before all of the values in the for statement are exhausted,
the loop must be written using a while statement. The following program is to print all
values beginning with n and continuing until a value that is a multiple of 20 is found. If such

12 1.7 Loops
Chapter 1 Procedural Programming

a value is found, the loop is to terminate without printing any additional values.

n = 25
i = n
stop = 0
while (i < 50) and (stop==0):
print(i, end=" ")
if ((i//20)*20)==i:
stop = 1
i += 1
print()

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

Although Python (and Java) supports a break statement that permits the programmer to exit
from a loop at any time, using a break statement is a poor programming practice and it will
not be used in these notes.

In Java, the for statement supports the addition of extra clauses that determine when the loop
is to terminate so it is not necessary to write the loop using a while statement.

int n;
int i;
int stop;

n = 25;
stop = 0;
for (i=n; (i<50)&&(stop==0); i++)
{
System.out.print(i +" ");
if ((i/20*20)==i)
{
stop = 1;
}
}
System.out.println();

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

Both Python and Java support control structures nested within other control structures so it is
valid to have a loop within a loop.

1.8 Functions/Methods

Python and Java both support functions that are very similar in appearance. In Python, the
function is declared before it is referred to. The definition of a function may include any
number of parameters passed from the calling function.

The following Python program shows how a function that prints the integer values from 0 to
(n-1) is defined and then called from the main function.

13 1.8 Functions/Methods
Chapter 1 Procedural Programming

def print_loop(n):
for i in range(0, n):
print(i)
return

print_loop(5)

If a function does not return a value, it is often referred to as a “void function”. A void
function may include a return statement that does not include a return value; however, the
return statement is not required.

In Java, functions are referred to as “methods”. For now, all Java methods will be specified
with the keyword static. The type of value returned by a method is either void (meaning
the method does not return a value) or a valid Java data type (such as int). A void method in
Java is the same as a Python void function – both may include a return statement that does
not specify a value to be returned. In the following example, the class definition is included
around the two methods.

public class FunctionTest


{
public static void main(String[] parms)
{
print_loop(5);
}

public static void print_loop(int n)


{
int i;

for (i=0; i<n; i++)


{
System.out.println(i);
}
}
}

In Java, a method may be defined either before or after it is referred to.

1.9 Lists/Arrays

An array is a programming construct that can store a collection of values. In Python, the more
generic term “collection” is used instead of array. Python has different types of collections
but we will focus on the “list” collection in this chapter. The following Python statement
creates a Python list that contains the integer values 1, 2, 3, and 4.

my_list = [1, 2, 3, 4]

The advantage of using a list (instead of individual integer values) is that the collection may
be passed to a function by specifying only the name of the list (instead of having to pass all of

14 1.9 Lists/Arrays
Chapter 1 Procedural Programming

the values individually).

The following Python program creates a list and then passes the list to the function
print_list which prints the elements of the list on the same line.

def print_list(my_list):
for i in range(0, len(my_list)):
print(my_list[i], end=" ")
print()
return

my_list = [1, 2, 3, 4]
print_list(my_list)

1 2 3 4

Python lists are “zero-based” (or “0-based”), meaning that the first element in the list is
element zero and is accessed by my_list[0] in the program above. The variable i in the
program above is used as a “subscript” – a value that specifies the position of an element in a
list that is to be accessed.

Python provides a variation of the for statement that makes the manipulation of lists easier.
The version below uses the modified for statement that specifies that the variable i is to be
assigned one value from the list each time through the loop. In this program, the variable i is
no longer a subscript, i contains an element of the list.

def print_list(my_list):
for i in my_list:
print(i, end=" ")
print()
return

The following Java methods illustrate the statements required to initialize a Java array and
then print the contents of the array using a Java method.

public static void main(String[] parms)


{
int[] my_list;
my_list = new int[] {1, 2, 3, 4};
print_list(my_list);
}

public static void print_list(int[] my_list)


{
int i;

for (i=0; i<my_list.length; i++)


{
System.out.print(my_list[i] +" ");
}
System.out.println();
}

1 2 3 4

15 1.9 Lists/Arrays
Chapter 1 Procedural Programming

As can be seen, Java requires more effort to declare the array, to pass the array as a parameter,
and to manipulate the array, but the structure of the program is very similar to the structure of
the Python program.

An array variable is declared in Java by placing square brackets [ and ] after the data type
that the array is to contain. The parameter of the print_list function must be declared to be
of the desired type, in this case, an integer array int[]. The number of elements in the array
is determined in a manner similar to that of Python; in Python, len(my_list) is used while in
Java, my_list.length is used.

Lists/arrays can be modified in both Python and Java but Python provides signiciantly more
flexibility than does Java.

The following Python program creates a new list and initializes the elements to the values 0,
1, 2, …, n-1 where n is passed as a parameter to the create_list function.

def print_list(my_list):
for i in my_list:
print(i, end=" ")
print()
return

def create_list(n):
my_list = []
for i in range(0, n):
my_list.append(i)
return my_list

my_list = create_list(5)
print_list(my_list)

0 1 2 3 4

The statement my_list = [] is used to create a new, empty list. The Python append function
is used to append an element to the end of an existing list. So the effect of this program is to
create a list that consists of the values 0, 1, 2, 3, 4 and then print that list.

The equivalent Java program is shown below.


public static void main(String[] parms)
{
int[] my_list;

my_list = create_list(5);
print_list(my_list);
}

public static int[] create_list(int n)


{
int[] list;
int i;

16 1.9 Lists/Arrays
Chapter 1 Procedural Programming

list = new int[n];


for (i=0; i<n; i++)
{
list[i] = i;
}
return list;
}

public static void print_list(int[] list)


{
int i;

for (i=0; i<list.length; i++)


{
System.out.print(list[i] +" ");
}
System.out.println();
}

0 1 2 3 4

Although the two programs are very similar (ignoring the syntactic differences), there is one
significant difference. In Python, a list is a dynamic structure: a list is typically created as an
empty list and then elements are added to the end of the list using the append function. In
Java, an array is a static structure: it is given an initial size and this size can not be modified.
Once a Java array has been created, the elements of the array are modified by replacing their
initial values. In the Java program above, the array is declared to be of size n (which is 5 in
this example). At this point, the array contains 5 elements (which are initialized to zero by
Java). Any of these 5 elements may be modified using an assignment statement such as:
list[3] = 4; However, the size of the array is set to 5 and this can not be changed.

The following function illustrates how the maximum value in a Python list can be determined.
def max_list(my_list):
max = my_list[0]
for i in my_list:
if i > max:
max = i
return max

The program does have some warts in it – if the list is empty, the program terminates
abnormally; also, the first element in the list is processed twice. However, these problems can
be fixed with a little effort.

The equivalent Java method is shown below.


public static int max_list(int[] my_list)
{
int max;
int i;

max = my_list[0];
for (i=1; i<my_list.length; i++)

17 1.9 Lists/Arrays
Chapter 1 Procedural Programming

{
if (my_list[i] > max)
{
max = my_list[i];
}
}
return max;
}

Python contains functions that permit the programmer to insert a new element at any location
in an existing list, delete an element from a list, and append to the end of a list. These
functions do not exist with arrays in Java but can be written by the programmer. The
following Java method remove_element removes an element from an existing array
(assuming that the element exists in the array). It is important to note that the parameter
passed to remove_element is the value of the element to be deleted, not the position of the
element to be deleted. The method System.arraycopy is described in more detail in the next
chapter.
public static void main(String[] parms)
{
int[] my_list;

my_list = create_list(5);
print_list(my_list);
System.out.println();

my_list = remove_element(my_list, 0);


print_list(my_list);

my_list = remove_element(my_list, 1);


print_list(my_list);

my_list = remove_element(my_list, 4);


print_list(my_list);

my_list = remove_element(my_list, 3);


print_list(my_list);
}

public static int[] create_list(int n)


{
int[] list;
int i;

list = new int[n];


for (i=0; i<n; i++)
{
list[i] = i;
}
return list;
}

public static int[] remove_element(int[] list, int element)


{
int[] new_list;
int position;
int i;

18 1.9 Lists/Arrays
Chapter 1 Procedural Programming

position = -1;
for (i=0; i<list.length; i++)
{
if (list[i] == element)
{
position = i;
}
}
if (position == -1)
{
new_list = list; // the element was not found, so return the original list
}
else
{
new_list = new int[list.length-1];
System.arraycopy(list, 0, new_list, 0, position);
System.arraycopy(list, position+1, new_list, position, new_list.length-position);
}
return new_list;
}

public static void print_list(int[] list)


{
int i;

for (i=0; i<list.length; i++)


{
System.out.print(list[i] +" ");
}
System.out.println();
}

0 1 2 3 4

1 2 3 4
2 3 4
2 3
2

As can be seen, deleting an element from an array is not a trivial exercise. However, Java
does contain a more sophisticated collection mechanism which is discussed in Chapter 9 –
Object Collections.

1.10 Passing Parameters

Parameters of functions are treated in essentially the same manner in both Python and Java. If
a numeric value is passed to a function, the value may be modified within the function but the
modified value does not replace the original value in the calling function. In the following
example, the value of n is passed to function1. n is modified within the function but this
modified value does not replace the original value of n in the calling function when
function1 returns.

19 1.10 Passing Parameters


Chapter 1 Procedural Programming

def function1(n):
print(n)
n += 1
print(n)
return

n = 10;
function1(n)
print(n)

10
11
10

Java works in exactly the same manner. Numeric values that are passed to a method may be
modified in the method but the modifications are not visible in the calling method.

In Python, if a list is passed to a function and an element of the list is modified in the function,
the modification is reflected back (or is visible) in the calling function. In the following
example, the third element of list is modified in function2. As can be seen in the output,
this modification is visible in the calling function.

def function2(list):
print(list)
list[2] += 10
print(list)
return

list = [1, 2, 3, 4]
function2(list) # list is updated by function call
print(list)

[1, 2, 3, 4]
[1, 2, 13, 4]
[1, 2, 13, 4]

In Java, the elements of an array may be modified in a method call and the changes are visible
in the calling method.

In Python, if the number of elements in a list is modified in a function (for example, by


appending a new element to the end of the list), the change is visible in the calling function.
If this operation is performed in a Java method, the changes are not visible in the calling
method. The following Java program illustrates this issue.

public static void main(String[] parms)


{
int[] my_list;
my_list = new int[] {1, 2, 3, 4, 5};
print_list(my_list);
add_element(my_list, 200);
print_list(my_list);
}

20 1.10 Passing Parameters


Chapter 1 Procedural Programming

public static void add_element(int[] my_list, int element)


{
int[] new_list;

new_list = new int[my_list.length+1];


System.arraycopy(my_list, 0, new_list, 0, my_list.length);
new_list[new_list.length-1] = element;
my_list = new_list;
print_list(my_list);
}

public static void print_list(int[] my_list)


{
int i;

for (i=0; i<my_list.length; i++)


{
System.out.print(my_list[i] +" ");
}
System.out.println();
}

1 2 3 4 5
1 2 3 4 5 200
1 2 3 4 5

In Java, if the size of an array in the calling method must be modified, the called method must
return the modified array and the calling method must assign the modified array to the
original array.

public static void main(String[] parms)


{
int[] my_list;
my_list = new int[] {1, 2, 3, 4, 5};
print_list(my_list);

my_list = add_element(my_list, 200);


print_list(my_list);
}

public static int[] add_element(int[] my_list, int element)


{
int[] new_list;

new_list = new int[my_list.length+1];


System.arraycopy(my_list, 0, new_list, 0, my_list.length);
new_list[new_list.length-1] = element;
my_list = new_list;
print_list(my_list);
return my_list;
}

public static void print_list(int[] my_list)


{
int i;

for (i=0; i<my_list.length; i++)


{
System.out.print(my_list[i] +" ");
}
System.out.println();

21 1.10 Passing Parameters


Chapter 1 Procedural Programming

1 2 3 4 5
1 2 3 4 5 200
1 2 3 4 5 200

Python supports keyword parameters but since this type of parameter is not supported in Java,
they will not be examined in these notes.

1.11 Scope

The scope of variables in Python is straightforward – variables used in a function are local to
that function. When the function terminates, the variables are destroyed. If a variable in a
calling function is required in a called function, the variable should be passed to the called
function as a parameter. The value of the variable can then be accessed without any
problems. Depending on the type of the variable, it may also be possible to modify the
variable in the called function and have the modification visible in the calling function (see
the previous Section: Passing Parameters).

Python permits variables that are given values in the calling function to be accessed (but not
modified) in a called function (without passing the variables as parameters). We will not
examine this technique since it is not a standard programming construct.

In Java, variables are local to the method in which they are declared. Java does not permit
variables that are declared in one method to be accessed in a called method. So, if the value
of a variable in a calling method is required in a called method, the variable must be passed to
the called method.

1.12 Data Types

Java implements a collection of data types that are referred to as “primitive data types”. As
we shall see later in these notes, the primitive data types are stored in a different manner than
non-primitive data types (which are objects). Java’s primitive data types are: byte, short,
int, long, float, double, char, and boolean. Each variable declared in a Java program
is given a specific type. The types byte, short, int, and long are used to represent integer
values of varying sizes. The types float and double are used to represent floating-point
values of different sizes. The type boolean is used to store a Boolean value (true or false).
The type char is used to store a single character. A variable that is given one type can not be
assigned a value of another type (unless the two types are compatiable or some additional
work is performed).

Java’s primitive data types are shown below. Python does not have “primitive data types” but
it does have some core data types that are similar to Java’s primitive data types.

22 1.12 Data Types


Chapter 1 Procedural Programming

Python Java Meaning


int an integer of any size
byte -128 <= integer <= 127
short -32768 <= integer <= 32767
int -2147483648 <= integer <= 2147483647
long approximately 18-digit integer
float floating-point value with 7-digit accuracy
float double floating-point value with 15-digit accuracy
bool boolean Boolean (true/false)
char one character

Python uses dynamic typing for its variables. This means that Python variables are not
declared to have a specific type; instead, when a Python variable is assigned a value, the type
of the value is stored with the actual value. So it is perfectly valid in Python to assign an
integer value to a variable, use the variable in statements that make sense for integers, and
then at a later time, assign a boolean value to the variable. As long as the variable is then
used in a boolean context, the program will work correctly.

Python permits the programmer to determine the type of the value currently stored in a
variable using the type() function (remember that a variable does not have a type in Python).

i = 25;
j = 25.5
print(type(i))
print(type(j))

<class 'int'>
<class 'float'>

To make the type output slightly nicer, the following function get_type can be used. (The _ _
used in the function is 2 underscore characters typed side-by-side.)

def get_type (var):


return var.__class__.__name__

i = 25
j = 25.5
print(get_type(i))
print(get_type(j))

int
float

If it is necessary to convert a value from one type to another, Python provides a collection of
functions to perform the conversion. The int() function converts its parameter to an integer
value; the float() function converts its parameter to a floating-point value.

23 1.12 Data Types


Chapter 1 Procedural Programming

The following statements illustrate the conversion between the integer and floating-point data
types in Python.

i = 25;
print(i)
j = float(i)
j += 0.5
print(j)
i = int(j)
print(i)

25
25.5
25

In Java, data types are converted from one type to another using Java’s “cast” mechanism. A
cast involves placing the desired type in parentheses before an expression or variable. The
following Java statements have the same effect as in the Python statements above.

int i;
float j;

i = 25;
System.out.println(i);
j = (float) i;
j += 0.5;
System.out.println(j);
i = (int) j;
System.out.println(i);

25
25.5
25

1.13 Strings

Python supports the data type str which is used to store a collection of characters (or a
character string). The following statements illustrate the use of character strings in Python.

s = "abc"
print(s)
print(type(s))

abc
<class 'str'>

In Python, strings may be delimited either by single quote marks (') or by double quote
marks ("). As long as a string begins and ends with the same delimiter, it doesn’t matter
which is used.

The type conversion functions in Python can be used to convert between numeric values and
strings. For example, the statements illustrate the conversions between an integer and a

24 1.13 Strings
Chapter 1 Procedural Programming

string.

i = int("123")
s = str(i)

A subset of the contents of a string can be extracted using the square brackets notation (this is
referred to as “slicing” in Python). The following example illustrates this process.

s = "abc"
t = s[1:2]
print(t)

The notation [from, to] means that the characters beginning with character from and
continuing up to but not including character to are extracted. As with arrays, character
indexing is zero-based. If only the from value is included, only the one corresponding
character is extracted. If the notation [from:] is used, this means extract characters
beginning at the from character and continuing until the end of the string. (The slicing
notation supported in Python contains additional options that we will not examine.)

Strings in Python are compared using the relational operators ( <, >, ==, etc.) When strings
are compared for equality in Python, it is important that the == operator be used. As we shall
see, this is not the case in Java.

Python contains a rich collection of functions that manipulate character strings. For more
information, take a look at the web or a Python textbook.

Java supports the primitive data type char which can be used to store one character. If you
want to store a collection of characters (or a character string) in Java, you could use a char[]
(array) but a more convenient mechanism for manipulating character strings in Java is the
data type String (which is not a primitive data type).

String s;

s = "abc";
System.out.println(s);

abc

Java also permits the programmer to extract a subset of the characters in a character string.
However, in Java, a method named substring is used to perform the processing. The
parameters of substring are almost the same as Python’s slicing notation. The only
difference is that in Java, if only one value is supplied, all characters beginning at that
position and continuing until the end of the string are extracted.

25 1.13 Strings
Chapter 1 Procedural Programming

String s;
String t;

s = "abc";
System.out.println(s);
System.out.println(s.substring(1,2));
System.out.println(s.substring(1));

abc
b
bc

In Java, when two strings are compared for equality, a special method, the equals method,
must be used instead of the relational operator ==.

String s;
String t;

s = "abc";
t = "abc";
if (t.equals(s))
{
System.out.println("The strings are equal");
}
else
{
System.out.println("The strings are not equal");
}

1.14 Modules/Classes

The statements and functions defined inside a Python file (a .py file) are referred to as a
Python module. Python includes many modules that provide useful functionality (so that the
programmer does not have to write all of the functions him/herself). A simple example is the
math module that contains many mathematical functions. Before the functions in a module
may be used, the module must be made available (imported).

import math

i = math.sqrt(25.0)
print(i)

5.0

Java also contains a variety of predefined methods and its equivalent of modules. For
example, the mathematical functions are also collected together into a Math “class”. This
class can be imported using the statement shown at the beginning of the following program.
To make life a little easier for the programmer, Java automatically imports the Math class
(along with a variety of other classes) so the import statement below is not actually
necessary.

26 1.14 Modules/Classes
Chapter 1 Procedural Programming

import java.lang.Math;

public class TestModules


{
public static void main(String[] parms)
{
double d;

d = Math.sqrt(25.0);
System.out.println(d);
}
}

5.0

1.15 Determining Prime Numbers

The following program determines the prime numbers that are less than n. A number is prime
if it does not have any integer divisors greater than 1 other than itself.

def isPrime(value):
result = True
count = 2
while (count<value) and (result):
if (((value//count)*count)==value):
result = False;
count += 1
return result

def generatePrimes(n):
list = []
for i in range(2, n):
if isPrime(i):
list.append(i)
return list

list = generatePrimes(40)
print(list)

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]

The corresponding Java program is shown below.

public class Primes


{
public static void main(String[] parms)
{
int[] list;

list = generatePrimes(40);
print_list(list);
}

public static int[] generatePrimes(int max)


{
int[] list;
int[] new_list;
int numPrimes;
int count;

27 1.15 Determining Prime Numbers


Chapter 1 Procedural Programming

list = new int[max];


numPrimes = 0;
for (count=2; count<max; count++)
{
if (isPrime(count))
{
list[numPrimes] = count;
numPrimes++;
}
}

new_list = new int[numPrimes];


System.arraycopy(list, 0, new_list, 0, numPrimes);
return new_list;
}

public static boolean isPrime(int value)


{
int count;
int sqrt;
boolean result;

result = true;
for (count=2; (count<value) && result; count++)
{
if (((value/count)*count)==value)
{
result = false;
}
}
return result;
}

public static void print_list(int[] my_list)


{
int i;

for (i=0; i<my_list.length; i++)


{
System.out.print(my_list[i] +" ");
}
System.out.println();
}
}

2 3 5 7 11 13 17 19 23 29 31 37

As can be seen, the Java program is more “wordy” than the Python program although the Java
program does not require more thought than the Python program (with the possible exception
of shrinking the array of primes to the exact size needed).

The prime number generation programs are presented without any discussion of how such
programs would be developed. The process of developing programs is discussed in the next
Chapter.

28 1.15 Determining Prime Numbers


Chapter 1 Procedural Programming

1.16 Differences between Python 2 and Python 3

The print function has been added to Python 3; in Python 2, print was a statement that did
not require parentheses.

print 1, 2, 3 # Python 2
print(1, 2, 3) # Python 3

There have also been some changes to the way that integers are defined and manipulated.

long # Python 2: the long type was removed in Python 3


a / b # Python 2: integer result
a / b # Python 3: real (floating point) result
a // b # Python 3: integer result

1.17 Differences between Python and Java

Java uses brace/curly brackets to start and end blocks, while Python uses indentation.

Java employs static typing, while Python is dynamically typed.

Java methods are explicitly declared to be static.

Java uses array.length while Python uses len(list).

There are several types of integer values in Java (depending on the maximum integer to be
manipulated); Python only has one type which can manipulate integers of any size.

The representation of Python data values is different from the representation of Java values,
particularly Java primitive data types.

1.18 Summary

In this chapter, we examined the fundamental constructs used in procedural programming.


This chapter has only scratched the surface of the Python language – Python contains many
elegant constructs and also some very powerful libraries of predefined functions.

29 1.18 Summary
Chapter 1 Procedural Programming

30 1.18 Summary
Chapter 2 Growing Algorithms

2 GROWING ALGORITHMS

2.1 Introduction

In this chapter, we introduce the process of “growing” software, that is, instead of writing an
entire program at once, the program is grown incrementally from a small, working portion of
code, adding functionality in small pieces until the desired result is achieved. This process is
also known as iterative development.

2.2 Searching a List

Searching the elements in a list for a specific element is one of the standard computer science
algorithms and is used in a wide variety of programs. In the following example, an array of
integers is searched for a specific integer. We begin by defining a simple main method that
indicates the parts of the program that must be defined. We will use this program
organization throughout these notes – the main method will not perform any explicit
processing; instead, it will call appropriate methods that perform the processing.

public static void main(String[] parms)


{
int[] list;
int searchValue = 10;
int result;

list = createList();
result = searchList(list, searchValue);
System.out.println("Result is: " +result);
}

The main method shown above defines the structure of the program that is to be created. A
method “createList” is used to initialize the array of integers and a method “searchList” is
used to examine the elements in the array of integers, looking for an element that is identical
to the value in “searchValue”. By defining the main method in this manner, we have reduced
the problem to smaller components and we can now examine these components individually.

We need a method createList that stores a collection of integer values in an array and returns
the array to the main program. Following our rule of doing the simplest thing that could
work, the createList method is initially defined with a “hard coded” array of integers. (Hard
coding means that the values are specified in the program instead of being supplied by the
user or read from a file.)

public static int[] createList()


{
int[] list = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

return list;
}

31 2.2 Searching a List


Chapter 2 Growing Algorithms

Note that since createList returns an array of integers, its return type must be declared as int[]
instead of void. While this version of createList is quite limited, it does provide a working
version that can be improved upon later. A method that works correctly but is not complete is
often referred to as a “stub” method.

Now, we need a searchList method that accepts the array and a simple integer value and
determines whether or not the integer is an element of the array. The searchList method is
initially created to return 0, the position of the first element in the array.

public static int searchList(int[] list, int searchValue)


{
int result;

result = 0;
return result;
}

At this point, the program actually works correctly – it creates an array of integers and
identifies the position of the integer that is being searched for in the array. While the program
works only for the array that is hard coded in the createList method, the program does identify
all of the necessary components. By first breaking the program down into smaller pieces and
writing very simple instructions that work (in a very limited manner), we can now concentrate
on improving the individual methods one-at-a-time.

Of course, if you know exactly what is required in each method, it is not necessary to grow
the program so slowly; instead, you can take bigger steps towards the solution. However, the
advantage of developing a program slowly is that if something goes wrong between one
iteration and the next, it is easier to identify the cause of the problem.

The goal of approaching program development in this manner is to ensure that the overall
design of the program is correct before adding the details. The design of a program is similar
to the blueprint of a building that is to be constructed. However, there are significant
differences between developing software and building (or engineering) a physical structure –
these issues will be addressed from time to time in these notes.

Now we improve the methods so that each becomes more general.

We initially hard coded the array in the createList method.

32 2.2 Searching a List


Chapter 2 Growing Algorithms

public static int[] createList()


{
int[] list;

list = new int[] {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
return list;
}

An alternative is to generate random values using the Math.random method.

public static int[] createList()


{
int[] list;
int count;
int numElements = 10;

list = new int[numElements];


for (count=0; count<numElements; count++)
{
list[count] = (int) (Math.random()*101.0);
}
return list;
}

The array is constructed to contain 10 random integer values in the range 0 to 100. (Recall
that Math.random returns a double value (in the range: 0 ≤ value < 1) so the random value
must be cast to an int before it can be stored in the array.) This version is a definite
improvement over the previous version but we will see that other versions of createList can
also be defined.

2.2.1 Linear Search

In our initial version of searchList, we simply returned the position of the first element in the
array. This allowed us to get the program working but this method now must be improved so
that it works correctly with all data.

public static int searchList(int[] list, int searchValue)


{
int result;

result = 0;
return result;
}

33 2.2 Searching a List


Chapter 2 Growing Algorithms

A simple linear search of the elements in the array can be used to locate the value.

public static int searchList(int[] list, int searchValue)


{
int count;
int result;

result = -1;
for (count=0; count<list.length; count++)
{
if (list[count] == searchValue)
{
result = count;
}
}
return result;
}

The searchList method now returns the location of the desired element in the array if the
element is found or returns -1 if the element is not found. Returning a value of -1 to
indicate that a method was not able to accomplish its task is a common technique.

In the method above, the loop continues until the end of the array even if the desired element
is located. While we are not going to attempt to optimize each program as much as is
possible, this is an example that is particularly easy to improve by modifying only the loop
condition so that the loop terminates as soon as the desired element is located.

public static int searchList(int[] list, int searchValue)


{
int count;
int result;

result = -1;
for (count=0; result==-1 && count<list.length; count++)
{
if (list[count] == searchValue)
{
result = count;
}
}
return result;
}

Now the loop continues as long as the desired value has not been located and there are still
more elements to be examined in the array.

If data are to be provided by a user, the createList method becomes somewhat more complex.
JOptionPane can be used to obtain each value from the user, but we don’t know in advance
how many elements will be entered. The simplest solution is to ask the user to specify the
number of elements that will be entered. Then, the array can be created so that it is exactly
the right size.

34 2.2 Searching a List


Chapter 2 Growing Algorithms

public static int[] createList()


{
int[] list;
int numElements;
int count;
int value;
String string;

string = JOptionPane.showInputDialog("Please enter the number of values:");


numElements = Integer.parseInt(string);
list = new int[numElements];

for (count=0; count<list.length; count++)


{
string = JOptionPane.showInputDialog("Please enter a number:");
value = Integer.parseInt(string);
list[count] = value;
}
return list;
}

Note that the array was declared at the beginning of the method but the array was not
instantiated (with an = new() statement) until later in the method when the actual number
of elements to be stored in the array was known. This is a common Java programming
technique.

There are other ways in which an unknown number of elements can be stored in an array and
we will examine some of these techniques later in this chapter.

Recall that when Java’s Graphical User Interface (GUI) components are used, the program
must import the GUI component(s) from the Swing package.

import javax.swing.JOptionPane;

Similarly, to ensure that the program terminates correctly when GUI components are used, the
statement below must be included at the end of the main method.

System.exit(0);

Having to type values into the console each time that the program is tested is annoying after
the first couple of iterations. It is often easier to generate random data and then switch to
user-supplied data only when all parts of the program are working.

An alternative to using JOptionPane is to read the data from a (disk) file that is provided by
the user. We will examine how to read information from files in Chapter 4 – File Input and
Output.

35 2.2 Searching a List


Chapter 2 Growing Algorithms

2.2.2 Linear Search of an Ordered List

If an array is sorted into ascending order, then the contents of the array are arranged so that
the value of the first element is less than or equal to the value of the second element, the value
of the second element is less than or equal to the value of the third element, and so on. The
following diagram shows the contents of an array that consists of 5 elements, with each
element less than or equal to all following elements.

We will examine techniques that sort the contents of an array into a particular order in
Chapter 13 – Sorting but for now we will assume that the integers are provided in sorted
order.

If the elements in an array have been sorted into ascending order, there are several ways in
which the array can be searched that make the search process more efficient. Our original
linear search algorithm searches the array until either the desired element is found or until the
end of the array is found.

public static int searchList(int[] list, int searchValue)


{
int count;
int result;

result = -1;
for (count=0; result==-1 && count<list.length; count++)
{
if (list[count] == searchValue)
{
result = count;
}
}
return result;
}

This search process can be improved slightly by stopping the search as soon as the desired
element is found or when an element with a value that is larger than the desired element is
found. The search is still a linear search but it stops earlier if the desired element is not found.

36 2.2 Searching a List


Chapter 2 Growing Algorithms

public static int searchList(int[] list, int searchValue)


{
int count;
int result;

result = -1;
for (count=0; searchValue>=list[count] && result==-1
&& count<list.length; count++)
{
if (list[count] == searchValue)
{
result = count;
}
}
return result;
}

The example works correctly in most cases but has a problem (or bug) in one particular
situation: if the desired element is greater than the largest element in the array, a subscript-
out-of-bounds exception is generated. This problem can be eliminated by reordering the for
condition to ensure that position is still within the array before evaluating the condition
searchValue>=list[count].

It is important to understand why the program above does not work correctly in all situations.
When learning to program, it is much more important to write code that is correct than to be
concerned about efficiency.

The correct version of the program is shown below.

public static int searchList(int[] list, int searchValue)


{
int count;
int result;

result = -1;
for (count=0; result==-1 && count<list.length && searchValue>=list[count]; count++)
{
if (list[count] == searchValue)
{
result = count;
}
}
return result;
}

This example illustrates how easily a bug that occurs only occasionally can be introduced into
a program. Many programmers do not perform sufficient testing of their programs and
problems such as this one may slip through the development process and then cause the
system to crash when the system is put into production.

37 2.2 Searching a List


Other documents randomly have
different content
sail area, 125;
heavy armament, 126;
peculiar gun mountings, 126;
burnt by Federals, 127;
raised, altered and refitted by Confederates, 127;
railway-iron armoured casemate, 127;
her destructive trial trip, 128;
duel with Monitor and gunboats, 128, 131, 132, 133, 137;
scuttled by commander, 133
Military mast, 166
Millwall Ironworks, 122
Modern guns and ships in war, 236
Modern heavy artillery construction, 282
Monitor:
Ericsson’s tender, 128;
officials’ interference with plans, 129;
derision and abuse, 129;
change in naval construction inaugurated by, 129;
as ram, 129;
Admiral Porter’s advocacy, 130;
peculiar shape, 131;
narrow escape, 131;
why name chosen, 131;
armament and armour, 131;
duel with Merrimac, 128, 131-3;
steering gear and anchor
out of reach of hostile fire, 133;
lost in rough weather, 133
Monitor:
in Prussian-Danish War, 151;
coast defence, 182;
double turreted, 151, 153;
craze for, 144
Mortar boats, 114
Napier, Sir C., 90
Napoleon I., proposed rescue from St. Helena, 293
Naval artillery: Later developments, 273
Naval corruption, 221
Necessity of armour protection, 146
New Georgia canoes, 31
New Guinea Lakatoi, 29
New Zealand (Maori) war canoes, 25 et seq.
Noble, Sir A., 281
Nordenfeldt, Dr., 299
(see Submarines)
Number of rowers in banked ships, 9

Oblong iron forts on steam rafts, 199


Old ships re-armed, 243
“One-ass power,” 15
Oscillating paddles, 81
Outriggers, 24, 25

Paddles:
Boxes and wheels, objection to, 81, 97;
frigates with, 96, 97, 106;
gunboats in action, 113;
trials against screws, 98;
war steamers with, 95;
war junks with, 35;
sponsons extended to carry cannon, 97
Palliser, Major, 274
Palmer’s, 306
Parodus, 6
Passengers called on to fight, 77
Penn, John, and Sons, 103, 178
Pett, Phineas, 54, 57
Phœnicians’ connection with Britain, 15;
war galleys, 6
Pickled human heads, 49
Pioneer of modern battleship, 244
Portholes, invention of, 42, 49;
plated ports, 120;
reduced size, 120;
diagonal plates, 125
Privateers, 140
Projectiles:
Armour-piercing, 265, 273;
Bessemer steel, 267;
Dutch cheeses, 76;
flat-headed, 268;
human heads, 73;
anti-war shell, 268;
Palliser, 164, 269, 274;
Whitworth, pointed and cylindrical, 274;
studded, 277;
steel, 268;
stone cannon balls, 40;
solid and hollow shot, 126;
resistance of armour to, 322-3;
lead-coated, 274;
velocity, 272;
weight, 274;
elongated shell, 85, 86
Propellers:
Adjustable, 123;
advantages, 148;
Dudgeon’s, 148;
Ericsson’s screw, 92;
Griffiths’, 125, 147;
Smith’s, 92;
Mangin, 122;
twin screws, 138, 148, 173;
adoption by Admiralty, 150;
first British twin screw ironclad, 150;
twin screw in United States of America, 125
Proposal to subject Cerberus to gunfire with crew on board, 184,
185
Protected ships (Japanese), 37

Queen Elizabeth and Navy, 50, 51;


second embassy to Turkey, 53

Rafts, 21
Railway locomotives as marine engines, 106
Raleigh, Sir Walter, as critic, 55
Ram, 5, 6, 7, 10, 119, 172, 190, 205
Ramberges, 44
Range-finding tower, 259
Rapid building, 192, 247
Ratings, 57, 59
Recessed ports, 150, 172
Reed, Mr., afterwards Sir E., 195
Remarkable French ironclads, 190-1
Rennie, J. and G., 94
Report on Royal Navy (1552), 50
Resistance of armour to projectiles, 322-3
Robinson and Russell, 98, 113
Russell, Scott, 90, 120
Russo-Japanese War:
Russian fleet’s departure for the Far East, 236;
British trawlers or Japanese torpedo boats, 236;
Russian fleet’s slow speed, 236;
going to destruction, 236;
Japan’s ships’ superior speed, 237;
Russians reach Japanese waters, 236;
sudden Japanese attack, 237;
Russian ships overloaded and filthy, 237;
Japanese gunnery superior, 237;
Russians defeated in two hours, 237;
Admiral Togo’s objects, incidents of the battle, 239-40
Russo-Turkish War:
Value of torpedo to Russians, 200;
powerful Turkish fleet, 212;
Turkish ships torpedoed, 212-3;
naval encounter, 214
Ruthven’s hydraulic propulsion, 186;
experiments, 187-8

Sailers converted into steamships, 105


Sailing warships with attendant steamers, 95, 107
Sakers, 74
Samoan war canoe, 26
Samuda, 194, 233
“Sappy timbers and rotten planking,” 241
Scouts, combination of gunboat, cruiser and destroyer, 311;
English and American, 311
Sea-fights of the Crusades, 14
Sea-going turret ship, 193
Secondary armaments, 251, 252, 303
Semmes, Capt. R., 140
Seppings, Sir R., 65
Serpentines, 45
Seventeenth century cannon, 74
Screws (see Propellers)
Shark’s-mouth rudders, 149
Shields of basket work, 6, 15
Ships Mentioned:
Aaron Manby, 90
Abyssinia, 183
Achilles, 172
Acorn, 310
Actinaut, 288
Active (1822), 89
Admiral Popoff, 180
Adventure, 60
Adventure (scout), 311
Affondatore, 153
Agamemnon (1853), 105
Agamemnon (1906), 252, 317
Agincourt (1865), 122, 167
Alabama, 141
Albatross, 185
Albatross (1899), 306
Albemarle (Confederate), 134, 286
Alecto, 98
Alexander III. (Russian), 238
Alexandra, 174, 216
Almirante Cochrane, 203, 296
Almirante Condell, 209, 210
Almirante Lynch, 209, 210
Amethyst, 201
Amphion (1895), 253
Antelope (Queen Elizabeth), 52
Archimedes, 92
Arethusa (1895), 253
Ark Royal, 52
Arrogant, 103
Arminius, 194
Ascension (Queen Elizabeth), 53
Assar-i-Chevket, 214
Assar-i-Tewfik, 212
Ataka Maru, 37
Atlanta (Confederate), 133
Atlanta (United States), 222
Audacious, 173
Avni-Illah, 212
Azazieh, 212
Bacchante, 256
Baltimore (United States), 224, 225, 227
Bangor (United States), 123
Barfleur, 260
Basilisk, 99
Battle Animal, 5
Beacon, 216
Beagle, 310
Bellerophon (1865), 148, 163, 172
Bellerophon (1907), 317
Belier, 163
Beloved of Amon, 5
Benbow, 245
Berenguela (Spanish, 1865), 198
Birkenhead, 99, 100
Bittern, 216
Black Eagle, 102
Black Galley, 51
Black Prince (1860), 129, 158
Black Prince (1904), 261
Blanco Encalada, 203, 296 et seq.
Bolivar (Venezuelan), 173
Bombe (1885), 305
Bonaventure, 52
Borodino (Russian), 238, 239
Boston (United States), 222, 227
Boxer, 306
Brilliant (36-gun frigate), 60
Brooklyn (United States, 1862), 140
Brooklyn (United States, 1895), 233
Buenos Ayres (Argentine), 263
Cabral (Brazil), 199
Caledonia (1794), 64
Caledonian, 172
Calliope, 257
Camperdown, 245
Canopus, 250, 260
Captain, 160, 161, 183
Caracon, 46
Castilla (Spanish), 227
Centurion (1897), 248
Cerberus, 183, 184, 185, 243
Charleston (United States), 223
Chesapeake (United States), 60, 61
Chicago (United States, 1883), 222
Christopher Spayne, 43
Collingwood, 317
Colombo (Brazil), 199
Colossus (1882), 244
Colossus (1911), 318, 320
Columbia (United States), 225
Comet (1821), 89
Commerce de Marseilles (French), 63
Comus, 257
Concord (Spanish), 228
Condor, 216
Congreve (French), 109
Conqueror (1882), 244
Conqueror (1911), 262, 318, 319
Constant Warwick, 57
Constellation (United States), 60
Constitution (United States), 60, 61
Courageux, 58
Covadonga (Chilian), 197, 205, 206
Cressy, 261
Cristobal Colon (Spanish), 232
Cushing (United States), 224
Cygnet, 216
Dandolo (Italian), 152, 177
Danton (French), 325
Dantzig (Prussian), 98
Daring, 306
Dartford, 261
David (Confederate), 136, 294
Decoy, 216
Delaware (United States), 325
Demologos, 82
Desperate, 306
De Tygre (Dutch), 193
Devastation (1869), 161 et seq., 176, 243
Dévastation (French, 1854), 109
Devonshire, 260
Diamond (1874), 256
Dictator, 138
Dolphin (United States), 222
Don Antonio de Ulloa (Spain), 229
Doncaster, 81
Don Juan (Austrian), 153
Dover, 93
Drache (Austrian), 153
Drake (1902), 260
Dreadnought (Caledonia), 64
Dreadnought (Queen Elizabeth), 52
Dreadnought (turret), 244, 240, 171, 176
Dreadnought (1906), 240, 313-321
Druid, 185
Duilio (Italian), 152, 177
Duncan, 250, 261
Dunderberg, 189
Duke of Wellington, 105
Dupuy de Lôme, 259
Dwarf, 94
“E,” 304
Edinburgh, 244
Edward, 47
Elburkah, 90
Elizabeth Jones, 52
Encounter, 103
Esmeralda (1865), 197
Esmeralda (1883), 258
Erebus (1854), 112
Ernest Renan, 263
Essex, 60
Faid Gihaad, 97
Far East, 150
Ferdinand Maximilian, 153, 155
Fingal, 133
Flora, 148
Foo-So (Japanese), 233
Formidabile (Italian), 153
Formidable, 250
Foudroyant (French), 109
Fulton the First, 82
Furor (Spanish), 232
Fury, 171
Gabriel Royal, 49
Garry Owen, 91
Gem of the Ocean, 225
George, 51
Glatton (1854), 111
Glatton (1869), 161, 182
Gibraltar (ex Sumter), 141
Gloire (French), 117-20
Glorious in Memphis, 5
Gorgon, 95
Goubet, 300
Grappler, 76
Grace de Dieu, 44
Great Britain, 93
Great Dragon, 17
Great Eastern, 93
Great Harry, 44, 45, 46, 47, 50
Greenock, 101
Guadeloupe, 91
Guerriere, 61
Gymnote, 300
Habsburg, 153
Handig Vlug, 241
Hardy, 274
Hartford (United States), 136
Hatteras, 141
Hebe, 149
Hebe (French frigate), 96
Hecate, 90
Hecla, 90
Hector (1860), 147, 172
Heiligerlee (Dutch), 192
Helicon, 172
Henry, 57
Henry Grace de Dieu, 44
Hercules (1866), 158, 173, 195, 243
Hercules (1911), 318
Hibernia (1790), 62
Holigost, 43
Holland, 295 et seq.
Holy Ghost, 44
Hood (1897), 247, 248, 321
Hornet, 306
Housatonic, 294
Huascar, 200 et seq.
Imperieuse (1881), 258
Inconstant (1869), 181, 257
Indiana, 232
Independencia (Peru), 202-206
Infanta Maria Teresa, 230, 232
Inflexible (1876), 175-8, 216, 241
Inflexible (1907), 261
Invincible, 58
Invincible (1876), 216
Invincible (1907), 253
Iowa, 233
Iris (1878), 253
Iron Duke, 173
Ironsides, 136
Janus, 306
Jesus, 43
Kaifu, 310
Kaiser (Austrian), 153
Kaiser Maximilian, 153
Katahdin, 222
Katherine, 57
Katherine Forteless, 50
Kearsarge, 141
Kearsarge (second), 225
Kentucky, 225
Key-ing, 35
King Edward VII., 251
King George (Greek), 194, 212
Kniaz Suvaroff, 238, 239
Krokodil, 192
Kron Prim, 194
Lady Nancy, 114
Lave (French), 109
Leander, 257
Leicester (galleon), 53
Leviathan, 58
Lightning (1823), 89
Lightning (1876), 302
Lightning (1894), 306
Lion (Queen Elizabeth), 52
Lion (15th century), 49
Liverpool, 261
Long Serpent, 18
Lord Clyde, 147
Lord Nelson, 314, 317
Lutfi-Djelil, 213
Magdala, 183
Magenta (French), 191
Magnificent, 248-50, 261
Mahmoudieh, 212
Maine (1886), 224, 226, 255
Majestic, 248-50, 262, 314, 322
Maori, 310
Marie de la Cordeliere, 45
Mary, 173
Mary Florence, 207, 208
Mary Rose, 49, 50, 52
Mastiff, 274
Megæra, 99
Merchant Royal, 53
Mercury (1878), 253
Mermaid (1842), 94
Merrimac, 124 et seq.
Messoudiye, 175, 212
Miantonomoh, 189
Minos Geraes, 324
Minin, 179
Minneapolis, 225
Minotaur (1865), 122, 147, 148, 158
Minotaur (1906), 261
Mohawk, 308
Moltke, 326
Monarch (1868), 159-61, 183, 216, 244
Monarch (1911), 318-20
Monitor, 92 (see Index)
Monkey, 89
Mouette (French), 113
Mrs. Grand, 50
Mute, 292
Nahant, 133
Naugatuck, 137
Nautilus, 291
Nemesis, 91
Neptune, 103
Neptune (1911), 318-20
Niger, 99
Nile, 244, 246, 321
Nix (Prussian), 113
Nonpareil, 52
Northumberland (1865), 122, 147, 148, 243
Novelty, 92
Novgorod, 180
Numancia (1864), 192, 197, 230
Ocean, 172
O’Higgins, 208
Old Ironsides, 61, 136
Olympia, 227
Onondaga, 190
Oquendo, 230
Oregon, 226, 233
Orel, 238, 239
Orion, 317, 318, 321
Orkanieh, 212
Osliabya, 239
Osmanieh, 212
Pallas (armour plate), 172
Pallas (36-gun frigate), 60
Pelayo, 230
Penelope (1843), 96
Penelope (1867), 150, 216
Peter the Great, 179
Phæton (1897), 254
Phœnix (British), 95
Phœnix (Stevens’), 82
Plongeur, 294
Pluton, 232
Powerful, 259
President (United States), 60
Prince (Prince Royal), 54
Princess Royal, 262
Princeton, 92
Prinz Eugen, 153
Quail, 306
Queen Mary, 262
Rainbow, 52
Raleigh, 227
Ramillies, 292
Rattler, 98
Rattlesnake, 305
Recruit, 113
Re d’Italia, 152
Re de Portogallo, 152
Regent, 45, 49
Reina Cristina, 227
Renown (1897), 248, 260
Research, 172
Resurgam, 295
Retribution, 106
Rhadamanthus, 90
Rio de Janeiro, 262
Rising Star, 88
Roccafortis, 20
Rochambeau, 189
Rolf Kraake, 151
Rossia (1896), 259
Royal Louis, 58
Royal Sovereign (1783), 63
Royal Sovereign (1861), 146, 243
Royal Sovereign (1897), 247, 248, 321
Royal William, 94
Rupert, 244
Rurik (1894), 259
Rurik (1906), 259
Salamander (Austrian), 153
Salamander (Prussian), 113
Salamander (1832), 89
Salem, 311
Sans Pareil, 245
Sapphire (1874), 256
Scorpion, 172
Scourge (United States), 124
Sea Devil (Russian), 294
Seraing, 186
Shah, 201
Shannon, 61
Shannon (1853), 105
Shenandoah (Confederate), 140, 143
Ship of Pharaoh, 5
Simoom, 99
Skeered-o’-Nothing (United States), 324
Southfield, 164
Sovereign, 45, 49
Sovereign of the Seas, 55
Speedwell, 51
Star, 306
Stromboli, 95, 114
St. Vincent (1909), 317
Submarine A1, 296
Success, 76
Sultan, 103, 167, 173
Sumter (Confederate), 140
Superb (1875), 175, 212, 216, 243
Superb (1908), 317
Superbe (French), 58
Swift, 309
Swiftsure (Queen Elizabeth), 52
Tartar, 309
Taureau, 163, 190
Temeraire (1876), 174, 216, 244
Temeraire (1907), 317
Tennessee (Confederate), 134
Terribile (Italian), 153
Terrible (steam frigate), 97
Terrible (1895), 259
Terror (1854), 110
Texas, 224, 225, 233
The Pitt, 76
Thetis, 113
Thunderbolt, 233
Thunderer (1869), 162, 171, 176
Thunderer (1911), 318, 319
Tonnante, 109
Trafalgar (1886), 246, 321
Transporter, 298
Trident, 99
Trinity, 44
Trinity Royal, 43
Triumph (1578), 51, 52
Triumph (1903), 251
Trusty (1854), 111
Tryeright, 51
Tsushima, 263
Turbinia, 306
United States, 60
Valorous, 97
Vanguard (1512), 52
Vanguard (1871), 173
Vanguard (1909), 317
Vesta, 214
Vesuvius, 95
Vesuvius (United States), 224
Victoria (Peruvian), 198
Victoria (Spanish), 230
Victoria (1859), 116
Victoria (1887), 245, 246
Victory, 52
Ville de Madrid, 198
Ville de Paris, 62
Viper, 188
Virginia, 124
Vixen, 188
Vizcaya, 230
Vladimir, 106
Von der Tann, 253
Vulcan, 303
Wampanoag, 256
Warrior, 118, 147, 243
Warspite (1881), 258
Waterwitch, 186
Weehawken, 134
Weser, 113
Whang-Ho, 35
Wyvern, 172
Yarra, 310
Yorktown, 223
Ysabel Segunda, 96
Shortland Island Canoes, 31
Simms, Lieut.-Commander W. S., U.S.N., 237, 312, 314
Slaves as rowers, 11
Sloops, 59
Solomon Island Canoes, 30, 31
Ship of 1486-50, 41
Ship construction:
Longitudinal, 120;
transverse, 120;
longitudinal watertight bulkheads, 171;
brass stern and rudder post, 182;
bracketed frames, 163;
sunk forecastle, 163
(see Freeboard)
“Ship of the Future,” 193
Ships with banks of oars, 7
Siamese native warships, 38
Soft-ended barbette ships, 244
Spanish-American War, 227 et seq.:
American Pacific fleet, 227;
Spanish naval force at Manila, 227;
American and Spanish fleets compared, 228;
Battle of Manila Bay, 228-9;
destruction of Spanish fleet, 229;
American Atlantic fleet, 230;
Spanish fleet, 230;
Admiral Cervera’s complaints, 230;
Spanish dash from Santiago; destruction of Spanish fleet, 230-
3;
Admiral Sampson’s 4th July present to the nation, 231
Spanish Armada, 51
Spur gearing, 98, 101, 103
Sponsons as gun platforms, 97
Speed, 202, 236, 237, 238, 239, 253, 302;
rapid firing guns and increased speed, 304;
of destroyers, 306;
with turbines, 308;
objections to high speed, 308;
Invincible and Von der Tann, 254;
former’s speed, how obtained, 255;
speed retarded by marine growths, 257;
importance of, in armoured cruisers, 260;
turbine engines, 262;
length, beam, and speed, 263, 264
Stanhope, Lord, 87
Steam rotated circular fort, 86
Steel:
adopted by United States of America, 222;
protective deck, 146;
supplanting iron, 179, 181;
gradual adoption in warships, 241;
early steel warship, 241;
advantages over iron, 243;
heavy armoured steel ship, 244;
first battleships for British Navy, 244;
single-turreted battleships, 246;
armour 20 inches thick, 246;
Harveyised steel armour introduced in British Navy, 248;
Renown 10-inch armour stronger than Royal Sovereign’s 18-
inch armour, 248;
hulls, wood sheathed, 259;
nickel steel armoured deck, 261;
chrome, 276
Steering gear:
protection, 133, 159;
lack of protection, 153;
first warship with steam steering gear, 123
Stevens’ floating battery, 83, 84;
ironclad ram, 138
Stitched canoes, 30;
planks, 21, 28
Stockton, Commodore R. F., 84, 128
Superiority of Dreadnoughts over pre-Dreadnoughts, 262
Swivel guns on paddle steamers, 126
Symonds, Captain, 173
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookultra.com

You might also like