Solution Manual For C Programming From Problem Analysis To Program Design 4th Edition Barbara Doyle Isbn 10 1285096266 Isbn 13 9781285096261
Solution Manual For C Programming From Problem Analysis To Program Design 4th Edition Barbara Doyle Isbn 10 1285096266 Isbn 13 9781285096261
1-1
Chapter 1
Introduction to Computing
and Programming
At a Glance
Chapter Objectives
Instructor Notes
Quick Quizzes
Coding Standards
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-2
Resources
Discussion Questions
Projects to Assign
Key Terms
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-3
Lecture Notes
Chapter Overview
In this chapter, students review the history of computers and programming languages, including
the evolution of C# and .NET. This chapter introduces data and describes how it is represented.
Primary types of hardware components are described; differing types of software are discussed.
This chapter explains the difference between structured and object-oriented programming and
includes the software development methodology used throughout the remainder of the book.
Chapter Objectives
In this chapter, students will:
Instructor Notes
History of Computers
Computers have become such an integral part of our lives that many of their functions are taken
for granted. Advances in computing are occurring every day, and the programs that distinguish
computers have become very complex. To reach this level of complexity, software development
has gone through a number of eras.
Computing dates back some 5,000 years. Many consider the abacus to be the first computing
device. The Analytical Engine, designed by Charles Babbage and his assistant, Lady Augusta
Ada Bryon, Countess of Lovelace, included input devices, memory storage, a control unit that
allowed processing instructions in any sequence, and output devices. This was the prototype for
what is known today as a general-purpose computer.
generation of general-purpose machines. The first era is distinguished by the use of vacuum
tubes.
The second generation was characterized by the invention of the transistor in 1956. The
software industry (FORTRAN and COBOL) was born.
The third generation, 1964–1971, saw computers become smaller, as transistors were squeezed
onto small silicon discs (single chips) that were called semiconductors. Operating systems were
first seen in third-generation systems. Many computer historians believe the present day to be
either the fourth or fifth generation of modern computing.
During the fourth generation, IBM introduced its personal computer (PC), and in the 1980s,
affordable clones of the IBM PC emerged. Defining a fifth generation of systems is somewhat
difficult because the generation is still young. Major advances in software are anticipated as
Integrated Development Environments (IDEs) such as Visual Studio make it easier to develop
applications for the Internet rapidly. Because of the programmability of the computer, the
imagination of software developers is free to conjure the computing functions of the future.
Quick Quiz
1. True or False: The first generation of computing is characterized by the use of vacuum
tubes.
Answer: True
2. True or False: The most recognized first computing device is the Pascaline.
Answer: False
4. The introduction of the IBM PC clones occurred during the _____________ generation of
computing.
Answer: fourth
System Software
System software is loaded when you power on the computer. Most people associate operating
systems with system software. Example operating systems are Windows 8, Android, iOS,
Windows 7, and Linux. These types of programs oversee and coordinate the resources on the
machine. They include file system utilities, which are small programs that take care of locating
files and keeping up with the details of a file’s name, size, and date of creation.
System software programs perform a variety of other functions: setting up directories; moving,
copying, and deleting files; transferring data from secondary storage to primary memory;
formatting media; and displaying data on screens. Another type of system software includes
compilers, interpreters, and assemblers.
Application Software
Application software consists of programs developed to perform a specific task. Word
processors, such as Microsoft Word, are examples of application software. The programs that
you write for this book will be application software.
Quick Quiz
1. What are the two types of software?
Answer: System and application
4. Identify two types of functions that are performed by operating system software.
Answer: Copy and delete files
4. Implement the code. During this phase, the typed program statements (source code) are
compiled to check for rule violations.
5. Test and debug. Good programmers often build test plans at the same time they are
analyzing and designing their solutions. This test plan should include testing extreme
values, identifying possible problem cases, and ensuring that these cases are tested.
Quick Quiz
1. True or False: A testing plan should be developed early on in the program development
process.
Answer: True
2. True or False: The first step in the program development process should be to design a
solution.
Answer: False
3. True or False: All programming methodologies should include exactly five steps.
Answer: False
Programming Methodologies
A methodology is a strategy, a set of steps, or a set of directions to solve a problem. The two
most popular programming paradigms used by programmers are structured procedural
programming and object-oriented programming.
Object-Oriented Programming
Viewed as a newer approach to software development, the concept behind object-oriented
programming (OOP) is that the focus is on determining the objects you want to manipulate
rather than the processes or logic required to manipulate the data. An entity is often defined as a
person, place, or thing. It is normally a noun. By abstracting out the attributes or characteristics
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-7
(data) and the behaviors or actions (processes on the data), you can divide complex phenomena
into understandable entities.
A class is like a template; an object is an instance of the class. When these data members are
associated with the class, an object is created or constructed. Through inheritance, it is possible
to define subclasses of data objects that share some or all of the parent’s class characteristics.
This is what enables reuse of code. A class diagram is one of the primary modeling tools used
by object-oriented programmers.
Whether you are using a procedural or object-oriented approach, you should follow the five
steps to program development. As with the procedural approach, the object-oriented
development process is iterative.
Quick Quiz
1. Structured approach is _____________ oriented.
Answer: process
Dennis Ritchie is credited with developing the C language. C++ is an extension of C; Bjarne
Stroustrup at Bell Labs is considered the father of C++ for his design work in the early 1980s.
Visual Basic, introduced in 1991, is derived from BASIC, a language developed in the 1960s.
The earlier versions of Visual Basic did not facilitate development using an object-oriented
approach. Earlier versions of Visual Basic did, however, facilitate easy creation of Windows-
based graphical user interfaces (GUIs). Java was introduced in 1995 and was originally called
Oak.
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-8
C# is one of the newer programming languages. It conforms closely to C and C++, but many
developers consider it akin to Java. There are a number of similarities between the languages. It
has the rapid graphical user interface (GUI) features of previous versions of Visual Basic, the
added power of C++, and object-oriented class libraries similar to Java. C# was designed from
scratch to work with the new programming paradigm,
.NET
NET is an environment in which programs run. It is not an operating system, but rather a layer
between the operating system and other applications. As such, it provides a platform for
developing and running code that is easy to use. Included in Visual Studio are tools for typing
program statements, and compiling, executing, and debugging applications. Included as part of
.NET are multi-language independence capability, Framework class library with over 2500
reusable types (classes), and the capability of creating dynamic web pages, web services and
scalable components.
Quick Quiz
1. Give three examples of high-level programming languages.
Answer: C#, Java, Visual Basic
Why C#?
Compilers targeting the .NET platform are available for a variety of programming languages.
• C# was the language created for .NET and was designed from scratch to work with
.NET.
• Most of the .NET Framework classes were written using the C# programming language.
• C#, in conjunction with the .NET Framework classes, offers an exciting vehicle to
incorporate and use emerging Web standards.
• C# is a simple, object-oriented language.
• Using the Visual Studio IDE and the .NET Framework, C# provides an easy way to
create graphical user interfaces.
• On December 13, 2001, the European Computer Manufacturers Association (ECMA)
General Assembly ratified C# and its common language infrastructure (CLI)
specifications into international standards. C# is being ported to other platforms such as
Linux.
• C# represents the next generation of languages.
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-9
Quick Quiz
1. _____________ was the language created for .NET.
Answer: C#
2. True or False: C# can be used for creating Web applications, but not Windows applications.
Answer: False
3. True or False: All of the .NET Framework classes were written in C#.
Answer: False
Windows Applications
Designed for desktop use and for a single platform, they run on PC desktops much like your
favorite word-processing program. Using the Integrated Development Environment (IDE) of
Visual Studio, you can drag and drop controls such as buttons, text boxes, and labels onto an
application.
Console Applications
Requests to the operating system are sent to display text on the command console display or to
retrieve data from the keyboard. These are the simplest types of applications to create.
In addition to these applications, class libraries and stand-alone components (.dlls), smart device
applications, and services can also be created using C#.
Quick Quiz
1. True or False: Output is displayed on the command windows for console applications.
Answer: True
Quick Quiz
1. True or False: No more than 256 characters can be placed on a single line when you are
typing your program statements.
Answer: False
2. True or False: You must type line numbers before you type the program statement.
Answer: False
3. You should use a(n) _____________ style when you type your program statements.
Answer: consistent
4. List two things you might do to make your program more readable.
Answer: Group and indent statements; properly line up curly braces
Elements of a C# Program
There are a number of entries that appear in every program.
Comments
Comments are not considered instructions to the computer and therefore have no effect on the
running of the program. When the program is compiled, comments are not checked for rule
violations. With C#, three types of commenting syntax can be added to a program: in-line,
multi-line, and XML document comments. Visual Studio displays comments in green, as does
the textbook.
In-Line Comments
Use two forward slashes (//) to mark the rest of the line as a comment. It is considered a one-line
comment because everything to the right of the forward slashes is ignored by the compiler.
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-11
Multi-Line Comments
Also called block comments, they are marked by typing a forward slash followed by an asterisk.
You must end the comment using the opposite pattern of an asterisk followed by a forward
slash. The comment can span many lines, and everything that appears between the comment
symbols is treated as a comment.
Using Directive
Adding a using directive permits the use of classes found in a namespace without having to
qualify their references using the namespace identifier. This can reduce the amount of typing
that would be necessary without the directive. The most referenced namespace is System. The
directive is added at the top of the program as shown below:
using System;
The System namespace contains classes that define commonly used types or classes such as the
Console class. The Console class enables programmers to write to and read from the console
window or keyboard.
Namespace
Use the keyword namespace to group semantically related types (classes) under a single
umbrella. The body for the namespace must be enclosed in curly braces ({ }). The namespace
surrounds the class definition.
Class Definition
Everything in C# is designed around a class. Every class is named, and it is tradition to name the
file the same name as the class name, except the filename will have a .cs extension. Like
namespaces, each class definition must be enclosed in curly braces { }.
Main ( ) Method
The Main ( ) method can be placed anywhere inside the class. It is the “entry point” for all
applications—where the program begins execution. A method is a collection of one or more
statements combined to perform an action. Typically, a method calls another method and can
return a value to the calling method. Methods communicate with each other by sending
arguments inside parentheses or as return values.
Method Body—Statements
Methods always appear with parentheses ( ). A call to any method always includes a set of
parentheses following the method name identifier, as do signatures for methods. Statements that
appear in a method are executed in sequential order. Once the end curly brace is encountered,
control returns back to the calling method. When the closing curly brace is encountered with the
Main ( ) method, the entire program ends.
WriteLine ( ) is defined in the Console class and can be called with or without arguments. To
have a blank line displayed on the standard output device, type Console.WriteLine( ); without
any arguments. Methods in this class include Read ( ), WriteLine ( ), and Write ( ). The method
Write ( ) differs from WriteLine ( ) in that it does not automatically advance the carriage return
to the next line when it finishes. Review Table 1-1. It describes the special escape sequence
characters that can be added to the string literal included as an argument to the Write ( ) or
WriteLine ( ) method.
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-12
The ReadKey( ) or Read ( ) methods are often used in a C# program to keep the output screen
displayed until the user clicks a key on the keyboard. ReadLine ( ) allows multiple characters to
be entered. It accepts characters until the Enter key is pressed.
Quick Quiz
1. What are the most common types of comments included in program? How are they
distinguished?
Answer: Block or multi-line and in-line or one-line comments. Block comments use /* and
*/ to end the comment. In-line comments are added using two forward slashes.
2. True or False: The Main ( ) method must be placed at the beginning of a program. .
Answer: False
4. What is the relationship between the name of the file that contains the program statement
and the name of the class?
Answer: They are named the same, except the file ends with a .cs file extension.
Use the built-in editor available with the Visual Studio IDE to type your program statements.
You then compile the source code from one of the pull-down menu options in the IDE and
execute the application using another menu option. As the Project Type, select Console
Application for the Template. The Visual Studio IDE generates much of the code for you. You
will want to change the name of the class and the source code file because Visual Studio names
every class Class1. You can do this using the Use the Solution Explorer Window.
If you run your program using DEBUG>Start Without Debugging, instead of DEBUG>
Start, you will not have to add the additional Console.Read ( ); or Console.ReadKey( ); to hold
the screen. This is done automatically for you.
Quick Quiz
1. True or False: The output produced by the compiler is a run of your application.
Answer: False
2. Describe the process the program statements go through after being typed into an editor.
Answer: The statements are compiled. When no rule violations occur, the compiler converts
the source code statements into Intermediate Language code. From there, a second step, just-
in-time compilation, is necessary to convert the IL code to the native platform-specific code.
Once in native code, the program can be executed and results seen.
3. True or False: Everything from typing the program statements to compiling and seeing the
results of the run can be done within Visual Studio.
Answer: True
4. What additional program statement must be added to hold the screen for viewing if the
application is run by selecting Debug> Start?
Answer: A Read ( ) or ReadKey( ) method must be called to hold the output screen.
Debugging an Application
The major categories of errors are syntax and run-time.
Syntax Errors
These are errors caught by the compiler and include things like typing errors created by
misspelling a name or forgetting to end a statement with a semicolon. In C#, a single typing
error may generate several error messages. The errors are displayed in the Task List window
found at the bottom of the IDE. A good exercise is to omit curly braces, semicolons, and
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-14
misspelled words so that you can see what kind of error messages each mistake generates. You
will then be more equipped to find those errors quickly.
It is best to fix the first error and then recompile rather than trying to fix all the errors in one
pass because one error may cause several error messages to be displayed.
Run-time Errors
Run-time errors are more difficult to detect than syntax errors. A program containing run-time
errors may compile without any problems, run, and produce results.
Quick Quiz
1. True or False: Syntax errors are more difficult to detect than run-time errors.
Answer: False
2. True or False: When you see the list of syntax errors, be sure to correct all of them prior to
building the application a second time.
Answer: False
No input is needed. The output produced is "Programming can be FUN!”. A prototype for the
output is shown. An algorithm is developed using a flowchart. Once the design is completed and
it is verified that the algorithm produces the correct output, the algorithm is translated into
source code. You can type source code statements into the computer using Visual Studio IDE.
From the generated code, you can remove the XML-style comments. The signature for Main ( )
can also be modified by removing the arguments “string[ ] args” inside the parentheses so that it
has an empty argument list. Also change the name of the class to a name that better represents
what the application is doing.
Note that the statements inside the Main ( ) method are executed in sequential order. The
ReadKey ( ) method is executed after the two WriteLine ( ) methods. ReadKey ( ) is used in a
program to keep the output screen displayed until the user clicks a key. During implementation,
the source code is compiled to check for rule violations.
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-15
Just because you have no compiler syntax errors and receive output does not mean the results
are correct. During this final step, test the program and ensure you have the correct result. The
output should match your prototype.
Quick Quiz
1. What is a prototype?
Answer: Mock-up of the desired end result
3. True or False: One of the first things you should do when you begin an application is change
the name of the class and source code filename.
Answer: True
Coding Standards
Following coding standards when you design classes leads to better solutions and reduces the
amount of time needed when you make changes to your program statements. Developing
standards that you consistently adhere to increases coding efficiency.
Pseudocode
Use verbs to indicate what type of actions should be performed.
Group items and add indentation
Resources
Current C# Language Specifications –
http://www.microsoft.com/en-us/download/details.aspx?id=7029
Pascaline –
http://www.thocp.net/hardware/pascaline.htm
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-16
Discussion Questions
Some interesting topics of discussion in this chapter include:
• What types of applications do you think will characterize the sixth generation of
computing or computing in the future?
• What characteristics make up a class representing a city?
• What other development environments other than Visual Studio are currently available
to be used with C#?
• What are the ways you can run a C# program?
• How do syntax errors differ from logic errors?
Projects to Assign
All of the Multiple Choice Exercises, Problems 1-20
Odd-numbered Short Answer Exercises, Problems 21-27
Programming Exercises, Problems 1, 2, 8, 9, and 10
Key Terms
➢ ASP.NET: a Microsoft programming framework that lets you create applications that
run on a Web server and delivers functionality through a browser
➢ abstracting out the attributes: determining the data characteristics associated with a
class object
➢ algorithm: a clear, unambiguous, step-by-step process for solving a problem
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-17
➢ American Standard Code for Information Interchange (ASCII): character set that
consists of the alphabet for the English language, plus numbers and symbols; the first
128 characters correspond to the Unicode character set
➢ application software: programs developed to perform a specific task
➢ assemblers: programs that convert the assembly programming language into native code
➢ behaviors: processes on the data of a class
➢ beta version: working version that has not been fully tested and may still contain errors
➢ block comment: a comment that can span more than one line that is marked by a
forward slash followed by an asterisk (/*) and ended with the opposite pattern (*/)
➢ bugs: program errors
➢ bytecode: Java’s intermediate language
➢ C# (C sharp): One of the newer high-level programming languages that is part of the
.NET paradigm
➢ characteristics: attributes of a class
➢ class: logical groupings of data and behavior members
➢ class diagram: one of the primary modeling tools used by object-oriented programmers
➢ COBOL (Common Business Oriented Language): a high-level programming
language
➢ Common Language Runtime (CLR): execution engine of .NET
➢ compiler: program used to check to make sure there are no rule violations in the
program statement and then translates the instructions written in a high-level
programming language into machine-readable format
➢ console applications: display text on the command console display
➢ constant: a data item that should keep the same value throughout the program
➢ definition of the method: method heading along with the complete body of the method
➢ desk check: mimicking the computer by walking through the program steps
➢ divide and conquer: break the problem into subtasks
➢ domain: range of the values for each input item
➢ encapsulated: packaging of the characteristics and behaviors together to form a class
➢ entity: a person, place, or thing
➢ escape character: a special string combination that begins with the backslash; when
used in combination with other characters, it has special meaning
➢ FORTRAN (Formula Translator): a high-level programming language
➢ Framework Base Classes: a collection of over 2,500 reusable types (classes) included
as part of the .NET Framework
➢ general-purpose computer: systems are electronic devices that process data and are
composed of hardware and software
➢ hardware: physical devices that you can touch
➢ high-level languages: instructions written in English-like statements
➢ identifier: the name given to a user-supplied or predefined entry
➢ IL: when there are no rule violations, the compiler converts the source code into
intermediate language; also called MSIL
➢ information hiding: making the data member private and accessible to that class only
➢ inheritance: way to define subclasses of data objects that share some or all of the
parent’s class characteristics
➢ in-line comment: one-line comment that is marked by two forward slashes (//) and ends
when the enter key is pressed
➢ instance: one example of the class
➢ instantiate: create an instance of the class
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-18
➢ instruction cycle: consists of fetching a program instruction from memory, decoding the
instruction, executing it, and then storing the result in memory
➢ Intermediate Language (IL): all languages targeting the .NET platform compile into
this intermediate language
➢ interpreters: check for rule violations line by line
➢ iterative approach: an approach that involves going back to the analysis or design stage
to make modification when problems are identified
➢ Java: a high-level programming language introduced in 1995 that was originally called
Oak
➢ JITer: program that reads the intermediate language (IL) code and translates or
produces the machine code that runs on the particular platform; after the code is
translated in this second step, results can be seen
➢ just-in-time (JIT) compilation: second step that is required before you see the results of
the application; it converts the IL code to the platform’s native code
➢ keywords: reserved words that have special predefined meanings
➢ logic errors: errors that cause an abnormal termination of the program or just produce
incorrect results
➢ low-level programming languages: programming languages that are not considered
close to the English language in terms of readability
➢ main memory: a device that holds instructions and data; also called primary storage or
random access memory
➢ method: collection of one or more statements combined to perform an action
➢ method invocation: call to a method
➢ methodologies: approaches used to solve computer-related problems
➢ Microsoft Intermediate Language (MSIL): when there are no rule violations, the
compiler converts the source code into intermediate language; also called IL
➢ multi-language independence: development of an application using a number of
programming languages
➢ multi-line comment: a comment that can span more than one line that is marked by a
forward slash followed by an asterisk (/*) and ended with the opposite pattern (*/)
➢ namespace: groups semantically related types under a single umbrella
➢ native code: machine language code of a particular computer
➢ .NET: an environment in which programs run that was designed to be a new
programming paradigm
➢ object: instance of the class
➢ object-oriented analysis, design, and programming: focus is on determining the
objects you want to manipulate rather than the processes or logic required to manipulate
the data
➢ object-oriented approach: the focus is on determining the data characteristics and the
methods or behaviors that operate on the data
➢ operating systems: types of programs that oversee and coordinate the resources on the
machine
➢ procedural programming: process-oriented approach that focuses on the processes that
data undergoes from input until meaningful output is produced
➢ programming language: used to write instructions for the computer
➢ programs: sets of instructions telling the computer exactly what to do
➢ prototype: mock-up of screens depicting the look of the final output
➢ pseudocode: steps are written in “pseudo” or approximate code format, which looks like
English statements
C# Programming: From Problem Analysis to Program Design, 4th ed. 1-19
➢ random-access memory (RAM): a device that holds instructions and data; also called
main memory or primary storage
➢ redistributable version: smaller download than the Software Development Kit and
includes the CLR and class libraries
➢ run-time error: form of logic error; run-time errors normally cause program crashes
(stopping execution) and the reporting of error messages
➢ scalable component development: segments of code, created as separate entities, that
can be stored independently, combined, and reused in many applications
➢ semantic meaning: specific way in which a program statement or keyword should be
used
➢ signature: the name of the method and its argument list
➢ software: programs; the sets of instructions that make the hardware function
➢ software maintenance: upgrade or change an application
➢ source code: program statements written using a programming language
➢ specifications: describes what the program should accomplish
➢ stepwise refinement: refine the logic by dividing and conquering
➢ syntax: the set of rules of the language that must be followed
➢ test plans: plans for how the program will be tested to ensure its correctness
➢ testing: the process of verifying the correctness of a program to ensure that you get
consistently accurate results
➢ top-down design: way to conquer each of the subtasks by further decomposing them
➢ Visual Studio: An IDE that includes a suite of products including several programming
languages, along with a large collection of development and debugging tools
➢ Web Forms: ASP.NET programmable Web pages that serve as a UI for Web
applications
➢ Windows applications: applications designed for desktop single platform use
➢ XML (Extensible Markup Language): language that provides a format for describing
data using tags similar to HTML tags
➢ XML documentation comments: comments that can be used to generate XML
documentation file