Figure 4.1. Class Declaration With One Method.: "Welcome To The Grade Book!"
Figure 4.1. Class Declaration With One Method.: "Welcome To The Grade Book!"
using System; public class GradeBook { // display a welcome message to the GradeBook user public void DisplayMessage() { Console.WriteLine( "Welcome to the Grade Book!" ); } // end method DisplayMessage } // end class GradeBook
Figure 4.2. Create a GradeBook object and call its DisplayMessage method. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Fig. 4.2: GradeBookTest.cs // Create a GradeBook object and call its DisplayMessage method. public class GradeBookTest { // Main method begins program execution public static void Main( string[] args ) { // create a GradeBook object and assign it to myGradeBook GradeBook myGradeBook = new GradeBook(); // call myGradeBook's DisplayMessage method myGradeBook.DisplayMessage(); } // end Main } // end class GradeBookTest
Figure 4.4. Class declaration with a method that has a parameter. (This item is displayed on page 136 in the print version) 1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 4.4: GradeBook.cs // Class declaration with a method that has a parameter. using System; public class GradeBook { // display a welcome message to the GradeBook user public void DisplayMessage( string courseName ) { Console.WriteLine( "Welcome to the grade book for\n{0}!", courseName ); } // end method DisplayMessage
Figure 4.5. Create GradeBook object and pass a string to its DisplayMessage method. (This item is displayed on page 136 in the print version) 1 // Fig. 4.5: GradeBookTest.cs 2 // Create GradeBook object and pass a string to 3 // its DisplayMessage method. 4 using System; 5 6 public class GradeBookTest 7 { 8 // Main method begins program execution 9 public static void Main( string[] args ) 10 { 11 // create a GradeBook object and assign it to myGradeBook 12 GradeBook myGradeBook = new GradeBook(); 13 14 // prompt for and input course name 15 Console.WriteLine( "Please enter the course name:" ); 16 string nameOfCourse = Console.ReadLine(); // read a line of text 17 Console.WriteLine(); // output a blank line 18 19 // call myGradeBook's DisplayMessage method 20 // and pass nameOfCourse as an argument 21 myGradeBook.DisplayMessage( nameOfCourse ); 22 } // end Main 23 } // end class GradeBookTest Please enter the course name: CS101 Introduction to C# Programming Welcome to the grade book for CS101 Introduction to C# Programming!
Figure 4.7. GradeBook class that contains a private instance variable, courseName and a public property to get and set its value. (This item is displayed on page 139 in the print version) 1 2 3 4 5 6 // Fig. 4.7: GradeBook.cs // GradeBook class that contains a courseName instance variable, // and a property to get and set its value. using System; public class GradeBook
7 { 8 private string courseName; // course name for this GradeBook 9 10 // property to get and set the course name 11 public string CourseName 12 { 13 get 14 { 15 return courseName; 16 } // end get 17 set 18 { 19 courseName = value; 20 } // end set 21 } // end property CourseName 22 23 // display a welcome message to the GradeBook user 24 public void DisplayMessage() 25 { 26 // use property CourseName to get the 27 // name of the course that this GradeBook represents 28 Console.WriteLine( "Welcome to the grade book for\n{0}!", 29 CourseName ); // display property CourseName 30 } // end method DisplayMessage 31 } // end class GradeBook
Figure 4.8. Create and manipulate a GradeBook object. (This item is displayed on page 143 in the print version) 1 // Fig. 4.8: GradeBookTest.cs 2 // Create and manipulate a GradeBook object. 3 using System; 4 5 public class GradeBookTest 6 { 7 // Main method begins program execution 8 public static void Main( string[] args ) 9 { 10 // create a GradeBook object and assign it to myGradeBook 11 GradeBook myGradeBook = new GradeBook(); 12 13 // display initial value of CourseName 14 Console.WriteLine( "Initial course name is: '{0}'\n", 15 myGradeBook.CourseName ); 16 17 // prompt for and read course name 18 Console.WriteLine( "Please enter the course name:" ); 19 string theName = Console.ReadLine(); // read a line of text 20 myGradeBook.CourseName = theName; // set name using a property 21 Console.WriteLine(); // output a blank line 22
23 // display welcome message after specifying course name 24 myGradeBook.DisplayMessage(); 25 } // end Main 26 } // end class GradeBookTest Initial course name is: '' Please enter the course name: CS101 Introduction to C# Programming Welcome to the grade book for CS101 Introduction to C# Programming!
Figure 4.12. GradeBook class with a constructor to initialize the course name. 1 // Fig. 4.12: GradeBook.cs 2 // GradeBook class with a constructor to initialize the course name. 3 using System; 4 5 public class GradeBook 6 { 7 private string courseName; // course name for this GradeBook 8 9 // constructor initializes courseName with string supplied as argument 10 public GradeBook( string name ) 11 { 12 CourseName = name; // initialize courseName using property 13 } // end constructor 14 15 // property to get and set the course name 16 public string CourseName 17 { 18 get 19 { 20 return courseName; 21 } // end get 22 set 23 { 24 courseName = value; 25 } // end set 26 } // end property CourseName 27 28 29 30 31 32 [Page 148] // display a welcome message to the GradeBook user public void DisplayMessage() { // use property CourseName to get the // name of the course that this GradeBook represents
33 Console.WriteLine( "Welcome to the grade book for\n{0}!", 34 CourseName ); 35 } // end method DisplayMessage 36 } // end class GradeBook Figure 4.13. GradeBook constructor used to specify the course name at the time each GradeBook object is created. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Fig. 4.13: GradeBookTest.cs // GradeBook constructor used to specify the course name at the // time each GradeBook object is created. using System; public class GradeBookTest { // Main method begins program execution public static void Main( string[] args ) { // create GradeBook object GradeBook gradeBook1 = new GradeBook( // invokes constructor "CS101 Introduction to C# Programming" ); GradeBook gradeBook2 = new GradeBook( // invokes constructor "CS102 Data Structures in C#" );
[Page 149] 16 17 // display initial value of courseName for each GradeBook 18 Console.WriteLine( "gradeBook1 course name is: {0}", 19 gradeBook1.CourseName ); 20 Console.WriteLine( "gradeBook2 course name is: {0}", 21 gradeBook2.CourseName ); 22 } // end Main 23 } // end class GradeBookTest gradeBook1 course name is: CS101 Introduction to C# Programming gradeBook2 course name is: CS102 Data Structures in C#
Figure 9.1. Time1 class declaration maintains the time in 24-hour format.
1 // Fig. 9.1: Time1.cs 2 // Time1 class declaration maintains the time in 24-hour format. 3 public class Time1 4 { 5 private int hour; // 0 - 23 6 private int minute; // 0 - 59 7 private int second; // 0 - 59 8 9 // set a new time value using universal time; ensure that 10 // the data remains consistent by setting invalid values to zero 11 public void SetTime( int h, int m, int s ) 12 { 13 hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); // validate hour 14 minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); // validate minute 15 second = ( ( s >= 0 && s < 60 ) ? s : 0 ); // validate second 16 } // end method SetTime 17 18 // convert to string in universal-time format (HH:MM:SS) 19 public string ToUniversalString() 20 { 21 return string.Format( "{0:D2}:{1:D2}:{2:D2}", 22 hour, minute, second ); 23 } // end method ToUniversalString 24 25 // convert to string in standard-time format (H:MM:SS AM or PM) 26 public override string ToString() 27 { 28 return string.Format( "{0}:{1:D2}:{2:D2} {3}", 29 ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ), 30 minute, second, ( hour < 12 ? "AM" : "PM" ) ); 31 } // end method ToString 32 } // end class Time1
Figure 9.2. Time1 object used in an application. (This item is displayed on pages 412 - 413 in the print version) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // Fig. 9.2: Time1Test.cs // Time1 object used in an application. using System; public class Time1Test { public static void Main( string[] args ) { // create and initialize a Time1 object Time1 time = new Time1(); // invokes Time1 constructor // output string representations of the time Console.Write( "The initial universal time is: " ); Console.WriteLine( time.ToUniversalString() );
15 Console.Write( "The initial standard time is: " ); 16 Console.WriteLine( time.ToString() ); 17 Console.WriteLine(); // output a blank line 18 19 // change time and output updated time 20 time.SetTime( 13, 27, 6 ); 21 Console.Write( "Universal time after SetTime is: " ); 22 Console.WriteLine( time.ToUniversalString() ); 23 Console.Write( "Standard time after SetTime is: " ); 24 Console.WriteLine( time.ToString() ); 25 Console.WriteLine(); // output a blank line 26 27 // set time with invalid values; output updated time 28 time.SetTime( 99, 99, 99 ); 29 Console.WriteLine( "After attempting invalid settings:" ); 30 Console.Write( "Universal time: " ); 31 Console.WriteLine( time.ToUniversalString() ); 32 Console.Write( "Standard time: " ); 33 Console.WriteLine( time.ToString() ); 34 } // end Main 35 } // end class Time1Test
[Page 413]
The initial universal time is: 00:00:00 The initial standard time is: 12:00:00 AM Universal time after SetTime is: 13:27:06 Standard time after SetTime is: 1:27:06 PM After attempting invalid settings: Universal time: 00:00:00 Standard time: 12:00:00 AM
Figure 10.4. CommissionEmployee class represents a commission employee. 1 // Fig. 10.4: CommissionEmployee.cs 2 // CommissionEmployee class represents a commission employee. 3 public class CommissionEmployee : object 4 { 5 private string firstName; 6 private string lastName; 7 private string socialSecurityNumber; 8 private decimal grossSales; // gross weekly sales 9 private decimal commissionRate; // commission percentage 10 11 // five-parameter constructor 12 public CommissionEmployee( string first, string last, string ssn, 13 decimal sales, decimal rate ) 14 {
15 // implicit call to object constructor occurs here 16 firstName = first; 17 lastName = last; 18 socialSecurityNumber = ssn; 19 GrossSales = sales; // validate gross sales via property 20 CommissionRate = rate; // validate commission rate via property 21 } // end five-parameter CommissionEmployee constructor 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 rate 65 66 [Page 471] // read-only property that gets commission employee's first name public string FirstName { get { return firstName; } // end get } // end property FirstName // read-only property that gets commission employee's last name public string LastName { get { return lastName; } // end get } // end property LastName // read-only property that gets // commission employee's social security number public string SocialSecurityNumber { get { return socialSecurityNumber; } // end get } // end property SocialSecurityNumber // property that gets and sets commission employee's gross sales public decimal GrossSales { get { return grossSales; } // end get set { grossSales = ( value < 0 ) ? 0 : value; } // end set } // end property GrossSales // property that gets and sets commission employee's commission public decimal CommissionRate {
67 68 69 70 71 72 73 74 75
get { return commissionRate; } // end get set { commissionRate = ( value > 0 && value < 1 ) ? value : 0; } // end set } // end property CommissionRate
76 77 // calculate commission employee's pay 78 public decimal Earnings() 79 { 80 return commissionRate * grossSales; 81 } // end method Earnings 82 83 // return string representation of CommissionEmployee object 84 public override string ToString() 85 { 86 return string.Format( 87 "{0}: {1} {2}\n{3}: {4}\n{5}: {6:C}\n{7}: {8:F2}", 88 "commission employee", FirstName, LastName, 89 "social security number", SocialSecurityNumber, 90 "gross sales", GrossSales, "commission rate", CommissionRate ); 91 } // end method ToString 92 } // end class CommissionEmployee
Figure 10.5. Testing class CommissionEmployee. (This item is displayed on pages 473 - 474 in the print version) 1 2 3 4 5 6 7 8 9 10 11 // Fig. 10.5: CommissionEmployeeTest.cs // Testing class CommissionEmployee. using System; public class CommissionEmployeeTest { public static void Main( string[] args ) { // instantiate CommissionEmployee object CommissionEmployee employee = new CommissionEmployee( "Sue", "Jones", "222-22-2222", 10000.00M, .06M ); [Page 474] 12 13 14 15 \n" ); 16 17 18 19 // display commission employee data Console.WriteLine( "Employee information obtained by properties and methods: Console.WriteLine( "{0} {1}", "First name is", employee.FirstName ); Console.WriteLine( "{0} {1}", "Last name is", employee.LastName );
20 Console.WriteLine( "{0} {1}", "Social security number is", 21 employee.SocialSecurityNumber ); 22 Console.WriteLine( "{0} {1:C}", "Gross sales are", 23 employee.GrossSales ); 24 Console.WriteLine( "{0} {1:F2}", "Commission rate is", 25 employee.CommissionRate ); 26 Console.WriteLine( "{0} {1:C}", "Earnings are", 27 employee.Earnings() ); 28 29 employee.GrossSales = 5000.00M; // set gross sales 30 employee.CommissionRate = .1M; // set commission rate 31 32 Console.WriteLine( "\n{0}:\n\n{1}", 33 "Updated employee information obtained by ToString", employee ); 34 Console.WriteLine( "earnings: {0:C}", employee.Earnings() ); 35 } // end Main 36 } // end class CommissionEmployeeTest Employee information obtained by properties and methods: First name is Sue Last name is Jones Social security number is 222-22-2222 Gross sales are $10,000.00 Commission rate is 0.06 Earnings are $600.00 Updated employee information obtained by ToString: commission employee: Sue Jones social security number: 222-22-2222 gross sales: $5,000.00 commission rate: 0.10 earnings: $500.00