Doyle: C#, 5th edition Page 1-1
1. d. sampleValue
2. c. floating-point
7. b. string nameOfBook;
8. a. classes
9. b. 417
C# Programming: From Problem Analysis to Program Design, 5th edition
Chapter 2
Doyle: C#, 5th edition Page 1-2
16. a. ans = value1 + value2 * value3 (value4 + 20 / 5 % 2) * 7;
8 6 4 7 3 1 2 5
17. a. valid
18. a. int noOfCorrectResponses;
Doyle: C#, 5th edition Page 1-3
19. a. int noOfCorrectResponses = 0;
b. decimal amountOwed = 0m;
20. a. x = 2; y = 7; z = 11
21. a. x = 2.5; y = 7.9; z = 69
22. a. Result is $67.00
Doyle: C#, 5th edition Page 1-4
23. Variables and constants represent areas in memory where values of a
particular data type can be stored. When you declare a variable or constant,
24. Format specifier is useful if you want to control the alignment of items.
Following the index ordinal before the colon, add a comma and then add a
25. a. needs a closing parenthesis for the Main method argument list
Doyle: C#, 5th edition Page 1-5
d. declaration for y needs a type or replace the semicolon in the
declaration for x with a comma.
e The ans argument to the Write( ) should not be enclosed in double
quotes.
f. If using static System.Console; is added, the System.write should
be changed to Write.
g. change the declaration of z to a double or suffix the value with an
F or f.
i. the format string is reversed. It should be {0:f2}
j. instead of using the plus to concatenate the string with the result,
use a comma to separate the argumentsbecause the format string
is included inside the string argument.
k. need an extra closing brace
Style issues include
a. identifier for inches should be in uppercase, since it is a constant
b. start the name of the class identifier with an uppercase character
c. second and subsequent declarations should be on separate lines
and indented under the previous declaration.
Doyle: C#, 5th edition Page 1-6
d. place a space before and a space after each of the arithmetic
operators for readability
e. add internal documentation explaining the purpose of the
application.
f. match beginning and ending curly braces
g. if a line spans to the next line, indent second and subsequent lines
Below is the working solution with the above-mentioned modifications.
/************************************************
* This program demonstrates use of arithmetic operators.
********************************************** */
using System;
namespace Chapter2
{
class Converter
{
static void Main( )
{
const int INCHES = 12;
int x = 100,
y = 10;
float z = 22.45f;
double ans;
ans = INCHES + z * x % y;
System.Console.Write(“The result is {0:f2} “, ans);
}
Doyle: C#, 5th edition Page 1-7
}
}