Width Specifier 2 C Programming From Problem Analysis

Width Specifier 2 C# Programming: From Problem Analysis to Program Design 4 th Edition 1

Width Specifier • Useful when you want to control the alignment of items on multiple lines • Alignment component goes after the index ordinal followed by a comma (before the colon) – If alignment number is less than actual size, it is ignored – If alignment number is greater, pads with white space • Negative alignment component places spaces on right Console. Write. Line("{0, 10: F 0}{1, 8: C}", 9, 14); 9 $14. 00 //Right justifies values C# Programming: From Problem Analysis to Program Design 2

Programming Example – Carpet. Calculator Figure 2 -15 Problem specification sheet for the Carpet. Calculator example C# Programming: From Problem Analysis to Program Design 3

Data Needs for the Carpet. Calculator Table 2 -18 Variables C# Programming: From Problem Analysis to Program Design 4

Nonchanging Definitions for the Carpet. Calculator Table 2 -19 Constants C# Programming: From Problem Analysis to Program Design 5

Carpet. Calculator Example Figure 2 -16 Prototype for the Carpet. Calculator example C# Programming: From Problem Analysis to Program Design 6

Algorithm for Carpet. Calculator Example Figure 2 -17 Carpet. Calculator flowchart C# Programming: From Problem Analysis to Program Design 7

Algorithm for the Carpet. Calculator Example (continued) Figure 2 -18 Structured English for the Carpet. Calculator example C# Programming: From Problem Analysis to Program Design 8

Carpet. Calculator Example (continued) Figure 2 -19 Class diagram for the Carpet. Calculator example C# Programming: From Problem Analysis to Program Design 9

Carpet. Calculator Example (continued) Figure 2 -20 Revised class diagram without methods C# Programming: From Problem Analysis to Program Design 10

/* Carpet. Calculator. cs Author: Doyle */ using System; namespace Carpet. Example { class Carpet. Calculator { static void Main( ) { const int SQ_FT_PER_SQ_YARD = 9; const int INCHES_PER_FOOT = 12; const string BEST_CARPET = "Berber"; const string ECONOMY_CARPET = "Pile"; int room. Length. Feet = 12, room. Length. Inches = 2, room. Width. Feet = 14, room. Width. Inches = 7; double room. Length, room. Width, carpet. Price, num. Of. Square. Feet, num. Of. Square. Yards, total. Cost; C# Programming: From Problem Analysis to Program Design 11

} room. Length = room. Length. Feet + (double) room. Length. Inches / INCHES_PER_FOOT; room. Width = room. Width. Feet + (double) room. Width. Inches / INCHES_PER_FOOT; num. Of. Square. Feet = room. Length * room. Width; num. Of. Square. Yards = num. Of. Square. Feet / SQ_FT_PER_SQ_YARD; carpet. Price = 27. 95; total. Cost = num. Of. Square. Yards * carpet. Price; Console. Write. Line("The cost of " + BEST_CARPET + " is {0: C}", total. Cost); Console. Write. Line( ); carpet. Price = 15. 95; total. Cost = num. Of. Square. Yards * carpet. Price; Console. Write. Line("The cost of " + ECONOMY_CARPET + " is " + "{0: C}", total. Cost); Console. Read(); } } C# Programming: From Problem Analysis to Program Design 12

Carpet. Calculator Example (continued) Figure 2 -21 Output from the Carpet. Calculator program C# Programming: From Problem Analysis to Program Design 13
- Slides: 13