Guide to Java: A Concise Introduction to Programming (2nd Edition) James T. Streib 2024 Scribd Download
Guide to Java: A Concise Introduction to Programming (2nd Edition) James T. Streib 2024 Scribd Download
com
https://ebookmeta.com/product/guide-to-java-a-concise-
introduction-to-programming-2nd-edition-james-t-streib/
OR CLICK HERE
DOWLOAD NOW
https://ebookmeta.com/product/concise-guide-to-databases-a-practical-
introduction-konstantinos-domdouzis/
ebookmeta.com
https://ebookmeta.com/product/a-concise-introduction-to-robot-
programming-with-ros2-1st-edition-francisco-martin-rico/
ebookmeta.com
https://ebookmeta.com/product/introduction-to-programming-with-java-a-
problem-solving-approach-3rd-edition-john-dean/
ebookmeta.com
https://ebookmeta.com/product/scarhaven-keep-1st-edition-j-s-fletcher/
ebookmeta.com
https://ebookmeta.com/product/how-america-got-its-guns-a-history-of-
the-gun-violence-crisis-1st-edition-william-briggs/
ebookmeta.com
https://ebookmeta.com/product/conspiracy-giordano-bruno-5-1st-edition-
s-j-parris/
ebookmeta.com
https://ebookmeta.com/product/mental-health-disorders-sourcebook-
health-reference-8th-edition-kevin-hayes/
ebookmeta.com
https://ebookmeta.com/product/literature-language-and-computing-
polina-eismont-maria-khokhlova-mikhail-koryshev-elena-riekhakaynen/
ebookmeta.com
Undergraduate Topics in Computer Science
Guide to Java
A Concise Introduction
to Programming
Second Edition
Undergraduate Topics in Computer Science
Series Editor
Ian Mackie, University of Sussex, Brighton, UK
Advisory Editors
Samson Abramsky , Department of Computer Science, University of Oxford,
Oxford, UK
Chris Hankin , Department of Computing, Imperial College London, London, UK
Mike Hinchey , Lero – The Irish Software Research Centre, University of
Limerick, Limerick, Ireland
Dexter C. Kozen, Department of Computer Science, Cornell University, Ithaca,
NY, USA
Andrew Pitts , Department of Computer Science and Technology, University of
Cambridge, Cambridge, UK
Hanne Riis Nielson , Department of Applied Mathematics and Computer Science,
Technical University of Denmark, Kongens Lyngby, Denmark
Steven S. Skiena, Department of Computer Science, Stony Brook University, Stony
Brook, NY, USA
Iain Stewart , Department of Computer Science, Durham University, Durham,
UK
Joseph Migga Kizza, College of Engineering and Computer Science,
The University of Tennessee-Chattanooga, Chattanooga, TN, USA
‘Undergraduate Topics in Computer Science’ (UTiCS) delivers high-quality
instructional content for undergraduates studying in all areas of computing and
information science. From core foundational and theoretical material to final-year
topics and applications, UTiCS books take a fresh, concise, and modern approach
and are ideal for self-study or for a one- or two-semester course. The texts are all
authored by established experts in their fields, reviewed by an international advisory
board, and contain numerous examples and problems, many of which include fully
worked solutions.
The UTiCS concept relies on high-quality, concise books in softback format, and
generally a maximum of 275–300 pages. For undergraduate textbooks that are
likely to be longer, more expository, Springer continues to offer the highly regarded
Texts in Computer Science series, to which we refer potential authors.
James T. Streib Takako Soma
•
Guide to Java
A Concise Introduction to Programming
Second Edition
123
James T. Streib Takako Soma
Program in Computer Science Program in Computer Science
Illinois College Illinois College
Jacksonville, IL, USA Jacksonville, IL, USA
This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
Preface
Purpose
The purpose of this text is to help the reader learn very quickly how to program using the
Java programming language. This is accomplished by concentrating on the fundamentals,
providing plenty of illustrations and examples, and using visual contour diagrams to
illustrate the object-oriented semantics of the language.
Need
This text attempts to fill the gap between the above two types of books. First, it provides
plenty of examples and concentrates primarily on the fundamentals of the Java
programming language so that the reader can stay focused on the key concepts. Second, by
concentrating on the fundamentals, it allows the text to be more concise and yet still
accessible to readers who have no prior programming experience. The result is that the
reader can learn the Java programming language very quickly and also have a good
foundation to learn more complex topics later.
v
vi Preface
The second edition retains all the features of the first edition. In addition to fixing any
known errors, any areas that could be clarified have been reworded. Features new to the
second edition, include the following:
Typically, there are three ways objects can be introduced to the beginning programmer:
Objects first.
Objects last.
Objects interleaved.
This text takes the latter approach where objects are discussed in Chapters 2, 5, and 9.
However, recognizing that some readers and instructors might want to use one of the first
two approaches, this text can be read using alternative orders. For example, should an
viii Preface
objects first approach want to be taken, after reading Chapter 1, Chapters 2 and 5 can be
read next, followed by Chapters 3 and 4. Should an object later approach want to be used,
Chapters 3 and 4 can be read prior to Chapters 2 and 5.
To help facilitate these alternative approaches, starting with Chapter 3, the Complete
Program sections at the end of each chapter have examples with and without using objects.
Note that Chapter 9 requires an understanding of arrays, which is covered in Chapter 7,
and it can be read after completing that chapter.
Scope
As mentioned previously, this text concentrates on the fundamentals of the Java
programming language such as input/output, object-oriented programming, arithmetic and
logic instructions, control structures, strings, arrays including elementary sorting and
searching, recursion, files, bit-wise logic, and parallel processing programming. As a result,
it might not cover all the details that are found in some other texts, and if necessary, these
topics can be supplemented by the instructor or reader, or covered in a subsequent text
and/or second semester course.
Audience
This text is intended primarily for readers who have not had any previous programming
experience; however, this does not preclude its use by others who have programmed
previously. It can serve as a text in an introductory programming course, as an introduction
to a second language in a practicum course, as a supplement in a course on the concepts of
programming languages, or as a self-study guide in either academe or industry. Although
no prior programming is assumed, it is recommended that readers have the equivalent of
an introduction to functions course that includes trigonometry which will help with
problem solving and understanding the examples presented in the text.
Acknowledgments
In addition to the reviewers of the first edition, the authors would like to thank Mark E.
Bollman of Albion College and James W. Chaffee of the University of Iowa for their
continued work on this edition. Also, the authors would like to acknowledge the students
of Illinois College who have read and used various sections of the first edition in the
classroom. On a personal note, James Streib would like to thank his wife Kimberly A.
Streib and son Daniel M. Streib. Takako Soma would like to thank her family and friends,
near and far.
Note that Java is a registered trademark of Oracle and/or its affiliates and that Windows
is a registered trademark of Microsoft Corporation in the United States and/or other
countries.
Feedback
The possibility of errors exist in any text, therefore any corrections, comments, or
suggestions are welcome and can be sent to the authors via the e-mail addresses below. In
addition to copies of the complete programs presented in the text, any significant
corrections can be found at the website below.
Preface ix
Website: http://www.jtstreib.com/GuideJavaProgramming.html
xi
xii Contents
1.8.5 Summing 41
1.8.6 Arithmetic Functions 42
1.9 Comments 43
1.10 Complete Program: Implementing a Simple Program 44
1.11 Summary 46
1.12 Exercises (Items Marked with an * Have Solutions in Appendix E) 47
2 Objects: An Introduction 51
2.1 Introduction 51
2.2 Classes and Objects 51
2.3 Public and Private Data Members 52
2.4 Value-Returning Methods 52
2.5 void Methods and Parameters 53
2.6 Creating Objects and Invoking Methods 54
2.7 Contour Diagrams 56
2.8 Constructors 62
2.9 Multiple Objects and Classes 66
2.10 Unified Modeling Language (UML) Class Diagrams 73
2.11 Complete Program: Implementing a Simple Class and
Client Program 75
2.12 Summary 77
2.13 Exercises (Items Marked with an * Have Solutions in
Appendix E) 78
3 Selection Structures 83
3.1 Introduction 83
3.2 If-Then Structure 83
3.3 If-Then-Else Structure 88
3.4 Nested If Structures 91
3.4.1 If-Then-Else-If Structure 91
3.4.2 If-Then-If Structure 94
3.4.3 Dangling Else Problem 96
3.5 Logical Operators 99
3.6 Case Structure 105
3.7 Complete Programs: Implementing Selection Structures 111
3.7.1 Simple Program 111
3.7.2 Program with Objects 114
3.8 Summary 116
3.9 Exercises (Items Marked with an * Have Solutions in Appendix E) 116
6 Strings 203
6.1 Introduction 203
6.2 String Class 203
6.3 String Concatenation 204
6.4 Methods in String Class 206
6.4.1 The length Method 206
6.4.2 The indexOf Method 206
6.4.3 The substring Method 207
6.4.4 Comparison of Two String Objects 209
6.4.5 The equalsIgnoreCase Method 211
6.4.6 The charAt Method 212
6.5 The toString Method 213
6.6 Complete Program: Implementing String Objects 215
6.7 Summary 219
6.8 Exercises (Items Marked with an * Have Solutions in Appendix E) 219
7 Arrays 223
7.1 Introduction 223
7.2 Array Declaration 223
xiv Contents
8 Recursion 265
8.1 Introduction 265
8.2 The Power Function 265
8.3 Stack Frames 274
8.4 Fibonacci Numbers 277
8.5 Complete Program: Implementing Recursion 289
8.6 Summary 291
8.7 Exercises (Items Marked with an * Have Solutions in Appendix E) 291
Index
0
Introduction to Computing Concepts
James T. Streiba* and Takako Somaa
a
Computer Science Program, Illinois College, Jacksonville, IL, USA
Abstract
In addition to an introduction to hardware and software concepts, including the concept of compiling,
interpreting, and executing a program, there is an introduction to computational thinking, software design,
and computer ethics.
Keywords
Hardware, Software, Computational Thinking, Software Design, Computer Ethics.
0.1 Introduction
Although this chapter is labeled as Chapter 0, that does not diminish its importance. The
reason for such a numbering is to allow readers with a previous introduction to computing
concepts and programming to proceed onto Chapter 1. However, for readers with no prior
introduction or for those who would like a refresher, this chapter provides an important
overview of hardware, software, computational thinking, software design, and computer
ethics.
0.2.1 Hardware
As many readers may already know from using application software such as a word
processor, a computer system is composed of two major parts: hardware and software.
Since this book is primarily about writing software, this section on hardware is
understandably brief. The hardware is the physical computer that includes five basic
components: the central processing unit (CPU), the random-access memory (RAM) or just
memory for short, input (typically a keyboard), output (typically a monitor), and storage
(often a disk) as shown in Fig. 0.1.
CPU
Input Output
RAM
Storage
0.2.2 Software
Compiler Assembler
The solution to making programming easier and allow programs to be used on different
machines is through the use of high-level languages which are more English-like and math-
like. One of the first high-level programming languages was FORTRAN (FORmula
TRANslation), which was developed in the early 1950s to help solve mathematical
problems. There have been a number of high-level languages developed since that time to
meet the needs of many different users. Some of these include COBOL (COmmon
Business Oriented Language) developed in the 1950s for the business world, BASIC
(Beginners All-purpose Symbolic Instruction Code) developed in the 1960s for beginning
programmers, Pascal in the 1970s previously used for teaching computer science students,
C in the 1970s for systems programming, and C++ in the 1980s for Object-Oriented
Programming (OOP).
Java is also an OOP language that was developed at Sun MicroSystems (which is now
a subsidiary of Oracle Corporation) and was released in 1995. OOP is a programming
methodology that makes it more convenient to reuse software and will be discussed further
in Chaps. 2 , 5 , and 9 .
If high-level languages are easier to learn and use, how can they be implemented on a
computer that can only understand machine language? Similar to assembly language
needing an assembler, the program needed to translate a high-level language to a low-level
language is a compiler or an interpreter. Although there is a one-to-one correspondence
between assembly language and machine language, there is a one-to-many correspondence
4 0 Introduction to Computing Concepts
between a high-level language and a low-level language. This means that for one high-
level language instruction, there can be many low-level assembly or machine language
instructions. Even though different CPUs need different compilers or interpreters to
convert a particular high-level language into the appropriate machine language, compilers
and interpreters allow the same high-level language to be used on different CPUs.
The difference between a compiler and an interpreter is that a compiler will translate
the high-level language instructions for the entire program to the corresponding machine
language for subsequent execution, whereas an interpreter will translate and then execute
each instruction one at a time. Further, a compiler might translate directly to machine
language, or it might translate the high-level language to assembly language, and then let
an assembler convert the assembly language program to machine language as shown in
Fig. 0.2. Once the machine language is created, it is subsequently loaded into the
FRPSXWHU¶V5$0DQGH[HFXWHGE\WKH&38
As mentioned above, an interpreter works slightly differently than a compiler. Instead
of converting an entire high-level program into machine language all at once and then
executing the machine language, an interpreter converts one line of the high-level program
to machine language and then immediately executes the machine language instructions
before proceeding on with the converting and executing of the next high-level instruction
(see Fig. 0.3). The result is that compiler-generated code executes faster than interpreted
code because the program does not need to be converted each time it is executed. However,
interpreters might be more convenient in an educational or development environment
because of the many modifications that are made to a program which require a program to
be converted each time a change is made.
Compile Interpreter
2. Then execute all the machine 2. And then execute only the corresponding
instructions machine instructions
Java is somewhat unique in that it uses both a compiler and an interpreter to convert the
high-level instructions to machine language. A compiler is used to convert the Java
instructions into an intermediate-level language known as bytecode, and then the bytecode
is converted into machine language using an interpreter.
Since the intent of Java was for portability on the World Wide Web, the advantage of
using both a compiler and an interpreter is that most of the translation process can be done
by the compiler, and when bytecode is sent to different types of machines, it can be
translated by an interpreter into the machine language of the particular type of machine that
the code needs to be run on (see Fig. 0.4).
Note that just as there can be a one-to-many relationship between high-level and low-
level instructions, there can be a one-to-many relationship between Java and bytecode.
However, unlike the one-to-one relationship between assembly language and machine
language, there can also be a one-to-many relationship between bytecode and machine
language, depending on the machine for which the bytecode is being interpreted.
Interpreter
Compiler
solution can be used to solve new problems. Abstraction is the filtering out of
unnecessary information. In other words, taking a step back from the specific details and
focusing on the big picture allows one to create a more generic solution. Algorithms are
step-by-step instructions to solve a problem. It is important to create a plan, an algorithm,
for the solution when solving a problem. Logical thinking is deductive inference of new
information on existing information. Computational thinking is a problem-solving
process that involves a number of core principles from computer science as mentioned
above.
But how can these principles be used in non-programming context, for example, solving
the Tower of Hanoi game? The Tower of Hanoi game consists of three pegs, and initially
one of the non-centered pegs contains several rings stacked in order of descending
diameter from bottom to top. The goal is to move the stack of rings to another non-
centered peg as shown in the Fig. 0.5.
During the process, only one ring can be moved at a time which means only a top ring is
removed among the towers at any given time. Consequently, several rings cannot be
moved at once. Another rule is that a bigger ring cannot be placed on the top of a smaller
one. At the start of the game, the only ring that can be moved is the smallest ring and it
may be placed on one of the two pegs. Next, there are two rings that can be moved. There
is no good reason to move the smallest one back to the original stack or to another peg.
So, the next move should be moving the second smallest ring. As it cannot be stacked on
the top of the smallest one, naturally it goes on the other peg. By understanding the rules,
logical thinking is used to determine the next step. When making a move, do not be
distracted by color of the rings or pegs, nor the sounds they make, but focus on the rings.
This is simplifying the problem using abstraction. As in Fig. 0.6 after successive moves,
there is a point where the largest ring is on one peg and rest of the rings are stacked on
another peg in order the largest to the smallest from the bottom to the top. Realize that
during the moves, a ring can be placed on the top of any ring that is bigger than itself, not
merely the next larger ring.
0.3 Introduction to Computational Thinking 7
Now the task is to move the stack that has one smaller number of rings. During the
process there will be a situation where the second largest ring is on the top of the largest
one and the rest of the rings are stacked in correct order on another peg as shown in Fig.
0.7.
Fig 0.7 The largest and the second largest rings in the final position
Notice that every time the largest ring of the particular stack is placed in the final
position, the next problem is to solve the same problem with one less number of rings,
which means the task to solve the Tower of Hanoi game is divided into smaller problems
using decomposition. Also realize that the same technique can be used to solve the
problem with different sizes by seeing the pattern. Making a plan of where to moving a
ring is step-by-step procedure, which is an algorithm.
be made between the syntax and the semantics of a program. Simply stated, the syntax is
the grammar of the language, and the semantics is the meaning or what each instruction
does. To explain further, syntax is the spelling of the individual words, where the
semicolons go, and so on. If mistakes are made, a compiler will detect what are known as
syntax errors, generate messages to the programmer, and the program will not be compiled
or executed. Although syntax is very important, there is a tendency for first-time
programmers to spend too much time learning syntax to avoid syntax errors. However,
there must be equal time spent on semantics to ensure that the program does what the
programmer intended it to do.
Even though there might not be any syntax errors, there can be what are called execution
errors or run-time errors, such as division by zero. When these types of errors occur, the
appropriate error messages are generated and execution stops. Even worse, there can also
be logic errors, which are mistakes in the logic of the program so that the program does
not do what was intended. The unfortunate aspect of logic errors is that they do not produce
any error messages which can make them extremely difficult to find and fix.
As an analogy, an individual might be able to build a small storage shed by just sawing
and nailing some lumber together without worrying about the overall design of the project.
However, with a larger project such as a house, apartment building, or office building, that
methodology would not be sufficient. Instead, there are many other people who must be
consulted, including the original customer who wants the building built, the architects who
work with the customer, the contractors, and the carpenters. The same holds true in the
world of programming where a programmer and/or systems analyst works with others such
as customers, users, and managers.
What are needed are various strategies and tools to help write programs correctly to
minimize logic errors. Just as in the above example where blueprints and plans are used by
the architect, there are techniques that can be used by analysts, software engineers, and
programmers. Although the complete process for developing software might not be needed
initially with smaller programs, it does not hurt to practice the various techniques on
smaller programs to gain familiarity, so that when one advances to more difficult projects,
one is comfortable with many of the techniques. Although the following techniques are
used primarily with non-object-oriented programs, they can be augmented with object-
oriented design techniques introduced in chapter 2 and used in larger programs.
0.4 Essentials of Software Design 9
There are many different methodologies and number of stages within the various
methodologies for solving problems that can be found in different texts, but upon closer
examination, they are all rather similar. They tend to include at least four stages, and they
are usually comparable to the following:
1. Analysis
2. Design
3. Implementation
4. Maintenance
0.4.2.1 Analysis
The analysis stage is where the needs of the user or customer are first determined.
Questions concerning the form and quantity of the input, the type of processing that needs
to be done, the storage requirements of data, and the type of output needed are asked and
clarified at this stage. This would be similar to a customer in a construction project trying
to determine what type of building should be built. In a first semester programming class,
this stage may or may not be included. Sometimes a professor might have already
completed the analysis stage and included what is needed in the programming assignment.
However, at other times, they might require this stage and a number of questions will need
to be asked by the student. This might be especially true when working on a team project
in a software design or senior capstone course.
0.4.2.2 Design
The design stage is where a project begins to take shape. It is similar to the architect
creating a set of blueprints and models for the user to examine, because changes are much
easier to make on paper or with the model than once the construction of the building has
started. Various tools such as pseudocode and Unified Modeling Languge (UML) diagrams
(discussed shortly) are used by systems analysts, software engineers, and programmers to
help design the program. Again, it is much easier to make changes during the design phase
than after the programming has begun.
0.4.2.3 Implementaion
The implementation stage is where the code is actually written, entered, compiled, and
syntax errors are corrected. Once the code is free of syntax errors, it is thoroughly tested.
This includes testing various components of the program to be sure each section is working
properly. If not, the code needs to be debugged to correct any logic errors. In addition to
the various components, the entire program needs to be tested to ensure that all the
components work together as planned. Sometimes errors are a result of not following the
design, whereas other times, it is not necessarily the code but rather the design itself that
has the error, in which case one has to go back and correct the error in the design. The
result is that the stages above do not necessarily need to be rigorously adhered to, but rather
when at a stage one may need to return to a previous stage for clarification or to fix a
possible error.
10 0 Introduction to Computing Concepts
0.4.2.4 Maintenance
The maintenance stage is where all the modifications and updates take place. In an
industrial strength program, more time is spent in the maintenance phase than all of the
three preceding stages. This is because once a program is up and running, there can be
numerous changes that need to be made over the lifetime of a program. This is another
reason why a program should be designed well in order to facilitate modifications later in
the life of a program. Unfortunately, beginning programmers do not often experience this
stage of a program, because once the concepts are learned from one programming
assignment, the program is often not used again and another program is assigned to
introduce the next set of concepts. However, in some upper-level courses, the assignments
get longer, existing programs might be modified and reused, and students get to have some
experience with the maintenance stage of programs. Regardless, it helps even beginning
students to design well-thought-out programs to gain practice in the event that a professor
decides it might be easier to modify an existing program rather than having to design a new
program from scratch, as done in the real world.
0.4.3.1 Pseudocode
One technique that can help during the design stage is the use of pseudocode.
Pseudocode is a combination of English and a programming language. Since it is not a
SURJUDPPLQJODQJXDJHWKLVLVWKHUHDVRQIRULWVQDPHDV³SVHXGR´FRGH7KHDGYDQWDJHRI
using pseudocode is that one can concentrate on the logic of an algorithm and not worry
about the syntax of a particular programming language. In fact, well-written pseudocode
should be understood by any programmer regardless of the programming language that
they use, and they should be able to convert the pseudocode into their particular
programming language. However, there can be many different versions and levels of detail
WKDWFDQEHLQFOXGHGLQSVHXGRFRGHVRLWLVEHVWWRFKHFNZLWKRQH¶VLQVWUXFWRURUFRPSDQ\
for any preferences or standards that are employed. As a simple example, consider the
following pseudocode on the left compared to the Java statement on the right:
At this time it is not nececessary to understand the Java code on the right. However, note
that the verbal description on the left is much easier to understand than the detailed Java
0.4 Essentials of Software Design 11
code. As a result, one does not need to concentrate on the intricate syntax, but rather the
semantics. As an alternative, notice the more specific pseudocode on the left.
Both the pseudocode and the Java code are known as assignment statements as will be
discussed in the next chapter. Note that an arrow is used in the pseudocode instead of an
equal sign in the Java code to indicate an assignment statement. This helps illustrate the
direction of assignment, since some languages use symbols other than an equal sign to
illustrate assignment. Also notice that a mathematical symbol is used instead of an asterisk
to illustrate multiplication. Lastly, a semicolon is not used since not all other languages use
them to terminate statements.
The result is that the pseudocode in the second example is more specific than the first
example which helps with the translation to Java, but it is also more generic than the Java
statement which helps in the translation to other languages as well. Again, these are just
two samples of pseudocode, so be sure to check your local guidelines and requirements
which should be used. In this text, when pseudocode is used, it will be written with as much
detail as possible so as not to be ambiguous and to help with the translation into Java.
0.4.3.2 OOP and UML
Since the term object has been used previously, what is an object? In the past, programs
were often written with large sections of code that were not very helpful to reuse when
another program needed to be written.
It can help with the understanding of the idea of objects to think of an automobile. An
automobile has many different parts such as steering mechanisms, transmissions, brakes,
etc. Instead of designing the transmission as unique to only one type of auto, a generic plan
for a transmission can be designed first. Then particular versions of the transmission could
be built to be put into different types of automobiles. The plans for the transmission can be
thought of as a class and the differing versions of the transmission as instances of that class,
or in other words, objects. This way a whole new design for a transmission does not need
to be created for each type of auto, but rather just a different variation. The same applies to
software, where a whole new complete design does not need to be created but just a
different version of the original class. Although this is just a brief glimpse of objects, this
same idea can be applied to software and a more complete discussion will be presented in
Chapter 2.
Unified Modeling Language (UML) is a visual method used to help contruct a program
using classes and objects. One might ask since Java is an Object Oriented (OO) language
and UML is helpful in creating an OO program, why would one need pseuodocode? The
answer is because each class and instance of a class or object contains code, pseudocode
can help with the design of the class. So whereas UML helps with the creating of classes
and objects, pseudocode helps with the creation of code within classes and objects. UML
will be discussed further in Section 2.10.
12 0 Introduction to Computing Concepts
0.4.3.3 Debugging
Even when all attempts to write a logically correct program are followed, the possibility of
logic errors still exists. The process of finding and fixing logic errors is known as
debugging. When trying to debug a program, a programmer should not start to randomly
alter code in the hope that the error might be fixed. Although this might work occasionally
with smaller programs, it rarely works as programs become larger and more complex.
Instead, one should look for patterns in the output in an attempt to isolate the problem.
Further, one needs to carefully check the program by walking through the code to ensure
that it is doing what was originally intended.
To assist in this process many Integrated Development Environments (IDEs), which are
used to enter, edit, compile, and execute a program, sometimes include debuggers that can
trace the contents of various memory locations to help locate a logic error. However, do
not rely on the debugger alone to help correct the problem, but rather use it as a tool to
assist in tracing the logic of the program. If a debugger is not available, well-placed output
statements at critical points in the program can help in the debugging process. In the end,
it is the programmer reading the code carefully to see what the code is actually doing, rather
than what one thinks it is doing, that will ultimately fix logic errors in a program. [9]
testing and correct operation of a program is imperative and is the responsibility of the
programmers.
The field that includes these instances and other related issues is known as ethics. Entire
stand-alone courses are offered, typically from the philosophy department, and
corresponding books have been written that address the many theories and the application
of those theories to specific instances. In addition, these ethical theories have been
applied to paticular areas such as business ethics, medical ethics, environmental ethics as
well as computer ethics. With respect to the latter, many colleges offer separate courses
in computer ethics that may be given as an elective or may be required for a major in
computer science.
Since entire books have also been written on the field of computer ethics it would not be
possible to discuss all the theories here. Fortunately, various professional organizations in
many different fields provide codes of ethics. The same is true in the field of computing
where the Institute of Electrical and Electronics Engineers (IEEE) Computer Society[3]
and the Association of Computing Machinery (ACM) [1] have each developed a Code of
Ethics. These codes help provide guidance when confronting various ethical situations
within the world of computing.
These codes are provided by their respective organizations and are available online. The
reader is encouraged to look at least one of these codes as possLEO\LQGLFDWHGE\RQH¶V
instructor. It might also prove interesting to look at both codes to see the similarities and
possible differences between them.
After examining a code of ethics such as the ACM Code of Ethics, there are some
exercises at the end of the chapter based on the discussion at the beginning of this section
that can serve either as a discussion in the classroom or as an essay question in a
homework assignment. Although it is beyond the scope of this text to examine the
theories and codes in any detail, there are a number of texts that examine theories and
LVVXHVLQPRUHGHWDLOVXFKDV³(WKLFVLQ&RPSXWLQJ$&RQFLVH0RGXOH´E\-RVHSK0LJJD
Kizza. [5]
0.6 Summary
Machine language and assembly language are low-level languages, where the former
uses ones and zeros and the latter uses mnemonics.
High-level languages are more English-like, where C, C++, and Java are examples of
high-level languages.
Compilers convert the entire high-level language program into machine language
before executing the machine language program, whereas interpreters convert a high-
level language program one instruction at a time and then execute only the
corresponding machine language instructions before converting the next high-level
instruction.
Java is a hybrid system, where the Java instructions are converted into an intermediate
language called bytecode using a compiler and then the bytecode is converted into
machine language using an interpreter.
Computational thinking is a problem-solving process that includes decomposition,
pattern recognition, abstraction, algorithms, and logical thinking. The above are skills
that you can apply in life in general.
14 0 Introduction to Computing Concepts
3. Compare the two codes of ethics mentioned previously in Section 0.5. Identify
one or more elements that are similar. If possible, identify one element that
appears in one code but does not seem to appear in the other.
4. Using one of the code of ethics such as the ACM Code of Ethics, or the code
assigned by the instructor, analyze the following scenarios as to the proper course
of action. Be sure to indicate which element in the code applies.
a. A student in a first-year course has asked a fellow student for assistance with
their programming assignment. After starting to look over the program for the
SRWHQWLDOHUURUWKHVWXGHQWVHHNLQJDVVLVWDQFHVD\V³7KLVLVVXUHWDNLQJDORWRI
time. Might it be easier to just send me a copy of the file containing your
SURJUDPDQG,FDQMXVWFKDQJHWKHQDPHDWWKHWRS"´$FFRUGLQJWRWKHFRGHRI
ethics selected or assigned, what should the student who is providing the
assistance do? What alternatives are there?
Vaino.
1.
Kun sir Archie kaupunkiin palatessaan kulki mökin ohi, seisoi Elsa
neiti yhä edelleen kynnyksellä, ja hän itki.
Vaan kun sir Archie nousi lähteäkseen pois, niin hän pyysi saada
suudella Elsaa.
Silloin Elsa kielsi sen ja lähti ovelle pakoon, mutta sir Archie
asettui eteen ja yritti suudella väkisin.
Silloin sir Archie väistyi Elsan luota. Hän ojensi hänelle vaan
kätensä hyvästiksi ja riensi pois.
Vaan Torarinin äiti sanoi Elsalle: »Oikein teit, kun lähetit minua
noutamaan. Ei nuoren neidon ole hyvä olla kahden kesken sellaisen
miehen kuin sir Archien kanssa. Kyllähän tiedät, Elsa, että
palkkasoturit ovat armotonta väkeä, eikä niillä ole Jumalasta tietoa.»
Kun Elsa kuuli tämän, niin hän aivan kalpeni ja sanoi: »Silloin se
on varmaankin ollut taivaan enkeleitä, joka teille toi sanan ja lähetti
teidät kotiin.»
2.
Elsa istui ääneti ja kuunteli joka sanaa minkä sir Archie virkkoi, ja
hän uskoi ne tosiksi. Ja sir Archie ajatteli, ettei ikinä ollut tavannut
niin helposti lumottavaa neitoa.
»Mikä teille tuli, sir Archie, miksette puhu enää?» kysyi Elsa.
Sir Archie aukoi ja sulki kättään ikäänkuin suonenvedossa.
»Niin, oi kun kerran tapaisin sen miehen!» Elsa sanoi. Hän seisoi
nyrkkiään puristaen sir Archien edessä.
»Et voi tavata sitä miestä», sanoi sir Archie. »Hän on kuollut.»
3.
Sir Archie ei voinut käsittää, miksi hänen mielensä aina oli täynnä
synkkiä ajatuksia. Niistä hän ei päässyt vapaaksi silloin kun Elsan
kanssa puheli, eikä silloin kun istui tovereineen juomassa. Vaikka
hän usein tanssi yökaudet ranta-aitoissa, eivät ne hänestä eronneet,
eikä hän saanut niistä rauhaa edes samoillessaan peninkulmien
päähän jäätyneelle merelle.
Sir Archiella oli kaksi ystävää, sir Reginald ja sir Filip, jotka alati
olivat hänen seurassaan. Häntä suretti, kun hän oli pahalla tuulella
eikä mistään tuntenut itseään onnelliseksi.
»Mikä sinua oikein vaivaa?» he usein sanoivat. »Miksi sinun
silmiäsi niin kuumottaa ja poskesi käyvät niin kalpeiksi?»
Sir Archie ei tahtonut sanoa heille mikä häntä kiusasi. Hän ajatteli:
Mitä he minusta sanoisivatkaan, jos huomaisivat heittäytyneeni näin
miehuuttomaksi? He eivät minua enää tottelisi, jos kuulisivat minun
ruvenneen katumaan tekoa, joka oli välttämätön.
Illalla Elsa käveli yksinään kujaa pitkin mäelle päin. Hän tuli
väsyneenä työstään ja ajatteli itsekseen: Tämä elämä on työlästä,
enkä minä siitä saa yhtään iloa. Minua ilettää koko päivän seista
siellä laiturilla kalanhajussa. Minua ilettää kuulla niiden naisten tylyllä
äänellä laskevan leikkiä ja nauravan. Minua ilettää nähdä ahnasten
lokkien kärkkyvän pöytien ympärillä siepatakseen kalapaloja läpi
käsieni. Kunpa edes joku tahtoisi tulla viemään minut pois täältä!
Silloin minä häntä seuraisin vaikka maailman ääreen.
Elsa kulki juuri kujan pimeintä kohtaa, kun sir Reginald ja sir Filip
astuivat varjosta hänen luokseen ja tervehtivät häntä.
Elsa neidillä oli kova kiire. Hän melkein juoksi kujaa alamäkeen.
Sir Filip ja sir Reginald saivat käydä rivakasti pysyäkseen hänen
rinnallaan.
Hänellä oli niin kiire sir Archien luo, ettei joutanut välittämään siitä
mikä ihan varpaittensa edessä vieri. Hän potkasi sen syrjään, mutta
se palasi heti ja kieri hänen edessään alas kujaa pitkin.
Elsa kuuli sen helähtävän hopealta, kun hän sitä potkasi, ja hän
näki sen välkkyvän ja kimaltelevan.
»Ei se olekkaan tavallinen kivi», Elsa ajatteli. »Luulenpa että se on
hopearaha.» Mutta hänellä oli niin kiire sir Archien luo, ettei katsonut
ehtivänsä nostaa sitä maasta.
Hän kumartui ja otti sen maasta. Se oli suuri pyöreä raha, joka kiilti
Valkoselta hänen kädessään.
»Minkä sinä siitä kadulta löysit, neiti?» kysyi sir Filip. »Se kiiltää
niin valkealta kuutamossa.»
»Katsotaas nyt mitä sinä kadulta löysit, neiti», sanoi sir Reginald,
pysähtyen lyhdyn alle.
Elsa neiti hyökkäsi sisään heidän luokseen. Hän piti rahaa ylhäällä
kädessään.
»Hoi, kuulkaa miehet», hän huusi, »nyt tiedän, että Aarne herran
murhaajat ovat elossa. Kas tässä on Aarne herran raha, jonka
löysin!»
»Olkoon nyt totta tai lorua mitä puhut», sanoi Torarin, »niin mitä
me voimme tehdä? Emme me siltä voi murhamiehiä löytää, vaikka
sinä tiedätkin heidän kulkeneen yhtä kaupunkimme katua.»
»Tule pois, Elsa, mukaani kotiin!» Torarin sanoi. »Ei nyt ole aika
neitosen juoksennella katuja ja kujia.»
Raatikellarissa.
1.
»Jos oma tyttäreni olisi sellainen kuin sinä», emäntä sanoi, »niin
antaisin saman kiellon. Suon sinulle parempaa kuin palveluspaikan
minun luonani.»
2.
Kuollut astui kaiken aikaa Elsasta kahta askelta edellä. Sinä iltana
oli kova myrsky, joka kapeissa kujasissa oli pusertaa kulkijan seinää
vasten, ja Elsa huomasi, että pahimpien puuskien aikana kuollut
asettui hänen ja tuulen väliin, suojelemaan häntä minkä hennolla
ruumiillaan voi.
»Tahdotko, Elsa, nyt olla täällä hyvin ääneti, jottei emäntä huomaa
minun hankkineen apulaista?»
»No hyvästi sitten, Elsa», sanoi kuollut. »Nyt pyydän sinulta vaan
yhtä. Ja se on, ettet tämän jälkeen minuun ylen kovasti suutu.»
Elsa teki nyt ensin työnsä valmiiksi. Hän huuhtoi kaikki maljat ja
kannut ja kuivasi ne. Sitte hän meni luukulta katsomaan, oliko sinne
pantu mitään lisää. Kun ei siellä mitään ollut, niin hän jäi luukun
ääreen seisomaan katsellen kellarisaliin.
Oli sellainen aika päivästä, jolloin ei ketään vieraita tavannut
kellariin tulla. Emäntä ei ollut istumassa pöytänsä takana eikä hänen
palvelijoitaan ollut saapuvilla. Ketään muuta huoneessa ei näkynyt
kuin kolme miestä, jotka istuivat suuren pöydän päässä. He olivat
vieraita, vaan tuntuivat liikkuvan kuin kotonaan, koska muuankin
heistä joka oli juonut maljansa pohjaan, ilman muut? kävi
täyttämässä sen suuresta viiniruukusta ja istahti sitte uudestaan
juomaan.
Sir Archie istui näet hiukan muista erillään. Hän vaan äänettömänä
tähysti eteensä eikä juonut. Hän ei ottanut osaa toisten
keskusteluun, ja kun hänelle jotakin virkettiin, niin hän ei edes
huolinut vastata.
Elsa seisoi ihan hiljaa ja katsoi huoneeseen. Hän näki, että hänen
kasvinsisarensa sir Archien edessä piti silmiään auki. Elsan
seurassa hän ei ollut yhtään kertaa nostanut silmiään maasta.
Mutta samassa kun tämä oli sanottu: »Juo, veikko! Vielä Aarne
herran rahoja riittää», silloin Elsa näki kuolleen nousevan penkiltä ja
katoavan.
3.