Answers are in blue.
Computer Science Illuminated, Seventh Edition Nell Dale, PhD; John Lewis, PhD |
CHAPTER 9 EXERCISES AND ANSWERS |
For Exercises 1–10, match the activity with the phase of the object-oriented methodology.
- Brainstorming
- Filtering
- Scenarios
- Responsibility algorithms
- Reviewing a list of possible classes, looking for duplicates or missing classes.
B查找重复和缺失就是过滤
-
- Asking “what if” questions. C场景算法
- Assigning responsibilities to classes. C场景算法
- Generating a first approximation to the list of classes in a problem.
A
-
- Assigning collaborators to a responsibility. C
- Developing algorithms for the responsibilities listed on a CRC card.
D开发算法给相应的责任
-
- Output from this phase is a fully developed CRC card for all classes.
C
-
- Output from this phase is the design ready to be translated into a program.
D翻译成程序
-
- During this phase, inheritance relationships are explored. B过滤
- Phase in which functional programming techniques are appropriate.
D编程
For Exercises 11–24, match the question with the appropriate translation or execution system.
- Interpreter
- Assembler
- Compiler
- Machine code
- What translates a high-level language into machine code? C
-
- What translates a Java program into Bytecode? C
- What executes Bytecode? A
- What translates an assembly-language program? B
- What is the output of an assembler? D
- What takes input in a high-level language and directs the computer to perform the actions specified in each statement? A
- What executes the Java Virtual Machine? D
- What is used to translate a program in ALGOL? C
- What is used to translate a program in APL? A
- 注意APL是解释器
- What is used to translate a program in COBOL? C
- What is used to translate a program in FORTRAN? C
- What is used to translate a program in Lisp? A
- Lisp也是解释器
- What is used to translate a program in PROLOG? A
- PROLOG也是解释器
- Which translator runs the most slowly? A
- 解释器是最慢的
- 注意那两个是解释器,其他就可以反着推了
- 再写一遍
- 就是APL,LISP
将高级语言转换为机器码的是 C(编译器)。
将Java程序转换为字节码的是 C(Java编译器)。
执行字节码的是 A(Java虚拟机)。
翻译汇编语言程序的是 B(汇编器)。
汇编器的输出是 D(目标代码或机器码)。
接受高级语言输入并指导计算机执行每个语句中指定操作的是 A(解释器或编译器)。
执行Java虚拟机的是 D(Java虚拟机)。
用于翻译ALGOL程序的是 C(编译器)。
用于翻译APL程序的是 A(解释器或编译器)。
用于翻译COBOL程序的是 C(编译器)。
用于翻译FORTRAN程序的是 C(编译器)。
用于翻译Lisp程序的是 A(解释器或编译器)。
用于翻译PROLOG程序的是 A(解释器或编译器)。
运行最慢的翻译器是 A(解释器或编译器)。
For Exercises 25–46, match the language paradigm and the language or the language description.
-
-
- Procedural过程
- Functional函数
- Logic逻辑
- Object-oriented面向对象
- Procedural language with some object-oriented features面向对象的过程语言
- Object-oriented language with some procedural features
- 具有一些过程特点的面向对象语言
- 好难记()
- Which paradigm most accurately describes FORTRAN? A过程
- Which paradigm most accurately describes C++? E
-
-
- Which paradigm most accurately describes PASCAL? A过程
- Which paradigm most accurately describes Java? F
- Which paradigm most accurately describes Lisp? B函数
- Which paradigm most accurately describes BASIC? A过程
- Which paradigm most accurately describes PROLOG? C逻辑PROLOG
- Which paradigm most accurately describes SIMULA? D
- Which paradigm most accurately describes ALGOL? A过程
- Which paradigm most accurately describes ML? B函数
- Which paradigm most accurately describes Scheme? B函数
- Which paradigm most accurately describes Python? D
- Which paradigm most accurately describes C? A
- Which paradigm most accurately describes Smalltalk? D
- The dominant languages used in industry throughout the history of computing software come from which paradigm? A
- Which paradigm did the Japanese choose for the fifth- generation computer?
C逻辑
-
- Which paradigm allows the programmer to express algo- rithms as a hierarchy of objects?
D面向对象
-
- Which paradigm allows the programmer to express algo- rithms as a hierarchy of tasks?
A过程
-
- Which paradigm allows the programmer to express algo- rithms as mathematical functions?
B函数
-
- Which paradigm has no assignment statement? B
- 没有赋值语句的是函数
- Which paradigm uses recursion exclusively to express repetition?
B使用递归的也是函数式
-
- Which paradigm has no variables? B
没有变量也是函数
Exercises 47–84 are short answer.
-
- What is the hallmark of an assembly language?
The hallmark of an assembly language is that each assembly-language instruction is translated into one machine-language instruction.
编译语言的特点是每个汇编语言指令被翻译成一条机器语言指令。
-
- Distinguish between an assembler and a compiler.
- 汇编器将汇编语言指令翻译成机器码。编译器将高级语言指令翻译成机器码(或汇编代码)。汇编器的翻译是一对一的:汇编语言中的一条语句被翻译成一条机器码语句。编译器的翻译是一对多的:一个高级语言指令被翻译成多条机器语言指令。
An assembler translates assembly-language instructions into machine code. A compiler translates high-level language instructions into machine code (or assembly code). The translation of an assembler is one to one: One statement in assembly language is translated into one statement in machine code. The translation of a compiler is one to many: One high-level language instruction is translated into many machine-language instructions.
-
- Distinguish between a compiler and an interpreter.
The output from a compiler is a machine-language program. That program may be stored for later use or immediately exe- cuted, but the execution is a distinct process from the trans- lation. An interpreter translates and executes together. The output from an interpreter is a solution to the original problem, not a program that, when executed, gives you the solution.
编译器的输出是一个机器语言程序。该程序可以存储以供以后使用,也可以立即执行,但执行是与翻译不同的过程。解释器将翻译和执行结合在一起。解释器的输出是对原始问题的解决方案,而不是一个在执行时给出解决方案的程序。
编译器就是一个成品,
解释器是一边做一边展示,就是一边翻译一边执行,输出的是原始问题的解决方案,而不是一个在执行失手给出解决方案的程序
-
- Compare and contrast an assembler, a compiler, and an interpreter.
All three are translators. They differ in the complexity of the languages they translate and in the output from the trans- lator. Assemblers and compilers produce machine-language programs, which, when run, solve the original problem; interpreters produce solutions to the original problem. Assemblers translate very simple languages; compilers and interpreters can translate very complex languages.
这三者都是翻译器。它们在所翻译的语言复杂性和翻译器输出方面有所不同。汇编器和编译器生成机器语言程序,运行时解决原始问题;解释器生成原始问题的解决方案。汇编器翻译非常简单的语言;编译器和解释器可以翻译非常复杂的语言。
-
- Describe the portability provided by a compiler.
A program written in a high-level language that is compiled can be translated and run on any machine that has a com- piler for the language.
用高级语言编写的程序经过编译后,可以在任何具有该语言编译器的计算机上翻译和运行
-
- Describe the portability provided by the use of Bytecode. Bytecode is the output from (usually) a Java compiler. There is a virtual machine for which Bytecode is the machine lan- guage. A program compiled into Bytecode can be executed on any system that has a simulator for the virtual machine. The Java Virtual Machine (JVM) executed Bytecode.
- 字节码是(通常是)Java编译器的输出。有一个虚拟机,字节码是其机器语言。编译成字节码的程序可以在具有该虚拟机模拟器的任何系统上执行。Java虚拟机(JVM)执行字节码。
- 具有JVM就可以执行字节码
- Describe the process of compiling and running a Java program.
A Java program is compiled into Bytecode, which can be executed on any system with a JVM.
Java程序被编译成字节码,可以在具有JVM的任何系统上执行。
-
- Discuss the word paradigm as it relates to computing. Programming languages reflect differing views of reality, which we call paradigms. We use these views (paradigms) to classify the languages.
- 编程语言反映了对现实的不同看法,我们称之为范式。我们使用这些看法(范式)来对语言进行分类。
- Distinguish between imperative and declarative paradigms. An imperative language describes the actions to be taken to solve the problem. A declarative language describes the solution expected, but not the steps to be taken.
- 区分命令式和声明式范式。命令式语言描述解决问题所需的操作。声明式语言描述期望的解决方案,但不描述步骤
- 命令方式的语言描述解决问题的步骤,声明就只是告诉你怎么做
- 命名有步骤,声明没有步骤
- What are the characteristics of the imperative paradigm? Programs describe the processes necessary to solve the problem.
- 程序描述解决问题所需的过程
- What are the characteristics of the functional paradigm? Programs are expressed as the evaluation of functions.
- ?程序表达为函数的评估
- What are the characteristics of the logic paradigm?
Rules of logic are used to deduce answers from facts and rules.
逻辑规则用于从事实和规则中推导答案。
-
- What are the characteristics of a declarative paradigm?
A declarative language describes the solution, not the steps to get there.
声明式语言描述解决方案,而不是实现步骤。
-
- How do you ask questions in a programming language?
To ask a question in a programming language, you make an assertion. If the assertion is true, the answer is true. If the assertion is false, the answer is false.
-
- What is a Boolean variable?
A Boolean variable is a place in memory, referenced by an identifier, that can contain true or false.
-
- What is a Boolean expression?
A Boolean expression is a sequence of identifiers, separated by compatible operators, that evaluates to true or false.
-
- Given Boolean variables one, two, and three, write an asser- tion for each of the following questions:
- Is one greater than both two and three? (one > two) AND (one > three)
- Is one greater than two, but less than three? (one > two) AND (one < three)
- Are all three variables greater than zero? (one > 0) AND (two > 0) AND (three > 0)
- Is one less than two or one less than three? (one < two) OR (one < three)
- Is two greater than one and three less than two? (two > one) AND (three < two)
- Write the operation table for Boolean operation AND.
- Given Boolean variables one, two, and three, write an asser- tion for each of the following questions:
A | B | A AND B |
false | false | false |
false | true | false |
true | false | false |
true | true | true |
-
- Write the operation table for Boolean operation OR.
A | B | A OR B |
false | false | false |
false | true | true |
true | false | true |
true | true | true |
-
- Write the operation table for Boolean operation NOT.
A | NOT A |
false | true |
true | false |
-
- What is a data type?
A data type is the description of a set of values and the basic set of operations that can be applied to values of the type.
-
- What is strong typing?
Strong typing means that each variable is assigned a data type and only values of that type can be stored in the variable.
-
- Define the following data types.
- Integer
- Define the following data types.
The range of integer values that a machine can represent.
-
-
- Real
-
The range of rational numbers that a machine can represent.
-
-
- Character
-
The characters in the character set that the machine supports.
-
-
- Boolean
-
The values true and false.
-
- Is the string data type an atomic data type? Justify your answer.
A string can be output like an atomic data type, but it is made up of characters, each of which can be accessed separately. Thus, you can argue either side.
-
- If the same symbol is used for both single characters and strings, how can you distinguish between a single character and a one-character string?
If the same symbol is used, a character cannot be distin- guished from a one-character string.
-
- What is a declaration?
A declaration is an instruction to the compiler that associates an identifier with a variable, an action, or some other entity within the language that can be given a name. The program- mer can then refer to that entity by name.
-
- Fill in the following table showing the appropriate syntactic marker or reserved word for the language shown based on your observation of the tables in this chapter.
Language | Python | VB.NET | C++ | Java |
Comments | # | ‘ | // | // |
End of statement | End of line or indention | End of line or comment | ; | ; |
Assignment statement | = | = | = | = |
Real data type | Float | Single | float | float |
Integer data type | Integer | Integer | int | int |
Beginning of declaration(s) | No declarations | Dim | none | none |
-
- How do the .WORD and .BLOCK assembler directives in the Pep/8 assembly language differ from the declarations in high-level languages?
.WORD allows us to associate an identifier with a word in storage and specify what should go into that word. .BLOCK allows us to associate an identifier with a specified number of bytes. In each case, the programmer knows the actual address associated with the identifier. Declarations in high- level languages give you the same functionality, but the pro- grammer does not need to worry about the actual address.
-
- Distinguish between instructions to be translated and instruc- tions to the translating program.
Instructions to the translating program tell the program to associate identifiers with objects, and if the objects are data, tell the program the data type of what can be stored in that place.
-
- Consider the following identifiers: Address, ADDRESS, AddRess, Name, NAME, NamE
- How many different identifiers are represented if the lan-
- Consider the following identifiers: Address, ADDRESS, AddRess, Name, NAME, NamE
guage is Python? 6
-
-
- How many different identifiers are represented if the lan- guage is VB.NET?
-
6
-
-
- How many different identifiers are represented if the lan- guage is C++ or Java?
-
6
-
- Distinguish between the definition of an object in the design phase and in the implementation phase.
An object in the design phase is an entity that has meaning within the context of the problem. An object in the imple- mentation phase is an instance of a class.
-
- Distinguish between the definition of a class in the design phase and in the implementation phase.
A class in the design phase is a description of a group of objects with similar properties and behaviors. A class in the implementation phase is a pattern for an object.
-
- Distinguish between a field and a method.
A field represents a property of a class. A method is a named algorithm that manipulates the data values in an object. Thus, a method represents behavior.
-
- How can objects relate to one another?
Objects can be related by containment, inheritance, or col- laboration. An object can contain another object as a field. An object can inherit the data and behavior of another object class. An object can collaborate with an object of its own class or another class.
-
- Discuss the differences between a top-down design and an object-oriented design.
Top-down design breaks the problem into successive levels of tasks; object-oriented design breaks the problem into suc- cessive levels of data objects.
-
- In this chapter, we outlined a strategy for developing an object-oriented decomposition.
- List the four stages.
- In this chapter, we outlined a strategy for developing an object-oriented decomposition.
Brainstorming, filtering, scenarios, and responsibility algorithms.
-
-
- Outline the characteristics of each stage.
-
Brainstorming is a group problem-solving activity that involves the spontaneous contribution of ideas from all members of the group. The output from this activity is a list of possible classes.
Filtering is a group activity in which the tentative list of classes is analyzed to determine if there are duplicates, if some classes share common attributes and behaviors, and if there are classes that really do not belong in the solution.
Scenarios is a group activity that determines the responsibil- ities of the classes. They ask “what if” questions and deter- mine if all possible situations have been considered.
Responsibility algorithms are the algorithms that implement the responsibilities. This phase is where the algorithms to carry out the solution get written.
-
-
- What is the output from each of the four stages? Brainstorming: A list of possible classes.
-
Filtering: CRC cards for the classes that survived this stage.
Scenarios: CRC cards with responsibilities outlined and col- laborators indicated.
Responsibility algorithms: Algorithms for each of the responsibilities.
-
-
- Are the stages independent? Explain.
-
No, each state is not independent. The first stage produces a tentative list that is used as input to the second stage. The second stage produces a list of classes that have survived the filtering stages as input to the third stage. The third stage produces completed CRC cards that are input to the fourth stage.
-
- Design the CRC cards for an inventory system for a car deal- ership, using brainstorming, filtering, and scenarios. Brainstorming: car, idnumber, color, date arrived at dealer- ship, date sold, name of buyer, cost
Filtering: car, date, person, list of cars, idnumber, color, cost Scenarios: CRC cards produced.
Class Name: Car | Superclass: | Subclasses: | |
Responsibilities | Collaborations | ||
Initialize itself (name, date, color, idnumber) | String, Date, String, String | ||
Get color | String | ||
Get dateBought | Date | ||
Get name | String | ||
Get idnumber | String | ||
Set Repair (date) | Date | ||
GetRepairDate | Date | ||
Sold (name, date) | Name, Date | ||
Get dateSold | Date | ||
Get buyer | Name | ||
Class Name: Date | Superclass: | Subclasses: | |
Responsibilities | Collaborations | ||
Initialize itself (month, day, year) | int, int, int | ||
Get month | int | ||
Get day | int | ||
Get year | int | ||
Class Name: Name | Superclass: | Subclasses: | |
Responsibilities | Collaborations | ||
Initialize itself (first, last) | String, String | ||
Get first | String | ||
Get last | String | ||
Class Name: List (from library) | Superclass: | Subclasses: | |
Responsibilities | Collaborations | ||
Insert (car) | Car | ||
Delete (car) | Car | ||
-
- Design the CRC cards for a data base for a zoo, using brain- storming, filtering, and scenarios.
Brainstorming: family name, name, date of birth, date bought, food, cageNumber, sex, date of last shots
Filtering: animal, name, date, food, cageNumber, sex Scenarios:
Class Name: Animal | Superclass: | Subclasses: | |
Responsibilities | Collaborations | ||
Initialize itself (familyName, name, dateOfBirth, dateBought, eatsWhat, cageNumber, sex) | String, String, Date, Date, String, String, Char | ||
Get familyName | String | ||
Get name | String | ||
Get food | String | ||
Get dateOfBirth | Date | ||
Get dateBought | Date | ||
Put shots(Date) | Date | ||
Get DateOfShots | Date | ||
Get cageNumber | String | ||
Get sex | Char | ||
Class Name: Date | Superclass: | Subclasses: | |
Responsibilities | Collaborations | ||
Initialize itself (month, day, year) | int, int, int | ||
Get month | int | ||
Get day | int | ||
Get year | int | ||
Class Name: List (from library) | Superclass: | Subclasses: | |
Responsibilities | Collaborations | ||
Insert (animal) | Animal | ||
Delete (animal) | Animal | ||
. . . | |||