info 3231 chapter 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

What are the three floating-point data types in C#? Briefly describe the characteristics of each type.

A floating-point number is one that contains decimal positions. C# supports three floating-point data types: float, double, and decimal. A float data type can hold as many as seven significant digits of accuracy. A double data type can hold 15 or 16 significant digits of accuracy. Compared to floats and doubles, the decimal type has a greater precision and a smaller range, which makes it suitable for financial and monetary calculations. For example, a decimal given the value 123456789.987654321 will appear as 123456789.987654321 (notice that it is accurate to the rightmost digit). A decimal cannot hold as large a value as a double can, but it can be accurate to more decimal places.

What is a variable declaration, what is its purpose, and what does the declaration include?

A variable declaration is the statement that names a variable and reserves storage for it. The declaration includes: * The data type that the variable will store * The variable name (its identifier) * An optional assignment operator and assigned value when you want a variable to contain an initial value * An ending semicolon

What is an enumeration and what are the advantages of creating an enumeration type? Show an example of an enumeration definition.

An enumeration is a set of constants represented by identifiers. An example of an enumeration definition is the following: enum DayOfWeek { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } Creating an enumeration type provides several advantages. For example, the DayOfWeek type above improves a program as follows: 1. Only the seven allowed values can be assigned to a DayOfWeek object. 2. Using an enumeration type makes a program type-safe (for example, you couldn't use an integer method such as addition on an object of type DayOfWeek). 3. The enum constants provide clearer self-documentation (it is more apparent what you mean when you assign a variable the value WEDNESDAY rather than the number 4, for example).

How is a named constant different from a variable? How would you create a named constant (and show an example)?

By definition, a variable's value can vary, or change. Sometimes you want to create a named constant (often simply called a constant), an identifier whose contents cannot change. You create a named constant similar to the way you create a named variable, but by using the keyword const. Although there is no requirement to do so, programmers usually name constants using all uppercase letters, inserting underscores for readability. This convention makes constant names stand out so that the reader is less likely to confuse them with changeable variable names. For example, the following declares a constant named TAX_RATE that is assigned a value of 0.06: const double TAX_RATE = 0.06;

Explain how to use the C# shortcut arithmetic operators -=, *=, and /=.

Each of these operators is used to perform an operation and assign the result in one step. For example: balanceDue -= payment subtracts a payment from balanceDue and assigns the result to balanceDue. rate *= 100 multiplies rate by 100 and assigns the result to rate. For example, this could be used to convert a fractional value stored in rate, such as 0.27, to a whole number, such as 27. payment /= 12 divides payment by 12 and assigns the result to payment. This could be used to change a payment value from an annual amount to a monthly amount due.

How can you compare strings using the Compare() method?

The Compare() method requires two string arguments and returns an integer result. When it returns 0, the two strings are equivalent; when it returns a positive number, the first string is greater than the second; and when it returns a negative number, the first string is less than the second. A string is considered equal to, greater than, or less than another string lexically, which in the case of letter values means alphabetically. That is, when you compare two strings, you compare each character in turn from left to right. If each Unicode value is the same, then the strings are equivalent. If any corresponding character values are different, the string that has the greater Unicode value earlier in the string is considered greater.

What are the nine integral data types in C# and how do you decide which is the most appropriate to use in a particular situation?

The nine types are byte, sbyte, short, ushort, int, uint, long, ulong, and char. The first eight always represent whole numbers, and the ninth type, char, is used for characters like 'A' or 'a'. The most basic of the integral types is int. An int uses four bytes of memory and can hold any whole number value ranging from 2,147,483,647 down to -2,147,483,648. Many programmers use int for most whole numbers. If you want to save memory and know you need only a small value, you can use one of the shorter integer types-byte, sbyte (which stands for signed byte), short (short int), or ushort (unsigned short int). For example, a payroll program might contain a variable named numberOfDependents that is declared as type byte, because numberOfDependents will never need to hold a negative value or a value exceeding 255; for that reason, you can allocate just one byte of storage to hold the value. If you use a type that is too large, you waste storage. If you use a type that is too small, your program won't compile.

What are the differences between the prefix increment operator and postfix increment operator?

When you only want to increase a variable's value by 1, there is no apparent difference between using the prefix and postfix increment operators. However, these operators function differently. When you use the prefix ++, the result is calculated and stored, and then the variable is used. In contrast, when you use the postfix ++, the variable is used, and then the result is calculated and stored.

Explain why implicit numeric conversions are necessary and how C# chooses operands to convert.

When you perform arithmetic with variables of the same type, the result of the arithmetic retains that type. For example, when you divide two ints, the result is an int. Often, however, you need to perform mathematical operations on different types. For example, you might need to multiply an int by a double. In this case, C# chooses a unifying type for the result and implicitly converts nonconforming operands to the unifying type, which is the type with the higher type precedence. In the case of multiplying an int by a double, the result will be a double.

What is the purpose of the ReadLine() method? How would you use it with a variable of type string? How would you use it with a variable of type double?

You use the ReadLine() method to accept user input from the keyboard. This method accepts all of the characters entered by a user until the user presses Enter. The characters can be assigned directly to a string. For example, the following statement accepts a user's input and stores it in the variable myString: myString = ReadLine(); If you want to use the data as a string-for example, if the input is a word or a name-then you simply use the variable to which you assigned the value. If you want to use the data as a number, then you must use a conversion method to convert the input string to the proper type, as in: itemPrice = Convert.ToDouble(myString);


Conjuntos de estudio relacionados

Unit 7 acids and bases chemistry test

View Set

CH 10 Motivation, Personality, and Emotion

View Set

Chapter 14: Relationships in the Workplace

View Set

Genetics Final 2013 someone elses

View Set

Commercial Property Policies (continued)

View Set

General CA Ins Law-Basic Ins Concepts and Principles

View Set

AP World History - 4.5 Key Terms

View Set